Changing image in ImageView on orientation change does not work - android

I have activity with android:configChanges="orientation|keyboardHidden"
I am trying to change image in ImageView when the device orientation changes with:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// refresh the instructions image
ImageView instructions = (ImageView) findViewById(R.id.img_instructions);
instructions.setImageResource(R.drawable.img_instructions);
}
This only works the first time when the phone is rotated but not after that.
Can someone pls tell me why this happens?

Peceps' own answer IMO is the correct one since it does not depend on giving different names to the drawables. It seems that the resource id is cached but the drawable is not, so we can supply the drawable to the ImageView instead of the resource id, making the solution a little more elegant (avoiding the try/catch block):
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// refresh the instructions image
ImageView instructions = (ImageView) findViewById(R.id.instructions);
instructions.setImageDrawable(
getResources().getDrawable(R.drawable.img_instructions));
}

I think you trying to do something like this
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// refresh the instructions image
ImageView instructions = (ImageView) findViewById(R.id.img_instructions);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
instructions.setImageResource(R.drawable.img_instructions_land);
} else {
instructions.setImageResource(R.drawable.img_instructions_port);
}
}

use this you can multiple time change orientation it's work fine
public class VersionActivity extends Activity {
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView) findViewById(R.id.imageView1);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
img.setImageResource(R.drawable.splash);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
img.setImageResource(R.drawable.icon);
}
}
}
for more information goto How to detect orientation change in layout in Android?

Thanks, both answers are correct and work if I am using different image name for portrait and landscape.
To use the same name this works:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// refresh the instructions image
ImageView instructions = (ImageView) findViewById(R.id.instructions);
// prevent caching
try {
instructions.setImageResource(0);
} catch (Throwable e) {
// ignore
}
instructions.setImageResource(R.drawable.img_instructions);
}

Related

do something on orientation change

I want to alter something, for example a variable value, when the user rotates the screen.
I know I can have the orientation using something like:
this
but I don't want to define if portrait do this, if landscape do this.
I want to just catch the current orientation and start doing the things I want.
You have to Override the onConfigurationChanged method in your activity and check the current orientation and do whatever you want like this
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Check orientation
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
DoSomethingForLandscapeOrientation();();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
DoSomethingForPortraitOrientation();
}
}
private void DoSomethingForLandscapeOrientation(){
Log.i("Method For","Landscape");
}
private void DoSomethingForPortraitOrientation(){
Log.i("Method For","Portrait");
}

Different widgets (views) for different screen orientations?

I'm quite new developing for Android so I have a very basic question.
Basically, I have two screen layouts, one for portrait and another for landscape orientation. I use the folders res/layout and res/layout-land. Both orientations are drawn fine, but I have different widgets (buttons) for each orientation, btnPortrait and btnLandscape.
The problem arises when I try to call onSetClickListener(). Because when the device is oriented in portrait the framework can't locate the btnLandscape and viceversa, I handle the orientation changes manually using onConfigurationChange(). It doesn't seem to work either.
My code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPortrait = (Button) findViewById(R.id.portraitButton);
tvPortrait = (TextView) findViewById(R.id.portraitText);
btnLandscape = (Button) findViewById(R.id.landscapeButton);
tvLandscape = (TextView) findViewById(R.id.landscapeText);
}
And the onConfigurationChange():
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
btnPortrait.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Portrait",Toast.LENGTH_SHORT).show();
}
});
}
else {
btnLandscape.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Landscape", Toast.LENGTH_SHORT).show();
}
});
}
}
None of the Toasts work.
Does somebody know what could be happening?
Thanks in advance.
Don't name them differently; the widgets for the same function should share the same ID whether it's in portrait or landscape.
If it's a widget that only exists or functions differently in landscape, just check if it's null after findViewById(), and if that returns null, you know you're not in a configuration with a layout that includes that button.
Just make sure that your button Id in layout-land is same Id in default layout
Update1 :
ok add not null check before using the widget like this
if( btnPortrait !=null ) {
// do what you want
}
I'm not sure by what you mean in the comments by "they're not the same button" but a way of doing what you want with two different resource ids would be as follows...
public class MyActivity extends Activity {
Button myButton = null;
TextView myTextView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.portraitButton);
if (myButton == null)
myButton = (Button) findViewById(R.id.landscapeButton);
myTextView = (TextView) findViewById(R.id.portraitText);
if (myTextView == null)
myTextView = (TextView) findViewById(R.id.landscapeText);
}
// The following method is your OnClickListener
public void sayHello(View v) {
switch (v.getId) {
case R.id.portraitButton :
// Do something for portrait
break;
case R.id.landscapeButton :
// Do something for landscape
break;
}
}
}
In your layout files you would then assign the OnClickListener using android:onClick as follows...
For portrait...
<Button
android:id="#+id/portraitButton"
...
android:onClick="sayHello" >
</Button>
For landscape...
<Button
android:id="#+id/landscapeButton"
...
android:onClick="sayHello" >
</Button>
In this way, the OnClickListener is set by the inflation process when setContentView(...) is called. It then simply comes down to it determining the resource id to decide what action needs to be taken.

