Search code examples
androidmenuandroid-edittextandroid-softkeyboardandroid-keypad

How could i do if User focussed on Edittext, menus should be displayed?


I have referred this link !

My needs : If i click or focussed on EditText, it should call Menus . In Menus, i wanna create two options. That is, normal writing and keyboard writing.

If user selects keyboard writing, it looks like Android Keyboard. Should be able to type it from keyboard. And next one, normal writing. This is like normal.

In Menus, if user selects normal writing then one screen should appear when it is clicked like MS-Paint application . With pencil , user should be able to write like we'll almost write in a paint application.

I am new to Android and learning though !

In my code, where should i do all the Functionalities of Menus ?

How could i do this ? Any helps or suggestion greatly appreciated !

And this is my Code :

else if(q.trim().equals("A"))
    {
fillUp=new EditText(context);
fillUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
     }
});
fillUp.setBackgroundResource(R.drawable.option);
fillUp.setId(9);
fillUp.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu arg0, View arg1,ContextMenuInfo arg2) {
}
});
fillUp.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
Toast.makeText(context, "Focused", Toast.LENGTH_LONG).show();
if (hasFocus) {
context.getWindow();
context.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        });
 Rl=new RelativeLayout.LayoutParams(weigth/4,height/15); 
        Rl.addRule(RelativeLayout.BELOW,1);
        Rl.addRule(RelativeLayout.CENTER_HORIZONTAL,1); 
        fillUp.setLayoutParams(Rl);
        fillUp.setGravity(Gravity.CENTER); 
        fillUp.setId(9);
        compLayout.addView(fillUp);

My XML Code :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/icon"
 android:icon="@drawable/keyboard" />
<item android:id="@+id/text"
android:title="Text" />
<item android:id="@+id/icontext"
android:title="Icon and text"
android:icon="@drawable/writing" />
</menu>

Solution

  • To block appearence of virtual keyboard you can use .setInputType(InputType.TYPE_NULL). To show menu MainActivity.this.openOptionsMenu(), like here:

    EditText editText = (EditText) findViewById(R.id.a);
                    //block kayboard
                editText.setInputType(InputType.TYPE_NULL);
                editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    

    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus == true) {
                                            //show menu
                            MainActivity.this.openOptionsMenu();
                        } else {
                            MainActivity.this.closeOptionsMenu();
                        }
                    }
                });