getRootView() not working - android

This is my xml file:
<?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"
android:background="#color/background"
android:orientation="vertical"
android:padding="15dip" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
Now in my code I would like to change the window's background color, and I'm doing it like this:
ListView listview = (ListView) findViewById(android.R.id.list);
View root = listview.getRootView();
root.setBackgroundColor(Color.parseColor("#bdbdbd"));
If I understood this correctly, this should change the background color of the listview's parent (in this case, the LinearLayout). However this is not working, what am I doing wrong?

I think you're confused, getRootView() won't get you that LinearLayout, but the parent of that view. See this other question. You should use listview.getParent() instead; note that it needs to be casted.

try to use getParent() in place of getRootView() to get the instance of the surrounding LinearLayout..

Related

How to dynamically change text of empty view in listfragment

I have red many solutions regarding this but none of them solved my problem.
What i want to do is, when my list view is empty its ok if the default empty text view shows up (which is declared in xml) but what if i want to change its text at some point in an activity that extends listfragment according to my need and make it visible....
Recently i tried this:
public void setEmptyText(CharSequence emptyText) {
ListView mListView = (ListView) NotificationsView.findViewById(android.R.id.list);
View emptyView = mListView.getEmptyView();
((TextView) emptyView).setText(emptyText);
}
It doesn't give any error but it is not solving the problem too. Any Help would be appreciated thanks
EDIT:
here is my layout of listfragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/fragment_bg"
android:divider="#null"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_style" >
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/fragment_bg"
android:text="No data" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
If you are using ListFragment all you need to do is call the method
setEmptyText("Your empty text"):
The system will recognize the view label with the android:id="#id/android:empty" id and will change the text for you. No need to reference it or do any cast, it's all built in the ListFragment
By default the listview or adapter (don't remember which one) makes it invisible. Call emptyView.setVisibility(View.VISIBLE); to make it visible.
This is how to change the value of a text view:
TextView tv = (TextView) findViewById(R.id.empty);
tv.setText('yourText');

ListView Greyed Out

So I've run into a pretty peculiar issue here. I am using a navigation drawer that has the framelayout to switch out content. I current have two fragments built, one is just fine and the only thing it has in it is a button, no layout specified. My second fragment though has a relative layout with a textview and a listview.
When I load the listview (which uses the textview) I get this:
As you can see, the listview is greyed out. I have no immediate colors set in my layout for this so it's not all grey text, and I can still click each item and scroll the list. I have no idea what is causing this, any tips?
EDIT Here are snippets of my code where I am working with the layout:
View rootView = inflater.inflate(R.layout.fragment_read_it, container,
false);
And here is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
And lastly my adapt and list:
ArrayAdapter<PressRelease> adapter = new ArrayAdapter<PressRelease>(
getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, pressReleases);
listView.setAdapter(adapter);
Please give specific color to textview like
<TextView
android:width="match_parent"
android:height="wrap_content"
android:textColor="#000000"
>
And in list view put cache color hint as transparent like
<ListView
android:width="match_parent"
android:height="wrap_content"
android:cacheColorHint="#andorid:color/transparent"
>
I found a solution in another thread.
Use android.R.layout.simple_list_item_1 with a light theme
I was using getApplicationContext() with my adapter, and for some reason using this with the light theme makes the colors on the listview light.

Adding LinearLayout to ListView using an adapter

I am trying to create a ListView where each entry in the list consists of a LinearLayout. I have an ArrayList that I have defined here:
ArrayList<LinearLayout> menuList;.
Later in my code, I define
LinearLayout dailyMenuLayout = new LinearLayout(ReturnMenus.this);, and every time I complete a layout to be added to the ListView, I use menuList.add(DailyMenuList)
The adapter that I have been trying to use is as follows -- but it crashes the app every time that it is triggered.
ListView myListView = (ListView)findViewById(android.R.id.list);
ArrayAdapter<LinearLayout> adapter = new ArrayAdapter<LinearLayout>(ReturnMenus.this, R.id.linear_layout_item, menuList);
myListView.setAdapter(adapter);
And this is the XML for the single row in the ListView:
<?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" >
<LinearLayout
android:id="#+id/linear_layout_item"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
Can somebody show me how to accomplish this? Thanks.
Use something like following layout for the row layout in your list rather than you current layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
.
.
.
.
.
</LinearLayout>
Here if you do not know exactly how many textView that you are going to use, use maximum number of TextView that using in your application. Then in your adapter class inside getView() you can get those TextView. If some rows not going to use all of these textViews then set them invisible as like this:
TextView txtView3 = (TextView)findViewById(R.id.text3);
txtView3.setVisibility(View.GONE)
Following tutorial helps you to create the listView.
http://www.vogella.com/tutorials/AndroidListView/article.html
You should bind data to adapter,such as strings,not the layout.
To see how to use ListView and Adapter,please check the demo project in your-android-sdk-dir\samples\android-XX\ApiDemos.

Create a List with a static textView

I need your help. My problem is that I'm trying to create a layout that have a static textView and in the bottom a List with an adapter. I don't know how to do that. There is what I want that the layout show me:
NameOfPerson //The static text
Exm1
Exm2
Exm3
The problem is show the list with the static text, and/or show the static text with the list. What I have to do? I have to use fragments?
Thanks,
What's wrong with that solution?
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Try this code :
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);
See ListView.html.addHeaderView.
Do your ListView as you normally would, and add the extra view with addHeaderView. Be sure to call addHeaderView before setAdapter though, otherwise it won't have any effect.

