how to detect tap anywhere on listview - android

I want an action to be performed every time I click on the screen . There is a list view inside Relative layout. When i set setOnCLickListener for relative layout alone when there is no listview inside it it is working but not with listview inside it. The items in the listview will be displayed only when I click on the screen each time . I tried setItemOnClickListener.Thats not working too.
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:clickable="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:background="#drawable/bg">
<ListView
android:id="#+id/list_msg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_below="#+id/meLbl"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:listSelector="#android:color/black"
android:transcriptMode="alwaysScroll"
android:divider="#null" />
<TextView
android:id="#+id/meLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="Amanda"
android:textColor="#color/white"
android:textSize="20dp" />
<TextView
android:id="#+id/friendLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Janet"
android:textColor="#color/white"
android:textSize="20dp" />
</RelativeLayout>
The click is working only on the two text views not on the screen.
I want the click to work on the screen

for find out clicked on screen :
getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(SecondActivity.this, "click on screen ", Toast.LENGTH_SHORT).show();
}
return false;
}
});
and for find out clicked on listView :
listView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(SecondActivity.this, "Clicked On ListView", Toast.LENGTH_LONG).show();
return false;
}
});

Related

Changing android:layout_weight makes OnTouchListener not working

I am trying to implement a virtual joystick that is sending data through a socket. I want data to be sent only when user is pressing (touching) the joystick. Everything was working fine with the code, but I decided that joystick view should be bigger. To achieve it i changed the android:layout_weight in the leftSide RelativeLayout from 4 to 3. After this adjustment on touch events are never registered. Joystick animation is working perfectly though. Reverting changes make the program work again.
What is happening here? What are the other ways to make view bigger on the screen?
I have the following layout file
<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="horizontal"
android:baselineAligned="false"
tools:context="com.my.package.DisplayControlActivity">
<RelativeLayout
android:id="#+id/leftSide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" >
<!-- working fine with android:layout_weight="4" -->
<SurfaceView
android:id="#+id/videoStream"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</SurfaceView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rightSide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:id="#+id/testImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:contentDescription="#string/test_image"
android:scaleType="fitStart"
android:src="#drawable/test_image" >
</ImageView>
<com.my.package.SquareJoystickView
android:id="#+id/virtual_joystick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</com.my.package.SquareJoystickView>
</RelativeLayout>
And here is OnTouchListerer part of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* some irrelevant code */
joystickView = (SquareJoystickView) findViewById(R.id.virtual_joystick);
isSending = false;
joystickView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
isSending = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isSending = true;
}
return true;
}
});
}

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"

Change ImageView source for some time after button click

I have two buttons and one imageview:
<RelativeLayout 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"
tools:context=".MainActivity"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" >
<ImageView
android:id="#+id/dog"
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/dogsec" />
<ImageButton
android:id="#+id/barkk"
android:contentDescription="#string/desc"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/dog"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:src="#drawable/bark" />
<ImageButton
android:id="#+id/rrrr"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/barkk"
android:layout_below="#+id/barkk"
android:layout_marginRight="41dp"
android:layout_marginTop="15dp"
android:contentDescription="#string/desc"
android:src="#drawable/rrr" />
</RelativeLayout>
a) I need to make Imageview change its' source to another one(I mean the image's default state is picture1, it should show picture2 and go back to picture1) on button "barkk" click. It should not show picture2 as long as barkk is pressed.
b) and I need to display picture3 as long as button rrrr is pressed.
I use exactly ImageView which should change its' source depending on case a) or b) as it is described above.
Help me please. Thanks in advance
Use this code.
myButton.setOnClickListener(new OnClickListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN) {
myImageView.setImageResource(R.drawable.myNewImage);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
myImageView.setImageResource(R.drawable.myNewImage);
}
}
};

Android RelativeLayout onclicklistener doesn't get fired

This is my XML layout - it's an info window.
i want that when the user clicks the #+id/fragment_info_relativelayout_content layout - the info window will be closed.
This is my XML:
<RelativeLayout
android:id="#+id/fragment_info_relativelayout_content"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="#drawable/background_info_window"
android:clickable="true" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_margin="5dp" >
<!-- The info textView -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_info_textview_info"
style="#style/text_shadow_black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:gravity="center"
android:text="Some Text "
android:textColor="#android:color/white"
app:isBold="true" />
</ScrollView>
<!-- Tap to close -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_info_textview_tap_to_close"
style="#style/text_shadow_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingRight="5dp"
android:text="#string/button_tap_to_close"
android:textColor="#android:color/white"
app:isBold="true" />
</RelativeLayout>
This is my code:
public InfoWindow(ViewGroup rootView)
{
this.container = rootView;
infoWindowView = LayoutInflater.from(rootView.getContext()).inflate(INFO_WINDOW_LAYOUT_ID, null);
//find info window's view
contentLayout = infoWindowView.findViewById(R.id.fragment_info_relativelayout_content);
contentLayout.setClickable(true);
contentLayout.setOnClickListener(this);
}
#Override
public void onClick(View v) {
//close the info window
close();
}
my problem is that the onClick method isn't being fired on every click on the layout...
it's weird since it does fire after clicking some clicks, or click at the bottom right on the textview...
any ideas ??
My suggestion, is that the ScrollView in the RelativeLayout handles all touch events, and don't let the RelativeLayout handle any touch events.
In this case, you should extend the ScrollView and make the onTouchEvent return false
#Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return false;
}
And the same to onInterceptTouchEvent
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
super.onInterceptTouchEvent(ev);
return false;
}
Just try it out and tell me what you got =)

How to catch RelativeLayout pressed state in Android?

I have Relative Layout with two TextViews inside.
How I can change text color TextViews, when RelativeLayout is pressed?
I tried used selectors, but Relative Layout don't support text color attribute.
Also a tried catch Relative Layout pressed state with OnFocus and OnTouch Listeners, but they don't working(OnFocusChangeListener) or working in one side(OnTouchListener).
The code:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="243dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/singup_button_selector"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:id="#+id/sungup"
android:textColor="#drawable/singup_button_text_selector"
android:focusableInTouchMode="true">
<TextView
android:id="#+id/singupTV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/singup"
android:textColor="#3c5775"/>
<TextView
android:id="#+id/singupTV2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#+id/singupTV1"
android:layout_marginTop="15dp"
android:gravity="center"
android:text="#string/singup_extended"
android:textColor="#506378"
android:textSize="15dp" />
</RelativeLayout>
You can set an onClickListener on the RelativeLayout, then in the onClick method, change the text colors in both TextViews in your code.
What worked for me was setting an OnTouchListener.
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
// focus in
} else{
// focus out
}
return false;
}
});

Categories

Resources