Search code examples
androidimagebuttonkeyboard

Android - Change a button once keyboard opens


I have a custom on-screen back button and I want to make it change to "Hide Keyboard" once the user taps on the EditText of my screen. I found some good code from this question, regarding detecting if the keyboard is open or not (Check out Reuben Scratton's Old Answer for what I did). I'm at the step in the .java file:

 @Override
public void onSoftKeyboardShown(boolean isShowing) {
    // do whatever you need to do here
}

How would I enter code here for the back button to change it's image once the keyboard is open?

Here is the code for my back button currently:

    <ImageButton
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="Back"
        android:scaleType="fitStart"
        android:src="@drawable/back_bar" />

I want to change it to:

 <ImageButton
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="Back"
        android:scaleType="fitStart"
        android:src="@drawable/hide_keyboard" />

Here is an image of what I'm trying to do in case I don't make any sense!! image


Solution

  • You could use :

    ImageButton backButton = (ImageButton)findViewById(R.id.back); 
    backButton.setImageResource(R.drawable.hide_keyboard);
    

    in your onSoftKeyboardShown method