get touch data (and not just keystrokes) when using keyboard - android

I want to obtain touch information (by obtaining motionEvents and not just click, keyUp,...) when I am using the keyboard on a TextEdit view.
I solved this for button views like this:
Button button = (Button) findViewById(R.id.buttonSubmit);
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Save event information (such as pressure, size,..) here
return false;
}
});
and so I am trying to use the same approach with KeyboardView with no luck
KeyboardView keyboard = (KeyboardView) findViewById(R.id.keyboardView);
keyboard.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Save event information (such as pressure, size,..) here
return false;
}
});
The code seems to never get inside the onTouch function when the keyboard is visible. Here's the layout.xml
<EditText android:id="#+id/edit_answer1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="#string/edit_answer" />
<Button
android:id= "#+id/buttonSubmit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/button_submit" />
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />

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

Distinguish two finger rotation and simple tap

In my fragment I have a TextView and underneath there are two buttons.
I need to implement a OnTouchListener to do two things:
When I rotate my two fingers, the TeXtView will rotate
When I tap the TeXtView, the buttons underneath are called
Right now it doesnt work but I cant find better solution.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_centerInParent="true"
tools:context="com.fjord.yalc.MainActivity"
android:id="#+id/player_fragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"/>
<Button
android:id="#+id/buttonDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"/>
</LinearLayout>
<com.fjord.yalc.AutoResizeTextView
android:id="#+id/player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:text="#string/normal_start"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.DialogWindowTitle"
android:textColor="#color/colorLightGray"
android:textSize="300sp"
android:textStyle="bold"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
JAVA:
Btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.setText(String.valueOf(++life));
}
});
mRotationDetector = new RotationGestureDetector(this);
TV.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int maskedAction = event.getActionMasked();
switch (maskedAction) {
case MotionEvent.ACTION_DOWN: {
return false;
}
case MotionEvent.ACTION_OUTSIDE: {
return false;
}
case MotionEvent.ACTION_MOVE: {
mRotationDetector.onTouchEvent(event);
break;
}
}
return true;
}
});
Ok, after a week I found the answer. I needed to dispatch my MotionEvent to parent of my buttons using dispatchTouchEvent. ACTION_POINTER_DOWN is for checking if user uses two fingers.
Now rotation goes to mRotationDetector and taps goes to onClickListeners of the buttons underneath the TextView player.
JAVA:
player.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
MotionEvent me = MotionEvent.obtain(event);
mRotationDetector.onTouchEvent(me);
switch (event.getActionMasked()){
case (MotionEvent.ACTION_DOWN):
flag=true;
break;
case(MotionEvent.ACTION_POINTER_DOWN):
flag=false;
break;
}
buttons.dispatchTouchEvent(event);
return true;
}
});

ScrollBar doesnt work in EditText

Hope all of you doing well.
My Problem is:
I have xml code like following:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_left_margin"
android:layout_marginRight="#dimen/activity_right_margin"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_bottom_padding"
android:paddingTop="#dimen/activity_top_padding" >
<EditText
android:id="#+id/custDetailsNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name_cust_all_add"
android:inputType="textPersonName"
android:textSize="#dimen/common_fontsize" />
<EditText
android:id="#+id/custDetailsDescEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="#string/description_add_all_cust"
android:inputType="textMultiLine"
android:lines="5"
android:minLines="3"
android:gravity="top|left"
android:scrollbars="vertical"
android:scrollbarStyle="insideInset"
android:overScrollMode="always"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:textSize="#dimen/common_fontsize" />
</LinearLayout>
</ScrollView>
In that i have two EditText in </ScrollView>, Second <EditText/> does not scrolling inner side. Problem is Full View is scrolled while i am scrolling but i want to also scroll vertically in second EditText when user enter more than 5 lines. In that ScrollBar is displaying but not scrolling.
How can i solve this.
Your Help would be appreciated.
This code should work.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.parentScrollView).setOnTouchListener(parentListener);
findViewById(R.id.custDetailsDescEditText).setOnTouchListener(childListener);
}
OnTouchListener parentListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.custDetailsDescEditText).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
};
OnTouchListener childListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
};
Try something like this in your code
ScrollView scroller = new ScrollView(this);
EditText et=(EditText)findViewById(R.id.yourEdittext);
scroller.addView(et);

Android onTouch Activity New XML File Error

