I can't seem to get this work. I already set popWindow focusable as to what I read on other forums but still no luck.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:background="#drawable/popbg"
android:orientation="vertical" >
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:background="#drawable/zcancel" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="SSID"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
java
case(R.id.settings):
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.drawable.cpanel2);
return true;
case MotionEvent.ACTION_UP:
v.setBackgroundResource(R.drawable.cpanel1);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popSwitchView = layoutInflater.inflate(R.layout.settings_xml, null);
final PopupWindow popWindow = new PopupWindow(popSwitchView);
popWindow.setWidth(LayoutParams.MATCH_PARENT);
popWindow.setHeight(LayoutParams.MATCH_PARENT);
popWindow.showAtLocation(popSwitchView, Gravity.CENTER, 0, 0);
popWindow.setOutsideTouchable(false);
popWindow.setFocusable(true);
Drawable d = getResources().getDrawable(R.drawable.popbg);
popWindow.setBackgroundDrawable(d);
Button CancelButton = (Button)popSwitchView.findViewById(R.id.cancel);
CancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popWindow.dismiss();
}
});
popWindow.showAsDropDown(v, 50, -30);
return true;
default:
return false;
}
I'm planning on creating a popwindow of setting for network configurations. I can't seem to fix my code for a good view for you guys .
ha, .found the answer.,
just did
popWindow.setFocusable(true);
popWindow.update();
thanks for the support!
credits from this topic
Keyboard not shown when i click on edittextview in android?
This worked for me:
popWindow.setFocusable(true);
popWindow.update();
Try adding this line before you show the popup:
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Nothing helped me, it still didn't appear on some devices, so I had to force showing it like that (kotlin):
val editText = customView.findViewById<EditText>(numberFieldId)
editText.onClick {
showKeyboard(editText, context)
}
private fun showKeyboard(mEtSearch: EditText, context: Context) {
mEtSearch.requestFocus()
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
Try this::
Programatically:
edittext.requestFocus();
Through xml:
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName">
<requestFocus />
</EditText>
Hope it Helps!!
Related
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.
How can I obtain an EditText as those bellow?
The x button removes the text inserted and the eye button displays the clear password as long as it's pressed. Note that I refer to them as buttons because I don't really know what they actually are nor if they are part of the EditText itself or if they are independent views.
You can just add a button at the right of your EditText
<FrameLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ScreenLoginContainer">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:hint="#string/enter_password_hint"
android:paddingRight="30dp"
style="#style/ScreenPasswordEditText"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+screen_card_ean_enter/show_password_button"
style="#style/ShowHidePasswordButton"/>
</FrameLayout>
You can use android:drawableRight
Set a Drawable to the right of the text, in EditText, in XML .
android:drawableRight="#drawable/Your_drawable"
You can use this :
android:drawableRight="#drawable/your_icon"
Also for click this answer
may help
This is the prefect way to create an EditText with cross button at the end:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:padding="5dp">
<EditText
android:id="#+id/calc_txt_Prise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_marginTop="20dp"
android:textSize="25dp"
android:textColor="#color/gray"
android:textStyle="bold"
android:hint="#string/calc_txt_Prise"
android:singleLine="true" />
<Button
android:id="#+id/calc_clear_txt_Prise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_gravity="right|center_vertical"
android:background="#drawable/delete" />
And now to clear the EditText upon click you can use
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditTextID.setText("");
}
});
And in order to make the password visible till the button is pressed, you can implement it this way:
yourButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
editText.setInputType(InputType.TYPE_CLASS_TEXT);
break;
case MotionEvent.ACTION_UP:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;
}
return true;
}
});
The design support library has a setPasswordVisibilityToggleEnabled method: https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setPasswordVisibilityToggleEnabled(boolean)
This would be good for your password edittext, but not the username one.
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());
I use popup menu with custom items.Problems like this
Red lines represents to default width size.
but i want custom smaller width size of items(represents red lines).
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainLayout.this, mSikBtn);
popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
return true;
}
});
popup.show();//showing popup menu
}
});
Layout xml file
<ImageView
android:id="#+id/popupBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/selector_sik"
android:layout_weight="1"
android:layout_margin="10dip"
/>
popup_menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="A)."/>
...
<item
android:id="#+id/three"
android:title="E)."/>
</menu>
Similar question: stackoverflow.com
I need to custom popup menu
Just like you, I tried a lot to change the width of the popup menu but was unsuccessful.
Somehow, I found a solution you might be looking for.
First, instead of using popup menu, I used popup window.
Step1: Making a row layout:
<?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="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#android:drawable/list_selector_background"
android:orientation="vertical"
android:padding="3dp">
<TextView
android:id="#+id/ItemA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="3dp"
android:text="A)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="B)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="C)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="D)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
</LinearLayout>
Step 2: Made a method to initialize popup window:
private PopupWindow initiatePopupWindow() {
try {
mInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = mInflater.inflate(R.layout.row, null);
//If you want to add any listeners to your textviews, these are two //textviews.
final TextView itema = (TextView) layout.findViewById(R.id.ItemA);
final TextView itemb = (TextView) layout.findViewById(R.id.ItemB);
layout.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
mDropdown = new PopupWindow(layout,FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,true);
Drawable background = getResources().getDrawable(android.R.drawable.editbox_dropdown_dark_frame);
mDropdown.setBackgroundDrawable(background);
mDropdown.showAsDropDown(pop, 5, 5);
} catch (Exception e) {
e.printStackTrace();
}
return mDropdown;
}
Step 3: Simply calling it in the onCreate():
public class MainActivity extends Activity {
ImageButton red, blue;
private PopupWindow mDropdown = null;
LayoutInflater mInflater;
Button pop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pop = (Button)findViewById(R.id.button1);
pop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initiatePopupWindow();
}
});
}
}
Hope this helps you..If it does, accept my answer..:)
Here's the screenshot:
http://imageshack.com/a/img585/7388/dxjo.png
I don't think you can set layout:width in menu, so you might have to create your own view layout. This might help:
Set menu items centered and full-width with ActionBarSherlock
http://developer.android.com/guide/topics/resources/menu-resource.html
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);