Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i change included xml layout to another layout on java code?

I included second layout to first layout like this:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="300dp"     android:layout_height="match_parent"     android:layout_gravity="clip_horizontal"     android:layout_alignParentLeft="true"      android:id="@+id/rlMenu"     >      <Button         android:id="@+id/bMusteriler"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="Musteriler"         android:textSize="45dp"         android:layout_alignParentLeft="true"          />  </RelativeLayout>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="match_parent"     android:layout_toRightOf="@id/rlEkranlar"      >      <include         android:id="@+id/include1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         layout="@layout/ikinci" />  </RelativeLayout> </RelativeLayout> 

Problem is how can I change included layout when clicked a button(on java code)?

like image 524
UurYsr Avatar asked Dec 21 '11 12:12

UurYsr


People also ask

How do I create a layout in another layout?

To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.

Which layout can be used as better replacement of RelativeLayout?

We can use LinearLayout inside RelativeLayout.

How do I change to linear layout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .


2 Answers

I suggest ViewFlipper inside RelativeLayout of your include statements. Try like this:

<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/vf"     android:layout_width="fill_parent"     android:layout_height="wrap_content" >      <include android:id="@+id/include1" layout="@layout/ikinci" />     <include android:id="@+id/map" layout="@layout/third_layout" />  </ViewFlipper> 

Access ViewFlipper as below. Initially first layout is output:

ViewFlipper vf = (ViewFlipper)findViewById(R.id.vf); 

For Button onClickListener:

button.setOnClickListener(new View.OnClickListener() {              @Override             public void onClick(View v) {                 // TODO Auto-generated method stub                 vf.setDisplayedChild(1);             }         }); 
like image 104
Yugandhar Babu Avatar answered Sep 28 '22 06:09

Yugandhar Babu


There is two ways to change layouts in code:

A. Change visibility. Include tow different layouts in your xml:

<include      android:id="@+id/id1"     android:visibility="visible"/> <include      android:id="@+id/id2"     android:visibility="gone"/> 

and in code use this:

findViewById(R.id.id1).setVisibility(View.GONE); findViewById(R.id.id2).setVisibility(View.VISIBLE); 

B. Manually add children. Use the same xml as you use now and add in code you can add or delete children:

yourRelativeLayout.removeAllViews(); yourRelativeLayout.addView(viewToInclude); 

Offtopic:

You don't need to write xmlns:android param in RelativeLayout. Just for the most top tag in layout file.

like image 36
Jin35 Avatar answered Sep 28 '22 05:09

Jin35