Accessing .xml file from MainActivity class - android

We have MainActivity.class , activity_main.xml and firstlayout.xml.
In firstlayout.xml there is a listview. I am in MainActivity.class and I want to access my listview. How can I do that when my listview is in another .xml file.
Something like
ListView lstview = (ListView) findViewById(R.id.lstview)
does not work, because I get a
null reference error
because my listview is not in mainactivity.xml. I also tried
setcontentView(R.layout.firstlayout)
but it didn't work either.
How can I access a xml file which is NOT in main_activity.xml ?

There are 2 possible way for useing ListView in your mainActivity.
1) You can simply include that layout (firstlayout.xml) into you activity_main.xml file.
example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/firstlayout"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
2) The second option is Layout Inflater.You can simply add other layout file directly into your java file.
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.firstlayout, this);

You can inflate the view with the layout inflater:
getLayoutInflater().inflate(R.layout.firstlayout,
parentView, //the view that logically contains the list view, or just the base view in the main layout
false // false if you want to attach the list view to the parent, true otherwise
);

You can inflate your own:
View firstLayout = getLayoutInflater().inflate(R.layout.firstlayout, null, false);
ListView listView = (ListView) firstLayout.findViewById(R.id.lstView);
Just ensure that firstlayout has a ListView with id lstview.

Related

Android listview inside viewpager not swiping

I have a listview inside of a viewpager. I set an empty view on the listview. If this listview starts empty, when i add something to it then right or left swipes on the listview won't switch between tabs. If the listview starts with something (not empty) when i set the adapter on it, then i can swipe. If i remove the setEmptyView on the listview, it always works whether it starts with data or without it.
How can i set an empty view and still get the listview to swipe between tabs?
This is how i set the empty view:
mListview.setEmptyView(findViewById(R.id.empty_view));
the empty view is a textview directly below the listview in a linearlayout
the empty view xml:
empty_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/empty_list"
android:textColor="#color/lighter_gray"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/no_item"
android:textStyle="italic">
</TextView>
I would suggest removing your empty view from the current layout file, and move it into its own dedicated layout file. Then inflate this new layout in your class when you want to use it as an empty view. The end result would be something like this (not compiled, coding from memory):
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
mListview.setEmptyView(v);
or maybe
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, mListview, false);
mListview.setEmptyView(v);

Custom ListView in ListFragment or Fragment?

I am working on a simple app, so that the main activity has two fragments that user can swipe to go to. On one of them, I want to have custom list. For example, something like this https://github.com/JakeWharton/SwipeToDismissNOA, where each item in the list can be deleted by swiping. I was able to get a regular list to work inside the ListFragment, but can't get this custom list to work. As I understand, ListFragment needs to have a simple .xml with one , but the one that I want to use, has a bit more stuff in it. Something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
android:padding="16dp">
<LinearLayout android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:listSeparatorTextViewStyle"
android:text="ListView" />
<ListView
android:id="#android:id/list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
So when I am trying to getListView() using this .xml, it either returns null or when I am trying to do something like this:
View view = inflater.inflate(R.layout.fragment_section_dummy, null);
ListView ls = (ListView) view.findViewById(android.R.id.list);
It throws this error:
10-15 20:25:28.895: E/AndroidRuntime(8081):
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView.
Thanks.
For me, is easiest work with my own IDs, so I would change your ListView ID to another one. For my example, I choose android:id="#+id/myList".
In your fragment, in your onCreateView method, try inflating your layout and getting your view by ID.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourDummyLayout, container, false);
//You should get your ListView there
ListView yourListView = rootView.findViewById(R.id.myList);
[......]
return rootView;
}
Try with this ;) Good luck !

Insert TextView in other XML file than current ContentView

