How to make the keyboard ready in a EditText(Android Studio) - java

I have in my layout a EditText and I don't want the user to click on it of the keyboard to appear but to be ready, I think this is something you put in the layout file but I really don't what it is.

You can add the basise <requestFocus /> on your layout (as describe here) or use the function requestFocus() of the EditText on the onCreate method of the Activity/Fragment (as describe here)

the property you are looking for is called focusable... and you need to set it true
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/PhoneNumberInput"
android:hint="Numero telefonico"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:focusable="true"/>

//Hide the Keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(serverURL.getWindowToken(), 0);
//Show the Keyboard
imm.showSoftInputFromInputMethod(serverURL.getWindowToken(), 0);
but under post runnable over the view.

Related

How to resolve focus snatching issue in Activity?

Background
Development Tool: Android Studio 2.1.3
Device: Android OS Ver. 4.4.2
I have an Activity with multiple Views. I wanted to focus a certain view (editText1 in this case) programatically based on user's previous actions. So I employed View.requestFocus() for this. Before this, I have set focusable and focusableInThouchMode of editText1 to true XML design file:
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".15"
android:inputType="numberDecimal"
android:maxLength="3"
android:text="1"
android:focusableInTouchMode="true"
android:focusable="true"
android:enabled="true" />
Ideally the scenario would be: If user has checked a certain myCheckBox before current action, move focus to dditText1, if else, return.
if(myCheckBox.isChecked()){
editText1.selectAll();
if(adet.requestFocusFromTouch()) {
Log.i(General.LOG_TAG, "editText1 has focus");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText1, InputMethodManager.SHOW_IMPLICIT);
}
return;
}
But In reality, some other View snatches focus instantly from editText1. I can even see that editText1 got focus and have selected a text inside it for an instant. Also, I can see in log that editText1 got focus.
My activity contains a LinearLayout and all other Views(CheckBox, EditText, ListView etc.) are inside it. Also I have set focusable and focusableInThouchMode to false for other views than editText1
Question
How can I prevent other views than my editText from getting/snatching focus in my scenario?
Is there an alternative approach for what I am trying to do here?
You could try to set android:focusable="false" for the components that should not get focused. Not sure I got your question right.

Custom button programmatically android

I'm creating a list of buttons in Android with the same icon for each of them and then set the text programmatically. So my list has:
ImageView (always the same) + Text (label for the icon which I set programmatically).
How can I create a something like the icon below but where I can change text dinamically?
Thank you!
You can use android:drawableLeft attribute in the TextView. Here's the sample:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView"
android:gravity="center_vertical"
android:layout_margin="16dp"
android:drawableLeft="#drawable/ic_launcher"
/>
And the result
I would suggest you using the following
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:drawableLeft="#drawable/button_icon"
... />
If you are looking for more customized button, then probably you should learn more about Custom Views:
https://developer.android.com/training/custom-views/index.html
What I understand from your question is that you want to change/set the text of the button programmatically.
for this use the following code in onCreate event:
Button mybutton = (Button) findViewById(R.id.your_button_name);
mybutton.setText("Your Text Here");

How to display a numeric keyboard on start of an activity?

When I searched I got an answer but that didnt help me. Well.
Now, in my manifest file I used with that particular activity like this:
android:windowSoftInputMode="stateVisible"
It works but shows only alpha keyboard! I want actually want numeric keyboard,please.
Is there a particular EditText you want to have focus first for the numeric keyboard?
If so then try this on the relevant EditText:
XML:
<EditText
android:id="#+id/myEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number">
<requestFocus/>
</EditText>
OR Runtime (onCreate):
EditText editText = new EditText(this);
editText.requestFocus();
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
In your focused EditText add this:
android:inputType="number"

How to make marquee in android repeat just once

I have tried marquee limit 1 but it does not work
<TextView
android:id="#+id/marqueText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="1"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:gravity="center"
android:singleLine="true"
android:textSize="26sp" />
</RelativeLayout>
It seems to me that you are looking for the marquee animation. There is no XML attribute that you set for that purpose
This is a good link for the marquee animation in android. Hope it helps!
The XML attribute simply fades the textview if it goes out of your device screen.
Try using setSelected(true) method of TextView in onCreate()
TextView textView = (TextView) findViewById(R.id.marqueText);
textView.setSelected(true);

