Google Search

Saturday, July 4, 2009

Switching between Layouts in Android with setVisibility

Some times you may want to hide part of the screen content or layout at run time and make it visible at later point in time because changing the whole activity or content view may not be feasible at certain scenario and simply does not work so setting visibility of Layout is a good idea below is sample code with RelativeLayout on how to do it

below is my main xml defined "layoutsample.xml"



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sampleLayoutExample"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
>

<RelativeLayout
id="@+id/layout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!-- Your Content view here -->
<TextView
android:id="@+id/sampleText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From Layout One"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</TextView>

</RelativeLayout>

<RelativeLayout
id="@+id/layout2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!-- Your Content view here -->
<TextView
android:id="@+id/sampleText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From Layout two"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</RelativeLayout>
</RelativeLayout>

Now in above xml i have defined two layouts and two textviews where sample text will be displayed in center of the screen, below is the JAVA code on how to manage two layouts



private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}

private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}

There are three basic values you can pass View.VISIBLE, View.INVISIBLE, View.GONE depending on whether you want to hide, show or discard the layout from view.

4 comments:

sumitasok said...

Thanks Manjunath...

shishir said...

can u post whole code

or can u mail @

shishir.bobby@gmail.com


Thanks and regards
shishir

ashoon said...

Nice. I've been hunting for days for this example. Thanks!

Tuan said...

Can you post the full Java code part, because when I try to do this, an error occurred when I call setContentView(). Thanks.

Post a Comment

Other Interesting Articles



Related Article Widget by ceveni

Search Powered by Google