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.

11 comments:

Anonymous 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.

Unknown said...

whoo hoo! thx.

Kevin said...

You could also accomplish a similar idea by using a ViewSwitcher. I have an old tutorial here: http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/

Jelena Nikolic said...

please can you send complete Java code on jejazz@gmail.com. I tried this and it doesn't work for some reason, and I am realy new at this

thank you so so much

Anonymous said...

If u want to switch between layouts I have another way :
In your main activity in onCreate()method define a button like this:

Button btnNewLayout = (Button)findViewById(R.id.btnNewLayout);
btnNewLayout.setOnClickListener(this);


And in the onClick() method...

public void onClick(View v) {

switch (v.getId()) {

case R.id.btnNewLayout:
setContentView(R.layout.newLayout);
break;
...
}

Make shure to have created the newLayout:)))

Deepak said...

Thanks Brother....
Love You....

Satya said...

Thanks a lot man it help me a lot

Anonymous said...

MANJUNATH,

Ig I have a button with android:onclick:="SwitchLayout1"

How should I write
private void SwitchLayout1()

thks

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google