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());
}
Related
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");
}
In my app I have MainActivty which should always be viewed in protrait. If I turn the device to landscape I switch to TrendsActivity (to show some graphs). This is done by the folliwing code in MainActivty:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Intent intent = new Intent(this, TrendsActivity.class);
startActivity(intent);
}
}
Then when I switch back to portait i simply call this in TrendsActivity:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
finish();
}
}
This finishes the activity an automaticly moves back to MainActivity. So far so good. But if I press the back button while in TrendsActivity I come back to MainActivity in landscape mode, and I don't want that. I tried to call the following code in MainActivity:
#Override
protected void onRestart()
{
super.onRestart();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
This does not work, because now the onConfigurationChanged() if MainActivity is never called...
What to do?
Don't you think you might be achieving a much cleaner design by simply specifying an alternate xml for landscape mode and letting your MainActivity behave accordingly? Even without this, launching another activity on orientation change isn't going to be what most users would expect.
use this in
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
in onResume() of MainActivty
You can try to set your orientation in android manifest only, try below code
android:screenOrientation="portrait"
so exactly your code in manifest looks something like
<activity
android:name=".yourActivityName"
android:screenOrientation="portrait"
</activity>
You should add this to the AndroidManifest:
android:configChanges="orientation"
this way you tell Android to call onConfigurationChanged when there is a change in the orientation.
I solved this by changing the code in my MainActivity to just change the layout XML like fremmedehenvendelser suggested (Tusen takk!)
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
setContentView(R.layout.activity_trends);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.activity_main);
}
}
Next problem is to set the theme to make this layout show in fullscreen. The code which is commented out does not work... Before I had this in my manifest for TrendsActivity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
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.
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);
}
}
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);
}