I want to insert a TextView into a LinearLayout that's in a different XML file that is being used as layout for a ListView. But it seems that I can't access the LinearLayout that's in the other XML file.
I run a loop that gets category titles, and for each title I wanna create a TextView with the title.
How I insert the TextView
LinearLayout Layout = (LinearLayout) findViewById(R.id.itemDesign); //When debugging I can see "Layout" is just null. itemDesign is also not in main.xml.
TextView title = new TextView(this);
title.setText(CatName);
Layout.addView(title);
XML used by ListView "single_list_item.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/itemDesign"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Name Label -->
<TextView android:id="#+id/name_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
<!-- Description Label -->
<TextView android:id="#+id/email_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"/>
</LinearLayout>
It sounds like you need to inflate your view. In getView() of adapter class
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.your_layout, parent, false);
Then to access your TextViews
TextView firstTV = (TextView) rowView.findViewById(R.id.some_id);
All about LayoutInflater
I guess, you are not setting the activity content view before using findViewById():
setContentView(R.layout.myLayout);
After that initialize your text view, and add width, height params to that. Then there should be no problems in adding that text view to the layout.
Maybe you should try invalidating the layout after you add the TextView:
Layout.addView(title);
Layout.invalidate();
I'm still not entirely sure what you want to achieve, maybe you could give us some more information regarding what you want to accomplish?
If you want to make a new ListView item based on an xml file, you need to inflate it first. Then you can call findViewById() on the resulting view to get the children.
LayoutInflater inf = LayoutInflater.from(getContext());
LinearLayout listItemLayout = (LinearLayout)inf.inflate(R.layout.single_list_item, null);
TextView nameLabel = (TextView) listItemLayout.findViewById(R.id.name_label);

ListView disappearing after getting data

I have a Fragment with a ListView. After setting ListView's emptyView everything works ok, but as soon as my adapter (which is extending BaseAdapter) gets some data not only emptyView but also whole Fragment containing my ListView and few other Views disappear.
If I do not use empty view everything works as it should.
Does my adapter need to implement something special for empty view to work? Or it is the way I set emptyView?
LayoutInflater inflater = getActivity().getLayoutInflater();
View emptyView = inflater.inflate(R.layout.empty_list_view,
(ViewGroup) mListView.getParent());
mListView.setEmptyView(emptyView);
Thank you,
Marcin.
So I figured it out.
I now add empty view directly the place it should be in XML and then setEmptyView of my ListView in Java, and that works like I wanted it to work.
XML:
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/empty_list_view" />
and Java part, setting emptyView:
View emptyView = getActivity().findViewById(R.id.empty_view);
mListView.setEmptyView(emptyView);
Adapter should implement isEmpty():
#Override
public boolean isEmpty()
{
return listItems.size() == 0;
}
I hope it will help someone in the future. ;)

Inflating a View Inside a Runnable

I am attempting to write code that would allow me to have 1-2 views at the top of a listview that are of a different arrangement than the other views in the listview. I decided to try the method of using addHeaderView() for the 1-2 views that will differ in display.
I have two xml layout files, one that defines the view format that most of the listview views will fall under (list_image_checkbox_row.xml) and one for the Header Views (catalog_featured_row.xml). The adapter constructor uses list_image_checkbox_row as its resource in the constructor.
I used a Thread to set the adapter and load the views. I am able to programmatically create views and use addHeaderView to at them to the listview (using the same image resource), but I get errors when I try to use addHeaderView on a view I have inflated from a layout xml file.
handler.post(new Runnable() {
#Override
public void run() {
lv = (ListView)findViewById(R.id.sources_list);
Activity context = BrowseSourceActivity.this;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.catalog_featured_row, null);
ImageView feat_view = (ImageView) view.findViewById(R.id.image);
feat_view.setImageResource(R.drawable.arrow);
lv.addHeaderView(feat_view);
setListAdapter(array);
}
});
I get a force close error and haven't been able to find anything in logcat (when I was trying to debug yesterday I got a NullPointerException). If I comment out the lv.addHeaderView(feat_view) line, the application does not force close on me. Here is the code for featured_catalog_row.xml:
<?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="70dip"
android:padding="5dip">
<ProgressBar android:id="#+id/spinner"
android:indeterminate="true"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerInParent="true"
/>
<ImageView android:id="#+id/image"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Any suggestions for how to get the inflater to work?
Perhaps you should store lv in an instance variable instead of looking it up dynamically. Note that both View and Activity have a method called findViewById, and so your results will depend on how your code is organized.

Categories

Resources