I have a problem with the view stub.
This is the ViewStub in my java file
ViewStub stub = (ViewStub) findViewById(R.id.stub1);
ArrayAdapter<String> content=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,files);
View inflated=stub.inflate();
ListView people=(ListView)inflated.findViewById(R.id.friends);
people.setAdapter(content);
This is the xml where I initialize the Viewstub
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "#+id/linear_details" >
<include layout="#layout/home_back_next_bar" />
<ViewStub android:id="#+id/stub1" android:inflatedId="#+id/showlayout"
android:layout="#layout/layout1" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</merge>
my layout file inflated in the view stub is as follows:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/friends"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
This code gives me a NullPointerException when creating instance of the listview to fill it.
Any Ideas what causes this problem and how to solve.
Thanks alot in advance
Error in Log cat:
The reason it didnt work:
According to the docs :
http://developer.android.com/reference/android/view/ViewStub.html#inflate()
You are doing a findViewById on "The inflated layout resource" which is the very listview you are looking for.
.
Instead of using
ListView people=(ListView)inflated.findViewById(R.id.friends);
I used
ListView people=(ListView)stub.inflate();
And it worked no idea why??. but it did :))
Related
I need to detach the gmt_option_list_dst_off.xml and attach a new layout gmt_option_list_dst_on.xml in the <include>tag, when button is clicked.
Following is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="6" >
<include
android:id="#+id/gmt_optionlist"
android:layout_weight="5"
layout="#layout/gmt_option_list_dst_off" />
<Button
android:id="#+id/dstbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="DST Off" >
</Button>
</LinearLayout>
Suggest me is this possible to attach a new layout to include tag at runtime.
I have gone through few post, and currently i am using
parentOfAllInGMT.addView(layoutInflater.inflate(R.layout.gmt_option_list_dst_on, parentOfAllInGMT, true), 0);
but this is not correct.
use addView() and removeViewAt called on LineaerLayout that contains include tag
Try this, it should do the trick:
ViewStub stub = (ViewStub) findViewById(R.id. gmt_optionlist);
stub.inflate();
Hope this helps.
My main xml layout file....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayoutPlayerActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundimage"
android:orientation="vertical" >
</LinearLayout>
I created the viewpager.....
that layout file......
<?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.support.v4.view.ViewPager
android:id="#+id/carousel_layout_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:overScrollMode="never" />
</LinearLayout>
I load the images into this view pager by creating three classes & another xml file......
now I want to add this viewpager into linearlayout of main xml file....
It means I want dynamically view the viewpager in mainxml file. how to that? pls give me answer
thankyou
If you have two Layouts such as LinearLayout you can add one onto the other with addView(). In your Activity it would look something along the lines of,
LinearLayout layout = (LinearLayout) findViewById(R.id.relLayoutPlayerActivity);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout.addView(inflater.inflate(R.layout.idOfLayoutHoldingYourViewPager, null));
There is a ListView in layout of my Activity.
listview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/MyListView" />
</LinearLayout>
I am using another layout for ListItem of ListView.
listitem_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TextView
android:id="#+id/txtTitle"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<Spinner
android:id="#+id/MySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
My Activity’s layout is the same with the listview_layout.xml’s.
I use SimpleAdapter cited the layout of listitem_layout.xml.
The problwm: Now I want to get the Spinner control ID of listview_layout.xm.
I use findViewById(MySpinner) in activity of ListView, it has the error of null pointer exception.
can not find. Because the Activity layout files use the listview_layout.xml’s and Spinner control in listview_layout.xml.
anyone's help is very thankful.
you cannot directly use the findViewById(spinnerId). You have to override the getView() method of Custom Adapter instead of Simple Adapter and inside that inflate the listitem_layout to a view(v) and then use v.findViewById(spinnerId).
Refer the below link which will help you in creating custom adapter (Weather Adapter in the link's example). in the example ImageView object is created in the GetView method. Instead of that you use Spinner.
I have a "Tools.java" that shows "tools.xml" correct? Within this "tools.xml" RelativeLayout have a layout id = #+/contentLayout that is empty.
And I have several other activits "tool1.java", "tools2.java" ... that shows "tool1.xml", "tool2.xml" ...
How to call the class "tool1.java" for example and have it show the "tool1.xml" within the RelativeLayout that is in "tools.xml"
Thanks
<?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"
>
<include
layout="#layout/yourxmlfilename" />
</RelativeLayout>
I want to decompose my UI into several XML Layouts. The first one would be the main layout, and the other ones would be the content layouts.
I would like to be able to set which content_layout should be included dynamically at run-time, so I don't want to set a "layout="#+layout/content_layout" in my XML file.
Here are my layouts:
main_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="800dp" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<include /> <!-- I WANT TO INCLUDE MY CONTENT HERE -->
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Cancel" />
</RelativeLayout>
content_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/whatever"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
content_layout2.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/whatever2"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
How can I do that?
Thanks!
<RelativeLayout android:id="#+id/rl" ...
In your code:
// get your outer relative layout
RelativeLayout rl = (RelativeLayout) findById(R.id.rl);
// inflate content layout and add it to the relative layout as second child
// add as second child, therefore pass index 1 (0,1,...)
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );
You could try using a ViewStub and just change which layout it is going to inflate programatically.
This answer discusses using one ViewStub.