GridView is not clickable - android

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
}
};

Related

Android: ItemClickListener in ListView, which contains GridView

I have a ListView and every item in this listview contains a gridview and a header textview. Now I want to recognize when the user clicks anywhere on the whole item, so I tried to set an onItemClickListener. Unfortunately this doesn't seem to work because I didn't get the debug log I added to the click method.
My ListView looks like the following
<ListView
android:id="#+id/listview_previous_issues"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:layout_marginBottom="#dimen/article_separator_margin_topbottom"
android:divider="#null"
android:dividerHeight="0dp" />
And the GridView of the list items are this:
<GridView
android:id="#+id/read_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:columnWidth="#dimen/settings_and_issues_read_issues"
android:stretchMode="columnWidth"
android:verticalSpacing="#dimen/paddingSmall"
android:horizontalSpacing="#dimen/paddingSmall"
android:listSelector="#00000000"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
Update: The call of onItemClickListener
mIssueList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("debug", "Klicke auf Ressort mit der Id " + pRessorts.get(position).mRessortId);
//Setze die Ressort-ID welche Topnews angezeigt werden
SharedPreferenceHelper.setLongForKey(Constants.PREFS_SHOWN_TOPNEWS_ID, pRessorts.get(position).mRessortId);
//Refreshe die Ansicht
mContext.refresh();
}
});
I think that the GridView is getting the click event instead of the listview, but I'm not sure if this is correct. Can anyone point out what I have to do to get my clickevents at the listview?
So, I was able to solve the problem on my own. Maybe this is helpful for anyone:
I override the dispatchTouchEvent method of my custom gridview, so that it doesn't get the touch event
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
return false;
}
Additionally, instead of an onItemClickListener I set a simple ClickListener at each listitem in the getView method of my ListAdapter and it worked.

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
}
});

android: extend the listitem click range

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. ;)

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
}

How to add OnClickListener to image button in the ListView Header in Android?

I have added a custom header to the ListView in android. The layout xml file for Header contains an Image button. How can I add an OnClickListener to this ImageButton
Header Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#336699">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add" />
</LinearLayout>
Code for binding the ListView (I have created a custom Adapter)
ListView listView1 = (ListView) findViewById(R.id.listView1);
LocationAdapter adapter = new LocationAdapter(this,R.layout.listrow,webresult);
View header = (View)getLayoutInflater().inflate(R.layout.listheader, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
When you add a header to listview it's considered the element 0 of your list. So you don't add an onClickListener but do it as you'd do with a regular row in a list:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0) //do your stuff
}
});

Categories

Resources