requesting focus of another EditText behaves in abnormal ways - java

I have four EditText's to read a PIN. Now that I have coded the Java side of the EditText it behaves in an abnormal way. After entering one digit in the first EditText it should request focus of the second EditText, however it goes to the third EditText.
And even more it is defined as numberPassword and still the digits are visible.
Here is the source code to the layout file :
<RelativeLayout
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/mpin_1"
android:layout_marginStart="20dp"
android:layout_centerVertical="true"
android:maxLength="1"
android:gravity="center"
android:layout_width="50dp"
android:inputType="numberPassword"
android:layout_height="50dp" />
<EditText
android:id="#+id/mpin_2"
android:layout_marginStart="20dp"
android:layout_toEndOf="#+id/mpin_1"
android:layout_centerVertical="true"
android:gravity="center"
android:maxLength="1"
android:layout_width="50dp"
android:inputType="numberPassword"
android:layout_height="50dp" />
<EditText
android:id="#+id/mpin_3"
android:layout_marginStart="20dp"
android:layout_toEndOf="#+id/mpin_2"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_width="50dp"
android:maxLength="1"
android:inputType="numberPassword"
android:layout_height="50dp" />
<EditText
android:id="#+id/mpin_4"
android:layout_marginStart="20dp"
android:layout_toEndOf="#+id/mpin_3"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_width="50dp"
android:maxLength="1"
android:inputType="numberPassword"
android:layout_height="50dp" />
</RelativeLayout>
The source code to the java file:
public class SpinActivity extends AppCompatActivity
{
private StringBuilder s_pin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin);
s_pin = new StringBuilder();
s_pin.setLength(0);
Button sPinButton = (Button)findViewById(R.id.set_s_pin_button);
final EditText e1 = (EditText)findViewById(R.id.mpin_1);
final EditText e2 = (EditText)findViewById(R.id.mpin_2);
final EditText e3 = (EditText)findViewById(R.id.mpin_3);
final EditText e4 = (EditText)findViewById(R.id.mpin_4);
e1.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(e1.getText().length()==1)
s_pin.append(e1.getText().toString());
//e1.setEnabled(false);
e2.requestFocus();
return false;
}
});
e2.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(e2.getText().length()==1)
s_pin.append(e2.getText().toString());
//e2.setEnabled(false);
e3.requestFocus();
return false;
}
});
e3.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(e3.getText().length()==1)
s_pin.append(e3.getText().toString());
//e3.setEnabled(false);
e4.requestFocus();
return false;
}
});
e4.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(e4.getText().length()==1)
s_pin.append(e4.getText().toString());
// e4.setEnabled(false);
return false;
}
});
sPinButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),s_pin.toString(),
Toast.LENGTH_SHORT).show();
}
});
}
}
How to fix this?

Use below code:
android:nextFocusDown="#+id/edittext"
edittext is the ID of the EditText to be focused.

Related

TextView is not selectable with ontouchListener