I have 3 XML files. The first one is for main activity. It has one button. When I touch this button, it works and it sends me to second XML file. Also the second screen has a button. Now, I want to make same operation here and I wanna go third XML file. But it doesn't work.
I did same procedure for all buttons. I can't find that where is my fault.
MainPageActivity:
public class MainPageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
ImageButton b1 = (ImageButton) findViewById(R.id.button_compare);
ImageButton b2 = (ImageButton) findViewById(R.id.button_find);
b1.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View comparePage, MotionEvent event) {
setContentView(R.layout.compare_pagee);
return true;
}
});
b2.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View findPage, MotionEvent event) {
setContentView(R.layout.find_page);
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_page, menu);
return true;
}
}
activity_main_page.xml:
<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=".MainPageActivity"
android:id="#+id/mainActivity" >
<ImageButton
android:id="#+id/button_compare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:adjustViewBounds="true"
android:scaleType="center"
android:src="#drawable/compare"
/>
<ImageButton
android:id="#+id/button_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="69dp"
android:adjustViewBounds="true"
android:src="#drawable/find"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal"
android:text="Tap the top to Compare!"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button_compare"
android:gravity="center_vertical|center_horizontal"
android:text="Tap the bottom to Find!"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Compare:
public class Compare extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.compare_pagee);
ImageButton b3 = (ImageButton) findViewById(R.id.compareButton);
b3.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View comparePage, MotionEvent event) {
setContentView(R.layout.compare_pagee);
return true;
}
});
}
}
compare_pagee.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#drawable/backgroundwallpaper"
android:clickable="true"
android:orientation="vertical"
tools:context=".Compare" >
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton
android:id="#+id/compareButton"
android:layout_width="139dp"
android:layout_height="74dp"
android:src="#drawable/comparebutton" />
<Spinner
android:id="#+id/spinner6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
You're not starting new Activities, you're merely switching layouts in the first Activity. So the first button will work because it has a listener assigned to it in MainActivity. However b3 is is another Activity which never gets started, so depending on what you want to do, you can either start a new Activity for each button click, or you can move b3's Listener inside MainActivity.
First method (new Activities)
ImageButton b1 = (ImageButton) findViewById(R.id.button_compare);
ImageButton b2 = (ImageButton) findViewById(R.id.button_find);
b1.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View comparePage, MotionEvent event) {
startIntent (new Intent (MainPageActivity.this, Compare.class));
return true;
}
});
b2.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View findPage, MotionEvent event) {
startIntent (new Intent (MainPageActivity.this, Find.class));//You will NEED to make a new Activity called Find.
return true;
}
});
The second way (resetting content views in the same activity). I do not recommend it, since it makes things messy and goes against what an Activity should be, but it's still a (bad) option.
ImageButton b1 = (ImageButton) findViewById(R.id.button_compare);
ImageButton b2 = (ImageButton) findViewById(R.id.button_find);
b1.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View comparePage, MotionEvent event) {
setContentView(R.layout.compare_pagee);
ImageButton b3 = (ImageButton) findViewById(R.id.compareButton);
b3.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View comparePage, MotionEvent event) {
setContentView(R.layout.compare_pagee);
return true;
}
});
return true;
}
});
b2.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View findPage, MotionEvent event) {
setContentView(R.layout.find_page);
return true;
}
});
}
As you can see, b3's Listener extremely redundant - it doesn't do anything useful.
I suggest rethinking what you want your code to do, and rewriting it properly, since this is not proper structure.

How to make a Layout listener ignore views on top of it - android

