android: extend the listitem click range - android

I have implemented a ExpanddableListView in my android application, but I'm not happy with the click range of the child items.
The OnCLickListener only gets triggered when I click excactly on the String that is my list item.
How can I extend the click range of the listitem to the whole row?
Here is my layout-xml "selectreasonlist_item.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"
android:orientation="vertical" android:weightSum="1">
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:clickable="false" android:focusable="false"/>
</LinearLayout>
In my Activity I have:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.selectreason);
final Context context = this;
//Get the ListView
expListView = (ExpandableListView) findViewById(R.id.xlvSelectReason);
//Preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader,listDataChild);
//Setting list adapter
expListView.setAdapter(listAdapter);
//Setting OnClickListener to child item
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//My Code
return false;
}
});
}
Thanks for your help
Cheers Nik

You can attach listener to the list using setOnGroupClickListener and setOnChildClickListener to handle its events to the listeners OnGroupClickListener and OnChildClickListener.

I found out that I have to configure android:padding in order to extend the click range.
I added
android:paddingRight="130dp"
to my list_item.xml layout. Solved. ;)

Related

ListView - differentiate between a checkbox selection and a text selection

Given the ListView below, I wanted to perform two different actions depending on whether the user selects the text (create a new activity) or clicks the associated checkbox (add it to a favorites list). Is this possible in with this setup or will I have to use a custom adapter or even a different layout?
<LinearLayout 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">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, teams));
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String team_name = adapterView.getItemAtPosition(i).toString().trim();
Intent intent = new Intent("blah.blah.blah");
intent.putExtra("team", team_name);
startActivity(intent);
}
});
A
If I am understanding you correctly, your ListView should contain TextBox and CheckBox and TextBox and CheckBox are clickable, not ListView itself.
For this you have to make custom adapter, where you will make listeners for both the TextBox and CheckBox.
public class CustomAdapter extends BaseAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// your_costum_view should contain textbox and checkboc
view = inflater.inflate(R.layout.your_costum_view, null);
// Get your checkbox and textbox and add listeners
TextView textView = (TextView) view.findById(R.id.textView);
textView.setOnClickListener...
Checkbox checkbox=(CheckBox)view.findById(R.id.checkBox);
checkBox.setOnClickListener...
return view;
}
}
your_costum_view layout example
<?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="wrap_content" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Using a custom adapter will help you keep trace of your views, it will be easier than use a default adapter that you can't control. For references: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown_custom
Using a custom adapter will definitely make your life easier. In the adapter you can make reference to both the checkbox and the textView and add an onClick Listener to each - from there you can also add code to handle each event. I would also suggest using a recyclerView instead of a ListView. It is the new thing in Android 5.0 and it really is easier to use then a regular ListView. Hope this helps:
RecyclerView Help
Set the CheckBox as focusable="false" in your XML layout.
android:focusable="false"
if don't run go to this link and see example because you need a create custom row in list view and set:
Checkbox checkbox=(CheckBox)view.findById(R.id.checkboxID);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do stuff
}
});

Click on item in android ListActivity not working

I have ListActivity with custom ArrayAdapter and following layout for list items:
<?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:orientation="horizontal"
android:paddingTop="5dip"
android:paddingBottom="5dip" >
<ImageView
android:id="#+id/image"
android:layout_width="48dip"
android:layout_height="48dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="24.5sp" />
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="12sp"/>
</LinearLayout>
My problem is that, I am unable to click any of the list items. What I've tried so far is
setting focusable, clickable to false (which I found might help) to my ImageView
but, unfortunately, problem remains. Please help, this has been bugging me all day. Thank you.
EDIT: OK, so William's answer helped my out with this one, only problem now is that click on item doesn't highlight it, when I change background colour to black (by setting new theme in manifest for this list activity).
This is my custom style for activity theme <style name="ContentsListTheme">
<item name="android:background">#color/black</item>
</style> from res/values/styles/ folder.
To make it clear, once again, default ListActivity background colour is white. I want it black. When I apply the style from above, list activity item highlighting when clicked is disabled. How can I enable it again, to the default highlighting colour?
In your oncreate try this
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
});
OR you can write this function which will handle the clicking on the list items
protected void onListItemClick(ListView l, View v, int position, long id) {}
I have this working code, and I think in your Java code is the problem.
public class NewsListActivity extends Activity implements OnItemClickListener
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new NewsDataArrayAdapter(NewsListActivity.this, headlines));
listView.setOnItemClickListener(this);
...
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if (v == null) {
return;
}
try this
ListView lv=(ListView)findViewById(yourlistview);
Lv.setonItemClickListener(this);
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
paret.getItemAtPositon(position);
//this returns you an object cast it into the same object you passed to your adapter cast to string if values passed were string
}

switching from a CustomAdapter ListView to an standard ArrayAdapter ListView fails

