Make button action canceled if dragged outside a certain area - java

I have a microphone button in my app with which user inputs voice. It is using an OnTouchListener to detect whether user holds down the button or not. If they hold the button but drag their finger outside the button, MotionEvent still considers action as "ACTION_DOWN", even if they touch near the edge of the screen, as long as they don't stop touching. We need to change that so if the user drags their finger off of the button then MotionEvent picks up as "ACTION_UP".
recordBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
//User stops touching the button.
case MotionEvent.ACTION_UP:
if(isRecording) {
stopRecording();
isRecording = false;
}
break;
//User touches the button.
case MotionEvent.ACTION_DOWN:
if(!isRecording) {
startRecording();
isRecording = true;
}
break;
}
return false;
}
});
XML:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.9">
<TextView
android:id="#+id/recordText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/custom_button"
android:layout_centerHorizontal="true"
android:layout_marginStart="8dp"
android:layout_marginTop="195dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:fontFamily="sans-serif"
android:text="#string/hold_the_mic_and_record"
android:textColor="#color/white"
android:textSize="20sp"/>
<Button
android:id="#+id/custom_button"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#drawable/microphone" />
</RelativeLayout>

Related

How to detect ACTION_SCROLL events on a chromebook?

I have implemented the onGenericMotionEvent method in my activity as follows:
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_SCROLL:
float scrollX = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
float scrollY = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
Log.d("MA", "Mouse scrolled " + scrollX + ", " + scrollY);
return true;
}
}
return super.onGenericMotionEvent(event);
}
Although I roll the wheel button of the mouse, I never get the ACTION_SCROLL event, but only ACTION_HOVER_ENTER, ACTION_HOVER_MOVE and ACTION_HOVER_EXIT events.
The view over which I want to scroll is defined as follows:
<TextView android:id="#+id/Report"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="1dp"
android:visibility="visible"
android:clickable="true"
android:focusable="true"
style="#style/ReportStyle"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:paddingLeft="#dimen/smallMargin_land"
android:paddingRight="#dimen/smallMargin_land"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/cmdRows"
app:layout_goneMarginTop="1dp"
/>
What is wrong in my implementation?

how to get value of a plain text when click a radio button android studio

I want to get the value of a EditText when the user clicks the other radio button. I wrote the code but it didn't work correctly.
When I select the Other Specify Radio button, I can't get the value of the EditText but if I click one of the other radio buttons first and after that if I click the other specify radio button then I can get the value of the EditText
Here's the code:
public void PackageRGOnClick(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()){
case R.id.singlePlayRadiobtn:
if(checked){
packageDetails="Single";
}
break;
case R.id.doublePlayRadiobtn:
if(checked){
packageDetails="Double";
}
break;
case R.id.otherRadiobtn:
if(checked){
packageDetails=otherPackageRadio.getText().toString().trim();
}
break;
}
}
Layout file:
<EditText
android:id="#+id/OtherPackageRadiotxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="66dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:inputType="textPersonName"
app:backgroundTint="#color/lightGray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/packageBartxt" />
<RadioGroup
android:id="#+id/packageRadioGroup"
android:layout_width="283dp"
android:layout_height="101dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/packageBartxt">
<RadioButton
android:id="#+id/singlePlayRadiobtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:buttonTint="#color/midblue"
android:text="Single"
android:onClick="PackageRGOnClick"/>
<RadioButton
android:id="#+id/doublePlayRadiobtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/midblue"
android:text="Double"
android:onClick="PackageRGOnClick" />
<RadioButton
android:id="#+id/otherRadiobtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/midblue"
android:text="Other Specify"
android:onClick="PackageRGOnClick" />
</RadioGroup>

Making button move on Touch at exact position where we touch

