Android: how to call get method to display height and width? - java

I have been trying to figure out how to find the height and width of the screen in android. i have found a way to get it but I dont know how to call the getScreenWidth and getScreenHeight to put values into my int height and int width?
public class Player {
public int across;
public int upDown;
public static int getScreenWidth(Context c) {
DisplayMetrics dmetrics = new DisplayMetrics();
((Activity) c).getWindowManager().getDefaultDisplay()
.getMetrics(dmetrics);
return dmetrics.widthPixels;
}
// Get screen height
public static int getScreenHeight(Context c) {
DisplayMetrics dmetrics = new DisplayMetrics();
((Activity) c).getWindowManager().getDefaultDisplay()
.getMetrics(dmetrics);
return dmetrics.heightPixels;
}
public Player() {
int width = dmetrics.widthPixels;
int height = dmetrics.heightPixels;
int rectSide = 1000;
//across = startPosX;
//upDown = startPosY;
}
i am trying to get it by using int width = dmetric.widthPixels; but this obviously isnt the way to call the getScreenWidth. Im pretty bad at this stuff, so any help would be great.

You can refer to Here
or Here
for your answer.
Ps - sorry , I can't comment as I am a beginner so I wrote the answer which should rather be a comment.

I know, really what im asking is how do i call this get method i have created to set values for width and height? – Phill
If you are in an Activity when you wish to call these static methods you could call them this way:
int width = Player.getScreenWidth(this);
int height = Player.getScreenHeight(this);
If you're not in an activity you'll need to know one and pass it instead of this.
Also be aware that, because of the cast, if you pass something that is a Context but not an Activity it won't work and you won't know it until run time.

Related

Attempting to change two public int values, but when returned are untouched

If it's not apparent by the question, I'm not very amazing at java yet. I'm just playing tonight with a few fundamentals that I would need to be comfortable with for a little idea I have, but have hit a problem. I may just be tired and missing something, if so I apologize.
This is the code in the Main.java class:
private int h,w;
public static void main(String args[]){
Main inst = new Main();
inst.Frame();
Second s = new Second();
}
public void Frame(){
this.h = 200;
this.w = 200;
}
public int getWidth(){
return w;
}
public int getHeight(){
return h;
}
It's a very basic program, just aiming to create two ints (h,w) and change their values in Frame(), and then using the two methods at the bottom return the 200 values in class 2.
This is second.java:
Second(){
this.Window();
}
public void Window(){
System.out.println("window()");
Main win = new Main();
int width = win.getWidth();
int height = win.getHeight();
System.out.println("width and height "+width+height);
}
The output in Window() simply gives two zeroes. this is countered by changing the values the moment they are created (Public int w=200,h=200), but this is not what I'm looking to do, as it needs to be changed at a further point.
Any ideas how to get the changes done in Frame() to appear in the Window ints? Thanks a lot :)
You're creating two independent instances of Main, each with their own h & w values. You change the values of the first instance, then print them from the second, unchanged instance.
You probably want to pass your Main instance as an argument to the Second constructor, so they can share the same data.
`Second(Main m){
this.Window(m);
}
public void Window(Main m){
System.out.println("window()");
Main win = m;
int width = win.getWidth();
int height = win.getHeight();
System.out.println("width and height"+width+height);
}`
You have a void method, not a constructor. Remove the void keyword.

Android - Getting return value of a thread

I start with Android and Java, and my English is not very good (sorry).
I'm doing a application which compare 2 pictures taking by the camera and return a float value = a ratio of identical pixels in the 2 pictures.
public static float comparePic(String img_1, String img_2){
Bitmap bitmap1 = BitmapFactory.decodeFile(img_1);
Bitmap bitmap2 = BitmapFactory.decodeFile(img_2);
int equ = 0;
//get one height and one width because we assume that pictures have always same sizes
int h = bitmap1.getHeight();
int w = bitmap1.getWidth();
for(int y=0;y<h;y++){
for (int x=0;x<w;x++){
int pixel1 = bitmap1.getPixel(x,y);
int pixel2 = bitmap2.getPixel(x,y);
//alpha doesn't matter, they're jpg
int redValue1 = Color.red(pixel1);
int redValue2 = Color.red(pixel2);
int greenValue1 = Color.green(pixel1);
int greenValue2 = Color.green(pixel2);
int blueValue1 = Color.blue(pixel1);
int blueValue2 = Color.blue(pixel2);
if (redValue1==redValue2 && greenValue1==greenValue2 && blueValue1==blueValue2){
equ++;
}
}
}
return (float)equ/(h*w);
}
When I execute it, I have this message : "I/Choreographer﹕ Skipped 730 frames! The application may be doing too much work on its main thread."
So I tried to do it with a thread, but I'm always failing because I don't know how to return the value.
Futhermore, by skipping frames I guess, the ratio is not correct.
I know that it's an asynchonous work, so what can I do ?
Thanks !
You can simply add a public (setter-)method in your main class (or wherever you need the value).
When your async thread is done call the method with equ as the parameter.
You obviously need to care about thread-safety so consider making the method sychronized.
You should give a try to AsyncTask. It's a class specifically prepared for background processing. It has some drawbacks (it's not good idea to do networking with AsyncTask), but for some picture comparison stuff it is a great tool.
Define an Asynctask with a interface and implement that interface in the main Activity.
That way you can notify that host activity that the background asynctask has completed.

