Android Show Soft Keyboard When First Activity Starts? - java

I need to display the virtual keyboard when the application starts, but so far I've failed.
I use this code in method "OnCreate"to display the virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(txtBuscar.getId(), InputMethodManager.SHOW_FORCED);
this code works fine on any screen at any time, but does not work when the "first" activity begins. Why?
I tried it when I start another activity and it works, but does not work when I start the "first"activity.
I tried to put this code in the events "OnCreate"and many more .... but it seems not work.
is there anyway to "force" to display the keyboard when I start the application?
Thanks in advance.

I Found the solution:
txtPassword.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(txtPassword, 0);
}
},200);
Thanks !!!

onCreate will not be called if the activity is first brought from background. Have you tried put that code in onResume?
onCreate is called only when activity is first start or the activity is killed and user navigate to the activity again. So if the activity is still alive but in background, it will not call onCreate.
On the other hand, onResume will be called every time the activity comes to foreground (visible on screen) from background.
Here is link to activity life cycle if you are interested http://developer.android.com/reference/android/app/Activity.html.
Hope it helps.

I faced with the same issue, this method below helped me
public static void showKeyboard(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}

Related

Press back button on main activity opens other activity

In my project i move from main activity to activity A and from activity A to activity B. From activity B using home menu on toolbar i jump back to main activity. Now when i press back button application should exit but it opens the activity A again.
You should make use of the launch flags to manage your activities in the back stack. As far as I understood your scenario, I think you need to use FLAG_ACTIVITY_CLEAR_TOP for starting your main/home activity.
Read more about launch flags: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP.
Also, take a look this for more details on managing activity back stack on Android: https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManagingTasks
Call the finish() method before starting the next activity to have it removed from the activity. Find more details and options here.
For Kotlin write this in your MainActivity :
override fun onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
For Java write this in your MainActivity :
#Override
void onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
Hope to it will work for you as good as for me
hey i write some code for you
Variables:
boolean backactivity = true;
CODE:
public boolean onOptionsItemSelected(MenuItem item){
if(backactivity==true)
{
finishActivity(1);
backactivity=false;
}else
{
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return true;
}

How to hide keyboard onResume when returning from another app for sharing [duplicate]

This question already has answers here:
How to close/hide the Android soft keyboard programmatically?
(126 answers)
Closed 3 years ago.
I am sharing some text to other apps using android default sharing
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, sharingString);
shareIntent.setType("text/plain");
startActivity(shareIntent);
The Issue:
When I share to another app i.e (WhatsApp, slack, etc) open the keyboard in that app (i.e. to edit the message, etc.), and then send or return back to my application. in some cases the keyboard remains open in that app and when I try to close the keyboard on resume using the following code it is not working.
public static void hideKeyBoard(Activity activity, View view) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (view == null) {
view = new View(activity);
}
if (imm != null) {
if(view.getWindowToken()!=null)
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
if(view.getApplicationWindowToken()!=null)
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
view.clearFocus();
}
And calling the function from onResume() of the fragment
if(getActivity()!=null){
Utils.hideKeyBoard(getActivity(),getActivity().getCurrentFocus());
}
I have found many answers related to hiding the keyboard but any answer is not working in this case.
Note: in the normal case, when I use the hideKeyBoard() method, it is working perfectly. It is just this case that is not working. Can anyone help me?
Edit
As I've mentioned above. that why this quesiton is not like the prviosly answered ones is explained in the above note. So kindle Plesase read that. And I've also tried this link. but not working.
You can try below code that may help you.
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Here is some bonus
create activity instance instead use getActivity() methos on fragment. getActivity() returns null when fragment not visible
on your fragment host activity
public static MainActivity mainActivity;
public static MainActivity getInstance() {
return mainActivity;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = this;
}
inside onResume() call hideKeyboard(MainActivity.getInstance())
Also, don't forget to add android:windowSoftInputMode="stateAlwaysHidden" in your Manifest

Soft keyboard auto show when use AsyncTask

I don't know why when I use AsyncTask to load data to an Adapter, after AsyncTask finishes, the soft keyboard shows on screen.
There is a Edittext in the activity. If I delete the EditText on the Activity, after the AsyncTask finishes, the soft keyboard does not show.
I use this code on onPostExecute() but it does not work:
protected void onPostExecute(String[] result) {
hideSoftKeyboard(SearchActivity.this);
RecommendAdapter mAdap = new RecommendAdapter(con, mLocation);
mListview.setAdapter(mAdap);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
Now I know the answer.It because I use so it always focus on .Add the android:focusable="true"
android:focusableInTouchMode="true" to the Layout so the soft keyboard not show.

Another page poping up instead of keyboard

I want to open calculator which I created. This is the code I wrote:
TextView.OnClickListener listener = new TextView.OnClickListener(){
public void onClick(View v) {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textOut1.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut2.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut3.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut5.getWindowToken(), 0);
startActivity (new Intent("com.easyPhys.start.calculator"));
}
};
textOut1.setOnClickListener(listener);
textOut2.setOnClickListener(listener);
textOut3.setOnClickListener(listener);
textOut5.setOnClickListener(listener);
But what happens is virtual keyboard opens and my calculator opens only than I press ENTER. What is wrong with my code?
Try the onClick event listener on the TextView, instead of the OnEditorActionListener.
For it to work you also need to add the following attribute to the TextView in the xml.
android:clickable="true"
Don't forget it, without it it won't work.
The OnEditorActionListener only fires when some action is performed on the editor, and that's why the activity only shows when you click the keyboard. On the other hand, the onClick listener should fire right after you click the TextView.

Show soft keyboard for dialog

I am displaying a dialog with an edittext view. However, the softkeyboard will open only if the user presses inside the editview. So I tried calling an InputMethodManager with the following code.
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);
The dialogField is the input field. However, when exactly am I supposed to do this? I tried it in the onStart() method of the dialog, but nothing happens. I also tried requesting the focus for the dialogField before, but that changes nothing.
I also tried this code
dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
public void onFocusChange (View v, boolean hasFocus)
{
if (hasFocus)
{
Main.log("here");
dialogInput.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
/*
InputMethodManager mgr =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(dialogField,0);
*/
}
}
});
in both versions. But no soft keyboard would like to appear. The Main.log is just a log, which shows me that the function is actually called. And yes, it is called.
I could get the keyboard with the SHOW_FORCED flag before the dialog opens. But then it will not close on exit. And I can only do that BEFORE I show the dialog. Inside any callbacks it does not work either.
Awesome question, I was trying to do that too and found a solution.
Using the dialog builder class AlertDialog.Builder you will have to invoke the dialog like this:
AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;
builder.set...
dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
This worked fine for me.
Note: you must import android.view.WindowManager.LayoutParams; for the constant value there.
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
Window window = dialog.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Kotlin
Here's the tested code.
val dialog = AlertDialog.Builder(requireContext()).apply {
setTitle(…)
setView(editText)
setPositiveButton(…)
setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
Make sure you access the window property from show() method. Getting window from create() method was returning null for me, so the keyboard wasn't showing.
Import AlertDialog from androidx.appcompat.app.AlertDialog.
Import WindowManager from android.view.
Dialog Fragment With Kotlin
override onStart Method
override fun onStart() {
super.onStart()
dialog.window?.
setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
)
}
if you want to close after dismiss then override dismiss method with below code
override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
Here's my solution, it's working well for dialog.
txtFeedback.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Maybe also you need to add this to your activity tag in AndroidManifest.xml for closing the keyboard when the dialog is dismissed.
android:windowSoftInputMode="stateAlwaysHidden"

Categories