Android - Programmatically Hide/Show Soft Keyboard [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you close/hide the Android soft keyboard programmatically?
First thing first I already saw this thread. I tried the accepted methods given there, but nothing worked for me.
I have two screens in my app.
First one has 2 EditText - One for username and one for password
Second one have one ListView, and an EditText - to filter the
listView
In my first screen, I want username EditText to have focus on startup and the Keyboard should be visible. This is my implementation (simplified by removing unnecessary/unrelated code).
#app_login.xml
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
android:paddingRight="20dip">
<EditText android:id="#+id/username"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Username"
android:imeOptions="actionDone" android:inputType="text"
android:maxLines="1"/>
<EditText android:id="#+id/password"
android:password="true"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Password" />
</LinearLayout>
#AppLogin.java
class AppLogin extends Activity{
private EditText mUserNameEdit = null;
private EditText mPasswordEdit = null;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.app_login);
mUserNameEdit = (EditText) findViewById(R.id.username);
mPasswordEdit = (EditText) findViewById(R.id.password);
/* code to show keyboard on startup.this code is not working.*/
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mUserNameEdit, InputMethodManager.SHOW_IMPLICIT);
}//End of onCreate()
}
Well, the keyboard is not showing at startup. And my design badly requires a keyboard there.
Now on to second page. As I already mentioned, I have a listView and EditText there. I want my keyboard to be hidden on startup only to appear when the user touches the editText. Can you believe it? whatever I tried soft Keyboard is showing when I load the activity. I am not able to hide it.
#app_list_view.xml
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText android:id="#+id/filter_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search" android:inputType="text"
android:maxLines="1"/>
<ListView android:id="#id/android:list"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_width="fill_parent"
android:focusable="true"
android:descendantFocusability="beforeDescendants"/>
</LinearLayout>
#AppList.java
public class MyListActivity extends ListActivity{
private EditText mfilterEditText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_list_view);
mFilterEditText = (EditText) findViewById(R.id.filter_edittext);
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mFilterEditText.getWindowToken(), 0);
}
}
To simplify
On Login Page (first Page) I want my keyboard to be visible on startup.
On SecondPage I want the keyboard to be hidden first, only to appear
when the user touches editText
And my problem is I am getting the exact opposite on both occasions. Hope someone faced this issue before. BTW I am testing on the simulator and HTC Desire phone.
#FINAL OUTCOME
Well, I got it working, with the help of all my friends here.
1. To Show keyboard on startup
Two answers worked for me. One provided by #CapDroid, which is to use a handler and post it delayed..
mUserNameEdit.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(mUserNameEdit, 0);
}
},50);
The second answer is provided by #Dyarish, In fact, he linked to another SOF thread, which I haven't seen before. But the funny thing is that this solution is given in the thread which I referenced at the start. And I haven't tried
it out because it had zero votes in a thread where all other posts have plenty of votes. Height of foolishness.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
For me, the second solution looked neat, so I decided to stick with it..But the first one certainly works.
Also, #Dyarish's answer contains a clever hack of using a ScrollView below EditText to give EditText the focus. But I haven't tried it, but it should work. Not neat though.
2. To hide keyboard at activity start
Only one answer worked for me, which is provided by #Dyarish. And the solution is to use
focusableInTouchMode settings in XML for the layout containing the EditTexts. This did the trick
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusableInTouchMode="true">
<EditText android:id="#+id/filter_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search" android:inputType="text"
android:maxLines="1"/>
<ListView android:id="#id/android:list"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_width="fill_parent"
android:focusable="true"
android:descendantFocusability="beforeDescendants"/>
</LinearLayout>
Anyway, I end up using Dyarish's answer in both cases. So I am awarding the bounty to him. Thanks to all my other friends
who tried to help me.
UPDATE 2
#Override
protected void onResume() {
super.onResume();
mUserNameEdit.requestFocus();
mUserNameEdit.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(mUserNameEdit, 0);
}
},200); //use 300 to make it run when coming back from lock screen
}
I tried very hard and found out a solution ... whenever a new activity starts then keyboard cant open but we can use Runnable in onResume and it is working fine so please try this code and check...
UPDATE 1
add this line in your AppLogin.java
mUserNameEdit.requestFocus();
and this line in your AppList.java
listview.requestFocus()'
after this check your application if it is not working then add this line in your AndroidManifest.xml file
<activity android:name=".AppLogin" android:configChanges="keyboardHidden|orientation"></activity>
<activity android:name=".AppList" android:configChanges="keyboard|orientation"></activity>
ORIGINAL ANSWER
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
for hide keyboard
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);
for show keyboard
imm.showSoftInput(ed, 0);
for focus on EditText
ed.requestFocus();
where ed is EditText
Adding this to your code android:focusableInTouchMode="true" will make sure that your keypad doesn't appear on startup for your edittext box. You want to add this line to your linear layout that contains the EditTextBox. You should be able to play with this to solve both your problems. I have tested this. Simple solution.
ie: In your app_list_view.xml file
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusableInTouchMode="true">
<EditText
android:id="#+id/filter_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search"
android:inputType="text"
android:maxLines="1"/>
<ListView
android:id="#id/android:list"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_width="fill_parent"
android:focusable="true"
android:descendantFocusability="beforeDescendants"/>
</LinearLayout>
------------------ EDIT: To Make keyboard appear on startup -----------------------
This is to make they Keyboard appear on the username edittextbox on startup. All I've done is added an empty Scrollview to the bottom of the .xml file, this puts the first edittext into focus and pops up the keyboard. I admit this is a hack, but I am assuming you just want this to work. I've tested it, and it works fine.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
android:paddingRight="20dip">
<EditText
android:id="#+id/userName"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="1"
/>
<EditText
android:id="#+id/password"
android:password="true"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Password" />
<ScrollView
android:id="#+id/ScrollView01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ScrollView>
</LinearLayout>
If you are looking for a more eloquent solution, I've found this question which might help you out, it is not as simple as the solution above but probably a better solution. I haven't tested it but it apparently works. I think it is similar to the solution you've tried which didn't work for you though.
Hope this is what you are looking for.
Cheers!
Try this code.
For showing Softkeyboard:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
For Hiding SoftKeyboard -
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
Did you try InputMethodManager.SHOW_IMPLICIT in first window.
and for hiding in second window use InputMethodManager.HIDE_IMPLICIT_ONLY
EDIT :
If its still not working then probably you are putting it at the wrong place. Override onFinishInflate() and show/hide there.
#override
public void onFinishInflate() {
/* code to show keyboard on startup */
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mUserNameEdit, InputMethodManager.SHOW_IMPLICIT);
}

Categories