In My application i am using two list view's in one activity, one for only items and one list view i am have add check box in every row.
My requirement is in below list view if item is checked i have to display on text view in the main activity if not have to display another activity .
i was not used ListActivity for this i just created from Activity
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="60px">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="60px">
<ListView android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_height="wrap_content" />
</LinearLayout>
above list view for only items for below list view i have to add check box. how can i achieve this.
my activity is..
public class Settings extends Activity {
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ListView list1 = (ListView) findViewById(R.id.list1);
ListView list2 = (ListView) findViewById(R.id.list2);
list2.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1, Display));
list2.setTextFilterEnabled(true);
list2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
}
});
list1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, Categories));
list1.setTextFilterEnabled(true);
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
}
});
}
static final String[] Categories = new String[] {
"Alarm1","Alarm2","Alarm3","Alarm4","Alarm5"
};
static final String[] Display = new String[] {
"24 Hours","Display Seconds"
};
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.provider, null);
TextView location = (TextView) convertView
.findViewById(R.id.description);
checkBoxImage = (ImageView) convertView.findViewById(R.id.check_id);
Provider p = (Provider) getItem(position);
location.setText(p.Description + " - " + p.Location);
return convertView;
}
Well here is the Complete Tutorial how you can include CheckBox in a List. Its the Best Tutorial Regarding the ListView & ListActivity.
Related
I am trying to show a ListView and my Java class extendsListFragment. While setting setOnItemClickListener on ListItem, the click Listener doesn't seem to work. Here is my code:
ListView_rssfeeds_tab.xml
<?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"
android:orientation="vertical"
android:id="#+id/relativeLayout"
tools:context=".interestingReads.RSSFeedsTab">
<ListView
android:id="#+id/rssFeedsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/diffBackgroundWhite"
/>
</RelativeLayout>
rss_item_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rssFeeds_LL_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
android:clickable="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="6dp"
>
<ImageView
android:id="#+id/rssFeeds_Image_list_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:contentDescription="#string/app_name"
android:padding="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="#+id/rssFeeds_Title_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rssFeeds_Description_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
RSSFeedsTab.java (Please NOTE: RSSFeedsTab is Fragment and not Activity)
public class RSSFeedsTab extends ListFragment implements OnItemClickListener {
.
.
.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
.
.
.
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
setListAdapter(simpleAdapter);
View RootView = getActivity().getLayoutInflater().inflate(R.layout.fragment_rssfeeds_tab, container, false);
ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);
RSSFeedsItemLL.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
System.out.println("********Reached onClick*********");
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
With this much implementation, I am able to get the ListView rssFeeds_LL_list_view. I am not sure, how I can use click event on those ListView such that I can get position in ListView ?
you have to use the listener OnItemClickListener to get the item which was clicked :
OnItemClickListener on Android developer
****Update****
In your code, you are using a ListFragment that already contains a Listview.
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
setListAdapter(simpleAdapter);
With this lines, you create an Adapter and give this adapter to the Fragment's ListView. Then you recover the RSSFeedsItemLL ListView from your layout and you set the OnItemClickListener on it :
ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);
RSSFeedsItemLL.setOnItemClickListener(new AdapterView.OnItemClickListener(){...}
But this ListView is never linked to an adapter.
You have two solutions :
Replace the ListFragment with a Fragment in your class definition and use the ListView in your layout file
Use the RSSFeedsItemLL ListView in your fragment
For the second case, you just have to override the method onListItemClick in your RSSFeedsTab class :
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
System.out.println("********Reached onClick*********");
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
super.onListItemClick(l, v, position, id);
}
You also have to remove the line android:clickable="true" in your LinearLayout definition in rss_item_list_row.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rssFeeds_LL_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="6dp"
>
...
Use onItemClickListener on ListView view: RSSFeedsItemLL
RSSFeedsItemLL.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
System.out.println("********Reached onClick*********");
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}});
References: OnItemClickListener
Finally I have resolved the issue by overriding method getView()
So, I changed only the line:
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
to
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to){
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
final int finalposition = position;
LinearLayout RSSFeed_singleItem=(LinearLayout) v.findViewById(R.id.rssFeeds_LL_list_view);
RSSFeed_singleItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"Link:==>"+localLinkArray[finalposition],Toast.LENGTH_SHORT).show();
}
});
return v;
}
};
I'm using a ListView with ArrayAdapter. There is a Button along with TextView in the listview.xml. I want to retrieve data from that TextView by Clicking over corresponding buttons.
My listview.xml is as follows:
<?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="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="90dp"
android:id="#+id/btConfirmfriend"
android:text="Confirm Friend"
/>
</RelativeLayout>
My PendingRequest Class is as follows:
public class PendingRequest extends AddFriend {
TextView tvPending;
Button btConfirmfriend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pendingrequest);
final String[] s= getIntent().getExtras().getStringArray("pending");
System.out.println(s[1]);
final ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listview,R.id.label,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
final View inflatedView = getLayoutInflater().inflate(R.layout.listview, null);
btConfirmfriend = (Button) inflatedView.findViewById(R.id.btConfirmfriend);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ServerConnection sc = new ServerConnection();
//here I want to retrieve the value from List
//System.out.println(s[adapter.]);
int status = sc.confirmRequest(s[listView.getSelectedItemPosition()]);
AlertDialog.Builder alert = new AlertDialog.Builder(PendingRequest.this);
if (status == 1) {
alert.setMessage("Friend has been Added");
alert.show();
} else {
alert.setMessage("Sorry for inconvinient!!Try Again");
alert.show();
}
}
});
}
}
My pendingrequest.xml file is:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#drawable/background"
android:layout_height="match_parent">
<Button
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#e9abab"
android:id="#+id/btSend"
android:text="Pending Requests"
/>
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
On clicking over the button nothing happening.onClick() method is not executing.Please help.
Since you need a click listener on a view inside your list item, you need to write your own adapter for that -
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, String[] list) {
super(context, 0, list);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Lookup view for data population
TextView label = (TextView) convertView.findViewById(R.id.label);
Button btConfirmfriend = (Button) convertView.findViewById(R.id.btConfirmfriend);
// Populate the data into the template view using the data object
label.setText(item);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getItem(position); //Get your selected text
}
});
return convertView;
}
}
And this is how you will set the adapter -
final CustomAdapter adapter = new CustomAdapter(this,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
I am working on a listView that will contain items that contain a custom spinner. The goal of what I am trying to do is to have an entry in each spinner, where when selected, the spinner itself will disappear and an edit text will appear.
In this sample I have provided, I am creating an entry in the spinner called "Create New" and when selected, an edit text should appear allowing you to type in a new name.
The issue I am seeing is when "Create New" is selected, the spinner will change to visibility.GONE and the edit text is shown. however you can never select the edit text. I believe getView is constantly being called and is possibly messing up what I am trying to accomplish.
Any tips would be great!
Activity.java
public class Activity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
List<String> categories = new ArrayList<>();
categories.add("Groceries");
categories.add("Gas");
categories.add("Travel");
List<String> entries = new ArrayList<>();
entries.add("first");
entries.add("second");
entries.add("third");
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(new ListAdapter(entries, categories));
}
private class ListAdapter extends ArrayAdapter<String> {
SpinnerAdapter spinnerAdapter;
public ListAdapter(List<String> entries, List<String> categories) {
super(Activity.this, 0, entries);
spinnerAdapter = new SpinnerAdapter(categories);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
final Spinner spinner = (Spinner) convertView.findViewById(R.id.spinner);
final EditText categoryEditText = (EditText) convertView.findViewById(R.id.edit_text);
spinner.setAdapter(spinnerAdapter);
spinner.setSelection(spinnerAdapter.getCount());
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// if the create item was selected, hide the spinner and show the edit text
if (position == spinnerAdapter.getCount()-1) {
spinner.setVisibility(View.GONE);
categoryEditText.setVisibility(View.VISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return convertView;
}
private class SpinnerAdapter extends ArrayAdapter<String> {
public SpinnerAdapter(List<String> categories) {
super(Activity.this, android.R.layout.simple_spinner_dropdown_item, categories);
categories.add("Create New");
categories.add("Choose a Category");
this.notifyDataSetChanged();
}
#Override
public int getCount() {
return super.getCount() - 1; // last item is a place holder
}
}
}
}
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
</ListView>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
On Launch
Drop Down Open
Select Create New and EditText appears, however it wont hold focus making it not editable
I found a solution to my problem. It turns out the real issue is having an Edit Text inside a ListView. I had to set this flag on my Activity definition in my Manifest and everything just worked!
<activity android:name= ".Activity" android:windowSoftInputMode="adjustPan"/>
Maybe someone would like to give more clarification? If so I will mark your answer as the correct one.
I want to hide the edit text and button field initially in list view and show that edit text and button only for a particular row (clicked row) in list view when that row is clicked.
I used following custom layout to create the list view
<?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="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/emp_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="#drawable/person" />
<TextView
android:id="#+id/emp_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/emp_avatar"
android:layout_toRightOf="#+id/emp_avatar"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/empPin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/emp_avatar"
android:layout_alignRight="#+id/emp_number"
android:layout_toRightOf="#+id/emp_avatar"
android:ems="10"
android:inputType="number"
android:focusable="false"
android:focusableInTouchMode="false"
android:visibility="invisible">
<requestFocus />
</EditText>
In this lay out I have used android:visibility="invisible"
Here is the code of Activity.java
public class MainPortal extends Activity {
private List<Employee> employees = new ArrayList<Employee>();
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
populateEmployeeList();
//populsteListView();
ListView list = (ListView) findViewById(R.id.employeeListView);
ArrayAdapter<Employee> adapter = new MylistAdapter();
list = (ListView) findViewById(R.id.employeeListView);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
et.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}
});
private void populateEmployeeList() {
...
}
private class MylistAdapter extends ArrayAdapter<Employee>{
public MylistAdapter(){
super(MainPortal.this,R.layout.item_view,employees);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView==null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
Employee currentEmployee = employees.get(position);
//Avatar
ImageView imageView = (ImageView) itemView.findViewById(R.id.emp_avatar);
imageView.setImageResource(currentEmployee.getAvatarId());
//Employee number
TextView emp_id = (TextView) itemView.findViewById(R.id.emp_number);
emp_id.setText(currentEmployee.getId());
et = (EditText) itemView.findViewById(R.id.empPin);
return itemView;
}
}
}
I used following codes withing the on click listener of list view to make edit text visible for item click.
editText.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
But after clicking a row edit text field does not appears. What I'm doing wrong here ? does any body have an idea how to do this ?
Initialization of EditText:
View itemView = convertView;
if(itemView==null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
et = (EditText) itemView.findViewById(R.id.editText1);
...
Thanks in advance!
You have EdiText in item_view.xml not in activity_main_portal. You are inflating a custom layout in getView
You need to initialize edittext
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
View v = (View)view.getParent();
EditText ed1 = (EditText) v.findViewById(R.id.empPin);
ed1.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}
});
My ListView in Activity:
ListView listView1 = (ListView) menu.findViewById(R.id.menuList);
String menuItems[] = new String[] { "My Wants", "Profile", "Notifications",
"Feedback", "Logout" };
listView1.setAdapter(new SideMenuAdapter(this, menuItems, listView1));
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1) {
Intent intent = new Intent(FeedListViewActivity.this,
UserProfileActivity.class);
startActivity(intent);
}
if (position == 0) {
showMyWants();
}
}
});
menu is :
menu = inflater.inflate(R.layout.horz_scroll_menu, null);
horz_scroll_menu.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu"
android:layout_width="1dp"
android:layout_height="1dp"
android:background="#FFFFFFFF"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF000000"
android:text="Menu"
android:textColor="#FFFFFFFF"
android:gravity="center" />
<ListView
android:id="#+id/menuList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#FFFFFFFF"
android:scrollbars="none" >
</ListView>
</LinearLayout>
My SideMenuAdapter:
public class SideMenuAdapter extends BaseAdapter {
private static final int TYPE_MAX_COUNT = 2;
private static LayoutInflater inflater = null;
private Activity activity;
public ImageLoader imageLoader;
public static String[] values;
ListView myList;
public SideMenuAdapter(Activity a, String[] sa, ListView lv) {
values = sa;
activity = a;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myList = lv;
}
public int getCount() {
return values.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView mainText;
public TextView sideText;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View vi = convertView;
LayoutInflater inflater = activity.getLayoutInflater();
vi = inflater.inflate(R.layout.side_menu_list_item, null);
holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu);
holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu);
vi.setTag(holder);
holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu);
holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu);
holder.mainText.setText(values[position]);
if (position == 2) {
holder.sideText.setText("3");
holder.sideText.setBackgroundResource(R.drawable.orange);
}
return vi;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
}
My Xml for ListView items:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sideMenuListItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="3dp" >
<TextView
android:id="#+id/sideText_sideMenu"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:clickable="true"
android:gravity="center"
android:textSize="20dp"
android:padding="5dp" />
<TextView
android:id="#+id/mainText_sideMenu"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/sideText_sideMenu"
android:clickable="true"
android:gravity="left|center_vertical"
android:textSize="20dp"
android:padding="5dp" />
</RelativeLayout>
when running this application on Emulator, if i click with mouse nothing happens. But when i select any item on the list using navigation buttons on the KeyBoard and click Enter, it works fine.
when running the app on a device. If i go on click any item of the list, like some 10-20 times it works sometimes.
Edit:
Actually everything worked fine when i was using predefined ArrayAdapter<String> and android.R.simple_list_item. But i want a custom adapter
why is it so?
Its because in my ListView item layout i added
android:clickable="true"
for both the TextViews. So when i click on the ListView item it indeed is a click on these TextView whose onClick is not implemented. Removing the clickable attribute from the TextViews solved my problem.
Thanks everyone
Try making the RelativeLayout clickable and focusable.
Layout File
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
Main Activity
public class MyListView extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS));
getListView().setTextFilterEnabled(true);
}
static final String[] PENS = new String[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"Rotring",
"Sheaffer",
"Waterman"
};
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}
}
try this one
http://www.mkyong.com/android/android-listview-example/
res/layout/list_fruit.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
ListView
public class ListFruitActivity extends ListActivity {
static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// no more this
// setContentView(R.layout.list_fruit);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,FRUITS));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}