0

Sorry if it is sort of duplicate question with this one, I have tried the solutions provided and on some other posts too but none of them are working for me.

I have drawn the EditText but it is not clickable (no keyboard appeared after touch)

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
private LinearLayout lL;
private EditText editText;
@Override
public void surfaceCreated(SurfaceHolder holder){
    lL = new LinearLayout(getContext());

    editText = new EditText(getContext());

    editText.setFocusableInTouchMode(true);
    editText.setClickable(true);
    editText.requestFocus();

    editText.setText("Hello World");
    editText.setVisibility(View.VISIBLE);
    lL.addView(editText);
    lL.setDrawingCacheEnabled(true);
    lL.measure(canvas.getWidth(), canvas.getHeight());
    lL.layout(0, 0, canvas.getWidth(), canvas.getHeight());
    lL.bringChildToFront(editTextView);
}

@Override
public void draw(Canvas canvas){

    canvas.drawBitmap(lL.getDrawingCache(),50,50,null);
}
}

I have also tried this from the solution I saw: (using xml to create the EditText)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</RelativeLayout>

Change EditText to this and remove canvas.drawBitmap():

EditText editText = (EditText)findViewById(R.id.editText1);

but the app always force closed when I did this.

Main problem is how to make EditText clickable and show the soft keyboard like when you just put it from xml without drawing it on canvas at all

Community
  • 1
  • 1
Rawr
  • 71
  • 1
  • 3
  • 10
  • Do not instanciate the views in your on draw method!!! The on draw is always called when the view or a part of the view is invalidated. create your layout in the constructor of your custom view and add it. – Fabian Jun 25 '15 at 15:18
  • you might wann use this editText2= (EditText) findViewById(R.id.editText2); firstEText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { //todo: something } } }); – ASP Jun 25 '15 at 15:19
  • Do not initialize view in draw method and could you post your logcat stack trace? – RajSharma Jun 25 '15 at 15:24
  • Fabian @CodeProcessor thanks. edited. and my logcat stack trace only consists of SurfaceView returned and locking canvas. – Rawr Jun 25 '15 at 15:31
  • @ASP I've tried that and write System.out.println("ABC"); replacing //todo but nothing printed after I touched the EditText – Rawr Jun 25 '15 at 15:33
  • How about setting tag for the editText in the GamePanel1 for ex- edittext.setTag("edit"); and In the mainactovty you might be doing something like this... GamePanel1 gp=new GamePane1(); EditText et=gp.getViewByTag("edit"); et.setOnCLcikListener(); jsut try to do this one.. – RajSharma Jun 26 '15 at 00:17
  • how to do it? cannot resolve symbol 'getViewByTag' on gp.getViewByTag – Rawr Jun 26 '15 at 00:53

3 Answers3

1

If you want to use edit text as to open something like date picker dialog you can use set focusable to flase. Hence clicking on edit text will open dialog etc.

Example : android:focusable="flase"

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
Waqas Ahmed
  • 153
  • 11
0

You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="enter code here"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
 android:descendantFocusability="beforeDescendants"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</RelativeLayout>

May this one helpful ;)

How to make EditText not focused when creating Activity

Community
  • 1
  • 1
Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16
  • umm can you please explain what's the purpose of that? I set it just now and nothing changed. – Rawr Jun 25 '15 at 15:43
  • Edit Text not Taking Focus – Anil Meenugu Jun 25 '15 at 15:55
  • I've typed android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true" and tried "false" too but none of them are making any changes. I have no idea why – Rawr Jun 25 '15 at 16:05
0

By default, if you click edittext, keyboard will be visible unless you did something to change the behaviour. No need to set clickable,focusable in xml layout. Just create a edittext & use it.