How to get EditTextPreference's layout items id - android

I have a EditPreferences.class which extends preferenceactivity and my preferences.xml layout in my xml folder
in the xml layout i used EditTextPreference control which have layout et_layout.xml
my question is how can i get my textview id in my EditPreferences.class
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="#string/app_name" >
<PreferenceCategory
android:icon="#drawable/batitle"
android:textColor="#000000"
android:title="كۆرۈنۈش" >
<ListPreference
android:defaultValue="#string/result_fontsize_default"
android:dialogTitle="تاللاڭ"
android:entries="#array/result_fontsize_keys"
android:entryValues="#array/result_fontsize_values"
android:key="result_fontsize"
android:summary="ئىزدەش نەتىجىسىنىڭ كۆرۈنۈش چوڭلۇقى"
android:textColor="#000000"
android:title="خەت چوڭلۇقى" />
<EditTextPreference
android:dialogIcon="#drawable/batitle"
android:dialogTitle="editText"
android:key="tel"
android:layout="#layout/layout" />
<Preference />
</PreferenceCategory>
</PreferenceScreen>
layout
<?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:gravity="right"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>

Inflate you xml
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.linear_layout, null);
textView2 = (TextView) view.findViewById(R.id.textView2);
Try this, hope it would help you out

Try this,
SharedPreferences prefs;
String x = prefs.getString("tel",defaultString value);
textView.setText(x);

The reason why a simple findViewById in your EditPreferences.onCreate doesn't work is because your layout with the TextView is created inside of a ListView and there is a delay before that actually happens when the activity is launched.
I'm not sure this is the best way, but one way to handle the event of ListView item population is to handle the OnHierarchyChange event for the ListView object. If you put this in your onCreate, after the preference layout has been loaded from what I assume is a call to addPreferencesFromResource, it should work.
ListView list = (ListView) findViewById(android.R.id.list);
list.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
#Override
public void onChildViewAdded(View parent, View child) {
TextView text = (TextView) child.findViewById(R.id.textView2);
if (text != null) {
text.setText("Something else here");
}
}
#Override
public void onChildViewRemoved(View view, View view2) {
}
});

Related

TextView gets overlaped(instead of Overwriting)in a Preference Screen

I am trying to build a preference screen which consists of a Layout with some TextViews and EditText Preference.
When I use textView.setText(inputString) in the Activity, it does not overwrite the existing TextView, instead it creates a new copy of TextView with inputString as text and it overlaps with the existing TextView. Is there any way that the setText overwrites the existing TextView itself.
This is the XML Layout with the TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/blue"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
></ListView>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:textColor="#color/pseudoBlack"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:layout_marginTop="100dp"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="100dp" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/textView11"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="21dp" />
</RelativeLayout>
And this is the Preference Screen I am using.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
>
<Preference android:layout="#layout/inner" android:key="innerkey"
/>
<Preference android:key="mskey"
android:title="show"
/>
<EditTextPreference android:key="editpref"
android:title="Add"
/>
</PreferenceScreen>
This is the Activity Class in which I am trying to do setText().
package com.example.asus.layoutcheck;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.widget.TextView;
public class SecondaryActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inner);
TextView txt1 = (TextView)findViewById(R.id.textView2);
TextView txt2 = (TextView)findViewById(R.id.textView5);
TextView txt3 = (TextView)findViewById(R.id.textView11);
txt1.setText("First Text");
txt2.setText("Second Text");
txt3.setText("Third Text");
final String message = "This is the message" ;
addPreferencesFromResource(R.xml.pref);
final Preference msg = findPreference("mskey");
msg.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
msg.setSummary(message);
return true;
}
});
Preference edit = findPreference("editpref");
}
}
I have added the screenshot of the result.
How can I overcome this Issue?Thanks in advance
replace setContentView(R.layout.inner); with addPreferencesFromResource(//your preferenceScreen);
I've encountered this similar issue, and it happens with other view as well like a TextureView inside a FrameLayout. I resized my TextureView smaller and I can still see the previous TextureView in the back flickering.
What made it work is funny -> I just put a black background color (probably any color) to my layout (LinearLayout) and it is working fine now.
I added: android:background="#000000"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:visibility="visible"
tools:context=".CameraActivity">
It seems the background is not cleared, if it doesn't know what color to clear it with.

EditText losing focus in ListView

I already read tons of similar questions, but I found no solution that fixed my problem, so please do not mark this as duplicate.
I have a ListFragment with a FrameLayout containing a ListView, populated through a custom ArrayListAdapter.
Each row of the list has three elements:
Checkbox, EditText and ImageButton.
My problem is that when I tap on an EditText to change the string it contains the keyboard shows up, but the focus immediately goes away and I'm not able to write.
Please note that I already have added to my Activity in the manifest file
android:windowSoftInputMode="adjustResize"
even the
android:descendantFocusability="afterDescendants"
android:descendantFocusability="beforeDescendants"
doesn't solve the problem.
Here is my XML code:
row_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_checkbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<EditText
android:id="#+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60px"
android:text="Test text"
android:singleLine="true"
android:layout_toRightOf="#id/item_checkbox"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/item_important"
android:layout_toStartOf="#+id/item_important" />
<ImageButton
android:id="#+id/item_important"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:visibility="visible"
android:layout_marginLeft="10dp" />
</RelativeLayout>
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
It would be great if you could suggest me some resource where to understand how focusability works in this case.
Thank you
UPDATE
I found out the problem is created by the keyboard showing up: I guess the view recycling is the reason why the EditText loses focus.
I solved my issue using the new RecyclerView class. It seems that was a problem of the ListView
Try this code
row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:focusable="false"/>
<EditText
android:id="#+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/item_important"
android:layout_toRightOf="#id/item_checkbox"
android:layout_toStartOf="#+id/item_important"
android:minHeight="60px"
android:focusable="true"
android:singleLine="true" />
<ImageButton
android:id="#+id/item_important"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="visible" />
</RelativeLayout>
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"/>
</FrameLayout>
Activity Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_list);
ArrayList<String> mArrayList = new ArrayList<String>();
/**mArrayList.add("Test");
mArrayList.add("Test");
mArrayList.add("Test");
mArrayList.add("Test");**/
CustomeAdapter adapter = new CustomeAdapter(this, mArrayList);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setItemsCanFocus(true);
listView.setAdapter(adapter);
}
public class CustomeAdapter extends ArrayAdapter {
ArrayList<String> mArrayList;
#SuppressWarnings("unchecked")
public CustomeAdapter(Context context, ArrayList<String> users) {
super(context, R.layout.row_item, users);
mArrayList = new ArrayList<String>();
mArrayList = users;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_item, parent, false);
viewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.item_checkbox);
viewHolder.mEditText = (EditText) convertView.findViewById(R.id.item_text);
viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.item_important);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mEditText.setText(mArrayList.get(position));
return convertView;
}
// View lookup cache
private class ViewHolder {
CheckBox mCheckBox;
EditText mEditText;
ImageView mImageView;
}
}
Did you set android:focusable="true" and android:focusableInTouchMode="true" on your EditText?
Also the android:windowSoftInputMode="adjustPan" in your AndroiManifest file.

