Abhishek Kumar Abhishek Kumar - 4 months ago 11
Android Question

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);
}

}
}

Answer

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 your 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);
}