Search code examples
javaandroidkeyboardtextview

Letting the user press an image to open the keyboard and set the text of a TextView


My problem is that I want to be able to press an image which will open the standard Android keyboard and let the user enter some text. I then want to put this text and set it as the text of a textView.

Here is my xml for the imageView and textView

  <ImageView
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="135dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:clickable="true"
                android:onClick="addDescription"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/descriptionText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.19"
                android:text="Click to add description" />

Here is the method I tried to use to get the image to open the keyboard, however this causes the app to crash when pressing the image.

public void openKeyboard(){
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}

Solution

  • The TextView class is not made to allow user editing. From the makers of Android: A TextView is a complete text editor, however the basic class is configured to not allow editing; see EditText for a subclass that configures the text view for editing.

    The only one brief kind of editing you can enable in a textView is to allow the copy of it's contents (i.e., your user will be able to copy textView's contents).