I have implemented ontouchListener in my activity to detect swipes left and right. However, enabling this feature stopped me from being able to make my text selectable and accordingly getting the default android text selection cursor, menu.
After many trials, now i can perfectly call the onLongClick() method when long clicking the textView. However, still the text is not selectable. also, whenever I disable the swipe detection, the Text selection works perfectly.
public class PreviewActivity extends AppCompatActivity
{
TextView question;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preview);
question = (TextView)findViewById(R.id.question);
question.setTextSize(TextViewSize);
question.setTextIsSelectable(true);
question.setLongClickable(true);
question.setFocusableInTouchMode(true);
View prev_act = (View) findViewById(R.id.question);
prev_act.setOnTouchListener(new OnSwipeTouchListener(this) {
#Override public void onSwipeLeft() {
if(RowIndex>0){
Qtitle = ListItems.get(RowIndex-1);
Query();
//text = Query();
//question.setText(text);
questionSC.scrollTo(0,0);
RowIndex--;
}
}
#Override public void onSwipeRight() {
if(RowIndex<ListItems.size()-1){
Qtitle = ListItems.get(RowIndex+1);
Query();
//text = Query();
//question.setText(text);
questionSC.scrollTo(0,0);
RowIndex++;
}
}
#Override public void onLongClick() {
Log.v(this.toString(), "Long click.");
question.setCursorVisible(true);
question.performLongClick();
}
});
}
And the OnSwipeTouchListener class is as the following:
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
#Override public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 250;
private static final int SWIPE_VELOCITY_THRESHOLD = 200;
#Override
public void onLongPress(MotionEvent e) {
onLongClick();
super.onLongPress(e);
}
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
public void onLongClick() {
}
}
I want to be able to select a portion (text) from the TextView and having the swipe detector along it.
Edited: added the XML file of the mentioned Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:paddingBottom="30dp"
android:background="#drawable/bg"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/next"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:scaleType="fitCenter"
android:src="#drawable/ic_navigate_before_black_24dp" />
<ImageButton
android:id="#+id/share"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:padding="9dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_share_black_24dp" />
<ImageButton
android:id="#+id/star"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:padding="9dp"
android:scaleType="fitCenter"
android:src="#drawable/star_border" />
<ImageButton
android:id="#+id/copy"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:padding="9dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_content_copy_black_24dp" />
<ImageButton
android:id="#+id/previous"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:scaleType="fitCenter"
android:src="#drawable/ic_navigate_next_black_24dp" />
</LinearLayout>
<ScrollView
android:id="#+id/questionSC"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="30dp"
android:layout_marginTop="50dp">
<LinearLayout
android:id="#+id/question_lo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/ge_thameen_book"
android:paddingBottom="20dp"
android:paddingTop="10dp"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20dp"
android:autoLink="web"
/>
</LinearLayout>
</ScrollView>
After trying for a while this is what worked highlighting and selecting text enabling copy,paste,selectall ... etc
you will need to add onTouch snippet in the prev_act.setOnTouchListener(new OnSwipeTouchListener(this) {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//do stuff here
Log.i("clicking", "onActionDown");
}else if(event.getAction() == MotionEvent.ACTION_UP) {
question.performLongClick();
}
return false;
}
#Override public void onLongClick() {
}
The answer was very simple:
#Override
public boolean onDown(MotionEvent e) {
return false;
}
I had to set the onDown() to return false.

How to change from onClick to onTouch method?

