1

Using the following layouts and views below, I cannot get the EditText et_messageText box to show up in my display properly. If I run this in the emulator, I am able to click in the area and get a "null" selection cursor, so I know the EditText is there, but the keyboard never shows, and I am not able to get input to work. What am I doing wrong?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

        <variable
            name="viewModel"
            type="com.example.project.model.MyViewModel" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.project.MainActivity">

        <com.example.project.view.ChannelHeaderView
            android:id="@+id/channelHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" />

        <com.example.project.view.MessageInputView
            android:id="@+id/message_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</layout>

message_input.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <data>
        <variable
            name="viewModel"
            type="com.example.project.model.MyViewMOdel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center_vertical"
        android:orientation="horizontal">


        <ImageView
            android:id="@+id/iv_send"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="13dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:srcCompat="@drawable/stream_ic_send" />

        <EditText
            android:id="@+id/et_messageText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="112dp"
            android:ems="10"
            android:inputType="text"
            android:text="@={viewModel.inputText}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/iv_send"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MessageInputView.java

public class MessageInputView extends RelativeLayout {

    private MessageInputBinding binding;
    private MyViewModel viewModel;
    public MessageInputView(Context context) {
        super(context);
        binding = initBinding(context);
    }
    public MessageInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        binding = initBinding(context);
        // Focus Listening
        binding.etMessageText.setOnFocusChangeListener((v, hasFocus) -> {

            if (hasFocus) {
                binding.etMessageText.setTextIsSelectable(true);
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(v,InputMethodManager.SHOW_FORCED);
            }
        });
    }

    private MessageInputBinding initBinding(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        binding = MessageInputBinding.inflate(inflater, this, true);
        binding.etMessageText.clearFocus();
        return binding;
    }

    public void setViewModel(MyViewMOdel model, LifecycleOwner lifecycleOwner) {
        this.viewModel = model;
        binding.setLifecycleOwner(lifecycleOwner);
        binding.setViewModel(viewModel);
    }
}

and finally in the onCreate() of MainActivity.java

binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setLifecycleOwner(this);

viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
viewModel.setPreferences(this);
binding.setViewModel(viewModel);
binding.channelHeader.setViewModel(viewModel, this);
binding.messageInput.setViewModel(viewModel, this);
Simson
  • 3,373
  • 2
  • 24
  • 38
Derek
  • 11,715
  • 32
  • 127
  • 228
  • Furthermore, It would appear the setOnFocusChangeListener() never actually fires. I cannot get the debugger to stop on a breakpoint at if(hasFocus) – Derek Feb 17 '20 at 04:53
  • maybe the constant layout doesn’t think the markup is what you think? Try change size 100x100. – PeDuCKA Feb 17 '20 at 07:56

1 Answers1

0

Why are you extending your MessageInputView class with RelativeLayout? I guess that your MessageInputView class is a Fragment so extend it with Fragment and use the default LayoutInflater inflater and ViewGroup container.

Hence your:

private MessageInputBinding initBinding(Context context) {
    LayoutInflater inflater = LayoutInflater.from(context);
    binding = MessageInputBinding.inflate(inflater, this, true);
    binding.etMessageText.clearFocus();
    return binding;
}

Should be like this:

private MessageInputBinding initBinding(LayoutInflater inflater, ViewGroup container) {
    binding = DataBindingUtil.inflate(inflater, R.layout.message_input, container, false);
    binding.etMessageText.clearFocus();
    return binding;
}
Aman Gupta
  • 211
  • 2
  • 7
  • If there was a problem with the inflater, I would not see the ImageView or the "Empty" EditText, correct? – Derek Feb 17 '20 at 15:09
  • True. Try to remove `binding.etMessageText.clearFocus();` this line from your code. Maybe it helps – Aman Gupta Feb 18 '20 at 07:24