I have a ListView in a file called a.xml.
<ListView
android:id="#+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbars="none"
android:layout_above="XYZ"
/>
In a.xml, I also include another file called b.xml.
<include android:id="#+id/bottombar" layout="#layout/b" />
For one of the settings of the ListView, I want to reference an id (XYZ) that exists in b.xml.
Is there a way I can do this?
I've tried the following:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="#+id/bottombar" layout="#layout/b" />
<ListView
android:id="#+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbars="none"
android:layout_above="#id/bottombar_background"
/>
</RelativeLayout>
Where #id/bottombar_background exists in #layout/b, but Eclipse throws "no resource" errors at me.
After you have used setContentView(R.layout.a) you should be able to use findViewById(R.id.my_id_in_b).
Use this syntax: android:layout_toLeftOf="#+id/my_id_from_b". It prevents any reference issues.
The plus in this instance tells the compiler to do a second pass on the layout, looking for that id on the second time through. Try it out - I know it works, as I use this syntax all the time.
Related
I would like to display the following five times
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/message"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/add"/>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/separator_height"
android:background="#color/separator"/>
Thus I store it in data_item.xml and try calling it via
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/data_one"
android:layout_height="match_parent">
<include layout="#layout/data_item"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/data_two"
android:layout_height="match_parent">
<include layout="#layout/data_item"/>
</LinearLayout>
However if I do that, then all of the items inside data_item.xml layout are going to have duplicate Ids. How would I go about making sure that they do not have duplicate ids, or at least somehow retrieving them? Let's say I want to get title view text from data_three liner layout. How would I do that?
the first question is why you don't use ListView instead and set values in your adapter? this is better.
But if you have to design your layout in this way, you have to at first get reference from your LinearLayout, the find your view by using LinearLayout reference, as following:
LinearLayout dataOne = findViewById(R.is.data_one);
TextView dataOneTitle = dataOne.findViewById(R.id.title);
I have an image in my main.xml as follows
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="#string/hello"
android:maxHeight="70dp"
android:maxWidth="70dp" />
but I need to pass it to another xml file. Is this possible?
In your case you are having data reference.
Yes, its possible. Exists a tag called in android that you can "include" another android xml layout in other android xml file. I put a example using your main.xml
In layout2.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 layout="#layout/main"/>
</LinearLayout>
More information about this tag, please check it out in the link below.
http://developer.android.com/training/improving-layouts/reusing-layouts.html
I want to make a activity like a "Dialog", and I know two ways so far:
Way 1) In Android ApiDemos, it is implemented by adding the attribute to the activity like
android:theme="#android:style/Theme.Dialog">
The result is: the new Activity appears on top of the existing activity, that is what I want.
Way 2) I try to invoke setTheme(android.R.style.Theme_Dialog) in the Activity.onCreate(Bundle) method, and the new activity also appears, but the background is all black. This is not what I want. Code is as below:
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Dialog);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.selecte_date);
Can anybody tell me how to implement the effect by writing Java code?
The easiest way is to add a second layout in you xml which is a overlay of existing one.
then you can set the overlay to visible in your activity. So then you got multiple views in 1 activity.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#android:color/transparent">
<ImageView
android:id="#+id/icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_alignParentTop="true"
android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/translucent_black"
android:visibility="false" >
</LinearLayout>
</RelativeLayout>
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 am trying to create template/BaseActivity class for other developers to use, as a part of framework.
I extended my class with TabActivity, and my xml looks like this/
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<View android:layout_width="fill_parent" android:layout_height="0dip"
android:background="#000" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="0dip" android:layout_marginRight="0dip" />
<View android:layout_width="fill_parent" android:layout_height="2dip"
android:background="#696969" />
<View android:layout_width="fill_parent" android:layout_height="1dip"
android:background="#fff" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
when I use this activity by extending in other project, I get an error stating, it is not able to get android.R.id.tabhost and this is necessary.
This happens, even if i call getTabHost() or findViewById() both scenarios.
Please note : I tried this without extending the TabActivity, for the use of views in tab. It works fine. But I want to use activities as my tab content.
I think this is the issue when we make it a library project and include it.
Please let me know if you need more explanation or if you know some workarounds, please suggest.
I got around with the issue.
Provided a library class which requires user to pass the current activity instance to my templateLibrary.
It calls sets the Conetnt as one of the layout file containing android tab.
Then get the tab host.
Let me know if anyone wants more details on this.
If you are developing a framework providing layout to user.