I'm creating music app and I need a change from onClick to the onTouch method so I can listen the sound at the moment I press the button and not only when release it. I can't figure out clearly what I need to add and change.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private SoundPool mSoundPool;
private int btn2;
private int btn5;
private int btn13;
private int btn14;
private int btn15;
private int btn16;
private int btn17;
private int btn18;
private int btn19;
private int btn20;
private int btn21;
private int btn22;
private float LEFT_VOL = 1.0f;
private float RIGHT_VOL = 1.0f;
private int PRIORITY = 1;
private int LOOP = 0;
private float RATE = 1.0f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mSoundPool = new SoundPool(24, AudioManager.STREAM_MUSIC,0);
btn2 = mSoundPool.load(getApplicationContext(),R.raw.r,1);
btn5 = mSoundPool.load(getApplicationContext(),R.raw.g,1);
btn13 = mSoundPool.load(getApplicationContext(),R.raw.k,1);
btn14 = mSoundPool.load(getApplicationContext(),R.raw.hat,1);
btn15 = mSoundPool.load(getApplicationContext(),R.raw.s,1);
btn16 = mSoundPool.load(getApplicationContext(),R.raw.a,1);
btn17 = mSoundPool.load(getApplicationContext(),R.raw.d,1);
btn18 = mSoundPool.load(getApplicationContext(),R.raw.c,1);
btn19 = mSoundPool.load(getApplicationContext(),R.raw.f,1);
btn20 = mSoundPool.load(getApplicationContext(),R.raw.e,1);
btn21 = mSoundPool.load(getApplicationContext(),R.raw.h,1);
btn22 = mSoundPool.load(getApplicationContext(),R.raw.b,1);
}
public void playC(View v){
mSoundPool.play(btn2,1,1,0,0,1);
}
public void playD(View v){
mSoundPool.play(btn5,1,1,0,0,1);
}
public void playE(View v){
mSoundPool.play(btn13,1,1,0,0,1);
}
public void playF(View v){
mSoundPool.play(btn14,1,1,0,0,1);
}
public void playG(View v){
mSoundPool.play(btn15,1,1,0,0,1);
}
public void playA(View v){
mSoundPool.play(btn16,1,1,0,0,1);
}
public void playB(View v){
mSoundPool.play(btn17,1,1,0,0,1);
}
public void playCC(View v){
mSoundPool.play(btn18,1,1,0,0,1);
}
public void playCS(View v){
mSoundPool.play(btn19,1,1,0,0,1);
}
public void playDS(View v){
mSoundPool.play(btn20,1,1,0,0,1);
}
public void playFS(View v){
mSoundPool.play(btn21,1,1,0,0,1);
}
public void playGS(View v){
mSoundPool.play(btn22,1,1,0,0,1);
}
Activity_Main XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:screenOrientation="landscape"
tools:context=".MainActivity">
<Button
android:id="#+id/btn22"
android:layout_width="76dp"
android:layout_height="70dp"
android:background="#android:color/holo_red_dark"
android:onClick="playGS" />
<Button
android:id="#+id/btn21"
android:layout_width="76dp"
android:layout_height="70dp"
android:background="#android:color/holo_red_dark"
android:onClick="playFS" />
<Button
android:id="#+id/btn20"
android:layout_width="76dp"
android:layout_height="70dp"
android:background="#android:color/holo_red_dark"
android:onClick="playDS" />
<Button
android:id="#+id/btn19"
android:layout_width="76dp"
android:layout_height="70dp"
android:background="#android:color/holo_red_dark"
android:onClick="playCS" />
<Button
android:id="#+id/btn18"
android:layout_width="76dp"
android:layout_height="70dp"
android:background="#android:color/holo_red_dark"
android:onClick="playCC" />
<Button
android:id="#+id/btn17"
android:layout_width="76dp"
android:layout_height="70dp"
android:background="#android:color/holo_red_dark"
android:onClick="playB" />
<Button
android:id="#+id/btn16"
android:layout_width="76dp"
android:layout_height="70dp"
android:background="#android:color/holo_red_dark"
android:onClick="playA" />
<Button
android:id="#+id/btn15"
android:layout_width="87dp"
android:layout_height="80dp"
android:background="#android:color/background_light"
android:onClick="playG"
/>
<Button
android:id="#+id/btn14"
android:layout_width="87dp"
android:layout_height="80dp"
android:background="#android:color/background_light"
android:onClick="playF" />
<Button
android:id="#+id/btn13"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="87dp"
android:layout_height="80dp"
android:background="#android:color/background_light"
android:onClick="playE" />
<Button
android:id="#+id/btn2"
style="#style/Widget.AppCompat.Button.Small"
android:layout_width="87dp"
android:layout_height="80dp"
android:background="#android:color/background_light"
android:onClick="playC" />
<Button
android:id="#+id/btn5"
android:layout_width="76dp"
android:layout_height="69dp"
android:background="#android:color/holo_red_dark"
android:onClick="playD" />
I tried follow a lot of examples from internet but has always ended not effective.
Step 1: Remove the android:onClick attributes so
<Button
android:id="#+id/btn5"
android:layout_width="76dp"
android:layout_height="69dp"
android:background="#android:color/holo_red_dark"
android:onClick="playD" />
becomes
<Button
android:id="#+id/btn5"
android:layout_width="76dp"
android:layout_height="69dp"
android:background="#android:color/holo_red_dark" />
Step 2: In the end of onCreate() method (just after btn22 = mSoundPool.load(getApplicationContext(),R.raw.b,1); and just before the closing }) insert this code
findViewById(R.id.btn5).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) playD(v);
return false;
}
});
Try it out with one of the buttons first, when it works proceed to the next buttons.
There are a lot of other ways of doing it, for example you could remove the method
public void playD(View v){
mSoundPool.play(btn5,1,1,0,0,1);
}
and instead have this in onCreate():
findViewById(R.id.btn5).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) mSoundPool.play(btn5,1,1,0,0,1);
return false;
}
});
#Pratik_Butani pointed at How to define setOnTouchListener for multiple buttons in Android fragment which are a little more complicated but will be fine to look at when you get a little more understanding, later on.

