Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Set ALIGN_PARENT_RIGHT is not working

I try to make an application, where the user can set a "one hand layout" for big devices to input the numbers with only one hand. (See the image for what I am meaning)

enter image description here

When I press on "L" the layout turns to left. This is working. But after this, when I press on L the layout doesn't move. I don't get why... Could you explain me what I am doing wrong please?

Button "L":

Button cmd_change = (Button) findViewById(R.id.cmd_changesite);

cmd_change.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Log.v("TEST: ", ""+shellpreference.getInt("CHANGESITE", 0));
        if(sharedpreference.getInt("CHANGESITE", 0) == 0){

            RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();

            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            relativeLayout.setLayoutParams(params);
            sharedpreference.setInt("CHANGESITE", 1);
        }else{
            RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();

            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            relativeLayout.setLayoutParams(params);
            sharedpreference.setInt("CHANGESITE", 0);
        }

    }
}); 

Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- Einstellung des Gesamtlayouts -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/bg1200x800"
    android:orientation="vertical" >

    <!-- Anzeige txt_standardsaetze -->


    <!-- Einbindung Scoll Funktion -->

    <RelativeLayout
        android:id="@+id/rl_betraege"
        android:layout_width="wrap_content"
        android:layout_height="180dp"
        android:layout_alignParentLeft="true" >

        <RelativeLayout
            android:id="@+id/relativeLayout5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="10dp" >

            <RelativeLayout
                android:id="@+id/relativeLayout4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp" >

                <TextView
                    android:id="@+id/txt_netto_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Netto"
                    android:textSize="18dip" />

                <TextView
                    android:id="@+id/TextView05"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:text=".-"
                    android:textSize="18dip" />

                <TextView
                    android:id="@+id/txt_nettobetrag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/TextView05"
                    android:text="0.00"
                    android:textSize="18dip" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/relativeLayout4"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/line"
                android:src="@drawable/line" />

            <RelativeLayout
                android:id="@+id/RelativeLayout02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/imageView2"
                android:layout_marginTop="10dp" >

                <TextView
                    android:id="@+id/txt_steuer_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="Steuer"
                    android:textSize="18dip" />

                <TextView
                    android:id="@+id/TextView07"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:text=".-"
                    android:textSize="18dip" />

                <TextView
                    android:id="@+id/txt_steuerbetrag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/TextView07"
                    android:text="0.00"
                    android:textSize="18dip" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/ImageView01"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/RelativeLayout02"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/line"
                android:src="@drawable/line" />

            <RelativeLayout
                android:id="@+id/RelativeLayout03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/ImageView01"
                android:layout_marginTop="10dp" >

                <TextView
                    android:id="@+id/txt_bruttodesc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="Brutto"
                    android:textSize="18dip"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/TextView10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:text=".-"
                    android:textSize="18dip"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txt_bruttobetrag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/TextView10"
                    android:text="0.00"
                    android:textSize="18dip"
                    android:textStyle="bold" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/ImageView02"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/RelativeLayout03"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/line"
                android:src="@drawable/line" />

            <ImageView
                android:id="@+id/ImageView04"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_below="@+id/ImageView02"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="1dp"
                android:background="@drawable/line"
                android:src="@drawable/line" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/relativeLayout5"
            android:layout_alignParentLeft="true" >

            <ImageView
                android:id="@+id/ImageView03"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignParentBottom="true"
                android:background="@drawable/line"
                android:src="@drawable/line" />

            <TextView
                android:id="@+id/company"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:text="Eingabe:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:gravity="right"
                android:text=".-"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/edit_betrag"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/company"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/TextView03"
                android:gravity="right"
                android:text="-"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_steuern"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/rl_betraege"
        android:layout_alignRight="@+id/rl_betraege"
        android:layout_below="@+id/rl_betraege"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp" >

        <Spinner
            android:id="@+id/spinner_steuer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/TextView01" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Steuer:"
            android:textSize="18dip" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_eingabeart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/rl_steuern"
        android:layout_alignRight="@+id/rl_steuern"
        android:layout_below="@+id/rl_steuern" >

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Eingabe:"
            android:textSize="18dip" />

        <RadioGroup
            android:id="@+id/radiogrp_netbrut"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/TextView02"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radiobutton_netto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/netto_calc_"
                android:textSize="18dip" />

            <RadioButton
                android:id="@+id/radiobutton_brutto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/brutto_calc_"
                android:textSize="18dip" />
        </RadioGroup>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_parent_changesite"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/rl_eingabeart"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >

        <RelativeLayout
            android:id="@+id/rl_changesite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/cmd_1"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignBaseline="@+id/cmd_2"
                android:layout_alignBottom="@+id/cmd_2"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toLeftOf="@+id/cmd_2"
                android:background="@drawable/zahlen"
                android:text="1"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_4"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_2"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toLeftOf="@+id/cmd_2"
                android:background="@drawable/zahlen"
                android:text="4"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_8"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignBaseline="@+id/cmd_7"
                android:layout_alignBottom="@+id/cmd_7"
                android:layout_toLeftOf="@+id/cmd_6"
                android:background="@drawable/zahlen"
                android:text="8"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_0"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:background="@drawable/zahlen"
                android:text="0"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_7"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_4"
                android:layout_toLeftOf="@+id/cmd_2"
                android:background="@drawable/zahlen"
                android:text="7"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_5"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_2"
                android:layout_alignLeft="@+id/cmd_2"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:background="@drawable/zahlen"
                android:text="5"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_point"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignLeft="@+id/cmd_2"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:background="@drawable/zahlen"
                android:text="."
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_back_betrag"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/cmd_point"
                android:background="@drawable/zahlen"
                android:text="&lt;-"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_clear"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignParentBottom="true"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/cmd_back_betrag"
                android:background="@drawable/zahlen"
                android:text="C"
                android:textSize="25dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/cmd_percent"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_back_betrag"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/cmd_back_betrag"
                android:background="@drawable/zahlen"
                android:text="%"
                android:textSize="25dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/cmd_3"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_back_betrag"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toLeftOf="@+id/cmd_clear"
                android:background="@drawable/zahlen"
                android:text="3"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_6"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_percent"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toLeftOf="@+id/cmd_percent"
                android:background="@drawable/zahlen"
                android:text="6"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_9"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_5"
                android:layout_toRightOf="@+id/cmd_8"
                android:background="@drawable/zahlen"
                android:text="9"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_changesite"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_percent"
                android:layout_alignLeft="@+id/cmd_percent"
                android:layout_marginTop="2dp"
                android:background="@drawable/zahlen"
                android:text="L"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_2"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_clear"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/cmd_0"
                android:background="@drawable/zahlen"
                android:text="2"
                android:textSize="25dp" />

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>
like image 987
Marco Seiz Avatar asked Dec 07 '25 00:12

