Fix Layout issue

97 Views Asked by At

Scenario: I've four buttons arranged Relative Layout. These button's text varies through the app life which makes button shrink or expand according to text length. Using: RelativeLayout.getChildAt().setText();

Q1) But I require the each button to occupy 40% of screen width but with varying height. Q2) And in particular my code requires to access buttons using getChildAt().

What Layout type should I use to replace RelativeLayout to set Button's width to 40% of screen width and in particular so I can access these Buttons using getChildAt()? SomeLayout.getChildAt(); so that the Layout is immediate parent of Buttons.

    <RelativeLayout android:id="@+id/buttonRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_margin="10dp">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/optionButton1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/optionButton2"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/optionButton3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/optionButton4"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>
3

There are 3 best solutions below

0
On BEST ANSWER

You are actually just in luck. Android just released a new percent support library. They don't have documentation yet, but here is a good sample project that you can use to see how to go about using the library. It basically allows you to size view widgets by percentage of the screen.

https://github.com/JulienGenoud/android-percent-support-lib-sample

And here are two articles talking about it as well:

http://www.androidauthority.com/using-the-android-percent-support-library-630715/

http://inthecheesefactory.com/blog/know-percent-support-library/en

2
On

Right now I can't think of any layout element that will allow you to do that. You can try this, on your code onCreate or on one of your init views/variables method, get the buttons and set the size to 40% of the screen, something like:

Display display = getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);

RelativeLayout rlParent = findViewById(R.id.<RELATIVE_LAYOUT_ID>);

for(int i = 0; i < 4; i++)
{
    View btn = rlParent.getChildAt(i);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)btn.getLayoutParams();
    params.width= screenSize.y * 0.4f;//40%
    btn.setLayoutParams(params);
}
0
On

Android percent support library does a awesome work of giving percentage ratio for android widgets on the screen there by replacing our traditional LinearLayout

Github demo Here !

<android.support.percent.PercentRelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/fifty_huntv"
        android:background="#ff7acfff"
        android:text="20% - 50%"
        android:textColor="@android:color/white"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%" />
    <TextView
        android:layout_toRightOf="@id/fifty_huntv"
        android:background="#ffff5566"
        android:text="80%-50%"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="50%"
        />
</android.support.percent.PercentRelativeLayout>

Really awesome !!!