I created some custom elements and I want to programmatically place them to the upper right corner (n pixels from the top edge and m pixels from the right edge) therefore I need to get the screen width and screen height and then set position:
int px = screenWidth - m;
int py = screenWidth - n;
Does anyone know how to get screenWidth and screenHeight in the main Activity?
emerals stream said:
I created some custom elements and I want to programmatically place them to the upper right corner (n pixels from the top edge and m pixels from the right edge) therefore I need to get the screen width and screen height and then set position:
int px = screenWidth - m;
int py = screenWidth - n;
Does anyone know how to get screenWidth and screenHeight in the main Activity?
Click to expand...
Click to collapse
Try google it
http://stackoverflow.com/questions/1016896/how-to-get-screen-dimensions
Try something like this
Code:
Point size = new Point();
WindowManager wManager = getWindowManager();
w.getDefaultDisplay().getSize(size);
(int)screenWidth = size.x;
(int)screenHeight = size.y;
Noted to self thrice via tapatalk
...or to use DisplayMetrics
Code:
DisplayMetrics dm = this.getContext().getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels
Related
It has been a while since I wrote a program for the last time, and when I got my Desire I decided it would be a good moment to start again.
After a few very simple test programs I decided to go a little further and try to access the sensors.
With a little research I found out I had to use public static float[] getOrientation (float[] R, float[] values) . This is what I found in the documentation:
float[] android.hardware.SensorManager.getOrientation(float[] R, float[] values)
public static float[] getOrientation (float[] R, float[] values)
Since: API Level 3
Computes the device's orientation based on the rotation matrix.
When it returns, the array values is filled with the result:
values[0]: azimuth, rotation around the Z axis.
values[1]: pitch, rotation around the X axis.
values[2]: roll, rotation around the Y axis.
All three angles above are in radians and positive in the counter-clockwise direction.
Parameters
R rotation matrix see getRotationMatrix(float[], float[], float[], float[]).
values an array of 3 floats to hold the result.
Returns
The array values passed as argument.
Click to expand...
Click to collapse
I didn't understand the use of float[] R part, so I decided to just give null as argument
Code:
float[] values = new float[3];
SensorManager.getOrientation(null, values);
TextView xText = (TextView) findViewById(R.id.TextView02);
TextView yText = (TextView) findViewById(R.id.TextView03);
TextView zText = (TextView) findViewById(R.id.TextView04);
xText.setText(String.valueOf(values[0]));
yText.setText(String.valueOf(values[1]));
zText.setText(String.valueOf(values[2]));
But my program fails when it comes to the line where I call getOrientation, so my guess is I shouldn't give null as argument. How should I use getOrientation()?
I am used to work with Flash and if we want to position an item at certain coordinates on the stage we just modify the .x and .y values. But I can't manage to find a way to this to a TextView (or any other item for that matter).
I did manage to get the width and height of the stage using this:
Code:
Display display = getWindowManager().getDefaultDisplay();
stageWidth = display.getWidth();
stageHeight = display.getHeight();
But, if I want for example to position a TextView called tv at the coordinates like this:
tv.x = stageWidth - tv.width - 30
tv.y = stageHeight - tv.height - 30
I don't know how to do it.
Thanks, and sorry if this is a stupid answer...maybe I am to used in working with Flash.
I'm try to create a app to track my reps sales but I can't figure out how to get int to use 0.0
example
int points;
points = 0;
This works but how would I get it to register 0.0 instead 0
Thanks for your help. I'm still kinda new at this.
What's about this?
Code:
int points;
points = (int) 0.0;
This will cut off all decimals. If you want to have the decimals, use double instead of int.
cdbaugher said:
I'm try to create a app to track my reps sales but I can't figure out how to get int to use 0.0
example
int points;
points = 0;
This works but how would I get it to register 0.0 instead 0
Thanks for your help. I'm still kinda new at this.
Click to expand...
Click to collapse
An int is an integer (so, no fractional part)
For a floating point number, you'll need something else.
float is one option
Code:
float points = 0.0f;
This may be worth reading.
cdbaugher said:
I'm try to create a app to track my reps sales but I can't figure out how to get int to use 0.0
example
int points;
points = 0;
This works but how would I get it to register 0.0 instead 0
Thanks for your help. I'm still kinda new at this.
Click to expand...
Click to collapse
Use Double, it can store the decimal values in it. Int cannot store the decimal values.
use double instead of float...that will give you 0.0 instead of 0...
I say 0 is exactly the same like 0.0
But you can change the look when turn it to string:
String.format(...)
Regards
coolbud012 said:
use double instead of float...that will give you 0.0 instead of 0...
Click to expand...
Click to collapse
double and float are both floating point representations (64 bit vs 32 bit)
Hello,
I want columns of equal width in a TableLayout, like this: http://androidadvice.blogspot.co.at/2010/10/tablelayout-columns-equal-width.html
but programmatically..
I create TextView's and add them to a TableRow, then I add the row to a TableLayout.
When I do the following:
Code:
tv.setLayoutParams(new TableLayout.LayoutParams(0, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
the TextView's aren't visible.
How should I do that?
Anyone?
Did you set anything to the TextView? If you use this for eg:-
TextView txtview = (TextView) findbyViewId(r.id.txtview);
Then set it to
txtview.setText("Awesome");
Sent from my Nexus 4 using XDA Premium 4 mobile app
That's the code:
Code:
table = (TableLayout) findViewById(R.id.timetable_tablelayout);
for (int i2 = 1; i2 < 11; i2++) {
// create a new TableRow
TableRow row = new TableRow(this);
for (int i = 0; i < 5; i++) {
String date = getKeyForIndex(i);
// create a new TextView
TextView t = new TextView(this);
t.setText(date);
// set width, height and weight
t.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
// with this line, the textview's are not visible. when i comment the line, everything is ok
// add the TextView to the new TableRow
row.addView(t);
}
// add the TableRow to the TableLayout
table.addView(row);
You should ideally be using
Code:
new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
In instead of of what you are using... At least some variation of it. I haven't tried it but damn it should work! Let me know!
Sent from my Nexus 4 using XDA Premium 4 mobile app
Im already using that..
// set width, height and weight
t.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
Click to expand...
Click to collapse
have you tried setting width to 0 and weight to 1?
width must be 0 for weight to work
yes, then I get an 'java.lang.ArithmeticException: divide by zero' exception
ok, figured it out
you have to use the parents view layoutparams, so in your case you have to use TableRow
so just change to
Code:
t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
thank you!
warlock9_0 said:
ok, figured it out
you have to use the parents view layoutparams, so in your case you have to use TableRow
so just change to
Code:
t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
Click to expand...
Click to collapse
Thank you, it's working now!!
Hi there
JSONObject nodeRoot = new JSONObject(returnMsg);
JSONArray res = nodeRoot.getJSONArray("result");
for (int i = 0; i < res.length(); i++) {
JSONObject childJSON = res.getJSONObject(i);
if (childJSON.get("data")!=null){
String value = childJSON.getString("data");
dataList.add(value);
JSONObject node=new JSONObject(value);
atList.add(node.get("temperature").toString());
In the last line node I get the "temperature":"6"
which is the value i want but now i have no idea to display it out , can i hav a guide here? tq
eric3231559 said:
Hi there
JSONObject nodeRoot = new JSONObject(returnMsg);
JSONArray res = nodeRoot.getJSONArray("result");
for (int i = 0; i < res.length(); i++) {
JSONObject childJSON = res.getJSONObject(i);
if (childJSON.get("data")!=null){
String value = childJSON.getString("data");
dataList.add(value);
JSONObject node=new JSONObject(value);
atList.add(node.get("temperature").toString());
In the last line node I get the "temperature":"6"
which is the value i want but now i have no idea to display it out , can i hav a guide here? tq
Click to expand...
Click to collapse
You should go thought the gettings started guides and basic listAdapter tutorials on d.android.com
Well, where do u want to display the data? Do u want to populate some view with it or just print it out to the console?
Thanks
OkieKokie said:
Well, where do u want to display the data? Do u want to populate some view with it or just print it out to the console?
Click to expand...
Click to collapse
Actually i wanna display it on my text view. I just know that inorder to get the data i want i have to use array form after slicing it
eric3231559 said:
Actually i wanna display it on my text view. I just know that inorder to get the data i want i have to use array form after slicing it
Click to expand...
Click to collapse
In the last line of ur code, u add the string value of the data to the arraylist. So for displaying ull need to use atList.get(index) where index is the number of the element in the arraylist.
So if u want to display the 1st element in the list then use urTextViewReference.setText(atList.get(0));