Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android- setWidth() of Button doesn't work, if ViewGroup's width is 'match_parent'/'fill_parent' in xml?

I defined a LinearLayout as root element in activity_main.xml.

CASE 1: From onCreate() I tried to add Button in this Vertical LinearLayout, what confused me is, as per Google's API, I tried to call setWidth(20) on button before adding it in ViewGroup, but Button occupied width 'match_parent' rather than 20dp.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:id="@+id/first_layout">
    </LinearLayout>

//Inside onCreate() of activity..    
    LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
            Button button = new Button(this);
            button.setText(R.string.click_on_me);
            button.setWidth(20);
            firstLayout.addView(button);

Case 1

CASE 2:on setting layout_width of LinearLayout to 'wrap_content', and calling setWidth(20), It now considered given explicit width value i.e 20dp.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:id="@+id/first_layout">
</LinearLayout>

//Inside onCreate() method
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
        Button button = new Button(this);
        button.setText(R.string.click_on_me);
        button.setWidth(20);//In this case, its working
        firstLayout.addView(button);

enter image description here

CASE 3: Finally, removing my custom call to setWidth(20), Button acquired width as 'wrap_content'.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:id="@+id/first_layout">

</LinearLayout>

//Inside onCreate() method.
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
        Button button = new Button(this);
        button.setText(R.string.click_on_me);        
        firstLayout.addView(button);

enter image description here

Ques : So it is clear by case-2, that using LayoutParams is not necessary, if I wish to explicitly use setWidth() method.Then In Case 4: i.e LinearLayout's width set as 'match_parent' and button.setWidth(20) is also called. But why Button is still not taking explicitly given width value, again output is exactly same as CASE 1.

Thanks in advance.

like image 373
itsMohitGoel Avatar asked Sep 10 '15 07:09

itsMohitGoel


People also ask

What does Match_parent mean in Android?

MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. Introduced in API Level 8.

What is Match_parent and Wrap_content in Android?

Conclusion : fill_parent and match_parent are the same, used when we want the height or width of a view to be as big as its parent view, fill_parent being deprecated. wrap_content is used when we want the view to occupy only as much space as required by it. You may also read : Android UI Layouts.

What is LayoutParams?

LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes for a list of all child view attributes that this class supports. The base LayoutParams class just describes how big the view wants to be for both width and height.


1 Answers

You need to define an appropriate LayoutParams for your button view. Then add it to your firstLayout.

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = XX;
params.width = XX;
button.setLayoutParams(params);
like image 95
Alex Chengalan Avatar answered Nov 15 '22 09:11

Alex Chengalan