android edittext request focus at specific position - android

I have an edittext view and button , Edit text has some default value.
when i click on button i give focus to the edit text. but problem is cursor is at the start of edit text. how to place cursor after the default text.
what i did.
mobile_text =(EditText)findViewById(R.id.mobile_text);
mobile_edit = (ImageView)findViewById(R.id.mobile_edit_icon);
mobile_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mobile_text.setEnabled(true);
mobile_text.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
My layout.xml file
<RelativeLayout
android:paddingTop="#dimen/pad_3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/pad_5dp"
android:paddingBottom="#dimen/pad_5dp"
>
<TextView
android:id="#+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MOBILE"
android:paddingLeft="2dp"
android:textSize="#dimen/txt_12sp"
/>
<EditText
android:paddingTop="#dimen/pad_5dp"
android:layout_below="#+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:id="#+id/mobile_text"
android:text="9581129423"
/>
<ImageView
android:clickable="true"
android:id="#+id/mobile_edit_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edit"
android:layout_below="#+id/mobile"
android:layout_alignParentRight="true"
android:gravity="right|end"
/>
</RelativeLayout>

Google Docs here : EditText - setSelection, Selection - setSelection
add this line
mobile_text.setSelection(mobile_text.getText().length());

Related

EditText doen't open keyboard in popup

This layout is used for popup window to change name of a textview.But the edittext doesnt show keyboard even on clicking the edittext. I'm using it in a fragment. fragment has a namefield which is meant to be changed on clicking OK in this popup.
This is the xml file for popup window.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="#+id/name_status_btns"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:elevation="2dp"
android:background="#fff"
android:text="CANCEL"
android:textSize="18sp"
android:textColor="#000"
android:id="#+id/cancel_change_name"/>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#b1b0b0"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"
android:textColor="#000"
android:textSize="18sp"
android:elevation="2dp"
android:background="#fff"
android:id="#+id/ok_change_name"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#b1b0b0"
android:layout_above="#id/name_status_btns"
android:id="#+id/name_status_horizontal_liner"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:id="#+id/name_status_edit_field">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="write your name here"
android:scrollHorizontally="true"
android:id="#+id/change_name"
android:focusable="true"
android:focusableInTouchMode="true"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/mainColor"
android:layout_below="#id/name_status_edit_field"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
This is the java file for above xml.
public class Name_Status extends AppCompatActivity implements View.OnClickListener {
EditText name_change;
RelativeLayout name_status_edit_field;
String name;
Button cancel_name_change , ok_name_change;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.name_status);
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.3));
name_change=(EditText)findViewById(R.id.change_name);
name_status_edit_field=(RelativeLayout)findViewById(R.id.name_status_edit_field);
name=name_change.getText().toString();
cancel_name_change=(Button)findViewById(R.id.cancel_change_name);
ok_name_change=(Button)findViewById(R.id.ok_change_name);
ok_name_change.setOnClickListener(this);
name_change.setOnClickListener(this);
name_status_edit_field.clearFocus();
name_change.setFocusable(true);
//name_change.update();
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.ok_change_name)
{FragActivity1 obj=new FragActivity1();
obj.changeName(name);
Intent intent=new Intent();
intent.setClass(Name_Status.this , MainActivity.class);
startActivity(intent);}
if(v.getId()==R.id.change_name)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(name_change.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
}
The main problem is your EditText has a 0dp width and a layout_weight attribute in a RelativeLayout, which does not make any sense.
Either make the parent of the EditText a LinearLayout or remove the layout_weight attribute from the EditText and give it a proper width:
<EditText
android:id="#+id/change_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="write your name here"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"/>
The following code is completely unnecessary, you should just remove it from onClick():
if(v.getId()==R.id.change_name)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(name_change.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
You can also try the following for EditText
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable())
editText.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
For more details refer Keyboard not shown when i click on edittextview in android?
EditText is not suppose to have 0dp width and layout_weight inside a RelativeLayout. Weight works inside LinearLayout. So change your EditText to below:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="write your name here"
android:scrollHorizontally="true"
android:id="#+id/change_name"
android:focusable="true"
android:focusableInTouchMode="true"/>
And change your OnClick to because EditText will take care of click by itself:
#Override
public void onClick(View v) {
if(v.getId()==R.id.ok_change_name){
FragActivity1 obj=new FragActivity1();
obj.changeName(name);
Intent intent=new Intent();
intent.setClass(Name_Status.this , MainActivity.class);
startActivity(intent);
}
}
And finally remove the below line from your OnCreate() method:
name_change.setOnClickListener(this);
Because EditText will open keyboard by itself when clicked.

