I have a ListView which has a Custom TextView which i got from here: FlowTextView
The problem is if i use a normal default TextView everything works perfectly fine,But if i use this custom text view all the Click Events (LongClick and OnClick) dont work if clicked inside the text view i.e. the click event only work at places where this Custom TextView doesn't reach to (The edges of my List Row).
I have tried
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="true"
But none of it worked, or maybe i did it in the wrong way...if you think any of these methods work please elaborate on how to use this
My implementation in the FlowTextView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#ff71ff34">
<uk.co.deanwild.flowtextview.FlowTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:text="Description"
android:textSize="24sp"
android:background="#1b1b1f"
android:textColor="#android:color/white">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ffffbb52"/>
</uk.co.deanwild.flowtextview.FlowTextView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CodeLearn 1"
android:background="#1b1b1f"
android:textColor="#android:color/white"/></RelativeLayout>
inside OnCreate
ListView mainList = (ListView)findViewById(R.id.listView1);
new Connect(mainList,this).execute(aa);
mainList.setClickable(true);
mainList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView <?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
// Toast.makeText(this, "Wrong Username/Password! TRY AGAIN!", Toast.LENGTH_LONG).show();
Log.d("long clicked","pos: " + pos);
return true;
}
});
activity_main.xml which contains the root listview
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"></ListView>
As said by #Vikram in comments
Check line 88 of FlowTextView.java: Link. That should answer your
question.
so basically the solution was to setOnTouchListener for that object to null
setOnTouchListener(null)
Related
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"
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 have a problem which i narrowed it down to a focus problem. At least it seems like a focus problem, i might be wrong.
Declerations:
LogListAdapter adapter;
Spinner filterTypeMenu;
ListView list;
onCreate:
list = (ListView) findViewById(R.id.loglist);
filterTypeMenu = (Spinner) findViewById(R.id.spinner1);
adapter = new LogListAdapter (this, R.layout.row, filteredLogs);
adapter.notifyDataSetChanged();
filterTypeMenu.setOnItemSelectedListener(this); // tried with inner class, same result
in my interface method when i complete my process:
..
..
adapter.notifyDataSetChanged();
..
..
and click listeners:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, Integer.toString(position),
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
My main 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/spinnerlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/filter_array"
android:prompt="#string/filter"/>
</LinearLayout>
<LinearLayout
android:id="#+id/listlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false">
<ListView
android:id="#+id/loglist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false">
</ListView>
</LinearLayout>
</LinearLayout>
Notify method is called repeatedly real quick and my list is updating quite fast due to behaviour of the process. When i remove the update block of the code, click events of the spinner is active, but when the update block is active, events are not working. I thought this was a focus problem but i couldn't be sure, since i the menu is clickable. The problem is that when i select one of the items in the list, spinner is not selecting that item and keep the old selected value, and the event of the click is not working either. Ideas will be much appreciated.
Funny, i fixed my problem with adding these two settings to the spinner, i was sure i tried this before but oh well, android focus is still a half mystery to me.
filterTypeMenu.setFocusable(true);
filterTypeMenu.setFocusableInTouchMode(true);
Actually i have a viewpager that holds a Imageview and a listview over it, So when i click on the Listview i need to hide the listview and the bottom bar to make the ImageView fullscreen for all the views in side the viewpager. And onclick of the image view get back the views. But my issues are:
I cant implement onClickListner on Listview, to make the whole view to handle the tap. Am forced to use the onItemclickListner and it only hides when you click on the item(which is not desired).
When i click Listview(which is inside Fragment) inside the pager it doesn't hide for all the views except for the currentview, But am able to hide the actionbar and the bottom bar(on the Fragment Activity).
a screen shot for reference:
How can i achieve this or this is there any better way that i can hide the views.
Here is my ListView in side a Fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:scrollbars="none" />
</RelativeLayout>
Here's my Fragment Activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/form" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="#+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="2dp" >
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btnSend"
android:drawableRight="#drawable/edit_keyboard"
android:inputType="textMultiLine"
android:maxLines="3" />
<ImageButton
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/send" />
</RelativeLayout>
</RelativeLayout>
Here am successful with the onitemclicklister to the ListView:
listComments.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (getSherlockActivity().getSupportActionBar().isShowing()) {
getSherlockActivity().getSupportActionBar().hide();
listComments.setVisibility(View.GONE);
form.setVisibility(View.GONE);
} else {
getSherlockActivity().getSupportActionBar().show();
listComments.setVisibility(View.VISIBLE);
form.setVisibility(View.VISIBLE);
}
}
});
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getSherlockActivity().getSupportActionBar().show();
listComments.setVisibility(View.VISIBLE);
form.setVisibility(View.VISIBLE);
}
});
Could you set the clickable property for the listView and check ? Also, whenever you add a new edittext to the do you add it dynamically ? If so that would mean that the you are adding items to the listView dynamically. The listView would not know if you have clicked on an item of the view itself hence a callback would get registered when you click the item as that takes preference.
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.