I have created an application in which there is a button which moves on touching it .
Now for onTouch I have implemented A different class.There are 2 classes one is main CircleMActivity.java and another for onTouch.
Now the app is running fine but there is one problem. When I am clicking on the button and moving it It is moving but there is a gap between the button and the screen touch.
What I want is to move it exactly where I touch it NOT at some distance later OR you can think that I want to move it at exact the cursor position.
How should I achieve it ?
Code:
CircleMActivity.java:
public class CircleMActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_circle_m);
Button b=(Button)findViewById(R.id.btn1);
MultiTouch mtb=new MultiTouch(this);
b.setOnTouchListener(mtb);
}
}
MultiTouch.java:
public class MultiTouch implements OnTouchListener{
float mPrevX,mPrevY;
CircleMActivity cm;
public MultiTouch(CircleMActivity circleMActivity) {
// TODO Auto-generated constructor stub
cm=circleMActivity;
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
float currentX,currentY;
int action=arg1.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
mPrevX = arg1.getX();
mPrevY = arg1.getY();
break;
case MotionEvent.ACTION_MOVE:
currentX = arg1.getRawX();
currentY = arg1.getRawY();
MarginLayoutParams marginParams = new MarginLayoutParams( arg0.getLayoutParams());
marginParams.setMargins((int)( currentX - mPrevX), (int)( currentY - mPrevY),0, 0);
LayoutParams layoutParams = new LinearLayout.LayoutParams(marginParams);
arg0.setLayoutParams(layoutParams);
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
activity_circle.xml:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#000000"
android:weightSum="100">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4.95"
android:background="#fff"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="X coord"
android:textColor="#ff00ee"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Y coord"
android:textColor="#ff00ee"
android:inputType="number" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Radius"
android:textColor="#ff00ee"
android:inputType="number" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textColor="#ff00ee"
android:hint="Colour"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4.95"
android:background="#drawable/custom" />
"
</LinearLayout>
So how should I achieve it ?
It is a simple coordinates maths . In your
case MotionEvent.ACTION_DOWN:
mPrevX = arg1.getX();
mPrevY = arg1.getY();
break;
Make
mPrevY = arg1.getY()+250;
Well It will go out of the child linear layout but it is looking good !

How to set onTouch for a layout(XML) that is added via inflation?

I'm trying to get control buttons to pop on/off screen when the screen is tapped. They will overlay a video. The problem I'm running in to is how to create a touch event for a layout that has been inflated.
The routine is as follows: 1) set initial content to the video; 2) inflate layout(XML), that contains the controls, over the top of the video; 3)(NEED TO DO) create onTouch event for the newly inflated layout that hides/shows the buttons when the user taps the screen.
Layout containing the buttons to show/hide:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dpad_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/tiltUp"
android:background="#drawable/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_row="0"
android:layout_column="1"/>
<Button
android:id="#+id/panRight"
android:background="#drawable/arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_row="1"
android:layout_column="2"/>
<Button
android:id="#+id/tiltDown"
android:background="#drawable/arrow_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="8dp"
android:layout_row="2"
android:layout_column="1"/>
<Button
android:id="#+id/panLeft"
android:background="#drawable/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_row="1"
android:layout_column="0"/>
</GridLayout>
<Button
android:id="#+id/zoomIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/zoom_in"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"/>
<Button
android:id="#+id/zoomOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zoom_out"
android:layout_marginRight="0dp"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#+id/zoomIn"/>
<Button
android:id="#+id/irisDarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#drawable/iris_darker"/>
<Button
android:id="#+id/irisBrighter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="#+id/irisDarker"
android:background="#drawable/iris_brighter"/>
<Button
android:id="#+id/focusFar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/dpad_grid"
android:layout_marginRight="20dp"
android:layout_alignParentStart="true"
android:background="#drawable/focus_far"/>
<Button
android:id="#+id/focusNear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/focus_near"
android:layout_alignTop="#+id/focusFar"
android:layout_alignStart="#+id/irisBrighter"/>
<Button
android:id="#+id/snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/camera"
android:layout_alignEnd="#+id/focusFar"
android:layout_alignParentStart="true"/>
<Button
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/gear"/>
</RelativeLayout>
Code for adding the video and inflating the above layout:
#Override
public void onResume() {
super.onResume();
RelativeLayout.LayoutParams relativeLayoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mv = new MjpegView(this);
setContentView(mv);
LayoutInflater inflater = getLayoutInflater();
getWindow().addContentView(inflater.inflate(R.layout.screen2, null), relativeLayoutParams);
Thanks for any help!
EDIT: here's what I'm currently working with. Why does my code not get into the touch event when I touch the screen?
LayoutInflater inflater = getLayoutInflater();
control_buttons = inflater.inflate(R.layout.screen2, null);
control_buttons.setOnTouchListener(new View.OnTouchListener() {
boolean gone = true;
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("In OnTouch");
if ( (event.getAction() == MotionEvent.ACTION_DOWN) && (gone == true) ){
getWindow().addContentView(control_buttons, relativeLayoutParams);
gone = false;
return true;
}
else if ((event.getAction() == MotionEvent.ACTION_DOWN) && (gone == false)) {
v.setVisibility(View.GONE);
gone = true;
return true;
}
return false;
}
});
This may work better for you. This will only work if the children in your RelativeLayout are either Buttons or GridLayouts that contain Buttons.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
for(int i = 0; i < relativeLayout.getChildCount(); i++) {
if(relativeLayout.getChildAt(i) instanceof Button) {
Button temp = (Button) relativeLayout.getChildAt(i);
if(temp.getVisibility() == View.INVISIBLE) {
temp.setVisibility(View.VISIBLE);
} else {
temp.setVisibility(View.INVISIBLE);
}
} else if(relativeLayout.getChildAt(i) instanceof GridLayout) {
GridLayout temp = (GridLayout) relativeLayout.getChildAt(i);
for(int g = 0; g < temp.getChildCount(); g++) {
Button tempButton = (Button) temp.getChildAt(g);
if(tempButton.getVisibility() == View.INVISIBLE) {
tempButton.setVisibility(View.VISIBLE);
} else {
temp.setVisibility(View.INVISIBLE);
}
}
}
}
}
return false;
}
});

Use onTouchEvent and onclick event together

I'm having trouble with enabling buttons while the onTouchEvent is active. In my app the user is supposed to press anywhere on the screen to see a text and while pressing the screen press a button. The problem is that while the onTouchEvent is active the buttons doesnt work. is there anyway to enable buttons while the onTouchEvent is active?
My code for the button and onToch looks like this.
public void correctWord(){
correct_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
theWord.setText(((global_rules) getApplication()).NewWord(getApplicationContext()));
}
});
}
public boolean onTouchEvent(MotionEvent event){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if(enableTouch) {
holdToView.setVisibility(View.INVISIBLE);
theWord.setVisibility(View.VISIBLE);
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
if(enableTouch) {
holdToView.setVisibility(View.VISIBLE);
theWord.setVisibility(View.INVISIBLE);
}
break;
}
return false;
}
edit, added the .xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rätt"
android:id="#+id/correct_btn"
style="#style/btn_style2"
android:layout_alignTop="#+id/skip"
android:layout_alignLeft="#+id/holdToView"
android:layout_alignStart="#+id/holdToView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/theWord"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="invisible"
android:textStyle="bold"
android:textSize="40dp"
android:textColor="#ffffff" />
</RelativeLayout>

Categories