EditText focus not going after finishing activity

In my form activity, I've 3 fields. After confirming the form to the server it finishes and go to the previous activity. But the problem , After finish the keyboard is still pop up and the previous activity has no input field.
Here is the layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#color/colorWhite"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_gravity="center"
android:layout_margin="20dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<sslwireless.com.easycorporate.Fonts.LoraBold
android:layout_marginBottom="20dp"
android:text="#string/change_password_title"
android:textSize="25sp"
android:textColor="#color/colorAccent"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/oldPassword"
android:background="#drawable/super_white_style"
android:padding="12dp"
android:layout_marginTop="15dp"
android:singleLine="true"
android:inputType="textPassword"
android:hint="#string/hints_old_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/newPassword"
android:background="#drawable/super_white_style"
android:padding="12dp"
android:layout_marginTop="15dp"
android:singleLine="true"
android:inputType="textPassword"
android:hint="#string/hints_new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/confirmPassword"
android:background="#drawable/super_white_style"
android:padding="12dp"
android:layout_marginTop="15dp"
android:singleLine="true"
android:inputType="textPassword"
android:hint="#string/hints_confirm_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/changePassword"
android:text="#string/change_password"
android:layout_marginTop="20dp"
android:textColor="#color/colorWhite"
android:background="#drawable/primary_dark_color_style"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Here what actually happening.
i also set the android:windowSoftInputMode="stateHidden" for both activity in the manifest file. is there any way I can remove focus after go back to the previous activity ?
In onStop() of the finishing Activity, disable all the EditText fields like this in order to make the soft keyboard disappear:
EditText et = (EditText)findViewById(R.id.confirmPassword);
et.setEnabled(false);
You may force IME to close (before you finish() your activity):
public void hideSoftKeyboard(#NonNull Activity activity) {
IBinder windowToken = activity.getWindow().getDecorView().getRootView().getWindowToken();
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(windowToken, 0);
}
Here's something that actually fixed an identical bug today.
#Override
public void closeKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//Log.d("RKZ", "isKeyboardShown("+((ViewGroup) findViewById(android.R.id.content)).getChildAt(0)+") = "+isKeyboardShown(((ViewGroup) findViewById(android.R.id.content)).getChildAt(0)));
if(view != null && Util.isSoftKeyboardShown(imm, view)) {
Util.hideKeyboard(ActivityASG.this, view);
}
}
And in your CHANGE PIN Button's onClick() just call it like this:
//close keyboard
callback.closeKeyboard(noteInput);
callback.closeKeyboard(mailInput);
//callback.closeKeyboard(yourCHANGE_PINview);
call below code for close keybord , call when click on button
InputMethodManager imk = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imk.isActive()){
imk.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide key board
}

Characters appear in the order as i click

