Change behavior of action key android with imeOptions doesnt work - java

i try to jump from a TextView element to the next editable Object in my Android activity. The next Object is a element of ListView, which is editable.
I tried to use the imeOptions but it still performs a newline in the TextEdit Object.
My XML looks like this:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="#string/question_hint"
android:id="#+id/questionField"
android:textColor="#color/text_color"
android:textCursorDrawable="#null"
android:maxLength="#integer/question_length"
android:imeOptions="actionNext"/>
<!--android:minLines="2"-->
I also added a codeline in the oncreate method of the activity:
questionField = (EditText) findViewById(R.id.questionField);
questionField.setImeOptions(EditorInfo.IME_ACTION_NEXT);
But it doesnt work. Anyone has an idea? Thanks

Try something like this:
questionField.setOnEditorActionListener(
new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView exampleView,
int actionId, KeyEvent event)
{
if ((actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_NEXT
|| actionId == EditorInfo.IME_ACTION_GO
|| (event != null
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER)))
{
return nextView.requestFocus() // the next view you want the focus to be on
}
else
{
return false;
}
}
});

Related

Android - Detecting when user touches a drawable left

I have a TextView that contains a DrawableRight, what I want to do is detecting when the user presses that icon in drawableRight, is that possible ? and if it is how can I do it ?
PS: I am working inside a fragment
TextView XML
<TextView
android:id="#+id/mTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textSize="22dp"
android:drawableRight="#mipmap/icn" //this is the drawable
/>
mTitle.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (mTitle.getRight() - mTitle.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
//drawable pressed
return true;
}
}
return false;
}
});

I can't do the key enter Go

I am beginner in android but not in Java , and have a problem.
Took hours losses in my application ignores keyboard to enter and you put the code below.
What I want is that when I do a thing to enter and go. Since it seems redundant and have not optimal OK button when you have an OK in the Enter itself.
t_Num = (EditText) findViewById(R.id.eTT);
...
t_Num.setFocusableInTouchMode(true);
t_Num.requestFocus();
t_Num.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.out.println("entra en Onkey" + event.toString());
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)){
tV.setText("OK");
System.out.println("Press OK");
return true;
}
return false;
}
});
Whit this code i get spam the keypress , but not spam the keypress OK.
And XML:
<EditText
android:layout_width="75dp"
android:layout_height="wrap_content"
android:inputType="numberSigned|phone"
android:digits="-0123456789"
android:ems="10"
android:id="#+id/eTT"
android:imeActionLabel="#string/OK"
android:maxLength="6"
android:maxLines="1"
android:layout_alignBottom="#+id/tV2_1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/btnOk"
android:layout_toEndOf="#+id/btnOk" />
thanks for help
PD : I search in this page but not get solution , font1 , font2 , etc...
I use search :)
Try this:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
//your code
return true;
}
return false;
}
});
This is if the editText has
android:imeOptions="actionGo"
in its xml. Same should apply with Enter. Just change the EditorInfo action.
EditText is a subclass of TextView.

Creating a Drag and Drop ImageButton

I have looked tutorials on how to make my imagebutton capable of drag and drop. With the code I have the imagebutton just disappears when I click it. and when I click anywhere else it does not come back.
Here is my code for the imagebutton:
mainHut = (ImageButton) findViewById(R.id.mainHut);
mainHut.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Toast.makeText(getApplicationContext(), "in the movement", Toast.LENGTH_SHORT).show();
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
mainHutSelected = true;
}//end if
if (event.getAction() == MotionEvent.ACTION_UP)
{
if (mainHutSelected == true)
{
mainHutSelected = false;
}//end if
}//end if
else if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if (mainHutSelected == true)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.setMargins((int)event.getRawX() - 25, (int) event.getRawY() - 50, 0, 0);
layout = (LinearLayout) findViewById(R.id.gllayout);
layout.removeView(mainHut);
layout.addView(mainHut, params);
}//end if
}//end else
return false;
}//end onTouch function
});
here is the xml for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bgm" >
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mainhut" />
</LinearLayout>
Any help is appreciated. Thanks.
To answer my own question...this is how I fixed my issue. It took me a long time to figure this out but here it is. The parameters are a little messy when it comes to my own imagebutton so I will have to figure out what to use to keep my finger in the center of the button, but this moves the button like I was wanting:
button.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (me.getAction() == MotionEvent.ACTION_MOVE )
{
LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight());
params.setMargins((int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()), (int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()));
v.setLayoutParams(params);
}
return false;
}
});

Android Hangman game - User input

