Touch not working on list view - android

I am using touch event on LinearLayout in which there is a list view(Its a child). When I touch on list view part touch event does not gets called. below is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llFavourite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/llProgressBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:gravity="center"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Loading"
android:indeterminate="true"
android:progressDrawable="#android:drawable/progress_indeterminate_horizontal"
android:visibility="gone" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:focusable="true">
</ListView>
</LinearLayout>
My animation:
final Animation slidedown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down_half);
llFavourite.setOnTouchListener(new OnSwipeTouchListener(MidnightMainActivity.this) {
#Override
public void onSwipeBottom() {
llFavourite.startAnimation(slidedown);
inflateFavouriteLayout(MODE_REFRESH);
}
});
but when I click on layout part event gets called. Is there any property which i should enable or disable?
Thanks in advance.

android:focusable="false"
android:focusableInTouchMode="false"
doesn't work for ImageButton.
In your layout xml, add this property to root layout
android:descendantFocusability="blocksDescendants"

from your calling class add
ListView lView = (ListView) findViewById(R.id.list);
lView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(position);
}
});

Related

Neither setOnItemSelectedListener nor setOnClickListener works for GridView

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.

TextView and Button in ListView - OnClick works, OnItemClick does not work

UPDATE
My button is as big as the view of each ListItem - maybe this causes the problem? Is it possible to press once and let both OnItemclick and OnClick respond?
I have a list view with two textviews and a button. I know there are a lot of questions being posted but I have read a lot of them and not one seems to work.
I want the OnItemClick of each view of the list view to work.
I also want the OnClick of the button inside each view of the list view to work.
Only the onClick of the button seems to respond
In my xml i have set android:focusable="false" for the button. For the textviews I have set android:textIsSelectable="false" android:clickable="false" android:focusable="false"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rectangle_order"
android:layout_gravity="center_horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/order_height"
>
<Button
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="#dimen/text_size"
android:textColor="#color/white"
android:rotation="90"
android:textIsSelectable="false"
android:clickable="false"
android:focusable="false"
/>
<TextView
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="#dimen/timer_size"
android:rotation="90"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="29dp"
android:textIsSelectable="false"
android:clickable="false"
android:focusable="false"
/>
</RelativeLayout>
</RelativeLayout>
this is the layout for my list view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="0.5"
android:layout_width="#dimen/order_height"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollbars="none"
>
</ListView>
</RelativeLayout>
In the GetView of my adapter i have :
Button b = (Button) rowView.findViewById(R.id.img);
b.setBackgroundResource(R.color.pending);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(numberOfTimers < 2){
view.setBackgroundResource(R.color.ongoing);
countDownTimer = new MyCountDownTimer(TIME_PANINI, 1000,timerTextView, view);
countDownTimer.start();
}
}
});
in my main activity i have a onItemClickListener set to the listView:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG_MAIN, "we are in select onitemclicklistener");
Toast.makeText(getApplicationContext(),"BOOOOH", Toast.LENGTH_SHORT).show();
}
});
Try to replace OnClickListener with OnTouchListener.
Like this:
b.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
if(numberOfTimers < 2){
view.setBackgroundResource(R.color.ongoing);
countDownTimer = new MyCountDownTimer(TIME_PANINI, 1000,timerTextView, view);
countDownTimer.start();
}
}
return false;
}
};);
When you return false at this listener, you mean "hey i'm not done with that action(press down), please let the action to the next View"

Android setOnclick listner to listview

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.

Android: Listview's row height changes when clicking on the checkbox it contains

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.

android how to access customized listview item

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.

Categories

Resources