Keyboard enter not working on some phones

So I have a problem on my Oneplus 3t that the enter key creates a new line and doesn't actually do action, but on Samsung phones it works fine. This is the EditText
<EditText
android:id="#+id/section"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_toEndOf="#+id/nav_button"
android:layout_toRightOf="#+id/nav_button"
android:background="#android:color/transparent"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:gravity="left"
android:inputType="textMultiLine|textNoSuggestions"
android:maxHeight="140dp"
android:scrollbars="vertical"
android:text="#string/section"
android:textColor="#color/textColor"
android:textSize="20sp" />
And this is how I handle the action
mSearch.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
switch (i) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
unReadySeach(section, mSearch, search, mSearch.getText().toString(), mNavButton, backArrow);
gridView.setAdapter(new QuoteImageAdapater(getApplicationContext(), reader.getSearchData(mSearch.getText().toString()), section));
searched = true;
return true;
}
}
return false;
}
});
Also in the virtual device on a pixel 2, when I press on the enter key on the keyboard itself it creates a new line but when I press on enter key on my own keyboard of my pc it does work.
Edit: I fixed it by changing the EditText to this
<EditText
android:id="#+id/searchText"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/section"
android:layout_alignBottom="#+id/section"
android:layout_alignStart="#+id/section"
android:background="#drawable/underline"
android:ems="10"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="textNoSuggestions|text"
android:maxHeight="140dp"
android:textColor="#color/textColor"
android:textColorHint="#color/textHint"
android:textCursorDrawable="#null"
android:textSize="20sp"
android:visibility="invisible" />
Try this one because solution you have used, it is not guaranteed for soft keys.
First Solution:
<EditText
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
Second Solution
mSearch.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
unReadySeach(section, mSearch, search, mSearch.getText().toString(), mNavButton, backArrow);
gridView.setAdapter(new QuoteImageAdapater(getApplicationContext(), reader.getSearchData(mSearch.getText().toString()), section));
searched = true;
return true;
}
return false;
}
});

Problems with onClickListener following getText() keyboard input in Android Studio

