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...
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.
Related
I want to have 3 layouts in my ScrollView but when I add them it actually doesn't scroll. I tried to put all layouts in different files, then include them, tried to put them in ListView and it doesn't work too. In this option, when I put in ScrollView the LinearLayout and there include the rest of the layouts the application show nothing. That's probably becouse I don't know how to refer to that nested layouts... This is my XML:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include layout="#layout/first"/>
<include layout="#layout/second"/>
<include layout="#layout/third"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
#EDIT views included (all are the same so I put just one):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
And in the main activity I put the objects into layouts like this:
setContentView(R.layout.activity_main);
RelativeLayout first = (RelativeLayout) findViewById(R.id.first);
RelativeLayout second = (RelativeLayout) findViewById(R.id.second);
RelativeLayout third = (RelativeLayout) findViewById(R.id.third);
...some code creating views...
first.addView(myView1);
second.addView(myView2);
third.addView(myView3);
Need to put fix size in included layout, for example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
I don`t understand why do you need to add view manually when you already added it to the layout.
You should also eliminate top Linear Layout since there is no use of it.
Image as seen in ICS
Where as when run in Gingerbread
XML code for the fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background"
android:id="#+id/homelayout" >
<ImageView
android:id="#+id/homepageLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background"
android:alpha="255"
android:scaleType="fitXY" />
</LinearLayout>
Any reason for such strange behavior?
try changing
android:layout_width="match_parent"
android:layout_height="match_parent"
to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
in your ImageView. I don't know how you use your XML, but by the looks of it you might also want to change te same parameters in the LinearLayout to "wrap_content". Now you are telling the ImageView to (probably) fill the whole area.
// Alex
The problem is observed only when you dynamically request for the actionbar from the code.
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
If you do it using styles, this problem is not seen.
It's is possible to "stack" fragments on top of each other?
Just tried to do this without really thinking about it and everything got kinda crazy.
I can (and probably should) create a ViewGroup instead if it's not possible, but I was just wondering.
Yes, it's possible. E.g. by wrapping them inside a FrameLayout like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.package.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<fragment android:name="com.package.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
The z-index depends on the order of the children within the layout. In this example FragmentTwo is above FragmentOne because it's the second child.
I have trouble accessing Views from a layout that is included in another layout.
Please take a look at this picture:
http://dl.dropbox.com/u/3473245/layout_includes.png
How do I access the 4 text views programmatically?
Its probably something really simple that I'm missing.
Thank you very much!
You can do as follows:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include android:id="#+id/item_base_lang" layout="#layout/dictionary_list_item" />
<include android:id="#+id/item_learn_lang" layout="#layout/dictionary_list_item" />
</LinearLayout>
dictionary_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/dictionary_list_item_text_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/dictionary_list_item_text_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
To set the text programmatically:
((TextView)findViewById(R.id.item_base_lang).findViewById(R.id.dictionary_list_item_text_header)).setText("item_base_lang_header");
((TextView)findViewById(R.id.item_base_lang).findViewById(R.id.dictionary_list_item_text_content)).setText("item_base_lang_content");
((TextView)findViewById(R.id.item_learn_lang).findViewById(R.id.dictionary_list_item_text_header)).setText("item_learn_lang_header");
((TextView)findViewById(R.id.item_learn_lang).findViewById(R.id.dictionary_list_item_text_content)).setText("item_learn_lang_content");
This Android wiki page shows how to use reusable UI components with XML layouts, but it doesn't show how to access nested reusable components from code.
Although it is fairly straightforward, it might be not so clear for those who are pretty new to Android Views.
The following two lines should help you get the languageHeader of both includes. You can do the same for languageText
findViewByid(R.id.activityBaseLangView).findViewById(R.id.languageHeader)
findViewByid(R.id.activityLearnLangView).findViewById(R.id.languageHeader)
I have lots of control repeated in my xml (a Button for instance). Is there any possibility to write the Button once in a xml and then import it in all the layout I need it?
You can use
<include layout="#layout/commonlayout" android:id="#+id/id" />
commonlayout.xml should be defined in res/layout where you can add the repeated parts.
As Labeeb P rightly said, it works.
Just want to add that you can also override parameters too:
<include
layout="#layout/commonlayout"
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2sp"
android:layout_marginRight="2sp"
/>
In addition to those great answers, you can also avoid code duplication by using the <merge> tag, like so:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/delete"/>
</merge>
The <merge> part gets stripped when you include it into other xml. This might help including more than a single Button at a time. See the official documentation.
You can use the default include XML tag to include an external layout:
<include layout="#layout/somelayout" />
This layout should have an outside ViewGroup that encapsulates the content or a merge tag to avoid having to use an unnecessary layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_world" />
</LinearLayout>
<!-- OR -->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_world" />
</merge>
Also, if you need a better way to include pieces of layout that acts like a container (a custom ViewGroup), you can use this custom ViewGroup. Note that this does not import an XML into another XML file, it inflates the content from the external layout and replaces into the view. It's similar to ViewStub, a "ViewGroupStub" like.
This lib acts as if the ViewStub could be used as following (note that this example does not work! ViewStub isn't a ViewGroup subclass!):
<ViewStub layout="#layout/somecontainerlayout"
inflate_inside="#+id/somecontainerid">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_world" />
</ViewStub>