I have a LinearLayout and I want to see when a user has touched it, and has released their finger. I thought I could do this by adding a onTouch listener and checking the event. This linearLayout holds a bunch of buttons.
My problem isn't the listener not working, it just isn't doing what I thought it would do. The listener only gets activated if ONLY the layout has been touched, which makes sense, but isn't what I want. If the user touches a button inside of the layout the layout listener isn't called, and I want it to be called.
Here is my code:
java code:
ll = (LinearLayout)findViewById(R.id.CategorySelect_lLayout_noID);
ll.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(CategorySelect.this, "action DOWN called", Toast.LENGTH_SHORT).show();
return true;
}
if(event.getAction() == MotionEvent.ACTION_UP){
Toast.makeText(CategorySelect.this, "action Up called", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
my xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:scrollbars="none"
>
<LinearLayout
android:id="#+id/CategorySelect_lLayout_noID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/CategorySelect_b_1"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_1"
/>
<Button
android:id="#+id/CategorySelect_b_2"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_2" />
<Button
android:id="#+id/CategorySelect_b_3"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_3" />
<Button
android:id="#+id/CategorySelect_b_4"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_4" />
<Button
android:id="#+id/CategorySelect_b_5"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_5" />
<Button
android:id="#+id/CategorySelect_b_6"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_6" />
<Button
android:id="#+id/CategorySelect_b_7"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_7" />
</LinearLayout>
</ScrollView>
Thanks for reading.
UPDATED CODE FOR COMMENTS:
My main xml file that is inflated from my activity:
<?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:background="#drawable/categoryselect_background"
android:scaleType="fitXY"
>
<TextView
android:id="#+id/CategorySelect_tv_chooseacat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:gravity="center"
android:text="#string/CategorySelect_tv_chooseacat"
android:textColor="#FF5500"
android:textSize="34dp"
android:textStyle="bold"
/>
<Button
android:id="#+id/CategorySelect_b_settings"
android:background="#drawable/menu_settings"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:scrollbars="none"
>
<com.tdubstudios.soundboard.hungergames.ScrollAbleLinearLayout
android:id="#+id/CategorySelect_lLayout_noID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/CategorySelect_b_cato"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_1"
/>
<Button
android:id="#+id/CategorySelect_b_cinna"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_2" />
<Button
android:id="#+id/CategorySelect_b_effie"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_3" />
<Button
android:id="#+id/CategorySelect_b_gale"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_4" />
<Button
android:id="#+id/CategorySelect_b_haymitch"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_5" />
<Button
android:id="#+id/CategorySelect_b_katniss"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_6" />
<Button
android:id="#+id/CategorySelect_b_peeta"
style="#style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/CategorySelect_b_7" />
</com.tdubstudios.soundboard.hungergames.ScrollAbleLinearLayout>
</ScrollView>
</RelativeLayout>
This is the ScrollAbleLinearLayout class:
package com.tdubstudios.soundboard.hungergames;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
public class ScrollAbleLinearLayout extends LinearLayout {
public ScrollAbleLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
return false; // Tell children not to use this event
}
}
Yes I know the naming on some things have changed from my previous code, that is because I changed it to hide what type of app I was making (from search results). However for whatever reason I no long care, so that's the reasoning behind that.
Thanks.
THIRD EDIT:
So I'm currently using
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
//requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
if(event.getAction() == MotionEvent.ACTION_DOWN){
return false;
}
if(event.getAction() == MotionEvent.ACTION_UP){
return false;
}
return true; // Tell children not to use this event
}
And it works the way I want it to, however it isn't practical. For it to actually register the button clicks you have to make sure you don't move your finger at all left/right/up/down. It has to be like exactly down and exactly up. I was trying to add a onFling method, and then tracking the distance the finger as moved. And if it has moved less then x amount of pixels then make the main method return false which will allow the buttons to work, but if it was actually a swipe/move then the button doesn't need to get activated. Here is what I mean:
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > MORE_THAN_X_INT) {
booleanVar = false;
return false;
}else{
booleanVar = true;
}
return false;
}
Then doing:
Override
public boolean onInterceptTouchEvent(MotionEvent event) {
//requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
if(event.getAction() == MotionEvent.ACTION_DOWN){
return false;
}
if(event.getAction() == MotionEvent.ACTION_UP){
return false;
}
return booleanVar; // Tell children not to use this event
}
I think that would work, but I can't test it because there isn't a onFling method for extends LinearLayout
Any suggestions?
Please note my returning true/false might be backwards, I'm not sure it's kind of confusing me. But if someone could lead me in a way to making that onFling method I can mess around with it to get it the correct values.
I have
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
//requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
while(event.getAction() == MotionEvent.ACTION_DOWN){
return true;
}
while(event.getAction() == MotionEvent.ACTION_UP){
return true;
}
return false; // Tell children not to use this event
}
and my buttons are never click able. But the ontouch listener works perfectly haha :|
I would recommend extending LinearLayout and using onInterceptTouchEvent to catch the touch event, then return true to force its children to not receive the events.
Something like this:
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return true; // Tell children not to use this event
}
}
And, then in your XML, change LinearLayout to com.yourpackagename.MyLinearLayout.
Addendum: If you want both the Buttons and the LinearLayout to receive the event, you can change the onInterceptTouchEvent to this:
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Whatever touch event code you want, using the "event" variable
return false; // Allow children to use the event
}
So I never really found the answer to my question, but I found a way that works. Instead of putting a listener on the layout, I am just adding the onTouchEvent method for the full activity rather than a specific view.
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(CategorySelect.this, "action DOWN called", Toast.LENGTH_SHORT).show();
}
if(event.getAction() == MotionEvent.ACTION_UP){
Toast.makeText(CategorySelect.this, "action Up called", Toast.LENGTH_SHORT).show();
}
return super.onTouchEvent(event);
}
It doesn't effect any of the buttons, so it's exactly what I wanted. Thank you so much Soxxeh for working with me.

Categories

Resources