Creating setColor method without parameter being passed?

I'm working on a homework project for an intro to java course. To practice calling methods and organizing tasks, we have to create two balloon objects s1 and s2 and modify their colors and altitudes using methods in a separate java class.
I have everything working fine, but not exactly to the requirements of the assignment. The sheet lists the method declarations and they cannot be changed, only the code within them can.
The method that is used to change a balloon's color is to be created as public void setColor(). This doesn't make sense to me, though. I'm using public void setColor(String color) for now.
How can I change the color property of a balloon object without passing anything to the setColor method?
I totally agree with #RealSkeptic but as your question says that changing the color without passing any value it means you need to generate the color each time your could use the following code. I'm not sure do this code is what you need.
public void setColor()
{
int red,green,blue;
red = green = blue = 0;
Random random = new Random();
int high = 255, low = 0;
red = random.nextInt(high-low)+low;
green = random.nextInt(high-low)+low;
blue = random.nextInt(high-low)+low;
color = new Color(red,green,blue);
//set this color to your balloon
}
Well, you can't specify any particular color without a parameter in the method. You can hardcode so that the color changes.
class Baloon {
private String[] colors = {"blue", "red" , "green"};
private int index = 0;
private String currentColor = colors[index];
public void setColor(){
index ++;
if (index = colors.length)
index = 0;
currentColor = colors[index];
}
}

Java : can't call overloaded constructor

here I am trying to call overloaded constructor after some condition has met in my main() program , which is given by variable a. Code works fine when I am using only default constructor , but I need to call overloaded constructor at some point and it fails . folowing is code:
overloaded constructor :
public Paddle(int a){
if(a ==1){
ImageIcon ii = new ImageIcon(this.getClass().getResource(paddle1));
image = ii.getImage();
}
else {
ImageIcon ii = new ImageIcon(this.getClass().getResource(paddle2));
image = ii.getImage();
}
width = image.getWidth(null);
height = image.getHeight(null);
resetState();
}
// further initialization --
default constructor :
public Paddle(){
ImageIcon ii = new ImageIcon(this.getClass().getResource(paddle));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
System.out.println(height+" "+width);
resetState();
}
thanks for help in advance, further queries can be asked but I think this piece of code has some problem . Thank you
Answer
You are not setting the instance variable image in the overloaded constructor.
Correct Way
The way you are doing it is violating DRY ( Don't Repeat Yourself )!
The best way is to make the no arg constructor call the overloaded one and then set the instance variable image in one place.
public Paddle(final int i)
{
if (i==1) { this.image = one thing }
else
{ this.image = another thing }
}
public Paddle() { this(0); }
A better way would be to just pass in the resource to a single constructor and be done with it, without seeing all the code, this looks overly complicated.

editfield weird height

In my application i have 2 types of editfields. One of them behaves like single line editfield, the other behaves like multi-line editfield (editarea). In this screen i have one header, one editfield and one editarea. When i enter some text to editfield, it clips the text and cursor. But, when i enter some text to editarea which includes a tailed character(y,g,q,p) editareas height is changing and editfieldact normal. If i dont enter tailed characters stuation does not change.
Here is my editarea class:
public class EditAreaField extends HorizontalFieldManager{
private net.rim.device.api.ui.component.EditField editArea;
public EditAreaField (){
// some code;
editArea.setPadding(25, 10, 0, 10);
}
public int getPreferredHeight() {
int height = Math.max(editArea.getHeight(), textFont.getHeight());
return height + editArea.getPaddingTop();
}
}
label1 -> editfield
label2 -> editarea
this is because you are making the size to change by using
int height = Math.max(editArea.getHeight(), textFont.getHeight());
instead of this try to give some fixed height. for example
height= Graphics.getScreenHeight()/5;
or you can also use setExtent inside the sublayout method of the manager
protected void sublayout(int maxWidth, int maxHeight)
{
layoutChild(_editField, _editField.getPreferredWidth(), _editField.getPreferredHeight());
setPositionChild(_editField, xpos,ypos);
setExtent(preferredHeight,preferredWidth);
}
I think it will work.
Please let me know
About the cursor painting - you did override drawFocus or/and onFocus or/and onUnfocus and don't repaint properly sometime.

Categories

Resources