listview and edittext softkeyboard

I have custom template with edittext field. When I click on "next" button on softkeyboard it move focus only two time - than button changed to "OK". List have 12 items.
Any way to navigate to all items, not only 2?
Can you help me please?
Im use this template for 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"
android:padding="3dp" >
<TextView
android:id="#+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:paddingBottom="3dp"
android:paddingTop="3dp" >
<EditText
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="left"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:width="100dp" />
</LinearLayout>
</LinearLayout>
And this xml for 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" >
<TextView
android:id="#+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calk_1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="382dp"
android:divider="#color/reddivider"
android:dividerHeight="#dimen/twodp"
android:focusable="true"
android:focusableInTouchMode="true"
android:smoothScrollbar="true" >
</ListView>
</LinearLayout>
Also, here my adapter right now:
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view, null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
CalcItem i = objects.get(position);
int last=getCount()-1;
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView hd = (TextView) v.findViewById(R.id.head);
TextView tx = (TextView) v.findViewById(R.id.text);
TextView ds = (TextView) v.findViewById(R.id.description);
EditText vl = (EditText) v.findViewById(R.id.value);
if (position==0){
vl.setNextFocusUpId(last);
vl.setNextFocusDownId(1);
} else if (position==last){
vl.setNextFocusDownId(0);
} else {
vl.setNextFocusDownId(position+1);
}
if (hd != null){
hd.setText(i.getHead());
}
if (tx != null){
tx.setText(i.getText());
}
if (ds != null){
ds.setText(i.getDescription());
}
if (vl != null){
vl.setText(Integer.toString(i.getValue()));
}
}
// the view must be returned to our activity
return v;
}
Use android:nextFocusUp="id" and android:nextFocusDown="id" - as described in the documentation.
Here's an example from the docs:
<LinearLayout
android:orientation="vertical"
... >
<Button android:id="#+id/top"
android:nextFocusUp="#+id/bottom"
... />
<Button android:id="#+id/bottom"
android:nextFocusDown="#+id/top"
... />
</LinearLayout>
As far as I know, Edit texts doesnt work well in ListViews and Recycler views,
I'll Recommend you to inflate separate views multiple times instead of making a ListView if you are dealing with edit texts.

Why my custom checkbox in listview can't be checked

