Gather Touches from MainActivity - android

I want to gather all the possible touches from my entire screen. I implemented a
#Override
public boolean onTouchEvent(MotionEvent e)
{
double x = e.getX();
double y = e.getY();
}
But now I added a TableView to the "scene" and when I click on it the above method is not being called because the TableView is swallowing the Touches.
Everything around the yellow area is clickable but the yellow area itself not. I want it to be clickable as well, basically I want that all the time the WHOLE screen area is clickable so I can store touch positions. How to do it?
EDIT:
The onTouchEvent method is in MainActivity class.
Here is the activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow android:layout_height="wrap_content">
<TextView
android:id="#+id/column1"
android:text="tapcount"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>

cover whole view with your own transparent, like this:
<RelativeLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!--your content inside-->
</LinearLayout>
<View
android:id="#+id/cover"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</RelativeLayout>
then use your code for View with id=cover and return always false in your onTouchEvent as in doc:
Returns
True if the event was handled, false otherwise.
with false hardcoded MotionEvent will be delivered to views below/under

in your onCreate() method try implementing something like this
View contentView = (View) findViewById(R.id.linearLayout1); //Your layout
contentView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(getApplicationContext(), event.getX() + " " + event.getY(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});

Related

how to detect tap anywhere on listview

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

Start outSide scroll after edittext scroll finished

I've tried every simple examples on stackoverflow but nothing can solve my problem. Here's my question
I have ScrollView in activity and I have scrollbars="vertical" option editText inside ScrollView.
I want to scroll outside scrollview's scroll after inside edittext scroll finish, like on top of the edittext scroll or bottom of the edittext scroll
I thought that it can be done with view catchs scroll lisetener but nothing worked.
Try the below sample code hope it works for you.
In XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aa8822"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aa8822"
android:orientation="vertical" >
<EditText
android:id="#+id/edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:ems="10"
android:maxLines="3"
android:scrollbars="vertical"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
Put some more view so the scroll view scroll works.
Now in your activity-
EditText edt = (EditText) findViewById(R.id.edt);
edt.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.edt) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});

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

EditText Scrollable with keyborad active

Well friend ..
I have a design so:
The main Scroll is very necessary when appear the keyboard... If i dont make that, the button can't see...
The problem is when i try move inside the Edittext with the keyboard active...
I tried this:
android:scrollbars="vertical"
and this:
setMovementMethod(new ScrollingMovementMethod());
but not solve this yet
Anyone help me?
grax advance!
Check this without using scroll bar:
add permission in menifest file for corresponding activity.
android:windowSoftInputMode="adjustPan"
Please try this:
Your layout should be similar to example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext"
android:layout_width="200dp"
android:layout_height="500dp"
android:text="some long text"
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
and in your java try this:
EditText edit = (EditText)findViewById(R.id.edittext);
edit.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.edittext) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
It could be helpful.

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 =)

Categories

Resources