I have a linear layout in a fragment with a bunch of checkboxes and various edittext widgets inside it. Basically like a quiz. A bunch of multiple choice(checkboxes) and a dozen short answer(edittexts) questions.
What I would like is for users to be able to click an edittext, type in an answer, then press DONE or click anywhere else on the layout and have the widget lose focus and the keyboard hide. Currently I am overriding the setOnEditorActionListener and setOnFocusChangeListener methods of each edittext to give focus back to a main layout, and hide the keyboard respectively. Here is the code for an edittext instance called "input_7d":
final EditText input_7d = (EditText) thisview.findViewById(R.id.txtinput_7d);
final LinearLayout parentLayout = (LinearLayout) thisview.findViewById(R.id.main_layout);
input_7d.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE) {
parentLayout.requestFocus();
}
return false;
}
});
input_7d.setOnFocusChangeListener(new TextView.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(parentLayout.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
This is annoying to do for every edittext I add, and it means editing lots of code if I remove them or add more in the future. What I would like to do is have a custom edittext class that can return focus to it's parent view/layout and hide the keyboard, then use that instead of the built in edittext. I'm very new to this and I haven't been able to find a way for a custom edittext to pass focus back to it's parent layout. Is there a better way to get a bunch of edittexts to all have this behavior and not have it all "hardcoded" into my fragment class?
So I could not find a way to have a edittext pass focus back to it's parent layout from inside the view itself. So instead I have opted to just disable focusable property of it when it
1) It loses focus (user clicked outside the view on something focusable, ie. The parent layout)
2) Finishes it's edit(user presses Done action on soft keyboard)
Surprisingly neither of these actions by default remove focus and the cursor from a default editText. At least not inside my scroll views.
So I added these lines to a custom view(myEditText) that extends the editText view:
this.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE) {
myEditText.setFocusable(false);
myEditText.setFocusableInTouchMode(false);
}
return false;
}
});
this.setOnFocusChangeListener(new TextView.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
myEditText.setFocusable(false);
myEditText.setFocusableInTouchMode(false);
hideKeyboardFrom(context, v);
}
}
});
this.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
myEditText.setFocusableInTouchMode(true);
myEditText.setFocusable(true);
return false;
}
});
I find it really annoying that to get simple functionality like not having the cursor blinking at me always or having the view not take focus when changing fragments and such you have to do such a weird workaround. Making a view unfocusable unless it's focused in which case it is focusable but only until it isn't focused again just seems dumb. Still wondering if there is a better way to do this for a large number of edits in one layout.
you can set a touch listener for the root layout and then remove the focus whenever not needed for the view
findViewById(R.id.rootView).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.requestFocusFromTouch() //check for y
return false;
}
});
Related
I am building an app, and I want the app to trigger an event when the user has entered text into the editText and clicks away from it or closes the keyboard? How can I do this?
I am not very skilled in Java, so I would be grateful if you provided some description or code.
You can use onFocusChange listener on your edit text to overcome this problem
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b==true){
//entered in the edit text
}
else {
//left edit text
}
}
});
Simply set an onFocusChangedListener on your EditText and configure what happens when the EditText loses focus. Here's an illustration:
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean bool) {
if(bool){
// here, the EditText is in focus
}
else {
//here, the EditText is no longer in focus.
// do what you want to do
}
}
});
I hope this helps.
I am attempting to use a popup menu that uses the view of a dynamically added EditText box.
When I do not create the new popup menu inside the onTouch method, the popup closes as expected but, I could not figure out a way to use the view of the touched EditText this way. Instead the popup would show up in the view of the last added EditText.
View.OnTouchListener subjectListener(final EditText editText) {
return new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
popupMenu = new PopupMenu(MainActivity.this, editText);
popupMenu.getMenu().add("works");
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
popupMenu.getMenu().close();
list.get((Integer) editText.getTag()).setText(item.getTitle());
return true;
}
});
return false;
}
};
}
With this code the popup menu shows up exactly where I want it too except it does not close unless I touch another EditText a few times. Which makes me think I am creating multiple popup menu's behind each other or something.
Any ideas?
popupmenu.dismiss();
You can use this to close popup
When my app starts, the user needs to touch on the screen before the real action starts. I have a textView which gives the hint to touch the screen.
After the screen is touched, I want the text to get invisible. Right now the textView never disappears and always stays in the front.
public class MainActivity extends Activity implements OnGestureListener
{
public boolean touched = false;
TextView mMyView;
public void onTouch()
{
mMyView.setVisibility(View.GONE);
touched = true;
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mMyView = (TextView)findViewById(R.id.textView6);
if(touched == true)
{
}
}
}
1.Always use if(something) if you want to see if it's true/false instead of writing if(something == true) [something is a boolian assigned with value true.]
2.If you point your views xml to a method using android:onClick like below,
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="onTouch" />
.
What's the point of implementing OnGestureListener?
If i do this onCreate i initialize my view
View myView = findViewById(R.id.my_view);
3.If i really want a touch i will do this
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// ... Respond to touch events --> tv.setVisibility(View.INVISIBLE);
return true; // if you return false with this then the listener will not be called for the subsequent ACTION_MOVE and ACTION_UP string of events.
}
});
Now you can see in the 3rd ones parameter there is a MotionEvent, you can identify the motion ACTION_DOWN , ACTION_MOVE and ACTION_UP
Now think have you ever used them. You got an idea in your head about a touch so tried to use touch events .. But you don't use them. So it does the same as what onClickListner does in your case. If you want motions use that 3rd one i gave.
But simply you can use
// view is the background layout
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something here --> Hide your text tv.setVisibility(View.INVISIBLE);
}
});
Those view onClickListner or setOnTouchListener you can directly use them inside onCreate or keep them inside a method and you can call that method from onCreate. Why to keep a boolean? It's nothing major
Note i considered myView as the background layout not your textView , background is the one you click / touch
So now you changed the questions code several times and I hope it´s the final change. Only than my answer could help.
You have done this in your onCreate():
if(touched == true)
{
tv.setVisibility(View.INVISIBLE);
}
But this is executed directly and has nothing to do with you onTouch() method. Let´s assume your onTouch() works correctly. Make the TextView global:
TextView mMyView;
initialize it in onCreate():
mMyView = (TextView)findViewById(R.id.textView6);
and then hide it in onTouch():
onTouch(View view){
mMyView.setVisibility(View.GONE);
}
But you have to be sure that your method onTouch() works. You can make a Toast or a Log to check. You have to be sure that:
-The TextView is inside your layout xml that you set with setContentView(R.layout.activity_game);
-The onTouch() method is declared in your TextView's xml attribute
android:onClick="onTouch"
and set clickable of your TextView to true:
android:clickable="true";
EDIT
If you implement onGestureListener() I guess the touch event is consumed by the listener and your TextView did not recognize onTouch(). If you don´t do any gesture detection in your activity, then remove this implementation.
You are checking if screen was touched in onCreate() which is called only once at the start of the activity. Initialize TextView globally and set its visibility inside onTouch(View v, MotionEvent event)
Also your onTouch() isn;t correct. You should override public boolean onTouch(View v, MotionEvent event)
public class MainActivity extends Activity
{
public boolean touched = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
TextView tv = (TextView) findViewById(R.id.textView6);
tv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
touched = true;
tv.setVisibility(View.INVISIBLE);
return true;
}
});
}
}
Instead of implementing OnGestureListener add a setOnTouchListener in your root view of your activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlTestView"/>
For example rlTestView is your activity's root layout id, then use below code in your oncreate method
((RelativeLayout)findViewById(R.id.rlTestView)).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
tv.setVisibility(View.GONE);
return true;
}
});`
Use the code below on the onCreate method and yes set the visibility as GONE instead of invisible. Also state the current visibilty of the TextView in the onTouch then set it to
tv.setVisibility(View.GONE);
This is what I'm trying to do. I have 3 text boxes, a submit button and image on the screen. I want the image to go away if any of the text boxes are touched to input data for the login credentials. The reason is that the soft keyboard pushes the image up into the text boxes when the user tries to input anything. I tried the setOnClickListener but it appears that takes two clicks into the field using the AVD. So I'm trying to use OnTouchListener and it's not going well.
It is asking me to remove the qualifier because of an 'expected Class or package'. It wants to remove the login from this line of code.
login.OnTouchListener(new View.OnTouchListener(){
It happens with the other two text boxes which are name 'phone' and 'password'.
Next, if I remove the qualifier then it complains about a Method call expected and wants to do an insert which changes the code to this.
new View.OnTouchListener(new View.OnTouchListener() {
Then it complains about 'OnTouchListener' is abstract; cannot be instantiated' and wants to implement a method, which gives an error at the end about an expected ). When this is added the whole cycle starts over with the same error messages.
Here's the pieces that I believe are important.
The import statement
import android.view.View.OnClickListener;
The variables that I'm using for the OnTouchListner.
final EditText phone = (EditText) findViewById(R.id.phone1);
final EditText login = (EditText) findViewById(R.id.uname);
final EditText pass = (EditText) findViewById(R.id.password);
The onTouchListener that I need so I can make the image invisible and not overlap the text box input.
login.OnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
image.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
The method signature you're looking for is login.setOnTouchListener(new View.OnTouchListener() { ... }
The reason this is happening is because OnTouchListener is a class, not a method.
The correct way to add an OnTouchListener is to call
setOnTouchListener( listener )
So, for your code,
login.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
image.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
The java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead," but that is not correct. I am using setOnItemClickListener to do some stuff based on the new selection, but I also need to do some stuff before the user changes the selection. Specifically, I am collecting data for each selection that needs to be saved to a file before moving to another selection, since the other selection is associated with different set of data. Is there a way to use setOnClickListener with an Android Spinner?
spinner.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do some stuff before the user changes the selection
...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// Do some stuff based onItemSelected
...
You can replicate the an onclick event using ontouch events
this.spinner=(Spinner)findViewById(R.id.spinner);
this.spinner.setClickable(false);
this.spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "spinner touch");
//replicating a click
if(event.getAction() == MotionEvent.ACTION_UP){
v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
}
return true;
}
});
You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:
Create a custom Spinner
In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item
Do a findViewById(android.R.id.text1) to get the TextView
Now set the onClickListener to the TextView