I am trying to set the checked state of my checkbox in listview, here's the code:
mShalatAdapter = new ArrayAdapter<String>(getActivity(),R.layout.pengaturan_list_item, R.id.label_shalat, waktuShalat);
mListShalat = (ListView) v.findViewById(R.id.pengaturan_checkbox);
mListShalat.setAdapter(mShalatAdapter);
CheckBox cb;
TextView txv;
for (int i = 0; i < mListShalat.getCount(); i++) {
View vListSortOrder = mListShalat.getAdapter().getView(i, null, null);
txv = (TextView) vListSortOrder.findViewById(R.id.label_shalat);
cb = (CheckBox) vListSortOrder.findViewById(R.id.status_shalat);
String labelShalat = txv.getText().toString();
Toast.makeText(getActivity(), labelShalat, Toast.LENGTH_SHORT).show();
if (cb.isChecked() == false) {
cb.setChecked(true);
}
}
And this is my pengaturan_list_item.xml layout:
<?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:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/label_shalat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="Label Ibadah"
android:textColor="#android:color/white"
android:textSize="20dp" />
<CheckBox
android:id="#+id/status_shalat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/ibadah_checkbox_list"
android:visibility="visible" />
</LinearLayout>
I got the value for TextView and CheckBox but I don't know why the CheckBox not showing the true state in device. Please help, thanks in advance.
my ibadah_checkbox_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true"
android:drawable="#drawable/checkbox_ibadah_checked" />
<item android:state_checked="false"
android:drawable="#drawable/checkbox_ibadah_unchecked" />
</selector>
It might be worth altering the way your adapter works.
Instead of creating a stardard ArrayAdapter, try inheriting from it instead, then inflating views from the list_item.xml inside the getView() method.
Once there, you can extract the checkbox from the view like you're doing in the loop & set its value according to what you need. This is the standard way of doing things like this.
My guess for why what you're doing is not working is that the ArrayAdapter is creating the views when it needs to display them, rather than in this loop you're using, so while it generates and gives you the views while you're requesting them, these views won't necessarily be the ones that are displayed.
Here's how I would do it:
public class MyArrayAdapter extends ArrayAdapter<String>
{
public MyArrayAdapter(Context context, int textViewResourceId, List<String> objects)
{
super(context, textViewResourceId, objects);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if(v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.pengaturan_list_item, null);
}
TextView tv = (TextView)v.findViewById(R.id.label_shalat);
CheckBox cb = (CheckBox)v.findViewById(R.id.status_shalat);
tv.setText(getItem(position));
if (!cb.isChecked())
cb.setChecked(true);
return v;
}
}
Then, set your adapter like this (objectList is your list of Strings for the labels):
mShalatAdapter = new MyArrayAdapter(this, R.layout.pengaturan_list_item, objectList);
mListShalat = (ListView) v.findViewById(R.id.pengaturan_checkbox);
mListShalat.setAdapter(mShalatAdapter);
Hope this helps :)
I think the problem is that in your layout. Your TextView has a layout_width of match_parent, and in that it covers the CheckBox and fills the whole width layout. So you need to divide the layout properly.
Try the layout below and tell me the effect.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:weightSum="2"
android:paddingTop="5dp" >
<TextView
android:id="#+id/label_shalat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="Label Ibadah"
android:textColor="#android:color/white"
android:background="#android:color/black"
android:textSize="20dp" />
<CheckBox
android:id="#+id/status_shalat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#drawable/ibadah_checkbox_list"
android:visibility="visible" />
</LinearLayout>
Add android:descendantFocusability="blocksDescendants" in your ListView.Hope it works....

Setting a layout to use as empty view for a ListView in case adapter has zero items in a Activity

How to use a layout as empty view for a listview when the adapter has zero elements?
setEmptyView is not working with this code :
public class ZeroItemListActivity extends Activity {
private ArrayList<String> listItems=new ArrayList<String>();
private ListView mMyListView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMyListView=(ListView) findViewById(R.id.MyListView);
mMyListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems));
LayoutInflater inflator=getLayoutInflater();
View emptyView=inflator.inflate(R.layout.empty_list_item, null);
//Empty view is set here
mMyListView.setEmptyView(emptyView);
}
public void addItem(View v){
listItems.add("list Item");
mMyListView.invalidate();
}
}
Layouts used : empty_list_item.xml
<?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">
<Button android:id="#+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Empty List,Click to add items"></Button>
</LinearLayout>
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="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/MyListView"></ListView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/AddItemsButton"
android:text="Add Items" android:onClick="addItem"></Button>
</LinearLayout>
The easiest way to get this done is to put the empty view in the main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/MyListView">
</ListView>
<!-- empty view -->
<LinearLayout android:id="#+id/emptyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty List,Click to add items">
</Button>
</LinearLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AddItemsButton"
android:text="Add Items"
android:onClick="addItem"></Button>
</LinearLayout>
Then, in your code, set your empty view like this:
mMyListView.setEmptyView(findViewById(R.id.emptyView));
The reason your code isn't working is that the ListView and Empty view need to be in the same view heirarchy. And your call to inflate pass null as the parent so they are in different heirarchies.
The Android Dev tutorial for List Activity covers this topic:
http://developer.android.com/reference/android/app/ListActivity.html
Basically have your class extend ListActivity. Set your list id to #id/android:listand the "empty view" id to #id/android:emptyin the same XML layout.
Hope this helps :)
When, inflating the view, you have to set the view parent to be part of the Layout hierarchy rather than pass null in some cases.
Replace
View emptyView = inflator.inflate(R.layout.empty_list_item, null);
with
View emptyView = inflator.inflate(R.layout.empty_list_item, (ViewGroup) mMyListView.getParent());
then you set the empty view as you did before with.
mMyListView.setEmptyView(findViewById(R.id.emptyView));

Categories

Resources