sadab
sadab

Reputation: 59

changing orientation of emulator does not change background

I have an activity that contains a background image, But I have to change the image when orientation changes to landscape. for that i have add onConfigurationchange(). But it does not work. the image does not change. pls help me

SamActivity.java

package com.example.samworkshop;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.widget.Button;
import android.widget.RelativeLayout;

    public class SamActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sam);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.sam, menu);
            return true;
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            RelativeLayout layout =(RelativeLayout)findViewById(R.id.sam);
            // Checks the orientation of the screen
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                            layout.setBackgroundResource(R.drawable.sam_back_land);
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
                layout.setBackgroundResource(R.drawable.sam_back);
            }
        }
    }

activity_sam.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sam"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/sam_back"
    tools:context=".SamActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="75dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="39dp"
        android:background="@drawable/buttonsel" />

</RelativeLayout>

Upvotes: 0

Views: 372

Answers (4)

Carnage
Carnage

Reputation: 117

Give Same Name to the both Background Images and put the image you wanted as portrait into regular drawable folder and the image you wanted as landscape into a new folder named "drawable-land". No need for onConfigurationChanged method. I hope this will work. Sorry for poor english.

Upvotes: 0

Jitender Dev
Jitender Dev

Reputation: 6925

Debugged your code and your method onConfigurationChanged() is not getting called. Because you not Specified android:configChanges to your activity inside your manifest file, which handles one or more configuration changes that the activity will handle itself.

Then i add

  android:configChanges="keyboardHidden|orientation|screenSize"

inside your Manifest file under your activity Tag and it worked like a charm :)

like this

 <activity
            android:name="SamActivity "
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Upvotes: 0

If your image will be static (1 image for portrait and 1 for landscape) you shouldnt do that in java, instead create two layouts one for landscape layout-land/activity_sam.xml and one for portrait layout/activity_sam.xml each specifies its own image and own layout parameter, also check your manifest if it disables orientation change.

Upvotes: 1

Afaq Baig
Afaq Baig

Reputation: 25

If you hav android phone then ckeck out on it. Some times emulator dosnt work with rotation.

Upvotes: 0

Related Questions