How to change the dynamically displayed Layout

Asked

Viewed 1,390 times

4

I want to do this, I currently have a Listview that is embedded within my activity_main.xml as follows:

<include layout="@layout/lista_categoria" />

I would like to know a way to display another layout (at runtime) within this include if Listview is empty

for example showing the layout_404.xml layout if you have nothing to display.

4 answers

3


Cannot change the contents of a <include>, but it is possible to do so in Viewgroup., because you can add and remove views his.

Where you want to replace a view on the other place a Framelayout and assign it a ID.

Create a xml for each of these views.

Create an object for each of them:

view1 = getLayoutInflater().inflate(R.layout.view1, null);
view2 = getLayoutInflater().inflate(R.layout.view2, null);

Get the reference to Framelayout:

frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

Add the first view at the Framelayout:

frameLayout.addView(layout1);

When you want to change view, remove the views of Framelayout and add the new:

frameLayout.removeAllViews();
frameLayout.addView(layout2);

If you just want to put a string when the list is empty, see this reply.

  • I’ve done it with include. It’s not as elegant as this alternative you posted, but it works.

  • @Tiagopcasemiro I saw your answer, but it doesn’t change layout, it just changes visibility. However, it is a valid option/solution.

0

You have to set Ids for layouts, type ex:

<LinearLayout ...
android:id="@+id/Layout1">

In Activity you want, instance a layout, and assign it by ID:

LinearLayout layout1 = (LinearLayout) findById(R.id.Layout1);

LayoutInflater layoutInflater = (LayoutInflater) 
    this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
layout1.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) ); 

Obs, attention to layout type, (linear, Relative.. etc)

0

<include id="@+id/layout_1" layout="@layout/lista_categoria" android:visibility="visible" />
<include id="@+id/layout_2" layout="@layout/layout_404" android:visibility="gone" />

View layout1 = findViewById(R.id.lista_categoria);
View layout1 = findViewById(R.id.layout_404);

// Para fazer a troca
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);

// Para desfazer a troca
layout1.setVisibility(View.VISIBLE);
layout2.setVisibility(View.GONE);

It is not elegant anymore it works. The listview already has an implemenation for empty layout. Look at this link: https://stackoverflow.com/questions/5565451/display-no-item-message-in-listview

0

Another way would be this.

 <TextView android:id="@+id/text1"
     android:textSize="16sp"
     android:textStyle="bold"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

 <TextView android:id="@+id/text2"  //Esse layout aqui seria seu layout 404 customizado
     android:textSize="16sp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.