combining wrap_content on parent and fill_parent on child
In theory what you are describing should not work ("Because it the parent gets it's height from the childs and vice-versa".) However, we made it work in LinearLayout because it was a very common use case. I recently added similar support to FrameLayout (this feature should be part of Honeycomb.) What you are doing is therefore perfectly valid and will work just fine.
Element with wrap_content width takes full width of parent
You can use layout_weight
as mentioned in @rajan answer, but with inequal distribution. eg:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:maxLines="1"
android:text="@string/dummy_paragraph"
android:textColor="?android:attr/textColorTertiary"
android:textSize="14sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_weight="0.3"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="@string/dummy"
android:textColor="?android:attr/textColorTertiary"
android:textSize="14sp"/>
</LinearLayout>
Or use RelativeLayout
as parent and play with alignements. eg:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:maxLines="1"
android:text="@string/dummy_paragraph"
android:textColor="?android:attr/textColorTertiary"
android:textSize="14sp"
android:layout_toLeftOf="@+id/textView3"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="@string/dummy"
android:textColor="?android:attr/textColorTertiary"
android:textSize="14sp"
android:layout_alignParentRight="true" />
</RelativeLayout>
How can I wrap content in both directions 2 views to best use parent space
First of all I tried to find if there are any default implementation of that in android default views but there no XML attribute to use for that, even in TableLayout
although we can make a simple formula to calculate that and give yours views similar height without care about the width!
Here is how:
In your class declare member variables of the views of interest!
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private Button btn_one;
private Button btn_two;
Then in the onCreate
method, after setContentView()
get the views by id for example
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout)findViewById(R.id.activity_main);
btn_one = (Button) findViewById(R.id.btn_one);
btn_two = (Button) findViewById(R.id.btn_two);
After that here comes the most important part! We need to find the width of the linear layout before we know how to divide it to the buttons.But we cant use the width by calling linearLayout.getWidth()
because we are in onCreate()
and the linear layout may have not been created yet and the method will return zero. So in the onCreate()
we are going to use a Runnable
that will give us a confirmation that the linearLayout
is made and so it will run.
In onCreate()
add the code:
linearLayout.post(new Runnable() {
@Override
public void run() {
balanceHeight(btn_one,btn_two);
}
});
}
balanceHeight
is the method that we are going to use to divide our width so as to balance Height.
Outside your onCreate()
but inside your activity class copy the following code!:
private void balanceHeight(Button btn_one, Button btn_two) {
int length1 = btn_one.getText().length();
int length2 = btn_two.getText().length();
int total = length1+length2;
int total_width=linearLayout.getWidth();
LinearLayout.LayoutParams params1=new LinearLayout.LayoutParams((int)(((float)length1/(float)total)*(float) total_width), ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams((int)(((float)length2/(float)total)*(float) total_width), ViewGroup.LayoutParams.WRAP_CONTENT);
btn_one.setLayoutParams(params1);
btn_two.setLayoutParams(params2);
}
This method accept Two buttons as parameters and get their text length compare them and use the linear layout width to divide it according to their content and available space. I have casted values to float to improve accuracy.
Note: When importing layout params class make sure its LinearLayout.LayoutParams
every root views has .LayoutParams
so make sure you import the right one.
Also if you real want button you can continue using it. But in Android any View is clickable if you real want a button do it, but if not use a TextView and set OnClickListener to it!
stretching a layout by widest child view and match_parent second child
Why not set both to match_parent
?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="Loooong"/>
<TextView
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:text="Short"/>
</LinearLayout>
You don't need onMeasure
()
LinearLayout children - all views should have the same height and at least wrap their content
Use a height of wrap_content
for the outer LinearLayout
, and use a height of match_parent
for both of the children.
I know it seems a little bit odd, but as long as none of the children of the outer LinearLayout
have a deterministic size (either fixed or wrap_content
), the resulting behavior will be that each child is the height of the tallest child.
Related Topics
Difference Between App-Debug.Apk and App-Debug-Unaligned.Apk
Zipalign - Command Not Found - MAC Terminal
Margins of a Linearlayout, Programmatically with Dp
List View Item Swipe Left and Swipe Right
How to Find Out Carrier's Name in Android
How to Get The Dimensions of a Drawable in an Imageview
Viewpager Update Fragment on Swipe
How to Use a Library Project in Android Studio
Remove Animation/Shifting Mode from Bottomnavigationview Android
How to Run a Specific Android App Using Terminal
Android 4.3: Ble: Filtering Behaviour of Startlescan()
Android: Share Session Between Webview and Httpclient
How to Clear The Android Stack of Activities