Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - xml - include same layout multiple times does not work

I'm trying to include the following layout twice:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<com.viewpagerindicator.TabPageIndicator
    android:id="@+id/indicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

like the following

<include
    android:id="@+id/include1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/view_pager" />

<include
    android:id="@+id/include2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/view_pager" />

Actually, the second view pager does not work if I do it like that... The second view pager always stays empty... If I copy my view_pager layout and change the ids in this layout (pager to pager2 and indicator to indicator2) everything works fine. Is there a better way to do that? Copying layouts to achieve that seems to make the include useless for multiple includes of the same layout....

I'm getting the references correctly I think, but though it just does not work if I include the same layout...

pager1= (ViewPager)(findViewById(R.id.include1).findViewById(R.id.pager));
pager2= (ViewPager)(findViewById(R.id.include2).findViewById(R.id.pager));

Everything works perfectly if I copy the layout...

Edit:

I think it has to do with the FragmentManager, because the view pagers have the same id... But I don't know how to solve that correctly...

like image 278
prom85 Avatar asked Jun 18 '13 14:06

prom85


People also ask

Can you use a same XML layout for 2 different activities?

Yes, you can. Just call "setContentView" with the same xml and it will use the same layout. Change whatever you need to programmatically during runtime.

How can use multiple layouts in one activity in Android?

To efficiently reuse 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.

Can an activity have multiple layouts?

Yes its possible. You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R.


1 Answers

Yes it can be done. You can inflate the layout many times, but you have to make the inclusion programmatically. See the answer to same kind of question.

like image 93
flexman Avatar answered Sep 23 '22 02:09

flexman