-1

I'm writing an android view (Android 12).

I have a linearlayout with editText controls on it.

I want to change the linearlayout background image when the soft keyboard is out and change it again when the keyboard is hidden.

I have tried to set a focus listener on each editText, but it won't help.

How can I achieve this?

Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
  • @mikeyaworski That's not how Stackoverflow works. You don't ask for up votes. That's rude. – Ahmad Dec 22 '13 at 08:02
  • @Ahmad It's not rude... it's passing on reputation for good answers. And I thought both answers had worked because the OP accepted one. But then later commented that they didn't. If an answer works and you accept it, you should also upvote it. I'm not deleting my comment because you said it's rude; I'm deleting it because apparently the answers haven't worked yet. – Michael Yaworski Dec 22 '13 at 08:08
  • @Ahmad assuming it was you who downvoted my answer: the OP wasn't the one who upvoted it anyways. *That's not how stackoverflow works.* You don't downvote an answer for that reason. – Michael Yaworski Dec 22 '13 at 08:11
  • No, I didn't down vote it. – Ahmad Dec 22 '13 at 08:12
  • 2
    I downvoted it because I disagreed with your request for upvotes. I can choose to upvote and downvote whatever I choose. Cheers. –  Dec 22 '13 at 08:14
  • @nil Then you need to grow up. I thought they had worked for the OP. Whatever you want though. Cheers. – Michael Yaworski Dec 22 '13 at 08:15

2 Answers2

1

try this:

   final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
    Rect r = new Rect();
    //r will be populated with the coordinates of your view that area still visible.
    activityRootView.getWindowVisibleDisplayFrame(r);

    int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
    heightDiff = convertPixelsToDp(heightDiff , this);

    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
        ... do something here
    }
 }
}); 

more info in this link and this

for working in all device change heightDiff to dp, and work with that and for changing that use following method:

public static float convertPixelsToDp(float px, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;
}
Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
1

First, add an id to your layout:

android:id="@+id/view"

So for example:

<LinearLayout
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

Then use this code from this question to determine if the soft keyboard is visible. You should probably put this in your onCreate method.

final View root = findViewById(R.id.view);
root.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = root.getRootView().getHeight() - root.getHeight();
        if (heightDiff > 100) { // more than 100 pixels is probably a keyboard
            // keyboard is shown
            layout.setBackground(getResources().getDrawable(R.drawable.idOfPic));
        } else {
            // keyboard is not shown
            layout.setBackground(getResources().getDrawable(R.drawable.otherPic));
        }
    }
});

Note depending on your layout (speaking from my own experience), the if (heightDiff > 100) may have to change. It might be if (heightDiff > 150) or something else; the pixel height is arbitrary.

Unfortunately, there is no real way to determine if the soft keyboard is visible (ridiculous). This is the best way it can be done.

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • then how would I use this? if it varies from one device to another? (100 or 150) – Elad Benda2 Dec 22 '13 at 06:56
  • @user1065869 I'm not exactly sure how it works for different devices. In my experience, it's always seemed to work. Get it working on your device and it will probably always work (I think). – Michael Yaworski Dec 22 '13 at 06:58
  • @user1065869 then set it to `if (heightDiff > 49) {`. Another problem you may be running into id that you are setting the id on the wrong layout. For instance, if you have a LinearLayout inside a ScrollView, you might need to put the id of the view inside the ScrollView instead of the LinearLayout, or vice versa. – Michael Yaworski Dec 22 '13 at 07:51
  • but if i set it to `> 50`, it won't work the same on another device. no? and I have set the id of the highest element - which is linearlayout – Elad Benda2 Dec 22 '13 at 07:53
  • @user1065869 try setting it to `> 49`. If it works on your device, it will probably work on them all. You can do some tests on other people's phones or emulators if you want to confirm it though. It's all about the height *difference*, not the height. So it should work for all devices. – Michael Yaworski Dec 22 '13 at 07:55
  • it gives me `heightDiff == 50` when the keyboard comes out and also `50` when it's hidden – Elad Benda2 Dec 22 '13 at 08:49
  • @user1065869 That happened to me once before. Try putting the view id in a different layout (and also take it out of the one it's currently in). – Michael Yaworski Dec 22 '13 at 08:52