For example. There is picture on the top of the screen below that there are some empty boxes and below the boxes there are some buttons. Every button has a character for text("a","c","t"). You click on a button and the button's text appear in the box. You can click them in the order you want to but the answer is "cat" so when you put the characters in the correct order then you got a toast.
I tried to do it with TextViews and Buttons. I can make the button disappear when i click on it and a textview appear in the same time. But every textview has a fix place on the screen, so i need to put every character in every box invisible and when i click on the "c" character it appear in the first box and the other "c" characters stay invisible. But if i click on the "a" first, then it appears in the second box because there is too much variation to do all. I'm not good at explaining but if anyone has an idea how to do that easier please response!
Here is my code:
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button b1;
Button b2;
Button b3;
TextView tg1;
TextView tg2;
TextView tg3;
TextView to1;
TextView to2;
TextView to3;
TextView tl1;
TextView tl2;
TextView tl3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.bg);
b1.setOnClickListener(this);
b2 = (Button)findViewById(R.id.bo);
b2.setOnClickListener(this);
b3 = (Button)findViewById(R.id.bl);
b3.setOnClickListener(this);
tg1 = (TextView)findViewById(R.id.tg1);
tg2 = (TextView)findViewById(R.id.tg2);
tg3 = (TextView)findViewById(R.id.tg3);
to1 = (TextView)findViewById(R.id.to1);
to2 = (TextView)findViewById(R.id.to2);
to3 = (TextView)findViewById(R.id.to3);
tl1 = (TextView)findViewById(R.id.tl1);
tl2 = (TextView)findViewById(R.id.tl2);
tl3 = (TextView)findViewById(R.id.tl3);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.bg:
b1.setVisibility(View.INVISIBLE);
tg1.setVisibility(View.VISIBLE);
tg2.setVisibility(View.INVISIBLE);
tg3.setVisibility(View.INVISIBLE);
break;
case R.id.bo:
b2.setVisibility(View.INVISIBLE);
to2.setVisibility(View.VISIBLE);
to1.setVisibility(View.INVISIBLE);
to3.setVisibility(View.INVISIBLE);
break;
case R.id.bl:
b3.setVisibility(View.INVISIBLE);
tl3.setVisibility(View.VISIBLE);
tl2.setVisibility(View.INVISIBLE);
tl1.setVisibility(View.INVISIBLE);
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="hu.szada.gombokelso.MainActivity"
android:orientation="horizontal">
<TextView
android:id="#+id/tl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
android:text="l"/>
<Button
android:id="#+id/bo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="86dp"
android:onClick="onClick"
android:text="o" />
<Button
android:id="#+id/bl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/bg"
android:layout_alignBottom="#+id/bg"
android:layout_alignParentLeft="true"
android:layout_marginLeft="36dp"
android:onClick="onClick"
android:text="l" />
<Button
android:id="#+id/bg"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/bo"
android:onClick="onClick"
android:text="g" />
<TextView
android:id="#+id/tg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/to1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
/// Second
<TextView
android:id="#+id/to2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl1"
android:layout_alignBottom="#+id/tl1"
android:layout_marginLeft="19dp"
android:layout_toRightOf="#+id/tl1"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
<TextView
android:id="#+id/tg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignLeft="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
/// Third
<TextView
android:id="#+id/tg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/bl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_alignLeft="#+id/tg3"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
<TextView
android:id="#+id/to3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_toRightOf="#+id/tl3"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
You might want to try a slightly different approach.
If I understand you correctly, you want to "type" a word out using given lettered buttons. Like one of those hangman style games.
Why not append the text views on the fly.
Something like
#Override
public void onClick(View v) {
//Grab the surrounding layout for the textviews
GridView answerGrid = (GridView)getViewById(R.id.answerGrid);
//Get the text that was on the button
Button b = (Button)v;
String btnText = b.getText().toString();
//Make a text view with text
TextView txt = new TextView();
text.setText(btnText);
//Append to text view container
answerGrid.addView(txt);
//Invisible button
b.setVisibility(View.INVISIBLE);
}
Haven't tested to see if this is perfect, but its a start.
=====
I've looked at your xml
Why not use GridViews?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
....>
<GridView android:id="#+id/answerGrid"
....>
<!-- Put nothing here. This is for answers -->
</GridView>
<GridView android:id="#+id/lettersGrid"
android:layout_below="answerGrid"
....>
<!-- Buttons in here -->
</GridView>
</RelativeLayout>
This way you can customise the number of rows/columns based on the length of the word you're playing with. And GridView will automatically give you a neat layout and spacing.
Have a look at the GridView doc and get it customised the way you want it.
See my edits above for the Java code.

How to get Focus in editText that is in a ViewSwitcher

