I have a button with dimensions:
btn1.width = 100;
btn2.height = 150;
I used the following code to resize:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) btn1.getLayoutParams();
layoutParams.width = 200;
layoutParams.height = 300;
btn1.setLayoutParams(layoutParams);
When I did this:
int newSize = btn1.getWidth;
The newSize has value 100, but not 200. It resized the button well, but after the resize, btn1.getWidth still holds the previous value. How can I instantly get the set size?
The actual width of the View does not get updated immediately.
Your best bet is to cache the size when you set it.
You will not directly get the height/width after setting to a view. If you want then you can get in "post" method:
btn1.setWidth(200);
btn1.setHeight(300)
btn1.post(new Runnable() {
#Override
public void run() {
Log.i("Tag", "Width:"+ btn1.getWidth() + " Height:" + btn1.getHeight());
}
});
Related
LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView
mainimage = new ImageView(HLActivity.this);
mainimage.setLayoutParams(params);
loader.DisplayImage(_pageslist.get(j).getUrl(), mainimage);
I already tried mainimage.getwidth() and mainimage.getHeight();
Now i want to findout the orignalwidth and height of the ImageView after
place the image in the imageview. can you please help me.
getWidth() and getHeight() are the indeed ones you are looking for, but you are trying to ask for the width and the height when the view is not measured yet. To fix this, we post a message to the ImageView, which will get executed after the measurement is done.
LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// mark it as final, so it can be accessed in the runnable.
final imageView mainimage = new ImageView(HLActivity.this);
mainimage.setLayoutParams(params);
mainimage.post(new Runnable() {
#Override
public void run() {
// get the width and the height
int width = mainimage.getWidth();
int height = mainimage.getHeight();
// do what you want to do with the sizes.
loader.DisplayImage(_pageslist.get(j).getUrl(), mainimage);
}
});
I am trying to generate a button programatically in the center of the current view (which is the whole screen in this case). I get the screen height and width then find the center of the screen. Then I try to use the LayoutParams to set the top and left margins, taking into account the button size - but the result is not centered. What am I doing wrong?
public class StartActivity extends Activity {
private int cScreenWidth;
private int cScreenHeight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
RelativeLayout ll = (RelativeLayout)findViewById(R.id.startLayout);
Display display = getWindowManager().getDefaultDisplay();
cScreenWidth = display.getWidth(); // deprecated but needed for API 10
cScreenHeight = display.getHeight(); // deprecated but needed for API 10
ImageButton myButton = new ImageButton(this);
myButton.setId(100);
myButton.setBackgroundResource(R.drawable.ic_launcher);
int size = Math.min(cScreenWidth / 3, cScreenHeight / 3);
LayoutParams lp = new LayoutParams( size, size );
lp.leftMargin = (cScreenWidth / 2) - (size / 2);
lp.topMargin = (cScreenHeight / 2) - (size / 2);
ll.addView(myButton, lp);
}
Try the following:
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)myButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
myButton.setLayoutParams(layoutParams);
This way you don't need to explicitly define the margins.
That's my source code to create a new ImageView:
ImageView image = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = screen_dimens(0.5, "w");
params.height = screen_dimens(0.5, "w");
image.setLayoutParams(params);
(screen_dimens(0.5, "w")=50% of screen width)
But now when I want to get the width of the Image, I get the following results:
image.getWidth() => 0
image.getLayoutParams().width => 360
Now what is the difference between those two functions? And how can I set the Image-width so the getWidth() method also returns the correct value?
Even you have set width and height for a ImageView or any View by using LayoutParams,
imageView.getWidth() always returns 0 until it is drawn in its parent.
Use this to set width and height :
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);
u can use
image.post(new Runnable() {
public void run() {
image.getMeasuredHeight();
}
});
instead of
image.getWidth()
I am trying to add a text to the textview for which i have set the width as Wrap_content. I am trying to get the width of this textview. But its showing 0 in all the cases. How Can i get the width of the textview after setting the text into it.
the code is as:
LinearLayout ll= new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ll.addView(tv);
tv.setText("Hello 1234567890-0987654321qwertyuio787888888888888888888888888888888888888888888888888");
System.out.println("The width is == "+tv.getWidth());// result is 0
this.setContentView(ll);
Please suggest.
Thanks in advance.
Views with the dynamic width/height get their correct size only after a layout process was finished (http://developer.android.com/reference/android/view/View.html#Layout).
You can add OnLayoutChangeListener to your TextView and get it's size there:
tv.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
final int width = right - left;
System.out.println("The width is == " + width);
});
You can not get the width of a View with dynamic size before the layout is completely built. That means there is no way you can get it in onCreate(). One way would be to create a class that inherits from TextView and overrides onSizeChanged().
When are you calling this? Has it already been drawn to the screen?
It sounds like you are calling getWidth() too early.
You can also take a look at this question.
You should use this:
textView.getMeasuredWidth();
this works for me:
RelativeLayout.LayoutParams mTextViewLayoutParams = (RelativeLayout.LayoutParams) mTextView.getLayoutParams();
mTextView.setText(R.string.text);
mTextView.measure(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int width = mShareTip.getMeasuredWidth();
//use the width to do what you want
mShareTip.setLayoutParams(mShareTipLayoutParams);
you can try this:
textView.measure(0,0);
int width = textView.getMeasuredWidth();
hello use this method:
textview.post(new Runnable() {
#Override
public void run() {
int width = textview.getWidth();
int height = textview.getHeight();
textview.setText( String.valueOf( width +","+ height ));
}
});
source: https://gist.github.com/omorandi/59e8b06a6e81d4b8364f
You can use this library to schedule the task of perform calculation on the width to the correct time after the view had been completely drawn
https://github.com/Mohamed-Fadel/MainThreadScheduler
Sample of use:
MainThreadScheduler.scheduleWhenIdle(new Runnable() {
#Override
public void run() {
int width = textview.getWidth();
int height = textview.getHeight();
textview.setText( String.valueOf( width +","+ height ));
}
});
I have created an array of buttons. Now I want to find the height and width of the button, and for that, I have used getWidth() and getHeight(). But the thing is that it always returns 0. Why is this happening? I have send my code, please check if anything is wrong.
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
public static void main(String[] args)
{
DBFacade dbFacade = new DBFacade();
dbFacade.pick();
}
//Create Button
for(int i=0;i<6;i++)
{
rowLayout=new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout,param);
for(int j=0;j<7;j++)
{
m_pBtnDay[i][j]=new Button(this);
rowLayout.addView(m_pBtnDay[i][j],param);
m_pBtnDay[i][j].setOnLongClickListener(this);
m_pBtnDay[i][j].setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
m_pBtnDay[i][j].setTextSize(12);
}
}
x=m_pBtnDay[i][j].getWidth();
y=m_pBtnDay[i][j].getHeight();
Log.d("width",Integer.toString(x));
Log.d("Height",Integer.toString(y));
return true;
Probably you are calling getWidth() and getHeight() too early: I think the UI has not been sized and laid out on the screen yet...
You can try to put that code inside this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Call here getWidth() and getHeight()
}
Another way
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
width = button.getWidth();
height = button.getHeight();
}
});
put this in your loop
x = m_pBtnDay[i][j].getWidth();
y = m_pBtnDay[i][j].getHeight();
Log.d("width", Integer.toString(x));
Log.d("Height", Integer.toString(y));
try it
You try to use the count-variable "i" and "j" to use outside from both for-loops. That should raise an exception because the are only availabil in each for-block.
Try to make the output in the second for-loop...
for (int i = 0; i<6; i++)
{
...
for(int j=0; j<7; j++)
{
....
x = m_pBtnDay[i][j].getWidth();
y = m_pBtnDay[i][j].getHeight();
Log.d("width", Integer.toString(x));
Log.d("Height", Integer.toString(y));
}
}
You can override the following View method, called during the layout phase.
protected void onSizeChanged (int w, int h, int oldw, int oldh)
This seems more appropriate, as it expressly accounts for the zero width and height you're observing:
This is called during layout when the size of this view has changed.
If you were just added to the view hierarchy, you're called with the
old values of 0.
Parameters w Current width of this view. h Current height of this
view. oldw Old width of this view. oldh Old height of this view.
See here.
Try this can also work:
btn.getDrawingCache().getHeight() and btn.getDrawingCache().getWidth()