I have searched a lot for a solution but nothing works.
Update: post at bottom
I have a ScrollView and a FrameLayout as child.
Then I set up some EditText programmatically with LayoutParams.
Furthermore, I activated the ime options, so it will go to the next Edittext automatically.
When it now goes to the next one, the layout doesn't scroll up and the keyboard hides the EditText.
Answers like android:windowSoftInputMode="adjustPan / adjustResize"
didn't work.
I also tried to scroll manually with
Spieler1.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
int current_pos = scroll2.getScrollY();
scroll2.smoothScrollTo(0,(int)(current_pos + height/4));
scroll2.post(new Runnable() {
#Override
public void run() {
Spieler2.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
return true;
}
return false;
}
});
Strange things happened: It wanted to scroll up, but then suddenly goes back to the original position covering the EditText.
I have no idea, why it doesn't scsroll up, but maybe you have.
Thanks for answering.
I suspect the smoothScrollTo() call causes the behavior you've seen. You might want to use the android:nextFocusDown attribute for your first EditText instead of setting the OnKeyListener.
Related
I'm banging my head with this problem which probably is simple but since I'm new to this topic I somehow haven't been able to figure it out yet.
I've successfully implemented dragging a view with onTouch method. I've also successfully implemented onLongClick and onClick methods. But both of these functionalities were implemented separately.
The problem, like the title says is when I want to join these functionalities. I want the onTouch method to be called when a user long clicks a view and I want a new activity to start when a user clicks a view.
Here is the pseudo code:
public class Website extends AppCompatActivity implements View.OnTouchListener{
TextView longpress;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_website);
longpress = (TextView) findViewById(R.id.longpress);
longpress.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view){
//I don't really know how to do this part
onTouch(View view, Motion Event event);
return true;
}
});
longpress.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Code for new activity comes here (I know how to do this part)
}
});
}
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction(){
case MotionEvent.ACTION_DOWN:
//Save initial coordinates of view <-- view.getX(), view.getY()
break;
case MotionEvent.ACTION_MOVE:
//Calculate dX and dY and setX and Y of the view (move view)
break;
case MotionEvent.ACTION_UP:
//If view is certain distance away from initial points do something
break;
}
}
}
Like I said, onTouch works on itself if I don't try to call it from onLongClick method. If I try to call onTouch(View view, MotionEvent event) from onLongClick method the problem occurs because onLongClick only receives one out of two arguments onTouch method should receive (onLongClick only receives view argument but it should also receive event argument).
Maybe I'm trying to do this in a totally wrong way but I have been looking at some documentation e.g. https://developer.android.com/training/gestures/ but still won't get an idea what to do.
(I would like to have a similar functionality to notifications on android phones)
So I've come to a solution which might or might not be a good one but for now it serves my functionality. If someone has a better solution and thinks mine is bad in some way please say so.
Here is the code:
boolean flag;
public boolean onTouch(View view, MotionEvent event){
int action = event.getAction() & MotionEvent.ACTION_MASK;
if(action == MotionEvent.ACTION_DOWN){
//do something on a down press
flag = true;
return true;
}
if(action == MotionEvent.ACTION_UP && flag == true){
//do something if we move finger away from screen we
//didn't move the view first
return true;
}
if(action == MotionEvent.ACTION_UP && flag == false){
//do something if we move finger away from screen and we moved
//the view before we moved the finger away from screen
}
if(action == MotionEvent.ACTION_MOVE){
//do something when moving the view
flag = false;
}
I have a ScrollView with a LinearLayout and then a bunch of TextViews inside that layout, the goal is to make a CustomListView that will open and close various TextViews along the LinearLayout However, I also want to make an autoscroll feature where when the user double taps the screen the screen will scroll by its self.
I already have the second part done, when ever the user touches the ScrollView I have extended ScrollView and implemented this:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("ScrollView Intercept", "ACTION_DOWN");
return true;
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
Log.d("ScrollView Intercept", "ACTION_UP");
return true;
} else if (ev.getAction() == MotionEvent.ACTION_CANCEL) {
Log.d("ScrollView Intercept", "ACTION_CANCEL");
}
return super.onInterceptTouchEvent(ev);
}
Now the ScrollView has all the touch events.
However, I still want the events for the CustomListView, I have tried to implement a equestDisallowInterceptTouchEvent but it doesnt seem to be working:
#Override
public boolean onTouchEvent(MotionEvent event) {
getParent().getParent().requestDisallowInterceptTouchEvent(true);
Log.d("touch listner","Touch");
return true;
}
PS. I have tried to se ListView in the past, but it was a but confusing so I just extended the TextView class and wanted to use on click and manually make the children Visible and Gone
I know this question has been asked in many different ways before, but even though I have looked at many other related questions about EditText focus, I have not found my solution.
Here is what I want to achieve:
When the user is done Editing an EditText I want it to loose focus.
When the user hits outside the EditText I want such EditText to lose
focus.
Whenever the soft-keyboard hides or is hidden I want EditText
to lose focus.
Whenever the User hits ENTER or BACK ARROW in the soft-keyboard, the EditText focus should be cleared
This is what I currently have:
I have two EditText in an activity, which I will call EditText_1 and EditText_2 for simplicity so that we know which EditText I am talking about.
When the user starts the activity, the EditText_1 would have no focus, a blinking cursor and the soft-keyboard would be hidden. I have already fixed that problem using;
android:focusableInTouchMode="true"
and
android:focusable="true"
After the previous fix in part1, when I start the activity and click on any EditText, it will gain focus, however, when I am done editing such clicked EditText and the soft-keyboard hides, the EditText will not lose focus and the cursor will still be blinking.
Another example happens when I am editing an EditText and click any other button outside the editText, it will not force EditText to lose focus or hide the keyboard.
The current solution I have is
public void onFocusChange(View v, boolean hasFocus){...}
so that, it will call some method such as force the EditText to lose focus:
EditText.clearFocus();
and do not show that annoying blinking cursor once I know EditText loses its focus:
EditText.setCursorVisible(false);
However, because hitting Done or outside EditText do not force it to lose focus, onFocusChange will not be called all the times(as in the examples provided).
Some of the solutions I cannot accept are:
Set the cursor visibility to false in the XML activity file and never and anywhere change it back to true.
setCursorVisible(false);
I want the cursor to be seen when needed and to be hidden when it is not needed.
Have a button that needs to be clicked by the user so that inside such button all methods needed will be called. I mean, it is not user-friendly at all. Users do not want to be forced to click a button just to hide all focus, blinking cursors...
Here comes the part many of you will tell me, every single of these issues have been solved in different questions. HOWEVER, I have not been able to implement multiple solutions which will do all points previously stated.
To make editText lose focus when you press outside of the keyboard you can try to setOnTouchListener to the view that is visible when the keyboard is shown. For example, it might be the parent layout, listView, recyclerView or any other significant in size view. In order to do that, just add code below inside of your onCreate method in activity:
findViewById(R.id.loginLayout).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
usernameEditText.clearFocus();
passwordEditText.clearFocus();
return false;
}
});
To make editText lose focus and/or hide keyboard when pressing some button on keyboard you can use the following code. There is an example of listener for Enter key. You may find all the other keys on official documentation.
yourEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP) {
yourEditText.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
return false;
}
});
This Solution works For SOFTKEYS, some code is from here
The final solution to hide keyboard and clear focus from the EditText would be;
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
name.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
Log.d(TAG, "actionID: " + actionId +" KeyEvent: " + event);
}
return false;
}
});
I have been search SO for days and have finally compiled enough answers to accomplish what I wanted. First off, it seems to be an often asked question but not really answered (at least not the way I was looking for it). I thought I would share my findings but I also have one small issue left that I would like to ask for help with. Here goes:
I have a TextView which displays a score. It starts at 0 and at an onClick event the score increments and updates the TextView (score is tracked as a byte - valScore).
onLongClick: This was the challenge. I want the user to be able to do a LongClick to correct/change the score. I first found a solution that utilized another layout.xml file with just an EditText element and the OK and CANCEL buttons. This was very cumbersome to change the score as it involved the LongClick, then the dialog opens, then you had to click on the EditText element to open the keyboard, then you enter the value, click DONE and then click OK. I shortened it by figuring out how to open the software keyboard automatically when the dialog opened. However, you still had to click DONE and then OK. I didn't like this action so I continued searching.
Days later I came up with a bit of code and then more and with a lot of playing/hacking around I came up with the following solution:
// set the onLongClickListener for tvScoreHome
tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
tvScoreHome.setInputType( InputType.TYPE_CLASS_NUMBER );
tvScoreHome.setFocusable(true);
tvScoreHome.setFocusableInTouchMode( true );
tvScoreHome.requestFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(tvScoreHome, InputMethodManager.SHOW_FORCED);
tvScoreHome.setText("");
tvScoreHome.setOnEditorActionListener( new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
valScoreHome = Byte.valueOf( tvScoreHome.getText().toString() );
// This part will hide the keyboard after input
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
tvScoreHome.setFocusable( false );
tvScoreHome.setFocusableInTouchMode( false );
tvScoreHome.setText( Byte.toString(valScoreHome) );
return true;
}
return false;
}
});
return true;
}
});
This works EXACTLY how I want. User performs LongClick the keyboard opens, the user enters the new value and clicks DONE. The TextView is updated and it works great!
The problem arises if the user changes their mind and hits the BACK button on the device. The keyboard closes (GOOD), but then the focus remains on the TextView instead of removing the focus like I do if the DONE button is pressed. So if you cancel out of a change every click after that results in the keyboard opening again instead of just incrementing the score -- until you actually type a value into the keyboard and click DONE (then the regular behavior takes over again. I need to setFocusableInTouchMode to FALSE if the BACK button is pressed.
The other issue is that the setText() method is executed even if the BACK button is pressed if a different value has been typed in. Even though valScoreHome isn't updated the TextView changes. On the next increment it goes to the correct number again, but the setText() should not execute if the BACK button is pressed.
Can someone help me figure this out please?
Both issues can be handled by subclassing TextView.
The back button press that closes the keyboard is handled by overriding onKeyPreIme.
To avoid updating the text when the user closes the keyboard, the score value is saved in the variable mScore, but only if the TextView is currently not focusable. That means, the TextView "remembers" the current value of the score, that was not entered by the user. When the user closes the the keyboard, the text is set back to the saved value.
public class ScoreTextView extends TextView {
private CharSequence mScore;
public ScoreTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setText(CharSequence text, BufferType type) {
if (!isFocusable()) {
mScore = text;
}
super.setText(text, type);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
setFocusable(false);
setFocusableInTouchMode(false);
setText(mScore);
}
return super.onKeyPreIme(keyCode, event);
}
}
I am trying to update a TextView which will show the system volume. I have managed to capture the current system volume and display it, but it doesn't update when the volume is turned up/down (obviously).
I know there is an easy solution somewhere I just cant think! onKeyListeners?
I am now using this but it doesnt work:
TextView sysVol = (TextView)findViewById(R.id.systemVolume);
sysVol.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
//system volume
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int i = curVolume * 4;
String aString = Integer.toString(i);
TextView sysVol = (TextView)findViewById(R.id.systemVolume);
sysVol.setText(aString);
return true;
}
return false;
}
});
Well, the pseudo approach would be the following scenario:
1) Attach a listener to the view
2) Filter the different events in the callback
3) Update the TextView upon the (event==volume_up OR event==volume_down)
Basically, you'd want to implement the View.onKey()-method for this to be a minor bump in the road, instead of the current roadblock-state.
Summary: Add the following snippet to your code (some modification needed)
public YourClassHere extends SomethingCool implements OnKeyListener /*<-- important part*/
{
public boolean onKey(View v) {
// do something when the selected button is pushed
return true;
}
}
I think the problem is that the textview is not refreshing after you have changed the text. Try putting sysVol.requestLayout() after you changed the text to refresh the textview. Also, I don't think you are suppose to put return false after the closing bracket of the onKey() method.