Android: set empty view to a list view

<TextView
android:id="#android:id/empty"
style="#style/FacebookFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/no_result_found"
android:textColor="#color/white" />
this is the xml code for empty view. I am setting empty view as follows:
list.setAdapter(adapter);
list.setEmptyView(findViewById(android.R.id.empty));
I have the items in the listview even then also before listview is populated the empty view is shown.
I don't want that empty view to be shown when listview contains some item.
Please help.
if you are using ListActivity then no need to set the empty view manually. Remove list.setEmptyView(findViewById(android.R.id.empty)); from your code.
Change your layout like below then it's automatically add EmptyView if list has no item:
<?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="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
You have to set the empty view before setting the adapter, if your MainActivity is not extending ListActivity.
Try this
list.setEmptyView(findViewById(android.R.id.empty));
list.setAdapter(adapter);
Sorry for the late answer!
Alternate method: Make your activity extend from ListActivity. And add a textview to your list layout with id android.R.id.empty
Try this
View view = getLayoutInflater() .inflate(<resource id of the empty view>, null);
ViewGroup viewGroup= ( ViewGroup)list.getParent();
viewGroup.addView(view);
list.setEmptyView(view);
list.setAdapter(adapter);
There are some correct answers, however, I think this is the best approach I've found:
View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);
addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView
listView.setEmptyView(emptyView);
listView.setAdapter(adapter);
Whatever #dhawalSodhaParmar has explained is correct. But there is a confusion when you look at the way the answer is put down.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SampleActivity">
<ListView
android:id="#+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:divider="#null"
android:dividerHeight="0dp" />
<!-- Empty view is only visible when the list has no items. -->
<TextView
android:id="#+id/empty_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO DOCUMENTS AVAILABLE!"
android:textColor="#525252"
android:textAppearance="?android:textAppearanceMedium"
android:visibility="gone"/>
</RelativeLayout>
Suppose if you already have a list view and adapter backing the same with data. And assuming everything is working fine, whoever wants to implement this empty list view screen has to start like below:
Step 1:
Modify the existing Activity XML file which has the list view with the one I have used here. You can use either a Linear Layout or Relative Layout, but preferably Relative because of the flexibility. And a textView like I have written.
Step 2:
Go to your Activity Java file, and after your setContentView
ListView emptyListView = (ListView) findViewById(R.id.list);
// set your adapter here
// set your click listener here
// or whatever else
emptyListView.setEmptyView(findViewById(R.id.empty_text_view));
That's it, now run and you are set!
// first of all create a global variable of Text view
// which is displayed when the list is empty
private TextView mEmptyStateTextView;
//then in onCreate(Bundle savedInstanceState)
/Setting empty text view
mEmptyStateTextView=(TextView) findViewById(R.id.emptyview);
view.setEmptyView(mEmptyStateTextView);
//then in onLoadfinished method
mEmptyStateTextView.setText(R.string.no_earthquakes);

Categories

Resources