I have a FragmentActivity which loads a RelativeLayout with a ListView above an EditText and a Button. I cannot seem to get the keyboard to show up in any method. When I touch it or set focus to it, nothing happens. When I automatically request focus, nothing happens. How do I get the keyboard to show up? Isn't this supposed to be automatic? I'd like to simulate GTALK's chat input.
activity_contact_chat.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/contact_detail_container" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:orientation="horizontal" > <Button android:id="@+id/details_record_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignTop="@+id/chat_message_input" android:text="Send" /> <EditText android:id="@+id/chat_message_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/details_record_button" android:ems="10" android:gravity="top|left" android:hint="@string/message_hint" android:lines="1" android:maxLines="10" android:paddingRight="10dp" android:paddingTop="10dp" android:scrollbars="vertical" > </EditText> <ListView android:id="@+id/messages_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/chat_message_input" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#000000" > </ListView> </RelativeLayout>
ContactChatFragment:
import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class ContactChatFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{ ListView messageList; Long contact_id; String contact_UUID, contact_name; private static final int CONTACT_CHAT_LOADER = 0x11; MessageListAdapter adapter; private static final String STATE_ACTIVATED_POSITION = "activated_position"; private int mActivatedPosition = ListView.INVALID_POSITION; public static final String EXTRA_CONTACTID = "item_id"; String contact_rowID; @Override public void onCreate(Bundle savedInstances) { super.onCreate(savedInstances); if (getArguments().containsKey(EXTRA_CONTACTID)) { contact_rowID = getArguments().getString(EXTRA_CONTACTID); //Let's get the contact String projection[] = { DBHelper.CONTACTS_ROWID, DBHelper.CONTACTS_UUID, DBHelper.CONTACTS_NAME, DBHelper.CONTACTS_PUBLIC_KEY }; Cursor contactCursor = getActivity().getContentResolver().query(Uri.withAppendedPath(ContactProvider.CONTENT_URI, String.valueOf(contact_rowID)),projection, null, null, null); if (contactCursor.moveToFirst()) { contact_id = contactCursor.getLong(contactCursor.getColumnIndex(DBHelper.CONTACTS_ROWID)); contact_UUID = contactCursor.getString(contactCursor.getColumnIndex(DBHelper.CONTACTS_UUID)); contact_name = contactCursor.getString(contactCursor.getColumnIndex(DBHelper.CONTACTS_NAME)); } } adapter = new MessageListAdapter(getActivity(), null, 0); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_contact_chat, container, false); messageList = (ListView) rootView.findViewById(R.id.messages_list); messageList.setAdapter(adapter); getLoaderManager().initLoader(CONTACT_CHAT_LOADER, null, this); /* TextView empty = new TextView(getActivity()); empty = (TextView) messageList.getEmptyView(); empty.setText(R.string.chat_no_messages); empty.setVisibility(View.GONE); ((ViewGroup) messageList.getParent()).addView(empty); messageList.setEmptyView(empty); */ EditText message_input_text = (EditText) rootView.findViewById(R.id.chat_message_input); message_input_text.clearFocus(); Button message_input_button = (Button) rootView.findViewById(R.id.details_record_button); message_input_button.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getActivity(), "Hi!",Toast.LENGTH_SHORT).show(); } }); return rootView; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mActivatedPosition != ListView.INVALID_POSITION) { // Serialize and persist the activated item position. outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition); } } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String[] projection = {DBHelper.MESSAGES_ROWID, DBHelper.MESSAGES_CONTENT, DBHelper.MESSAGES_CONTENT_LOCATION, DBHelper.MESSAGES_LOCATION, DBHelper.MESSAGES_READ_DATE, DBHelper.MESSAGES_UUID_FROM, DBHelper.MESSAGES_UUID_TO}; CursorLoader cursorLoader = new CursorLoader(getActivity(), MessageProvider.CONTENT_URI, projection, null, null, null); return cursorLoader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { adapter.swapCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { adapter.swapCursor(null); } }
Just remove
android:descendantFocusability="blocksDescendants"
in main RelativeLayout. You will get the focus in the Edittext.BlockDescendants will prevent the child layouts from getting focus. Check http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability
You can use either of the methods to show the soft keyboard when the Activity gets focus,
You can add folowing code to the EditText in the Layout xml,
android:focusable="true" android:focusableInTouchMode="true"
Also, you can set it in the code as,
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
or,
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.showSoftInput(edittext, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With