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 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);
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);
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With