Search code examples
androidandroid-layoutresizeandroid-buttonandroid-relativelayout

Android Relative Layout: Button Layout params conflict


I'm trying to make my first Android Calculator, the first code I wrote resulted in a calculator of small buttons, which don't re-size according to the screen size.

I have searched in the Internet and found some ways to do it, and here's the part of the code responsible for that - I have 20 buttons, numbered from 0 to 19 -

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    int width,height;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
    {
        WindowManager w = getWindowManager();
        w.getDefaultDisplay().getSize(size);
        width = size.x;
        height = size.y;
    }
    else
    {
        width = display.getWidth();
        height = display.getHeight();
    }
    Button b;
    TextView text1 = (TextView)findViewById(R.id.screen);
    text1.setWidth(width);
    width/=6;
    height/=7;
    text1.setHeight(height);
    for (int i=0;i<20;i++)
    {
        String ButtonName = "button"+i;
        int ButtonId = getResources().getIdentifier(ButtonName, "id", "calculator.essam.net");
        b = (Button)findViewById(ButtonId);
        RelativeLayout.LayoutParams par = new RelativeLayout.LayoutParams(b.getLayoutParams());
par.width = width;
par.height = height;
        b.setLayoutParams(par);
    }
}

In the XML File, listed are my button layout rules, such as toLeftOf, toRightOf, above, below, alignParentTop, alignParentLeft, alignParentRight, and alignParentBottom.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".WelcomeActivity" >


<TextView
    android:id="@+id/screen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:text="" />

<Button
    android:id="@+id/button0"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button18"
    android:layout_alignParentLeft="true"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="0"
    android:text="@string/number0" />

<Button
    android:id="@+id/button1"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button0"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button4"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="1"
    android:text="@string/number1" />

<Button
    android:id="@+id/button2"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button10"
    android:layout_below="@+id/button5"
    android:layout_toLeftOf="@+id/button3"
    android:layout_toRightOf="@+id/button1"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="2"
    android:text="@string/number2" />

<Button
    android:id="@+id/button3"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button15"
    android:layout_below="@+id/button6"
    android:layout_toLeftOf="@+id/button13"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="3"
    android:text="@string/number3" />

<Button
    android:id="@+id/button4"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button7"
    android:layout_toLeftOf="@+id/button5"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="4"
    android:text="@string/number4" />

<Button
    android:id="@+id/button5"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button8"
    android:layout_toLeftOf="@+id/button6"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="5"
    android:text="@string/number5" />

<Button
    android:id="@+id/button6"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button9"
    android:layout_toLeftOf="@+id/button12"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="6"
    android:text="@string/number6" />

<Button
    android:id="@+id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/screen"
    android:layout_toLeftOf="@+id/button8"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="7"
    android:text="@string/number7" />

<Button
    android:id="@+id/button8"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/screen"
    android:layout_toLeftOf="@+id/button9"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="8"
    android:text="@string/number8" />

<Button
    android:id="@+id/button9"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/screen"
    android:layout_toLeftOf="@+id/button11"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="9"
    android:text="@string/number9" />

<Button
    android:id="@+id/button10"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button19"
    android:layout_toRightOf="@+id/button0"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="."
    android:text="@string/dot" />

<Button
    android:id="@+id/button11"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button12"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/screen"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="+"
    android:text="@string/signPlus" />

<Button
    android:id="@+id/button12"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button13"
    android:layout_alignParentRight="true"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="-"
    android:text="@string/signMinus" />

<Button
    android:id="@+id/button13"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button14"
    android:layout_alignParentRight="true"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="*"
    android:text="@string/signTimes" />

<Button
    android:id="@+id/button14"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button17"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/button15"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="/"
    android:text="@string/signDivide" />

<Button
    android:id="@+id/button15"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button16"
    android:layout_toRightOf="@+id/button10"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="="
    android:text="@string/signEquals" />

<Button
    android:id="@+id/button16"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/button17"
    android:layout_toRightOf="@+id/button19"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="("
    android:text="@string/openBracket" />

<Button
    android:id="@+id/button17"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag=")"
    android:text="@string/closedBracket" />

<Button
    android:id="@+id/button18"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="CLR"
    android:text="@string/clear"
    android:textSize="12sp" />

<Button
    android:id="@+id/button19"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button18"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="click"
    android:tag="DEL"
    android:text="@string/backSpace"
    android:textSize="12sp" />

</RelativeLayout>

The problem is when I use RelativeLayout.LayoutParams All my layout rules are overwritten, and when I use button.setWidth() and button.setHeight(), the layout rules are kept the same, but the size doesn't change.


Solution

  • You should set width and height on existing layout params and not replace it with a new one. You can access the layout params created during layout inflation with getLayoutParams(). That is, replace

    RelativeLayout.LayoutParams par = new RelativeLayout.LayoutParams(b.getLayoutParams());
    

    with

    RelativeLayout.LayoutParams par = b.getLayoutParams();
    

    However, I suggest you forget about this kind of layout and learn to use LinearLayout layout_weight mechanism to make view sizes relative to available space.