Ted pottel
Ted pottel

Reputation: 6983

Trying to change the width of a button

I'm trying to get the width of the screen and then change the width of the button. I'm using setWidth, and the width is not changing (I set it to a small numer, 30, so I could tell if the size change. code mBut = (Button)findViewById( R.id.butRington); // this is not working, set the size small so i can really tell mBut.setWidth(30); mBut.setOnClickListener(this);

layouut

 <LinearLayout 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" 
android:orientation="horizontal" > 

        <Button
        android:id="@+id/butVol"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="24px"
        android:text="Volume"   
        android:textColor="#ff0000ff"

    />

    <Button
        android:id="@+id/butRington"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textSize="24px"
        android:text="Rington"  
        android:textColor="#ff0000ff"

    />  

Upvotes: 0

Views: 64

Answers (3)

felipe.barboza
felipe.barboza

Reputation: 26

Getting the width of the screen:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x

Now you need to get your button, and then set the new width:

Button b = (Button) findViewById(R.id.button1);
b.setWidth(width);

Upvotes: 0

Joseph Selvaraj
Joseph Selvaraj

Reputation: 2237

After hanging layout_widht to wrap_content, I am able to change the width to what every size I need.

<Button
    android:id="@+id/butRington"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="R"
    android:textColor="#ff0000ff"
    android:textSize="24px" />

Upvotes: 0

fedepaol
fedepaol

Reputation: 6862

You need to modify the LayoutParams of your button.

mBut.setLayoutParams(new LinearLayout.LayoutParams(30, xxxx));

where xxx is the height of your button. You can retrieve it by

ViewGroup.LayoutParams params = mBut.getLayoutParams();
params.height;

Check this post

Upvotes: 1

Related Questions