Marco Seiz


1 Answers

The problem is with the parent layout. It is set to wrap_content.

Change the width of the parent RelativeLayout to match_parent and most likely the problem should be fixed.

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/RelativeLayout01" >

Updated Answer

Along with the above the problem is that, when the preference changes between 0 to 1 and back to 0 you never remove the previously assigned rule. (You only call params.addRule(..) ). This is the problem as now it has both ALIGN_PARENT_RIGHT and ALIGN_PARENT_LEFT.

To resolve this, you could use the removeRule() method:

if(sharedpreference.getInt("CHANGESITE", 0) == 0){
    RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();

    params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    relativeLayout.setLayoutParams(params);
    sharedpreference.setInt("CHANGESITE", 1);
}
else {
    RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();

    params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    relativeLayout.setLayoutParams(params);
    sharedpreference.setInt("CHANGESITE", 0);
}

This method is only supported from API LEVEL 17 (JELLY_BEAN_MR1).

For lower version, simply create a new layout param and assign it to the RelativeLayout as follows:

if(sharedpreference.getInt("CHANGESITE", 0) == 0){
    RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); // add other rules from XML also
    relativeLayout.setLayoutParams(params);
    sharedpreference.setInt("CHANGESITE", 1);
}
else {
    RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // add other rules from XML also
    relativeLayout.setLayoutParams(params);
    sharedpreference.setInt("CHANGESITE", 0);
}

Note: for method 2, you will have to add other rules from your XML file programmatically as you create a new instance of layout params each time so those defined in XML are not applied anymore.

like image 171
Amulya Khare Avatar answered Dec 08 '25 15:12

Amulya Khare