I have a small Android Studio app to test the handling of keyboard inputs. It has an EditText field for a name, and a second EditText field for a decimal number. I am using setOnEditorActionListener to get the user input. I then store the entries in a string and double in a separate public class then attempt to display them in a TextView field when a button is clicked. I am concerned to ensure that I've actually definitely got the data in (so it can be used later by other activities).
In the public class, I initialize the string to "****" and the double to 0.0. But when I click the button to display the entered text, the four stars disappear and the field goes blank.
I haven't even tried displaying the entered number because the name text input and display are not working.
What am I doing wrong please? Any help would be much appreciated. Code follows.
activity_main.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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Name"
android:ems="10"
android:id="#+id/textin"
android:layout_marginTop="63dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:imeOptions="actionNext"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint= "Number"
android:ems="10"
android:id="#+id/numberin"
android:layout_below="#+id/textin"
android:layout_centerHorizontal="true"
android:imeOptions="actionDone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="echo"
android:id="#+id/echo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/echo"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"/>
</RelativeLayout>
MainActivity.java
package com.example.owner.keyboardinput;
//imports here
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView echo = (TextView) findViewById(R.id.echo);
final Button button = (Button) findViewById(R.id.button);
final EditText textin = (EditText) findViewById(R.id.textin);
textin.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_NEXT) {
String inputText1 = textin.getText().toString();
Common.str = inputText1; // Store in common public class
echo.setText(Common.str); // But input doesn't appear!
}
return handled;
}
});
final EditText numberin = (EditText) findViewById(R.id.numberin);
textin.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
String inputText2 = numberin.getText().toString();
echo.setText(inputText2);
//Common.dbl = Double.valueOf(inputText);
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
handled = true;
}
return handled;
}
});
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
echo.setText(Common.str); // Field goes blank!
}
});
}
Common.java
package com.example.owner.keyboardinput;
/**
* Created by Owner on 13/11/2015.
*/
public class Common
{
public static String str = "****";
public static double dbl = 0.0;
}
You're calling setOnEditorActionListener() on the textin TextView twice. From your code, it looks like the second call should be on numberin.
...
final EditText numberin = (EditText) findViewById(R.id.numberin);
numberin.setOnEditorActionListener(new TextView.OnEditorActionListener()
...
You didn't use setOnEditorActionListener() for numberin, change one of textin.setOnEditorActionListener() to numberin.setOnEditorActionListener()

How to hide part of layout when editText is selected

Is there a way how to hide some buttons when i click on EditText and keybord shows?
I have this layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/InnerRelativeLayout">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="#+id/table">
<TableRow
android:id="#+id/tariffRow">
<TextView
android:layout_column="1"
android:text="Název"
android:padding="3dip" />
<EditText
android:id="#+id/tariffName"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout android:id="#+id/InnerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button android:id="#+id/okBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Ok"/>
<Button android:id="#+id/stornoBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Storno" />
</LinearLayout>
Now when the editText is clicked to type some value i need to hide that linearLayout android:id="#+id/InnerRelativeLayout" because otherwise it is still visible above the keyboard.
Need to set a FocusChangeListener to the EditText which triggers whenever focus changes on that EditText as follow:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// hide relative layout
} else {
// show relative layout
}
}
});
It is not actually that simple, here is my solution how I got it working.
First Need to create MyEditText.java
public class MyEditText extends EditText {
private KeyImeChange keyImeChangeListener;
public MyEditText(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public void setKeyImeChangeListener(KeyImeChange keyImeChange){
keyImeChangeListener = keyImeChange;
}
public interface KeyImeChange {
public boolean onKeyIme(int keyCode, KeyEvent keyEvent);
}
#Override
public boolean onKeyPreIme (int keyCode, KeyEvent keyEvent){
if(keyImeChangeListener != null){
return keyImeChangeListener.onKeyIme(keyCode, keyEvent);
}
return false;
}
}
Then layout.xml, change following
<EditText
android:id="#+id/tariffName"
android:gravity="right"
android:padding="3dip" />
Into
<!-- change yourapp -->
<com.yourapp.MyEditText
android:id="#+id/tariffName"
android:gravity="right"
android:padding="3dip" />
Add also android:focusable="true" to your LinearLayout
<LinearLayout android:id="#+id/InnerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:focusable="true">
Then put into your activity like MainActivity.java,
import fi.hgs.apps.MyEditText.KeyImeChange;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
removeFocus();
MyEditText myEditText = (MyEditText) findViewById(R.id.tariffName);
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
showLinearLayout();
} else {
hideLinearLayout();
}
}
});
myEditText.setKeyImeChangeListener(new KeyImeChange() {
#Override
public boolean onKeyIme(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
removeFocus();
}
return false;
}
});
}
Add also these to do the actual job.
public void removeFocus() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.requestFocus();
}
public void showLinearLayout() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.setVisibility(View.VISIBLE);
}
public void hideLinearLayout() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.setVisibility(View.INVISIBLE);
}

Categories