I am currently creating a Hangman android application and I am having trouble registering inputted letters and updating the game. I input a letter with the onscreen keyboard into the EditText created, and nothing happens.
Below is the code I am using:
// Setting up user input
input = (EditText) findViewById(R.id.inputguess);
input.setFocusable(true);
input.setFocusableInTouchMode(true);
//Getting user input
input.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
String temp;
String newLetter;
newLetter = input.getText().toString();
temp = (String)enteredText.getText();
if (temp.indexOf(newLetter.toUpperCase(Locale.ENGLISH)) >= 0) {
input.setText("");
return true;
}
input.setText(""); // clearing input
entered += newLetter.toUpperCase(Locale.ENGLISH); // adding inputted letter to the entered string
enteredText.setText(temp + newLetter.toUpperCase(Locale.ENGLISH));
word.setText(hideString(text, newLetter.toUpperCase(Locale.ENGLISH)));
The XML code of my EditText is the following:
<EditText
android:id="#+id/inputguess"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/guessedwords"
android:layout_alignLeft="#+id/button1"
android:layout_marginBottom="24dp"
android:ems="10"
android:hint="#string/guessletter"
android:inputType="text"
android:maxLength="1"
android:singleLine="true"
android:imeOptions="actionDone">
</EditText>
After researching, the only reason I can think of is the type of listener being used, but I am not entirely sure. Any help would be greatly appreciated (Let me know if I should add more of my code).
Actually, the OnKeyListener only works with hardware keyboards. To make use of the software (onscreen) keyboard, you must add a TextWatcher
editText.addTextChangedListener(new TextWatcher() { ... })

Android multitouch issue

