Android onTouch Activity New XML File Error - android

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.

Related

Problem with the auto-dismiss of dialog in setOnLongClickListener

I have problem with setOnLongClickListener. When I use this method I want the dialog, which will open, to automatically close by itself (which the method's return false should do) when I take my finger off the screen. Is it possible to do this with this method, or are there other suitable methods?
Create Dialog
protected void showDialogLongClick(final int position){
final Dialog dialog = new Dialog(activity);
dialog.setCancelable(true);
View v = activity.getLayoutInflater().inflate(R.layout.onlongclick_card_mygift,null);
dialog.setContentView(v);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
nameLong = v.findViewById(R.id.name_longclick);
descriptionLong = v.findViewById(R.id.gift_description);
addressLong = v.findViewById(R.id.gift_address);
imageCategory = v.findViewById(R.id.small_category);
nameLong.setText(myGift.get(position).getName());
descriptionLong.setText(myGift.get(position).getDescription());
addressLong.setText(myGift.get(position).getAddress());
String cat = myGift.get(position).getCategory();
changeCategoryImage(cat, imageCategory);
dialog.show();
}
Listener
holder.card.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
showDialogLongClick(position);
return false;
}
});
XML card dialog
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_card_mygift"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/name_longclick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_weight="1"
android:layout_marginEnd="10dp"/>
<ImageView
android:id="#+id/small_category"
android:layout_weight="0"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#F44336"
android:textSize="22sp"/>
<TextView
android:id="#+id/gift_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFFFF"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#F44336"
android:textSize="22sp"/>
<TextView
android:id="#+id/gift_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFFFF"
android:textSize="20sp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//Dismiss the dialog
}
return false;
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
//Create the dialog
return true;
}
});
Use setOnLongClickListener for creating the dialog and use setOnTouchListener for dismissing the dialog.
So you should add setOnTouchListener to the view. Call dialog.dismiss() when MotionEvent action is MotionEvent.ACTION_UP (user lifts his finger).
If the view is inside a scroll view the views onTouchListener does not get called. A simple solution is to add an onTouchListener to the scroll view and dismiss the dialog when user lifts his finger.
scrollView.setOnTouchListener( new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//Dismiss the dialog
return true;
}
return false;
}
});

How to click on a child when a palm is rested on a parent

Currently i am unable to click on the buttons when the palm is rested on the screen.Want to able to touch yes/no buttons even though palm rested on screen.Please check below image for more information.
My activity code:: overide onTouch method of yes/no buttons not calling when palm rested and touching buttons
no_btn = (Button) findViewById(R.id.no_btn);
yes_btn = (Button) findViewById(R.id.yes_btn);
txt = (TextView) findViewById(R.id.txt);
bg = (LinearLayout) findViewById(R.id.root);
views_parent = (LinearLayout) findViewById(R.id.root1);
//views_parent.setFocusable(true);
views_parent.setFocusableInTouchMode(true);
bg.setFocusableInTouchMode(false);
bg.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent arg1) {
return true;
}
});
yes_btn.setId(1);
no_btn.setId(0);
no_btn.setOnTouchListener(this);
yes_btn.setOnTouchListener(this);
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("child onTouch>>>","onTouch>>>2>>"+v.getId());
if (v.getId() == 0)
{
isTouchable = false;
}else if (v.getId() == 1)
{
isTouchable = false;
}
return false;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/root"
tools:context="com.example.build.multitouch.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/txt"/>
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:id="#+id/root1">
<Button
android:id="#+id/no_btn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="0"
android:text="no"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="#+id/yes_btn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="0"
android:text="yes"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

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

Android OnTouchListener Calling Wrong View

I have two views in my activity in my Android app. One View (we'll call this viewOne) has a bunch of SeekBars while the other view (we'll call this viewTwo) contains a Drawable that is redrawn whenever a user touches it.
viewTwo has an OnTouchListener. The issue is that when I touch viewOne to move a SeekBar, viewTwo's touch listener is getting called and this creates a huge amount of lag due to it needing to redraw every time the listener is called.
Why is this happening and how can I stop it? Thanks!
EDIT:
Main Activity Layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<com.rfoo.viewOne
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:id="#+id/vOne"/>
<com.rfoo.viewTwo
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:id="#+id/vTwo"/>
</LinearLayout>
View One Layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:id="#+id/sb1"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:id="#+id/sb2"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:id="#+id/sb3"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:id="#+id/sb4"/>
</LinearLayout>
View Two Layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myView"/>
</LinearLayout>
Main Activity Code:
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initializations here...
// And then:
View viewTwo = (View)findViewById(R.id.vTwo);
viewTwo.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.vTwo) {
vTwo.invalidate();
}
}
}
You implemented View.OnTouchListener but you haven't used it. I think your purpose is using that to trigger onTouch()
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initializations here...
// And then:
View viewTwo = (View)findViewById(R.id.vTwo);
viewTwo.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.vTwo) { // you can use viewTwo instead of v as well
viewTwo.invalidate();
}
}
}
Well i am almost sure the problem is because the unused nested layouts i tried this and worked fine for me :
public class exampleActivity extends AppCompatActivity {
private static final String DEBUG_TAG = "Motion event";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_layout);
findViewById(R.id.myView).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return (v.getId() == R.id.myView) &&
handleGesture(MotionEventCompat.getActionMasked(event));
}
});
}
private boolean handleGesture(int action) {
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d(DEBUG_TAG, "Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,"Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
Log.d(DEBUG_TAG,"Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d(DEBUG_TAG,"Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return false;
}
}
}
And this is my xml layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".25"
android:id="#+id/sb1"/>
<SeekBar
android:layout_width="match_parent"
android:layout_weight=".25"
android:layout_height="0dp"
android:id="#+id/sb2"/>
<SeekBar
android:layout_width="match_parent"
android:layout_weight=".25"
android:layout_height="0dp"
android:id="#+id/sb3"/>
<SeekBar
android:layout_width="match_parent"
android:layout_weight=".25"
android:layout_height="0dp"
android:id="#+id/sb4"/>
<View
android:layout_weight="1"
android:background="#color/appframe__colorAccent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/myView"/>
</LinearLayout>

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

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" />

Categories

Resources