I have a form with a lot of ViewSwitcher that change a TextView to an EditText with a clic.
The thing is I must clic 2 times to be able to insert value : Here are the screenshots :
The good thing would be not passing by the second state....
Here is my code.
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:text="Colonia:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/android_blue" />
<ViewSwitcher
android:id="#+id/vs2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical" >
<TextView
android:id="#+id/txt_colonia"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:onClick="TextViewClicked"
android:paddingLeft="12dp"
android:text="Colonia"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/et_colonia"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusableInTouchMode="true"
android:text="Colonia 13"
android:textAppearance="?android:attr/textAppearanceLarge" />
</ViewSwitcher>
</TableRow>
And here is the java part :
switcher = (ViewSwitcher) findViewById(switchId);
View newView = switcher.getNextView();
if (newView instanceof TextView) {
((TextView) newView).setText(valor);
} else if (newView instanceof EditText) {
((EditText) newView).setText(valor);
((EditText) newView).setFocusableInTouchMode(true);
((EditText) newView).requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
switcher.showNext();
Try switching to the edittext and then formatting it,
viewswitcher.showNext();
edittext.requestFocus();
edittext.setSelection(edittext.getText().length());
Try this:
If you are in a fragment
switcher.showNext();
edittext.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, 1);
If you are in an Activity
switcher.showNext();
edittext.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, 1);
A little late, but might help someone else...

Hide Keyboard for AutoCompleteTextView

I have two EditText and two AutoCompleteTextView in my layout.
But I like to hide the keyboard only for the AutoCompleteTextView.
But it does not hide the keyboard and when I touch the AutoCompleteTextView, Keyboard still appear, how can I hide the keyboard?
I implemented as
final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);
final TrackerInfo newInfo = new TrackerInfo();
String[] type = {"Vehicle", "Person", "Pet", "Others"};
final AutoCompleteTextView actvtype1 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> typeadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_multiple_choice,type);
actvtype1.setThreshold(1);
actvtype1.setAdapter(typeadapter);
actvtype1.setTextColor(Color.BLACK);
String[] model = {"TS102", "TS103"};
final AutoCompleteTextView actvtype2 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView2);
ArrayAdapter<String> modeladapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_multiple_choice,model);
actvtype2.setThreshold(1);
actvtype2.setAdapter(modeladapter);
actvtype2.setTextColor(Color.BLACK);
final AlertDialog.Builder alert = new AlertDialog.Builder(this).setTitle("New Tracker").setView(addView);
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromInputMethod(actvtype1.getWindowToken(), 0);
keyboard.hideSoftInputFromInputMethod(actvtype2.getWindowToken(), 0);
alert.setPositiveButton("ADD", new DialogInterface.OnClickListener()
{
#SuppressLint("SimpleDateFormat")
public void onClick(DialogInterface dialog, int whichButton)
{
}
}).setNegativeButton("Cancel", null).show();
My layout is as follow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/IDnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/IDnumber" />
<EditText
android:id="#+id/IDeditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<TextView
android:id="#+id/SimCardNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Sim_card_number" />
<EditText
android:id="#+id/SimCardEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
/>
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/description" />
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:dropDownHeight="100dp"
android:text=""/>
<TextView
android:id="#+id/model"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Tracker_model"
/>
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:dropDownHeight="100dp"
android:text=""/>
</LinearLayout>
I'm not sure if this is exactly what you're looking for but this is the code that is used to force the keyboard from popping up at the bottom of the screen:
actvtype1.setInputType(InputType.TYPE_NULL);
This will definitely stop the keyboard appearing when you click on that TextView. Hope it helps!
Request for the focus on autotext as :
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView2"
..
.../>
<requestFocus />
And then use this code:
if(autocomplete.hasfocus()){
hideSoftKeyboard();
}
private void hideSoftKeyboard() {
if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText)
{
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
To hide keyboard from onItemClick after clicking on list item in AutoCompleteTextView
public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
// whatever else should be done
}
use this code in your Autocomplete TextView onClick
YourClass.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Categories

Resources