I am not able to make my list view clickable.I have tried all the given solutions for same type of questions and had tried to change focusable and
focusableintouchmode values to both true and false.
Sometimes it works (i.e text in listview gets clicked) after i click the button 10/12 times.
Below are my xml and java files.
I have also attached the screenshot of logcat when my row number 9 got clicked after i pressed it more than 20 times.
enter image description here
Please help..
Activity_card.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hppc.business.Card">
<ListView
android:id="#+id/lvUsers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants">
</ListView>
</RelativeLayout>
entry1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal"
android:padding="13dp">
<ImageView
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/image" />
<linearlayout>.....</linearlayout>
</linearLayout>
card.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(Card.this, Manual.class);
Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
Log.e("YESSSS",Integer.toString(position));
startActivity(i);
}
});
you have to make your list view focusable:
android:focusable="true"
android:focusableInTouchMode="true"
Related
I have such a problem. I'm trying to get id of row in ListView. To do so I'm using setOnItemClickListener:
wyswietlenieZadan = (ListView)findViewById(R.id.ListView);
mAdapter = new ArrayAdapter<>(this, R.layout.zadania, R.id.opisZadania, listaZadan);
wyswietlenieZadan.setAdapter(mAdapter);
wyswietlenieZadan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Twoje id: " +id, Toast.LENGTH_SHORT).show();
}
});
Here is my zadania.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<TextView
android:id="#+id/opisZadania"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="20sp"
android:layout_marginStart="32dp"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:clickable="true"
android:contextClickable="true" />
<ImageButton
android:layout_width="wrap_content"
app:srcCompat="#android:drawable/ic_menu_close_clear_cancel"
android:id="#+id/task"
android:layout_marginEnd="19dp"
android:layout_height="wrap_content"
style="#android:style/Widget.ImageButton"
android:background="#android:drawable/ic_delete"
android:onClick="usunZadanie"
android:layout_alignTop="#+id/opisZadania"
android:layout_alignParentEnd="true"
android:focusable="false" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_edit"
android:id="#+id/imageView"
android:layout_toStartOf="#+id/task"
android:layout_alignTop="#+id/task"
android:onClick="edytujZadanie"
android:focusable="false" />
</RelativeLayout>
It doesnt work but when I use simple_list_item_1.xml as layout instead of zadania.xml, I can get id of each record. Anybody knows why it doesn't work with my custom layout?
Change:
Toast.makeText(getApplicationContext(), "Twoje id: " +id, Toast.LENGTH_SHORT).show();
to:
Toast.makeText(getApplicationContext(), "Twoje id: " +position, Toast.LENGTH_SHORT).show();
as the position variable gives you the position of the item you just clicked. If you clicked the first item it will show Twoje id:0 and if you clicked the second item it would show Twoje id:1 and so on...
The GridView is the following:
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp"
android:fadeScrollbars="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="afterDescendants"/>
The Java code is the following:
gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(iColumnCount);
gridview.setAdapter(dta);
gridview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int iPosition, long l) {
utility.logd("gridview", "selected item position:" + iPosition);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
utility.logd("gridview", "nothing selected.");
}
});
gridview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
utility.logd("gridview", "item clicked");
}
});
Only onNothingSelected() is called once after the apps starts. None of the other listeners is called when the focus on the GridView is changed with a remote control, or any item is clicked.
Could anyone offer a tip on how to make the listeners work?
Everything else of the GirdView works as expected.
Edit: The following is the layout for each grid cell:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_gravity="center"
android:id="#+id/biyee_relativeLayoutRoot">
<ImageButton
android:id="#+id/imageButton"
android:background="#drawable/button_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="snapshot"
android:layout_gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_margin="0dp"
android:padding="10dp"
android:src="#drawable/ic_launcher"
android:onClick="onClick_imageButton"/>
<LinearLayout
android:id="#+id/linearLayoutCaption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_overlay"
android:orientation="vertical"
android:layout_margin="10dp"
android:padding="5dp">
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textViewModel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Model"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="#+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Time"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<TextView
android:id="#+id/textViewPosition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="n"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_margin="15dp"/>
</FrameLayout>
Edit (2):
Per the tip of Daniel Nugent, I removed the ImageButton from the custom item view layout for the purpose of testing, and OnItemSelectedListener started working perfectly. Therefore the culprit is ImageButton. It seems any clickable object would ruin OnItemSelectedListener.
Edit (3): For the sake of testing, I used the following layout for the custom item view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="300dp"
android:orientation="vertical"
android:background="#color/red">
</LinearLayout>
OnItemSelectedListener works perfectly, but OnClickListener and OnItemClickListener are never called no matter whether I use a mouse or remote control to click an item.
Try using OnItemClickListener instead of onItemSelectedListener
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
//Your Logic here
}
);
onItemSelectedListener will do the job if you implements some kind of selection like arrows or some other thing.
From developer.android.com
Callback method to be invoked when an item in this view has been
selected. This callback is invoked only when the newly selected
position is different from the previously selected position or if
there was no selected item.
Impelmenters can call getItemAtPosition(position) if they need to
access the data associated with the selected item.
I have a ListView with a custom adapter and list items containing TextView(s) only. The list items have an OnItemClick method set in the onCreate callback method.
templatesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(DEBUG_TAG, "templatesListView onClick()");
//item is selected from the cursor to get necessary data
Log.d(DEBUG_TAG, "ListView count: " + templatesListView.getCount());
Log.d(DEBUG_TAG, "messagesCursor count: " + messagesCursor.getCount());
if (position >= messagesCursor.getCount()) {
Log.d(DEBUG_TAG, "Unable to access element " + position + ", it does not exist in the messagesCursor. Cursor count: " + messagesCursor.getCount());
}
messagesCursor.moveToPosition(position);
final String selectedItemName = messagesCursor.getString(1);
AlertDialog.Builder builder = new AlertDialog.Builder(SendMessageActivity.this);
builder.setTitle(selectedItemName).setMessage("Do you want to use template: "+selectedItemName+"?");
//Use template onClick
builder.setPositiveButton("Use", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int x) {
messageEditText.setText(selectedItemName);
}
});
//Cancel onClick
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int x) {
}
});
builder.show();
}
});
The ListView in the activity layout file is defined as:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/templatesListView"
android:layout_alignParentRight="true"
android:clickable="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/sendButton" />
The list item is defined in a separate layout file as:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/name_textView" />
The onClick method is called correctly when I run the app on Android 4.4.4, but when I run it on Android 5.1.1 it is not called at all.
The list item layout has been also created for v21+ separately, please find the code below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Medium Text"
android:id="#+id/name_textView"
android:singleLine="true"
android:textColor="#color/foreground_material_light"
android:theme="#android:style/Widget.Material.Light.Button.Borderless" />
Do you guys know what should I change to make it work on API level 21+ ? Is that a matter of the xml file only (attributes?) or should I change the implementation? Cheers!
Just remove this,
android:clickable="true"
from your ListView. It is actually blocking the clicks from getting transferred to the individual rows.
SOLVED: I think the additional layout file was confusing for the application. It was named exactly the same as the original one, but with (v21) added at the end of it. After removing the second file it works fine.
Looking at this closely related problem and the selected answer, I think you should make some changes to your code.
Seeing that you have the LinearLayout which has a ListView as a child view - and this ListView has the LinearLayout as a child, which in turn has TextView as a child? In this case, perhaps the child views of your ListView should have the onClickListener. This means setting the following attributes to your name_textView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:id="#+id/name_textView" />
Then keep the templatesListView as :
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/templatesListView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/sendButton" />
And finally, change your list-item linear layout as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:weightSum="1">
Give it a try and let me know if this helps.
I got an activity that contains a single listview.
This listview displays a relativeLayout with a bitmap and a checkbox and when no bitmap is available, a text is displayed.
My problem is with rows that contains no bitmap. When I click on the checkbox, the height of the row changes (increase when checkbox is checked, de crease when unchecked)
activity.xml
<?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" >
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/gradient_divider"
android:dividerHeight="1dp" />
</LinearLayout>
row.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="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:layout_toLeftOf="#+id/registrationCheckBox"
android:filter="true" >
</ImageView>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:paddingLeft="5dp"
android:layout_toLeftOf="#+id/registrationCheckBox" >
</TextView>
<CheckBox
android:id="#+id/registrationCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:paddingRight="5dp" >
</CheckBox>
</RelativeLayout>
My activity code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
hideActionBar();
// init the controls
initControls();
// populate the listView
this.adapter = new RegistrationAdapter(this, refreshContent());
this.listView.setAdapter(this.adapter);
this.listView.setItemsCanFocus(false);
this.listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
this.listView.setOnItemClickListener(
new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
...
}
}
);
}
Any idea on how to prevent my row height to change when clicking on the checkbox ?
I think you are missing the important part of this code, and that is the Adapter code.
How are you handling switching between bitmap and text?
Perhaps set the ImageView to a fixed height (tall enough to fit in any actual image), or provide a dummy invisible image when there is no real image to show.
i tryied to custom my ListView , so i use this item layout 「 list_item.xml」
<?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:gravity="center_vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="10dp"
android:textSize="16dip"
android:textColor="#000000"
android:gravity="center_vertical"
android:background="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1">
</TextView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#FFFFFF"
android:padding="10dip"
android:gravity="center_vertical">
<ImageView
android:id="#+id/pause"
android:src="#drawable/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pauseBtn_onClick"
/>
<ImageView
android:id="#+id/play"
android:src="#drawable/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playBtn_onClick"/>
</RelativeLayout>
</LinearLayout>
just as what you saw, there are 2 images in this layout, what i wanna do is,
when user click any item , the play_icon(the second image in the listview item) , will
disappear.
so i write code as the following :
mp3_listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View _view, int index , long arg3)
{
String _url = musicList.get(index).get("link").toString();
LinearLayout _container = (LinearLayout)mp3_listView.getChildAt(index);
RelativeLayout _container2 = (RelativeLayout) _container.getChildAt(1);
ImageView img = (ImageView)_container2.getChildAt(1);
img.setVisibility(View.INVISIBLE);
Toast.makeText(tabDigTest.this , "GG : "+_container.findViewById(R.id.play) , Toast.LENGTH_SHORT).show();
}
}
but something strange occurred .... more than one item's img be hidden because of the:
img.setVisibility(View.INVISIBLE);
i don't why because i only set one img to INVISIBLE ......i think i need help O_o
To retrieve the user click, I guess you set up an OnItemClickListener.
ListView lv = (fecth your list vieW)
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//you might want to use 'view' here
}
});
In the onItemClick method, the view parameter is the view you returned in your ListAdapter's getView method. So if say, you have returned a LinearLayout containing three childrens with id ("textview", "imageview1", "imageview2"), you would simply call view.findViewById(R.id.imageview1) to retrieve the first ImageView.