Reputation: 111
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.
Upvotes: 0
Views: 1199
Reputation: 21733
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" />
Upvotes: 0
Reputation: 10938
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
Upvotes: 1