android-layout-weight
How do I set android:layout_columnWeight=”1″ programmatically to an element in an android support v7 Gridlayout
I found this method from GridLayout: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/android/widget/GridLayout.java#GridLayout.spec%28int%2Cfloat%29 So maybe you could try something like this: ((GridLayout.LayoutParams) this.getLayoutParams()).columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
Android how to display 2 listviews in one activity one after the other
activity_main.xml <ScrollView xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:fillViewport=”true” > <LinearLayout android:layout_width=”match_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:padding=”10dip” > <TextView android:layout_width=”match_parent” android:layout_height=”wrap_content” android:gravity=”center_vertical” android:text=”ANDROID” /> <ListView android:id=”@+id/listView1″ android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_marginTop=”10dip” android:background=”#B29090″ > </ListView> <TextView android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_marginTop=”10dip” android:gravity=”center_vertical” android:text=”IOS” /> <ListView android:id=”@+id/listView2″ android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_marginTop=”10dip” android:background=”#4A9C67″ > </ListView> </LinearLayout> </ScrollView> MainActivity.java package com.example.listviewin1xmldemo; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.MeasureSpec; … Read more
android:layout_height 50% of the screen size
Set its layout_height=”0dp”*, add a blank View beneath it (or blank ImageView or just a FrameLayout) with a layout_height also equal to 0dp, and set both Views to have a layout_weight=”1″ This will stretch each View equally as it fills the screen. Since both have the same weight, each will take 50% of the screen. … Read more
What is android:weightSum in android, and how does it work?
Adding on to superM’s and Jeff’s answer, If there are 2 views in the LinearLayout, the first with a layout_weight of 1, the second with a layout_weight of 2 and no weightSum is specified, by default, the weightSum is calculated to be 3 (sum of the weights of the children) and the first view takes … Read more
Set the layout weight of a TextView programmatically
You have to use TableLayout.LayoutParams with something like this: TextView tv = new TextView(v.getContext()); tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f)); The last parameter is the weight.
Linear Layout and weight in Android
3 things to remember: set the android:layout_width of the children to “0dp” set the android:weightSum of the parent (edit: as Jason Moore noticed, this attribute is optional, because by default it is set to the children’s layout_weight sum) set the android:layout_weight of each child proportionally (e.g. weightSum=”5″, three children: layout_weight=”1″, layout_weight=”3″, layout_weight=”1″) Example: <LinearLayout android:layout_width=”fill_parent” … Read more
How to set layout_weight attribute dynamically from code?
You can pass it in as part of the LinearLayout.LayoutParams constructor: LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f ); YOUR_VIEW.setLayoutParams(param); The last parameter is the weight.