0

I am working on an app whereby the Imagebutton sources in a linear layout are not similar for portait and landscape mode. This made me set two drawable resource files, one to reference to landscape and the other for portrait inside the xml. However, in my code, i need to reference these drawable resource files too for my widget to respond to clicks. Is there a logic i can implement to reference my imagebuttons in portait and for landscape separately?

My layout for portrait mode (sw360-port)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/back_main"
        tools:context="com.example.app">
        <ImageView
            android:id="@+id/imageWallpaper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/audio_wallpaper"
            android:visibility="invisible"
            android:layout_marginTop="79dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
        <ImageButton
            android:id="@+id/playAudio"
            android:onClick="clickAudioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            android:background="@drawable/playbutton_style"
            android:layout_marginTop="28dp"
            android:layout_below="@+id/imageWallpaper"
            android:layout_centerHorizontal="true" />
        <LinearLayout
            android:id="@+id/linearView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:splitMotionEvents="false"
            android:orientation="horizontal"
            android:weightSum="1"
            android:layout_alignParentBottom="true">

            <ImageButton
                android:id="@+id/audioStreamButton"
                android:onClick="showAudio"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:scaleType="fitXY"
                android:background="@drawable/audio_style"/>

            <ImageButton
                android:id="@+id/eventStreamButton"
                android:onClick="showEvents"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:scaleType="fitXY"
                android:background="@drawable/event_active"/>

            <ImageButton
                android:id="@+id/videoStreamButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:scaleType="fitXY"
                android:background="@drawable/video_style" />

            <ImageButton
                android:id="@+id/moreStreamButton"
                android:onClick="showMore"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:scaleType="fitXY"
                android:background="@drawable/more_style"/>
        </LinearLayout>

</RelativeLayout>

My layout for landscape mode (sw360-land)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back_land"
    tools:context="com.example.app">

    <ImageView
        android:id="@+id/imageWallpaper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/wallpaper_land"
        android:visibility="invisible"
        android:paddingBottom="30dp"
        android:layout_marginTop="76dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="66dp"
        android:layout_marginEnd="66dp" />
    <ImageButton
        android:id="@+id/playAudio"
        android:onClick="clickAudioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="20dp"
        android:paddingLeft="70dp"
        android:background="@drawable/playbutton_style"
        android:layout_centerInParent="true"
        android:layout_below="@+id/imageWallpaper"/>
<LinearLayout
    android:id="@+id/linearView"
    android:orientation="vertical"
    android:weightSum="1"
    android:layout_width="match_parent"
    android:layout_height="280dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">
    <ImageButton
        android:id="@+id/audioStreamButton"
        android:onClick="showAudio"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:scaleType="fitXY"
        android:background="@drawable/audio_style2"/>

    <ImageButton
        android:id="@+id/eventStreamButton"
        android:onClick="showEvents"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:scaleType="fitXY"
        android:background="@drawable/eventsactive_land"/>

    <ImageButton
        android:id="@+id/videoStreamButton"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:scaleType="fitXY"
        android:background="@drawable/video_style2"/>

    <ImageButton
        android:id="@+id/moreStreamButton"
        android:onClick="showMore"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:scaleType="fitXY"
        android:background="@drawable/more_style2" />
</LinearLayout>

</RelativeLayout>

My code:

  public void showEvents(View view) {
       if(!eventButtonClicked ) {
           eventWindowButton.setImageResource(R.drawable.event_active);
           audioWindowButton.setImageResource(R.drawable.audio_style);
           moreWindowButton.setImageResource(R.drawable.more_style);
           listView.setVisibility(View.VISIBLE);
           imageViewAudio.setVisibility(View.INVISIBLE);
           playAudioButton.setVisibility(View.INVISIBLE);
           eventButtonClicked = true;
           audioButtonClicked = false;
       } else {
           eventButtonClicked = false;
       }
    }

    public void clickAudioButton(View view) {
        if (boundToAudioService) {
            boundToAudioService = false;
            playAudioButton.setImageResource(R.drawable.stopbutton_style);
          audioService.play();
        } else {
            playAudioButton.setImageResource(R.drawable.playbutton_style);
            audioService.pause();
            boundToAudioService = true;

        }
    }
 public void showAudio(View view) {
        if (!audioButtonClicked) {
            audioButtonClicked = true;
            eventButtonClicked = false;
            audioWindowButton.setImageResource(R.drawable.audio_active);
            eventWindowButton.setImageResource(R.drawable.event_style);
            moreWindowButton.setImageResource(R.drawable.more_style);
            listView.setVisibility(View.INVISIBLE);
            imageViewAudio.setVisibility(View.VISIBLE);
            playAudioButton.setVisibility(View.VISIBLE);

        } else {
            audioButtonClicked = false;
        }

        }

As you can see, i need to set Image resource for my landscape layout e.g

eventWindowButton.setImageResource(R.drawable.event_active2);  videoWindowButton.setImageResource(R.drawable.video_active2);

please help me

Benfight
  • 73
  • 10
  • Refer to these checks https://stackoverflow.com/questions/3674933/find-out-if-android-device-is-portrait-or-landscape-for-normal-usage and add your code in particular – Yousef khan Jul 13 '17 at 08:10

2 Answers2

0

Like you created the specific layout for the sw360-portand sw360-land you can create drawables for these versions and place them inside the drawable-sw360dp-port and drawable-sw360dp-land folders

Jameido
  • 1,344
  • 1
  • 11
  • 21
0

You can use onConfigurationChanged to know about the current orientation of the device and based on that you can do what you want to do.

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
ANUJ GUPTA
  • 905
  • 13
  • 22