I am making a chatting application for Android. It has a list view to display chats, an EditText to enter text and button to send it. Whenever the activity launches, the keyboard automatically appears. But when I cancel the keyboard by pressing back button, the keyboard doesn't reappear on clicking the EditText. I,ve tried adding focusable
attribute to the edittext but it didn't work. Please tell me what I am doing wrong.
XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/main_activity"
>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="12dp"
android:clickable="true"
android:focusable="true"
app:backgroundTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_menu_send" />
<EditText
android:id="@+id/editText"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:ems="10"
android:hint="Type here"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textCapSentences"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/floatingActionButton3"
app:layout_constraintHorizontal_bias="0.472"
app:layout_constraintStart_toStartOf="parent" />
<ListView
android:id="@+id/messages"
android:layout_width="395dp"
android:layout_height="647dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/floatingActionButton3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
ActivityMain.java file:
public class MainActivity extends AppCompatActivity {
private static int SIGN_IN_REQUEST_CODE = 1;
private FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout mainActivity;
FloatingActionButton fab;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.sign_out_menu)
{
AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Snackbar.make(mainActivity,"Signed out successfully",Snackbar.LENGTH_SHORT).show();
finish();
}
});
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == SIGN_IN_REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
Snackbar.make(mainActivity,"Welcome " + FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
displayChatMessages();
}
else
{
Snackbar.make(mainActivity,"Login failed.",Snackbar.LENGTH_SHORT).show();
finish();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = findViewById(R.id.floatingActionButton3);
mainActivity = findViewById(R.id.main_activity);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = findViewById(R.id.editText);
FirebaseDatabase.getInstance().getReference().push().setValue(new ChatMessage(
FirebaseAuth.getInstance().getCurrentUser().getEmail(),
editText.getText().toString()
));
editText.setText("");
}
});
if(FirebaseAuth.getInstance().getCurrentUser() == null)
{
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(),SIGN_IN_REQUEST_CODE);
}
else
{
Snackbar.make(mainActivity,"Welcome back " + FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
displayChatMessages();
}
}
Query query = FirebaseDatabase.getInstance().getReference().child("chats");
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query,ChatMessage.class)
.setLayout(R.layout.list_item)
.build();
private void displayChatMessages()
{
ListView listView = findViewById(R.id.messages);
adapter = new FirebaseListAdapter<ChatMessage>(options) {
@Override
protected void populateView(@NonNull View v, @NonNull ChatMessage model, int position) {
TextView textView = v.findViewById(R.id.username);
TextView textView1 = v.findViewById(R.id.messageText);
TextView textView2 = v.findViewById(R.id.time);
textView.setText(model.getUsername());
textView1.setText(model.getMessageText());
textView2.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getTime()));
}
};
listView.setAdapter(adapter);
}
}
try this :
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
or,
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(edittext, 0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.