I have a activity , in a activity one edit text having some hint text.
when i start activity by default focus gone on edit text and hint invisible.
please tell me how to make hint visible
Thank you
Add in the page an empty view:
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width = "0dp"
android:layout_height = "0dp"/>
This view will gain the focus and prevent EditText from getting it
Add this line in onCreate before setContentView()
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
also if you have requestFocus in xml part then remove it
After setContentView():
KeyListener keyListener = new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, false);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
setInput(YOUR_EDIT_TEXT, keyListener, imm);
setInput method:
private void setInput(EditText _editText, KeyListener keyListener, InputMethodManager imm){
_editText.setKeyListener(keyListener);
if (_editText.isFocusable()) {
_editText.setFocusable(true);
_editText.setFocusableInTouchMode(true);
_editText.requestFocus();
}
_editText.setSelection(_editText.getText().length());
imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(_editText, InputMethodManager.SHOW_IMPLICIT);
}
Related
I would like to programmatically click an editText once a button has been pressed.
When a radio button "pitot_area" is selected, I want the editText associated with this selection to be programmatically clicked, so the user has one less "click" on the screen and it happens automatically.
So far I have tried
performClick()
callOnClick()
requestFocus()
the area i would like this code to be within is below:
root.pitot_area.setOnClickListener {
setAreaLabels(root)
evNodeItem.kvParams = false
evNodeItem.PitotRectArea = false
evNodeItem.PitotRoundArea = false
evNodeItem.areaParams = true
root.area_edit_text.requestFocus()
another method I have tried is
root.area_edit_text.setFocusableInTouchMode(true);
root.area_edit_text.setFocusable(true);
root.area_edit_text.requestFocus();
for the above lines of code, I have in the .xml file for the editText
android:focusable="false"
android:focusedByDefault="false"
An error does not show and the code builds, it seems this code is not read in kotlin? Does anyone have any ideas on how to do this? Thanks!
edit
ive looked at-> Focus Edit Text Programmatically (Kotlin) question sugggested below and was unable to implement it for my code.
To set direct focus to the EditText you have to open keyboard after setting focus to the EditText.
write this lines into your button click:
For Activity:
button.setOnClickListener {
editText.isFocusableInTouchMode = true
editText.requestFocus()
val inputMethodManager: InputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInputFromWindow(llMainView.applicationWindowToken, InputMethodManager.SHOW_FORCED, 0)
}
For Fragment:
button.setOnClickListener {
editText.isFocusableInTouchMode = true
editText.requestFocus()
val inputMethodManager: InputMethodManager = activity!!.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInputFromWindow(llMainView.applicationWindowToken, InputMethodManager.SHOW_FORCED, 0)
}
My activity has a single text field, which is editable, I want it so that when the activity is started the keyboard doesn't automatically start up, it should come up only when the user clicks on the editTiext field.
Any help?
In your manifest file add this line android:windowSoftInputMode="stateHidden"to your activity!
It seems like when your activity starts your TextView (since you said text field I suppose you have a TextView but the property exists on other views as well) receives automatically focus. Try looking at the TextView properties to find one that is about the object receiving focus.
public static void hideKeyboard(Context mContext){
//Hide a keypad write down on onCreate
((Activity) mContext).getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
public static void showKeyboard(Context mContext,EditText edittext){
//Show a Keyboard when you click on Edittext
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
}
I have an activity with lots of edittext. whenever I load that activity, the keyboard appears and eats half of the screen which makes that activity's look bad. So is there any way to hide keyboard when I load that activity.
in your onCreate() use this..
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Add this two line in your activity's XML file in the RootLayout i.e. either relative or linear(whatever you have taken) :
android:focusableInTouchMode="true"
Add this line in activity manifests file
android:windowSoftInputMode="stateHidden"
In your AndroidManifest.xml add the attribute android:windowSoftInputMode:
<activity android:name="your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
You can do this using intputmethodmangare... using the following code..
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Put this code on the onCrete function:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
view.clearFocus();
}}, 50);
where view is your EditText
The runnable is because the code might be executed before the editText is rendered.
I created a method which I call in all the required Activity classes in the onCreate event. Worked for me in all scenarios.
public class ClassLib {
public static void hideKeyboard(Activity activity) {
//Hide keyboard
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
I have 2 EditTexts in the MainActivity Layout. If i run the application normally the 1st EditText gets focused but the softkeyboard is not openned.
but when i used this:
public class TestingActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et1 = (EditText) findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);
et2.requestFocus();
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
}
}
expecting the 2nd EditText will get focus and softkeyboard will be openned.
I only get focus, but the softkeyboard is openned only when i click on the EditText.
Thank You
Try specifying the android:windowSoftInputMode attribute in your AndroidManifest.xml file for your activity.
For example:
<activity android:name=".TestingActivity" android:windowSoftInputMode="stateVisible|adjustResize" />
You probably don't need any of the code that uses InputMethodManager in your Activity.
I notice that one reason for the keyboard not showing up is selecting an inputtype not supported by the specific Android device. For instance InputType.TYPE_NUMBER_VARIATION_NORMAL will not work on my Asus Transformer (no keyboard shows up), while InputType.TYPE_CLASS_NUMBER will work just fine.
et2.clearFocus();
et2.requestFocus();
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
I meet the problem on Android N platform and resolve it by refocusing the editview.
I don`t know the real reason why the editview should be cleared first,but it works fine for me.
Sometimes you will need to post-delay showing keyboard command, so in my case, i did the following
editText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}, 300);
For getting the focus to particular edittext just add the tag inside your edit text.
<EditText
android:id="#+id/etBox"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="enter into editbox"
>
<requestFocus/>
</EditText>
In my application I have a single EditText together with some TextViews, button and a spinner. My EditText receives focus since it is the only focusable view in this activity, I believe. My EditText shows with an orange border and cursor on the field.
Now I would like to remove the focus from this field (I don't want the cursor and border to show). Is there a way to do this?
I have been able to focus on the button by doing button.seFocusableInTouchMode() and button.requestFocus(). But this highlights the button and is obviously not what I want.
new to android.. tried
getWindow().getDecorView().clearFocus();
it works for me..
just to add .. your layout should have:
android:focusable="true"
android:focusableInTouchMode="true"
Did you try to use old good View.clearFocus()
check this question and the selected answer: Stop EditText from gaining focus at Activity startup It's ugly but it works, and as far as I know there's no better solution.
I will try to explain how to remove the focus (flashing cursor) from EditText view with some more details and understanding. Usually this line of code should work
editText.clearFocus()
but it could be situation when the editText still has the focus, and this is happening because clearFocus() method is trying to set the focus back to the first focusable view in the activity/fragment layout.
So if you have only one view in the activity which is focusable, and this usually will be your EditText view, then clearFocus() will set the focus again to that view, and for you it will look that clearFocus() is not working.
Remember that EditText views are focusable(true) by default so if you have only one EditText view inside your layout it will aways get the focus on the screen. In this case your solution will be to find the parent view(some layout , ex LinearLayout, Framelayout) inside your layout file and set to it this xml code
android:focusable="true"
android:focusableInTouchMode="true"
After that when you execute editText.clearFocus() the parent view inside your layout will accept the focus and your editText will be clear of the focus.
I hope this will help somebody to understand how clearFocus() is working.
if Edittext parent layout is Linear then add
android:focusable="true"
android:focusableInTouchMode="true"
like below
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText/>
............
when Edittext parent layout is Relative then
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<EditText/>
............
I know is too late, but for somebody whit the same need editText.setFocusable(false) si what you are looking for.
Use the attached code to give the focus to "someone else", this is OK if you have a view where you simply want to dismiss the keyboard and release the focus, you don't really care about who gets it.
Use like this:
FocusHelper.releaseFocus(viewToReleaseFocusFrom)
public class FocusHelper {
public static void releaseFocus(View view) {
ViewParent parent = view.getParent();
ViewGroup group = null;
View child = null;
while (parent != null) {
if (parent instanceof ViewGroup) {
group = (ViewGroup) parent;
for (int i = 0; i < group.getChildCount(); i++) {
child = group.getChildAt(i);
if(child != view && child.isFocusable())
child.requestFocus();
}
}
parent = parent.getParent();
}
}
}
Doc:
The method traverses from the child view and up the view tree and looks for the first child to give focus to.
Edit:
You can also use the API for this:
View focusableView = v.focusSearch(View.FOCUS_DOWN);
if(focusableView != null) focusableView.requestFocus();
i had a similar problem with the editText, which gained focus since the activity was started. this problem i fixed easily like this:
you add this piece of code into the layout that contains the editText in xml:
android:id="#+id/linearlayout"
android:focusableInTouchMode="true"
dont forget the android:id, without it i've got an error.
the other problem i had with the editText is that once it gain the first focus, the focus never disappeared. this is a piece of my code in java, it has an editText and a button that captures the text in the editText:
editText=(EditText) findViewById(R.id.et1);
tvhome= (TextView)findViewById(R.id.tv_home);
etBtn= (Button) findViewById(R.id.btn_homeadd);
etBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
tvhome.setText( editText.getText().toString() );
//** this code is for hiding the keyboard after pressing the button
View view = Settings.this.getCurrentFocus();
if (view != null)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
//**
editText.getText().clear();//clears the text
editText.setFocusable(false);//disables the focus of the editText
Log.i("onCreate().Button.onClickListener()", "et.isfocused= "+editText.isFocused());
}
});
editText.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(v.getId() == R.id.et1)
{
v.setFocusableInTouchMode(true);// when the editText is clicked it will gain focus again
//** this code is for enabling the keyboard at the first click on the editText
if(v.isFocused())//the code is optional, because at the second click the keyboard shows by itself
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
//**
Log.i("onCreate().EditText.onClickListener()", "et.isfocused= "+v.isFocused());
}
else
Log.i("onCreate().EditText.onClickListener()", "the listener did'nt consume the event");
}
});
hope it will help to some of you!
For me this worked
Add these attributes to your EditText
android:focusable="true"
android:focusableInTouchMode="true"
After this in your code you can simply write
editText.clearFocus()
Just find another view and give it focus instead.
var refresher = FindViewById<MvxSwipeRefreshLayout>(Resource.Id.refresher);
refresher.RequestFocus();
I've tryed much to clear focus of an edit text. clearfocus() and focusable and other things never worked for me.
So I came up with the idea of letting a fake edittext gain focus:
<LinearLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--here comes your stuff-->
</LinearLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fake"
android:textSize="1sp"/>
</LinearLayout>
then in your java code:
View view = Activity.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
fake.requestFocus();
}
it will hide the keyboard and remove the focus of any edittext that has it. and also as you see the fake edittext is out of screen and can't be seen
Just include this line
android:selectAllOnFocus="false"
in the XML segment corresponding to the EditText layout.
You just have to clear the focus from the view as
EditText.clearFocus()
If I understand your question correctly, this should help you:
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1 .setFocusable(false);
You only have to set the ViewGroup with the attribute:
android:focusableInTouchMode="true"
The ViewGroup is the layout that includes every child view.
Just add this line in your parent layout(e.g., constraint or Linear)
android:focusableInTouchMode="true"
Since I was in a widget and not in an activity I did:
getRootView().clearFocus();
<EditText android:layout_height="wrap_content" android:background="#android:color/transparent" android:layout_width="match_parent"
android:clickable="false"
android:focusable="false"
android:textSize="40dp"
android:textAlignment="center"
android:textStyle="bold"
android:textAppearance="#style/Base.Theme.AppCompat.Light.DarkActionBar"
android:text="AVIATORS"/>