So I'm working on a game where two players play simultaneously on one screen. For the sake of simplicity let's say both players have one button on their side of the screen, buttons B[0] and B[1]. I want to implement multitouch so that both buttons can be pressed with no unresponsiveness, but as soon as one of them does ACTION_DOWN on his button, the other one becomes unresponsive. I read all the duplicate questions and understood that the second touch would be referenced as ACTION_POINTER_DOWN so I wrote this piece of code:
#Override
public boolean onTouch(View buttonPressed, MotionEvent action)
{
if(buttonPressed.getId()==B[0].getId())
{
if(action.getAction() == MotionEvent.ACTION_DOWN || action.getAction() == MotionEvent.ACTION_POINTER_DOWN)
B[0].setImageDrawable(BPressed[0]);
if(action.getAction() == MotionEvent.ACTION_UP || action.getAction() == MotionEvent.ACTION_POINTER_UP)
B[0].setImageDrawable(BOk[0]);
}
if(buttonPressed.getId()==B[1].getId())
{
if(action.getAction() == MotionEvent.ACTION_DOWN || action.getAction() == MotionEvent.ACTION_POINTER_DOWN)
B[1].setImageDrawable(BPressed[1]);
if(action.getAction() == MotionEvent.ACTION_UP || action.getAction() == MotionEvent.ACTION_POINTER_UP)
B[1].setImageDrawable(BOk[1]);
}
return true;
}
So as far I can tell if I put 1 finger on B[0] and leave it there (no ACTION_UP) and then put another on B[1] the listener should fire onTouch(B[1],ACTION_POINTER_DOWN) but i think it doesn't since when I do that B[0] has BPressed drawable and B[1] has BOk drawable. Where did I go wrong? Sorry if I made a duplicate, but I read everything and can't seem to find a simple solution. Every solution regarding this issue I found gives much more than I need.
Both buttons are connected with the listener like this:
B[0].setOnTouchListener(this);
B[1].setOnTouchListener(this);
I've read this:
ACTION_POINTER_DOWN and ACTION_POINTER_UP are fired whenever a secondary pointer goes down or up.
But it doesn't seem to be true.
EDIT:
The new code, still the same result:
#Override
public boolean onTouch(View buttonPressed, MotionEvent action)
{
if( buttonPressed.getId()==B[1].getId() )
Toast.makeText(context, "One", 500).show();
int act = action.getAction() & MotionEvent.ACTION_MASK;
if(buttonPressed.getId()==B[0].getId())
{
if(act == MotionEvent.ACTION_DOWN || act == MotionEvent.ACTION_POINTER_DOWN)
B[0].setImageDrawable(BPressed[0]);
if(act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_POINTER_UP)
B[0].setImageDrawable(BOk[0]);
}
if(buttonPressed.getId()==B[1].getId())
{
if(act == MotionEvent.ACTION_DOWN || act == MotionEvent.ACTION_POINTER_DOWN)
B[1].setImageDrawable(BPressed[1]);
if(act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_POINTER_UP)
B[1].setImageDrawable(BOk[1]);
}
return true;
}
Also, the toast:
if( buttonPressed.getId()==B[1].getId() )
Toast.makeText(context, "One", 500).show();
can never be seen if I keep my finger on B[0].
After masking:
Something changed. Let's say I hold my finger on B[0]. Before the masking when I press B[1] nothing happens. After masking if I keep my finger on B[0], and I tap my finger ANYWHERE on the screen it cycles the B[0]. So if I go:
finger1_DOWN on B[0] it changes to Pressed drawable
while(1)
{
finger2_DOWN anywhere nothing changes
finger2_UP anywhere B[0] changes to Ok drawable
}
I create my imagebuttons in OnCreate:
B[0] = (ImageButton) findViewById(R.id.ImageButton05);
B[0].setOnTouchListener(this);
B[1] = (ImageButton) findViewById(R.id.ImageButton15);
B[1].setOnTouchListener(this);
I tried jboi's approach. I don't care if the button was pressed first or second so I just did this:
private class MPListener implements OnTouchListener
{
#Override
public boolean onTouch(View buttonPressed, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(buttonPressed.getId()==B[0].getId())
B[0].setImageDrawable(BPressed[0]);
if(buttonPressed.getId()==B[1].getId())
B[1].setImageDrawable(BPressed[1]);
}
if(event.getAction() == MotionEvent.ACTION_UP)
{
if(buttonPressed.getId()==B[0].getId())
B[0].setImageDrawable(BOk[0]);
if(buttonPressed.getId()==B[1].getId())
B[1].setImageDrawable(BOk[1]);
}
return true;
}
}
I removed implements onTouchListener from my activity and added
B[0].setOnTouchListener(new MPListener());
B[1].setOnTouchListener(new MPListener());
Nothing changed. I can still press both buttons independently, but not at the same time.
EDIT:
So I made a fresh new activity in case something else was the problem. Here's my new activity's full code:
package com.example.projectname;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class About extends Activity
{
private class MPListener implements OnTouchListener
{
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP)
{
if(v.getId()==B[0].getId())
B[0].setImageDrawable(Bok[0]);
if(v.getId()==B[1].getId())
B[1].setImageDrawable(Bok[1]);
}
else if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(v.getId()==B[0].getId())
B[0].setImageDrawable(Bpressed[0]);
if(v.getId()==B[1].getId())
B[1].setImageDrawable(Bpressed[1]);
}
return true;
}
}
ImageView[] B;
Drawable[] Bok,Bpressed;
#Override
protected void onCreate(Bundle savedInstanceState)
{
//standard oncreate stuff
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_about);
Context context=getApplicationContext();
B=new ImageView[2];
Bok=new Drawable[2];
Bpressed=new Drawable[2];
Bok[0] = context.getResources().getDrawable(R.drawable.bblue);
Bok[1] = context.getResources().getDrawable(R.drawable.bpurp);
Bpressed[0] = context.getResources().getDrawable(R.drawable.bpressedblue);
Bpressed[1] = context.getResources().getDrawable(R.drawable.bpressedpurp);
B[0] = (ImageView) findViewById(R.id.imageView1);
B[1] = (ImageView) findViewById(R.id.imageView2);
B[0].setImageDrawable(Bok[0]);
B[1].setImageDrawable(Bok[1]);
B[0].setOnTouchListener(new MPListener());
B[1].setOnTouchListener(new MPListener());
}
}
Everything is the same as always. As soon as I press one button the other one goes unresponsive.
Android can sort out for you, what was pressed in what order and if it is still touched or not. Just give the two Buttons different listeners. Something like this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("TEST", "Begin onCreate");
super.onCreate(savedInstanceState);
Log.d("TEST", "End onCreate");
setContentView(R.layout.test);
findViewById(R.id.upperTouchable).setOnTouchListener(new MyTouchListener());
findViewById(R.id.lowerTouchable).setOnTouchListener(new MyTouchListener());
}
private boolean lowerIsTouched = false;
private boolean upperIsTouched = false;
private void setInfo() {
if(lowerIsTouched && upperIsTouched)
((TextView) findViewById(R.id.info)).setText("both touched");
else if(lowerIsTouched)
((TextView) findViewById(R.id.info)).setText("only lower is touched");
else if(upperIsTouched)
((TextView) findViewById(R.id.info)).setText("only upper is touched");
else
((TextView) findViewById(R.id.info)).setText("non is touched");
((TextView) findViewById(R.id.lowerTouchable)).setText(lowerIsTouched? "touched":"not touched");
((TextView) findViewById(R.id.upperTouchable)).setText(upperIsTouched? "touched":"not touched");
}
private class MyTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(v.getId() == R.id.lowerTouchable)
lowerIsTouched = true;
else if(v.getId() == R.id.upperTouchable)
upperIsTouched = true;
setInfo();
}
else if(event.getAction() == MotionEvent.ACTION_UP) {
if(v.getId() == R.id.lowerTouchable)
lowerIsTouched = false;
else if(v.getId() == R.id.upperTouchable)
upperIsTouched = false;
setInfo();
}
return true;
}
}
and the corresponding layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FFFFFFFF"
android:text="Non touched"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/upperTouchable"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="Not touched"
android:background="#FFF0F0F0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lowerTouchable"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="Not touched"
android:background="#FFFFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

Categories

Resources