I recently got interested in Android coding and working my way through several tutorials. however, right now i m stuck for a day and hope you guys can help me out.
basically i want to create a main menu and a submenu for every main menue entry. (right now this is only the case for the first entry)
For the main menu I used a ListView with different icons (realized with a custom adapter). Selecting one of the menu entries works fine in general, but if I choose to configure the onitemclick to start a new activity which is another (simple) ListView which uses the ArrayAdapter it breaks after switching into the submenu activity.
Strangely if i use the ArrayAdapter in both ListViews it is no problem; also when using CustomAdapter in both.
Here is the code, thanks in advance for any help ;)
main menu:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array contentmenu
String[] contentmenu = getResources().getStringArray(R.array.mainmenu_cats);
// storing icons in Array
TypedArray iconarray = getResources().obtainTypedArray(R.array.mainmenu_icons);
// Binding resources Array to ListAdapter
this.setListAdapter(new ImageAndTextAdapter(this, R.layout.list_item, contentmenu, iconarray));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
switch( position )
{
case 0: Intent newActivity0 = new Intent(getApplicationContext(),SubActivity.class);
startActivity(newActivity0);
break;
case 1: Intent newActivity1 = new Intent(getApplicationContext(),SubActivity2.class);
startActivity(newActivity1);
break;
}
}
});
}
submenu:
public class SubActivity extends ListActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] submen_1 = getResources().getStringArray(R.array.submenu_1);
// Binding resources Array to ListAdapter
setListAdapter(new ArrayAdapter<String>(ctx, R.layout.list_item1, R.id.submen1, submen_1));
ListView sublv1 = getListView();
// listening to single list item on click
sublv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
Intent newActivity = new Intent(getApplicationContext(),Page1.class);
// sending data to new activity
newActivity.putExtra("position",position);
startActivity(newActivity);
}
});
}
xml main menu:
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/option_icon"
android:layout_width="48dp"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
sub menu:
list_item1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/icon1"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:src="#drawable/ic_action_google_play" />
<TextView
android:id="#+id/submen1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
Currently you are not defining Hight and Width for LinearLayout in both list_item.xml and list_item1.xml layouts . add height-width as :
In list_item.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- PUT YOUR ImageView or other xml elements here -->
</LinearLayout>
and do same for list_item1.xml layout

How to implement onItemClickListener in List Activity

following is my ListActivity, onCreate code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fakeTripBuilder();
setContentView(R.layout.cutom_list_view);
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.custom_row_view,
new String[] {"stop","distance","time","icon","mode"},
new int[] {R.id.text_stop,R.id.text_distance, R.id.text_time, R.id.icon, R.id.text_mode});
populateList();
setListAdapter(adapter);
}
inside the the same class i have the following method
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Log.d("Travel","You choosed!");
Object o = this.getListAdapter().getItem(position);
Toast.makeText(this, "You Choosed"+o.toString(), Toast.LENGTH_LONG).show();
}
and here is the "custom_list_view.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="wrap_content"
android:clickable="false"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
/>
</LinearLayout>
Problem is, onListItemClick method do not call when i click on it. Help !!
To get a reference to the ListView in a ListActivity you should call:
ListView lv = getListView();
You do not need to set a click listener, it already has one. Simply override:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Object item = lv.getAdapter().getItem(position);
}
And in your xml file, the ListView should have the id:
android:id="#android:id/list"
Edit
You need to remove
android:clickable="false"
from the layout to allow any child views to be clicked
A lot has changed since this question was asked in 2012. This is an update to the accepted answer
To use onItemClickListener in your ListActivity, just override the onListItemClick(...) method like this:
public void onListItemClick(ListView listView, View view, int position, long id) {
ListView myListView = getListView();
Object itemClicked = myListView.getAdapter().getItem(position);
Toast.makeText(getApplicationContext(), "Item Clicked: " + item + " \nPosition: " + id,
Toast.LENGTH_SHORT).show();
}
Notice the getItem() method instead of getItemAtPosition() in the accepted answer
Remember: for this to work, you need to have a listview with id list, else your app will crash.
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
/>
Try overriding the onListItemClick method if you want to catch click events on your list.
ListView lv = (ListView) findViewById(R.layout.cutom_list_view);
You should use R.id.listview_id instead, that should be defined in R.layout.cutom_list_view
like so:
<ListView
android:id="#+id/listview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
EDIT:
R.layout.cutom_list_view is id of your layout file, but you should find listview by its own id, defined like android:id="#+id/listview_id", so the id will be R.id.listview_id

GridView is not clickable

I have the following layout:
<?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">
<GridView
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:numColumns="auto_fit"
android:verticalSpacing="5dip"
android:horizontalSpacing="2.5dip"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"
android:listSelector="#drawable/transparent"
android:padding="5dip"
android:scrollbarStyle="outsideOverlay"
android:clickable="true" />
</RelativeLayout>
Now I use a custom Adapter that inflates my own views and sets them as the items in the gridview and set a listener on the clicks in the gridview.
GridView grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(new ProductAdapter(getApplicationContext(), products));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
}
});
The problem is that the listener is not firing if I press on one of the cells in the gridview. If I use the trackpad to navigate around in the grid and I press the trackpad the listener is firing. Could it be that there is some other view that is capturing the click? My custom view consists of a LinearLayout with an ImageView and a TextView i read that inflating a layout for the gridview caused a problem at some places.
How can I make the items of the grid clickable?
Edit There is no difference between inflating the layout in my adapter or just instantiating a single view and returning this for the cellview.
Ok very very stupid error on my part. I reused another adapter that deactivated the views inside it.
For you all to know if you set this inside your Adapter
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return false;
}
The views will not be clickable. Just as it is intended to be ;)
GridView grid = (GridView)findViewById(R.id.grid);
grid.setOnItemClickListener(mOnClickGridCell);
OnItemClickListener mOnClickGridCell = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do stuff
}
};

Categories

Resources