Android: How to inflate different layout from different buttons?

Artiom Starovoit

I trying to do making something tabs but using button.

but each time I click on button it add me a row. I want it to change the all layout to another..

MainActivity

public class MainActivity extends Activity {
Button btn1;
Button btn2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(switchListener);
    btn2 = (Button) findViewById(R.id.button2);
    btn2.setOnClickListener(switchListener);

}

private OnClickListener switchListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        //ScrollView sv = (ScrollView) findViewById(R.id.MyScrollView);

        LinearLayout ll = (LinearLayout) findViewById(R.id.MyLinearLayout);

        LayoutInflater inflater = LayoutInflater
                .from(getApplicationContext());

        if (btn1 == v) {

            View v1 = inflater.inflate(R.layout.firstview, null);

            ll.addView(v1);

        } else {

            View v2 = getLayoutInflater().inflate(R.layout.secondview, null);

            ll.addView(v2);
        }

    }
};

}

and here my Main XML file

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Layout 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Layout 2" />
</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <ScrollView
        android:id="@+id/MyScrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/MyLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

        </LinearLayout>

    </ScrollView>
</TableRow>

and here my other two layouts that I want to inflate

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="First View"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center" 
    android:background="@android:color/holo_blue_light"/>

and the second

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Second View"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center" 
    android:background="@android:color/holo_red_light"/>

duggu

First make match parent your tableview.

and now do below on your code:-

   if(ll.getChildCount() > 0) {
       ll.removeAllViews(); 
   }

   if (btn1 == v) {

        View v1 = inflater.inflate(R.layout.firstview, null);

        ll.addView(v1);

    } else {

        View v2 = getLayoutInflater().inflate(R.layout.secondview, null);

        ll.addView(v2);
    }

the problem is you are just adding child on layout one by one so you need to remove first then add into layout so try above code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to inflate a layout multiple times but with different data in its listview?

From Dev

baseAdapter in android and inflate different layouts

From Dev

multiple buttons in fragment, how to redirect to a different layout

From Dev

Android how to open same activity with different relative layout views when onclick buttons?

From Dev

Android - How to add several buttons with different layout_margins in a LinearLayout programmatically?

From Dev

Actual Android layout is different from designer

From Dev

Android - How to inflate an activity layout to fragment?

From Dev

Android - How to inflate an activity layout to fragment?

From Dev

How to load a different keyboard layout for subtype in android?

From Dev

How to know screen Size to different layout in android

From Dev

Inflate different layouts in adapter

From Dev

android: inflate views into a layout

From Dev

Inflate programmatically for android layout

From Dev

android: inflate views into a layout

From Dev

Not able to inflate a layout in android

From Dev

How to inflate layout in EditMode?

From Dev

How to set different icons to different buttons in Qt?

From Dev

How to set different icons to different buttons in Qt?

From Dev

Android: different layout to support different fontSize

From Dev

How to make a list of buttons listen to event from a different element

From Dev

android fragment - how to inflate the layout and then make the UI functions

From Dev

android - how to inflate a map fragment in an already inflated layout?

From Dev

Android same layout different data

From Dev

android layout at different screen sizes

From Dev

Working with android different layout resources

From Dev

Android same layout different data

From Dev

Inflate two layouts with different functionalities

From Dev

Android: Unable to inflate EditText in the layout

From Dev

How to re-inflate layout from outside of onCreateView

Related Related

  1. 1

    How to inflate a layout multiple times but with different data in its listview?

  2. 2

    baseAdapter in android and inflate different layouts

  3. 3

    multiple buttons in fragment, how to redirect to a different layout

  4. 4

    Android how to open same activity with different relative layout views when onclick buttons?

  5. 5

    Android - How to add several buttons with different layout_margins in a LinearLayout programmatically?

  6. 6

    Actual Android layout is different from designer

  7. 7

    Android - How to inflate an activity layout to fragment?

  8. 8

    Android - How to inflate an activity layout to fragment?

  9. 9

    How to load a different keyboard layout for subtype in android?

  10. 10

    How to know screen Size to different layout in android

  11. 11

    Inflate different layouts in adapter

  12. 12

    android: inflate views into a layout

  13. 13

    Inflate programmatically for android layout

  14. 14

    android: inflate views into a layout

  15. 15

    Not able to inflate a layout in android

  16. 16

    How to inflate layout in EditMode?

  17. 17

    How to set different icons to different buttons in Qt?

  18. 18

    How to set different icons to different buttons in Qt?

  19. 19

    Android: different layout to support different fontSize

  20. 20

    How to make a list of buttons listen to event from a different element

  21. 21

    android fragment - how to inflate the layout and then make the UI functions

  22. 22

    android - how to inflate a map fragment in an already inflated layout?

  23. 23

    Android same layout different data

  24. 24

    android layout at different screen sizes

  25. 25

    Working with android different layout resources

  26. 26

    Android same layout different data

  27. 27

    Inflate two layouts with different functionalities

  28. 28

    Android: Unable to inflate EditText in the layout

  29. 29

    How to re-inflate layout from outside of onCreateView

HotTag

Archive