I have a layout which has 3 RelativeLayout
s which are placed as follows.
Switch
ImageButton
s placed verticallyButton
s placed verticallyBy default the state of the Switch
is in the off state.
What I want is
Switch
is in the off state RelativeLayout3 should be placed or should overlap RelativeLayout2, i.e the 3rd layout should come up at position 2Switch
is enabled (i.e in the on state), Relativelayout2 should be visible and Relativelayout3 should come below it.Below is my layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bckgrnd_4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Task Name"
android:id="@+id/textView5"
android:layout_alignBottom="@+id/editText"
android:layout_alignStart="@+id/textView6" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="@string/hint_name"
android:layout_marginTop="37dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"
android:id="@+id/button5"
style="@style/btnStyleBeige"
android:layout_marginTop="37dp"
android:layout_below="@+id/textView5"
android:layout_alignStart="@+id/switch1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Set Location"
android:id="@+id/textView6"
android:layout_alignBottom="@+id/button5"
android:layout_toStartOf="@+id/editText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Always alert me"
android:id="@+id/textView7"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/btnStyleBeige"
android:id="@+id/switch1"
android:layout_alignTop="@+id/textView7"
android:layout_alignEnd="@+id/editText" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relative2"
android:layout_marginTop="37dp"
android:layout_below="@+id/switch1"
android:layout_alignParentStart="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton3"
android:src="@drawable/cal"
style="@style/btnStyleBeige"
android:layout_marginStart="42dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton4"
style="@style/btnStyleBeige"
android:src="@drawable/alert"
android:layout_marginStart="67dp"
android:layout_alignBottom="@+id/imageButton3"
android:layout_toEndOf="@+id/imageButton3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relative3"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Task"
style="@style/btnStyleBeige"
android:id="@+id/button6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
style="@style/btnStyleBeige"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/button6"
android:layout_marginStart="37dp" />
</RelativeLayout>
</RelativeLayout>
Please help me...
You can put a listener for the Switch
and toggle the visibility of the layout based on whether the Switch
is on or off. For example,
Switch s = (Switch) findViewById(R.id.switch1);
s.setOnCheckedChangedListener (this);
...
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
RelativeLayout relative2 = (RelativeLayout) findViewById(R.id.relative2);
relative2.setVisibility(isChecked ? View.GONE:View.VISIBLE);
}