Tobias Lukoschek
Tobias Lukoschek

Reputation: 405

ImageButton is not visible after other layout is set to visible on real device

I have an ImageButton which will be moved after it was clicked (onClickListener is implemented) and an LinearLayout which will be set to visible when the ImageButton clicked (also in its onClick method).

Strange behaviour: On my virtual device it works fine but on the real device the ImageButton is not visible again after the LinearLayout is set to visible. I also set the layout delayed (with a second) to visible and the ImageButton was moved correctly but as I said it is not visible again (also when I set it explicitly on VISIBLE) when the LinearLayout was set to visible)

Any ideas how to fix this problem?

The onClickListener implementation (I know hardcoded values are unresponsive but the App works only for one specific device):

private void setUpMenuBar() {
    final LinearLayout menuLayout = (LinearLayout) findViewById(R.id.menu_layout);

    menuLayout.setVisibility(View.INVISIBLE);
    final int menuLayoutWidth = menuLayout.getWidth();
    final ImageButton openMenuBtn = (ImageButton) findViewById(R.id.btn_open_menu);
    openMenuBtn.setOnClickListener(new View.OnClickListener() {
        boolean menuVisible = false;
        @Override
        public void onClick(View v) {
            Log.d("onClick", "menu visible: " + menuVisible + ", Rigth: " + openMenuBtn.getRight() + ", layoutWidth: " + menuLayoutWidth);
            if(menuVisible) {
                openMenuBtn.setRight(1920);
                openMenuBtn.setLeft(1720);
                menuLayout.setVisibility(View.INVISIBLE);
                menuVisible = false;
            }
            else {
                openMenuBtn.setRight(1520);
                openMenuBtn.setLeft(1320);
                menuLayout.setVisibility(View.VISIBLE);
                menuVisible = true;

            }
            Log.d("onClick", "menu visible: " + menuVisible + ", Rigth: " + openMenuBtn.getRight() + ", left: " + openMenuBtn.getLeft());
        }
    });
}

And the XML layout:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:src="@mipmap/arrow"
    android:background="@null"
    android:id="@+id/btn_open_menu"
    />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/menu_layout"
    android:orientation="vertical"
    android:background="#000000"
    android:layout_gravity="right">
    <ImageButton
        android:layout_width="200dp"
        android:layout_height="20dp"
        android:layout_gravity="right"
        android:layout_weight="33"
        android:layout_marginBottom="2dp"
        android:src="@mipmap/zahnrad"
        android:background="@drawable/btn_menu_settings"/>
    <ImageButton
        android:layout_width="200dp"
        android:layout_height="20dp"
        android:layout_gravity="right"
        android:layout_weight="33"
        android:layout_marginBottom="2dp"
        android:src="@mipmap/schraubendreher"
        android:background="@drawable/btn_menu_settings"/>
    <ImageButton
        android:layout_width="200dp"
        android:layout_height="20dp"
        android:layout_gravity="right"
        android:layout_weight="34"
        android:src="@mipmap/fragezeichen"
        android:background="@drawable/btn_menu_settings_roundet"/>
</LinearLayout>

Upvotes: 0

Views: 72

Answers (2)

mfaisalhyder
mfaisalhyder

Reputation: 2284

as you said it works on virtual device but not on Real one.

You have set params of setLeft() and setRight() to huge values of 1920 and 1720 respectively. This might be pushing the ImageButton out of the screen of your real device depending upon pixels of the display your device packs.

Try with smaller values of paramaters to those methods and see, does it draw the views on display.

Upvotes: 1

Sabish.M
Sabish.M

Reputation: 2060

Get Display Height and width using

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int height = displaymetrics.heightPixels;

then calculate setRight value and setLeft value from width and height

Upvotes: 0

Related Questions