Is there a way to have the layout change when the user flips device

I have code to set a different layout for portrait and landscape. Is there a way to change the layout while the activity is running, if the user flips the device?
Code to set layout
void SetView()
{
int w=getWindowManager().getDefaultDisplay().getWidth();
int h=getWindowManager().getDefaultDisplay().getHeight();
if (w>h)
setContentView(R.layout.mainHor);
else
setContentView(R.layout.mainVer);
}
Make two new folders in res called layout-land and layout-port. Put your landscape layout in layout-land and your portrait in layout-port. Make sure the xml files have the same name.
Also, as was mentioned in the comments, making just the layout-land folder is good enough as long as you also have a default xml for the Activity in the layout folder. If for any reason Android can't find the layout needed in the alternative layout-[orientation] folder, it will take what it finds in layout.
make two folder under res folder with name layout and layout-land. Put seprate layouts for portrait and landscape.
and then override onConfigurationChanged
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.homepage_screen);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.mainHor);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.mainVer);
}
}
Do this
#Override
public void onConfigurationChanged(Configuration configuration) {
super.onConfigurationChanged(configuration);
if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.landscape_layout);
} else if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.portrait_layout);
}
}

change layout of the activity at run time in android

i am developing one android application,there i need to change layout of activity at run time
i want to know about ,how to change a layout of an activity as view mode change from portrait to landscape or visa-verse
please anyone tell me what is feasible and efficient solution for above problem.
Thanks in Advance
Best approach is to have a folder for landscape orientation also, you will name it like
layout-land
you will put another main.xml inside that folder, android framework will automatically select that layout for landscape orientation.
More info here from android guide for supporting multiple screen
Code for detecting orientation change
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
SharedVariables.isScreenOrientPotrail=false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES){
SharedVariables.isScreenOrientPotrail=true;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.your_dyanmic_layoutid, null, true);
add rowview to your mainlayout.
i am pasting code here..try this..
public void onConfigurationChanged(Configuration orient)
{
super.onConfigurationChanged(orient);
// Checks the orientation of the screen
if (orient.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//code here for LANDSCAPE..
));
}
else if (orient.orientation == Configuration.ORIENTATION_PORTRAIT)
{
//code here for PORTRAIT ..
}
}
CAll Method;
#Override
public void onCreate(Bundle savedInstanceState)
{
onConfigurationChanged(getResources().getConfiguration());
}

Android some buttons do not show when turned to landscape

I wonder how can I display the same design when I turn the view to landscape view can anyone help me with this. I tried this code but it didn't do anything
the manifest
android:configChanges="keyboardHidden|orientation"
#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();
}
}
#Override
public void setContentView(int layoutResID)
{
super.setContentView(layoutResID);
}
Did you put the android:configChanges flag inside an Activity deceleration in your Manifest?
Try:
<activity android:name="myActivity" android:label="#string/app_name"
android:configChanges="orientation">
</activity>
If you don't want any layout change when your device rotates, this should suffice, and android will just recalculate and redraw the view again.
If you do want the layout to be changed though, you'll need to load a different layout on your onConfigurationChanged function:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
if (newConfig.orientation == android.content.res.Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.portarit_view);
}
else if (newConfig.orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.landscape_view);
}
}
BTW, you can have a decent preview of how it will look like in your layout xml "Graphical Layout" tab.

Categories

Resources