0

I'm facing a strange problem. When I add an edittext using the following piece of code:

edit = new EditText(context);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
edit.layout(0, 0, (int)searchPane[W], (int)searchPane[H]);
edit.setBackgroundColor(Color.TRANSPARENT);
edit.setGravity(Gravity.CENTER_VERTICAL);
edit.addTextChangedListener(SearchBox.this);
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);

and draw it using edit.draw(Canvas) in the onDraw method of View. I'm calling edit.requestFocus() in onTouchMethod though it does not show the input keyboard on focus request. Any ideas how to make edittext accept text and show input keyboard?

Thanks in advance.

Oleg Bondarenko
  • 101
  • 2
  • 11
  • "and draw it using edit.draw(Canvas) in the onDraw method of View." huh? Is the EditText measured and laid out inside the view, or just drawn? – FunkTheMonk Jan 03 '14 at 11:16
  • What does it matters? – Oleg Bondarenko Jan 03 '14 at 19:19
  • Did you figure this out and have an answer to post? It's ok to post your own answer if one of the others don't help you enough that you want to accept them, but it's good to accept an answer so other people with the same problem find it – Nick Cardoso Mar 22 '16 at 12:17

2 Answers2

1

Your EditText isn't part of the View hierarchy as you've never added it as a child to your view group; as such it will not receive touch events (among other things) unless you implement that in your view group.

Add a call to addView(edit) and move edit.layout(0, 0, (int)searchPane[W], (int)searchPane[H]) into onLayout.

It probably won't be as simple as that, creating custom views is an advanced undertaking - are you sure what you're trying to do isn't possible using one (or combination) of the existing view layouts?

If not, you should read http://developer.android.com/training/custom-views/index.html

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
0

The only combination which worked for me :

edit.requestFocus()
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, InputMethodManager.SHOW_FORCED);

Paired with adding input mode to the activity

<activity android:name="..."
              android:windowSoftInputMode="adjustResize" />
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124