I am currently developing an app with Samsung Galaxy Tab 2 as my device.
My Code in the XML is:
<EditText
android:id="#+id/analysis_text"
style="#style/icon_text"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:onClick="onBtnClicked"
/>
When this code executes, the full screen data entry mode (Extract Mode) is triggered automatically only in certain situations.
I would like my users to get a full data entry screen while editing text in this control, regardless of the screen or positioning of the control.
Another rephrase of this question:
How do I force full screen data entry mode for EditText boxes in my activity no matter what the content is?
I solved this issue, not really solved, but found a valid work-around.
The workaround is that I designed a text editor (which looks similar to the fullscreen UI) and on click of each of those EditBoxes the new UI activity is triggerred (with the startActivityForResult() so that once they are completed control is handed back to the calling activity) and after completion of edit the text is transferred back into the main screen.
I also ensured that those boxes which transfer the control do not take focus so that they immediately transfer control to the new UI.
This way I have been able to implement a cancel button, which now actually allows the user to go back without saving the changes he accidentally makes.
I am open to new ideas, but this has worked for me.
Code in Activity:
public void onBtnClicked(View v) {
EditText current_text_box = (EditText) v;
Intent intent = new Intent(ObservationActivity.this,
TexteditorActivity.class);
intent.putExtra("start_text", current_text_box.getText().toString());
intent.putExtra("start_position", current_text_box.getSelectionStart());
startActivityForResult(intent, v.getId());
}
Code in XML:
<EditText
android:id="#+id/observation_text"
style="#style/icon_text"
android:focusable="false"
android:imeOptions="flagNoExtractUi"
android:inputType="textMultiLine"
android:onClick="onBtnClicked" >
</EditText>
To create the full screen UI I used code, you can use any like (http://android-richtexteditor.googlecode.com/)
You Can Try This
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
yourEditText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
});
Import LayoutParams of your Parent Layout. Your answer will also work well.
Related
Recently while developing an app, I faced an issue. I have searched a lot on google but couldn't find any solution. In the end I came across this Android issue tracker
To explain my issue, I have made a sample App.
Basic Working of my Sample App
I have a screen, which has an EditText, a Button and a RelativeLayout.
Width and Height of RelativeLayout is 0px. It is just a view to move focus away from EditText.
When App is launched focus is on RelativeLayout, not on EditText(so that there is not blinking cursor in it.)
When a user clicks on Button I just move focus to RelativeLayout using requestFocus() call on RelativeLayout.
When user taps on EditText, keyboard comes up. I can enter text in that.
What I want to achieve
If I change orientation of phone when keyboard is visible then after orienation changes, keyboard should stay.
If keyboard is visible and some other activity comes on top of it for e.g. alarm, facebook chat heads, opening something from notification area, locking unlocking device, etc.. then on returning back to sample app keyboard should be visible.
How I am achieving this
In onSaveInstanceState(), I check if focus is on EditText then put a boolean variable in Bundle.
In onStop(), I am setting a one boolean flag wasEditing = true.
In onRestoreInstanceState(), I checked if Bundle has flag value set in onSaveInstanceState(). If yes then I am make wasEditing = true.
In onResume(), I check this wasEditing and if it is true, I request focus for EditText.
After that I call imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT,resultRec)
Where I am getting problem
Sometimes after executing this call, Keyboard is not visible in few cases, like during orientation change.
When I put logs I have found this function is returning false
But if I make this showSoftInput() call with some delay of 100ms using mEditText.postDelayed() in onResume() everything works fine.
Question
In what cases this function returns false and why delay is working?
Note
Although I have solved my problem using delay, but I still want to know why it is behaving like that.
This is a problem I ran into today as well. Of my 8 android devices only 1 has the problem and it's running Android 4.0.4.
The problem was fixed by adding
mEditText.requestFocus();
mEditText.requestFocusFromTouch();
before calling
mEditText.showSoftInput(...)
You'll see the resultcode from showSoftInput is now true. I noticed that after the mEditText.requestFocus() the isFocused() was still false. Probably a bug in Android 4.0 and perhaps 4.1.
Please call showKeyboard after your mEditText is attached to window.
Please make sure the time you make the call.
PROBLEM:
I faced with this keyboard not showing up problem. I found the following solution inspired by this answer but not their solution! In short the reason for this mess is that the request focus and the IMM provided service can only run on a view that is created and active. When you do all these on the creation phase onCreate(Bundle savedInstance).. or onCreateView(LayoutInflater inflater... and the view is still in initializing state, you won't get an active view to act on! I have seen many solutions using delays and checks to wait for that view to get active then do the show keyboard but here is my solution based on the android frame work design:
SOLUTION:
in your activity or fragment override the following make sure your view has the access (define it in the top of the activity/fragment):
#Override
public void onStart() {
yourView.requestFocus();
showSoftKeyboard(yourView);
super.onStart();
}
public void showSoftKeyboard(View view) {
if(view.requestFocus()){
InputMethodManager imm = (InputMethodManager)
mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
I have an ActionView with menu item on ActionBar (using ActionBarSherlock), I'm able to display an EditText as a search field in it. It's an input to launch another Activity with a CustomView in ActionBar which it displays the same layout (I don't use anything to force the SoftKeyboard to appear in this second activity, there is no problem here). When I want to make the Soft Keyboard appears/disapears automatically when the view collapse in first activity, I use:
openKeyboard method
mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
closeKeyboard method
mImm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
I use the ActionExpandListener to make the SoftKeyboard appears or disappears when the View expands or collapse. With these two methods above, I have the expected result. I found this on several questions on SO (especially on Close/hide the Android Soft Keyboard and Showing the soft keyboard for SearchView on ActionBar or Forcing the Soft Keyboard open).
Just to understand, when I used SHOW_IMPLICIT or SHOW_FORCED alone, it was no effect on lower versions (as 2.+). The EditText was focused but the keyboard didn't show up (so, you guess it was a bad thing). In recent versions (as 4.+ for example), it was a nice effect and no problem. Then, I forced keyboard to show up with the openKeyboard method above.
Now, I got some troubles with this...
On lower versions, I got "empty" space before and after the keyboard created/destroyed, I can live with this. BUT in recent versions, I got "empty" space which it displays when I return to the first Activity. And it's here during less than one second, but sufficient to see that!
To better understand what happens, see the image below:
1. Second Activity: I press the Up Home Button - the keyboard disappears properly.
2. (back to) First Activity: my ListView is covered by a "empty" space (background color in my application). And it disappears (this is the same height of the SoftKeyboard, no possible doubt!)
I guess it's because I forced the keyboard to appear in my first activity although I also forced the keyboard to hide when I go the second, but how can I resolve the "empty" space when I return to the first activity?
Summary
1) A activity => press item in menu > view collapse > show the keyboard > tap text > send it > hide keyboard > launch B activity.
2) B activity => setCustomView in actionbar > show the keyboard only if the edittext is focused/clicked > tap text > send it > hide keyboard > refresh content > press home button > return to A activity
3) A activity => "empty" screen > screen disappears.
Any help will be very appreciate.
Thanks for your time.
EDIT
I add my code of my first class, to see if someone tells me what I'm doing wrong. Maybe it's my code which makes the issue.
Menu (ActionView)
ActionBar actionBar;
MenuItem itemSearchAction;
EditText mSearchEdit;
InputMethodManager mImm;
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
itemSearchAction = menu.findItem(R.id.action_search);
View v = (View) itemSearchAction.getActionView();
mSearchEdit = (EditText) v.findViewById(R.id.SearchEdit);
itemSearchAction.setOnActionExpandListener(this);
return true;
}
OnActionExpandListener
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
actionBar.setIcon(R.drawable.ic_app_search); // change icon
mSearchEdit.requestFocus(); // set focus on edittext
openKeyboard(); // the method above
mSearchEdit.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
closeKeyboard(); // same method as above
// new Intent() to second activity
// perform with startActivity();
itemSearchAction.collapseActionView(); // collapse view
return true;
}
return false;
}
});
// add a clicklistener to re-open the keyboard on lower versions
mSearchEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openKeyboard();
}
});
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
actionBar.setIcon(R.drawable.ic_app_logo); // change icon again
if(!(mSearchEdit.getText().toString().equals("")))
mSearchEdit.setText(""); // reinitial the edittext
return true;
}
OnOptionsItemSelected
// I had this verification when I make new Intent() to
// a new activity, just in case (works like a charm)
if(itemSearchAction.isActionViewExpanded())
itemSearchAction.collapseActionView();
ActionView (Item + layout)
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_app_search"
android:title="#string/action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/search_actionview" />
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SearchEdit"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="right|bottom" android:gravity="left"
android:layout_marginBottom="6dip"
android:hint="#string/action_search"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:singleLine="true"
android:cursorVisible="true"
android:inputType="text"
android:imeOptions="actionSearch|flagNoExtractUi"
android:imeActionLabel="#string/action_search"
android:focusable="true" android:focusableInTouchMode="true"
android:background="#drawable/bt_edit_searchview_focused" >
<requestFocus />
</EditText>
UPDATE
I see a lot of similar issues, with EditText in ActionBar which not makes the keyboard appear even the focus has set. I tried this again (even if I already tested several time):
/*
* NOT WORKING
* Sources: https://stackoverflow.com/questions/11011091/how-can-i-focus-on-a-collapsible-action-view-edittext-item-in-the-action-bar-wh
* https://stackoverflow.com/a/12903527/2668136
*/
int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
getWindow().setSoftInputMode(mode);
postDelayed() to show: .showSoftInput(mSearchEdit, InputMethodManager.SHOW_IMPLICIT); - 200ms (Not working on lower versions)
postDelayed() to hide: .hideSoftInputFromWindow(mSearchEdit.getWindowToken(), 0); - 200ms
new Runnable() on edittext => requestFocus() + showSoftInput(SHOW_IMPLICIT/FORCED/HIDE_NOT_ALWAYS/HIDE_IMPLICIT_ONLY)
It seems with me, only SHOW_FORCED|HIDE_IMPLICIT_ONLY can force the keyboard to show automatically when the view collapse. After this, in all versions, I must to make a hideSoftInputFromWindow to 0 for hiding it.
BUT this undisplays the keyboard even if the edittext is pressed, so I added an ClickListener to force the keyboard to show again (this happens only on lower versions).
UPDATE2:
It's clearly weird, when I try to make a little Thread like I saw in many SO answers (with/without ABS), nothing happens in lower versions.
I tried a different way. I created the new thread to have a short time before call the new intent for hide the keyboard. I had the keyboard which forced to close, OK. And then I opened the new activity, OK. But now when I return, it's worth! The "empty" space is also on lower versions when I come back. I did this:
// close the keyboard before intent
closeKeyboard();
// make the intent after 500 ms
Handler handler = new Handler();
Runnable runner = new Runnable() {
public void run() {
// new intent with startActivity()
}
};
handler.postDelayed(runner, 500);
// collapse the View
itemSearchAction.collapseActionView();
It gives me headaches! I don't understand why in my case, the tip above not working whereas on other answers, when they use a new thread to show/hide the keyboard, this works perfectly.
NOTE: my tests were on (emulator:) GalaxyNexus, NexusS, NexusOne and (real devices:) Samsung GalaxySL (2.3.6) and Nexus4 (4.4).
If someone can help me with this ugly situation. Thanks in advance.
Did you tried this one?
setContentView(R.layout.activity_direction_3);
getWindow().getDecorView().setBackgroundColor(
android.R.color.transparent);
Remove Translucent theme:
android:theme="#style/Theme.AppCompat.Translucent"
and use your Activity in manifest.xml as:
<activity
android:name=".SearchActivity"
android:screenOrientation="portrait">
I had the same issue.
Try to add in Manifest, in First Activity:
android:windowSoftInputMode="adjustPan"
I have found the solution here:
Soft Keyboard pushes layout of my activity out of screen
I made a textview clickable then it triggers an intent, it works but only once. After clicking the textview the first time it's no longer clickable and I have no idea why. Your help will be appreciated.
<TextView android:text="Click Me" android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="#+id/textView1"
android:textSize="50dp" android:focusable="false" android:longClickable="true"></TextView>
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Example.this, Alert.class));
return false;
}
});
What does the alert class do? If it is an activity it could be that it is being laid over the top of your current activity so although you can see your activity, it's not at the top of the stack/in the foreground so you're not actually pressing the TextView, you're pressing a transparent activity that is over the top of it.
The easiest way to check that is to press the TextView, then press your device's back key and see if the TextView responds to the click.
Or are you sure you're not setting the same layout in Alert.class? That would make it look like it's the same activity but if the Alert class doesn't set the click listener, nothing is going to happen.
The fact that you're starting an activity with an intent and that's making an instance of another class (which I assume is also an activity) stops the click working to me is seriously suggesting that Alert is getting the click somehow instead of Example. When you say things work fine if you remove the intent backs that up as well. Maybe you could post the full source of both classes?
Do you have an onClickListener that disables the textview?
By returning false from onLongClick Android would also invoke the onClick listener if you have one.
Also you could try to remove android:focusable="false"
I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.
How can I prevent this?
Use this attributes in your layout tag in XML file:
android:focusable="true"
android:focusableInTouchMode="true"
As reported by other members in comments it doesn't works on ScrollView therefore you need to add these attributes to the main child of ScrollView.
You can add this to your Android Manifest activity:
android:windowSoftInputMode="stateHidden|adjustResize"
I have several implementations described here, but now i have added into the AndroidManifest.xml for my Activity the property:
android:windowSoftInputMode="stateAlwaysHidden"
I think this is the easy way even if you are using fragments.
"stateAlwaysHidden" The soft keyboard is always hidden when the
activity's main window has input focus.
If you have another view on your activity like a ListView, you can also do:
ListView.requestFocus();
in your onResume() to grab focus from the editText.
I know this question has been answered but just providing an alternative solution that worked for me :)
Use this in your Activity's code:
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
https://stackoverflow.com/a/11627976/5217837 This is almost correct:
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
But it should be SOFT_INPUT_STATE_HIDDEN rather than SOFT_INPUT_STATE_ALWAYS_VISIBLE
I had a simular problem, even when switching tabs the keyboard popped up automatically and stayed up, with Android 3.2.1 on a Tablet. Use the following method:
public void setEditTextFocus(EditText searchEditText, boolean isFocused)
{
searchEditText.setCursorVisible(isFocused);
searchEditText.setFocusable(isFocused);
searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused) {
searchEditText.requestFocus();
} else {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
}
}
In the onCreate() and in the onPause() of the activity for each EditText:
setEditTextFocus(myEditText, false);
For each EditText an OnTouchListener:
myEditText.setOnTouchListener(new EditText.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
setEditTextFocus(myEditText, true);
return false;
}
});
For each EditText in the OnEditorActionListener:
myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
.......
setEditTextFocus(myEditText, false);
return false;
}
});
And for each EditText in the layout xml:
android:imeOptions="actionDone"
android:inputType="numberDecimal|numberSigned" // Or something else
There is probably more code optimizing possible.
((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);
I have found this simple solution that worked for me.Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Hope it will work for you .
this is the solution I am using, is not the best solution but it's working well for me
editComment.setFocusableInTouchMode(false);
editComment.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
editComment.setFocusableInTouchMode(true);
editComment.requestFocus() ;
return false;
}});
Interestingly, this documentation https://developer.android.com/training/keyboard-input/visibility.html states that when an activity starts and focus is given to a text field, the soft keyboard is not shown (and then goes on to show you how to have the keyboard shown if you want to with some code).
On my Samsung Galaxy S5, this is how my app (with no manifest entry or specific code) works -- no soft keyboard. However on a Lollipop AVD, a soft keyboard is shown -- contravening the doc given above.
If you get this behavior when testing in an AVD, you might want to test on a real device to see what happens.
This has some good answers at the following post : Stop EditText from gaining focus at Activity startup. The one I regularly use is the following code by Morgan :
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="#id/autotext"
android:nextFocusLeft="#id/autotext"/>
NOTE : The dummy item has to be PLACED RIGHT BEFORE the focusable element.
And I think it should work perfectly even with ScrollView and haven't had any problems with accessibility either for this.
This occurs when your EditText automatically gets Focus as when you activity starts. So one easy and stable way to fix this, is simply to set the initial focus to any other view, such as a Button etc.
You can do this in your layout XML, no code required..
Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !
EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
Now keyboard is open enjoy :)
android:windowSoftInputMode="stateHidden|adjustResize"
Working fine
Add below code to your top of the activity XML and make sure the View is above EditText
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"/>
android:focusableInTouchMode="true"
Add the above line to xml of EditText or TextInputLayout which has focus and is causing the softInputKeyboard to pop up.
This solved the problem for us and now the keyboard doesn't popup
search_edit_text = (EditText) findViewById(R.id.search_edit_text);
search_edit_text.requestFocus();
search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
This works for me guys fragment may have some different syntax . THIS WORKS FOR ACTIVITY
Use this in your Activity's code:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
If your view has EditText and Listview then Keyboard will open up by default.
To hide keyboard from popping up by default do the following
this.listView.requestFocus();
Make sure you are requesting focus on listview after getting view for editText.
For e.g.
this.listView = (ListView) this.findViewById(R.id.list);
this.editTextSearch = (EditText) this.findViewById(R.id.editTextSearch);
this.listView.requestFocus();
If you do it, then editText will get focus and keyboard will pop up.
Probably a really basic answer to this but google is throwing up nonsense.
Ok, so i have a bunch of nested linear layouts, each one contains a textview and an imageview. What i want is my textview to be linked so that when a user clicks on the text, it will take the user to a new page that is in the same project. Not a website or anything.
Appreciate any help!!
You can add an onTouchListner to you TextViews and when user clicks on it you simply launch a new Activity. Google for adding touch listeners and then for launching activities and you will find the components you need.
Activities are like pages in Android apps.
Yeah, Juhani's right. You can use an onTouchListener. It's actually really simple. Just create a new .java file that loads the Layout you want in the onCreate. In the code you just use this line for the onClickListener:
startActivity(new Intent(this, newjavafile.class));
and add the new Activity to your manifest. I had something close to this exact problem. The nice thing about doing it this way is the back button on the phone/device works to get you back to the main screen.
First write attribute in TextView Like,
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Click Here" />
And in Java File
TextView text= (TextView) findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this,SecondActivity.class));
}
});