I think this is implementable since screen rotation behaviour can go up to the application level.
Yes it is implementable!
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ActivityInfo
http://developer.android.com/reference/android/content/pm/ActivityInfo.html
Refer the
link:
Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);
buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html
Yes, you can set the screen orientation programatically anytime you want using:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
for landscape and portrait mode respectively. The setRequestedOrientation() method is available for the Activity class, so it can be used inside your Activity.
And this is how you can get the current screen orientation and set it adequatly depending on its current state:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
final int orientation = display.getOrientation();
// OR: orientation = getRequestedOrientation(); // inside an Activity
// set the screen orientation on button click
Button btn = (Button) findViewById(R.id.yourbutton);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch(orientation) {
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
}
});
Taken from here: http://techblogon.com/android-screen-orientation-change-rotation-example/
EDIT
Also, you can get the screen orientation using the Configuration:
Activity.getResources().getConfiguration().orientation
Wherever possible, please don't use SCREEN_ORIENTATION_LANDSCAPE or SCREEN_ORIENTATION_PORTRAIT. Instead use:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
These allow the user to orient the device to either landscape orientation, or either portrait orientation, respectively. If you've ever had to play a game with a charging cable being driven into your stomach, then you know exactly why having both orientations available is important to the user.
Note: For phones, at least several that I've checked, it only allows the "right side up" portrait mode, however, SENSOR_PORTRAIT works properly on tablets.
Note: this feature was introduced in API Level 9, so if you must support 8 or lower (not likely at this point), then instead use:
setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
Use this to set the orientation of the screen:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
and don't forget to add this to your manifest:
android:configChanges = "orientation"
A working code:
private void changeScreenOrientation() {
int orientation = yourActivityName.this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
showMediaDescription();
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
hideMediaDescription();
}
if (Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}, 4000);
}
}
call this method in your button click
Android-Kotlin:
to make rotate X:
binding.btnRotateHorizontal.setOnClickListener {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
to make rotate Y:
binding.btnRotateVertical.setOnClickListener {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
Yes ، Hariharan answer works fine . but you should add below line to AndroidManifest.xml in activity tag :
android:screenOrientation="fullSensor"
android:configChanges="orientation|screenSize"
if not add above line , Hariharan answer not work .
Thanks Benny !
mine worked with:
setLandscapeIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setLandscapeIcon.setVisibility(View.GONE);
setPortraitIcon.setVisibility(View.VISIBLE);
}
});
setPortraitIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
setLandscapeIcon.setVisibility(View.VISIBLE);
setPortraitIcon.setVisibility(View.GONE);
}
});
In Manifest
android:configChanges = "orientation|screenSize"
android:screenOrientation="portrait"
NOTE: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); was not working on my device
Related
I tried using search but didn't find a solution for this problem.
I have a camera application with fixed landscape orientation. An orientationEventListener properly rotates all my buttons. There are, however, some buttons that shall show a dialog or alertdialog and these dialogs are not rotated. Here is the relevant shortened code in MainActivity
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(CameraActivity.this);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.layout_dialog_mode);
// set up texts in layout_dialog_mode
and
private void createMenu() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] modeS = {getString(R.string.mode_1), getString(R.string.mode_2), getString(R.string.mode_3), getString(R.string.mode_4), getString(R.string.mode_5)};
builder.setTitle(getString(R.string.mode_title));
builder.setSingleChoiceItems(modeS, mode, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
// set things as chosen by user
}
dialog.dismiss(); /* close menu after selection */
}
});
builder.show();
}
layout_dialog_mode is a linearLayout containing TextViews and ImageViews.
The rotation of buttons works like this:
private void initialiseOEListener() {
oEListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
#Override
public void onOrientationChanged(int orientation) {
try {
if (orientation == ORIENTATION_UNKNOWN) return;
orientation = (orientation + 45) / 90 * 90;
switch (orientation) {
// set params and orientation
}
ImageButton ibu = (ImageButton) findViewById(R.id.button0);
ibu.setRotation(orientation);
ibu = (ImageButton) findViewById(R.id.button1);
ibu.setRotation(orientation);
// and so on
} catch (Exception e) {
}
}
};
Now my question: Is it possible to rotate the dialogs in same manner as the buttons? At the moment, they are displayed in landscape since I set device orientation for this Activity to landscape. When user for example rotates the phone to portrait and clicks button1, dialog will be shown wrong because it is not rotated.
Many thanks
Ok so I resolved the issue by creating separate Activity for each dialog. The problem is now, when the new foreground dialog Activity is rotated, the background Activity will rotate as well! Can I somehow set the background Activity orientation when I rotate foreground Activity?
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 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());
}
If the user is putting data in edittext fields and orientation get changed, the onConfigChange method is called and new layout is populated,with empty edittext fields, i want to retain the data in edittext fields entered by user befor orientation change.
Any kind of help will highly appreciated.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int orientation_default = getResources().getConfiguration().orientation;
switch (orientation_default) {
case Configuration.ORIENTATION_PORTRAIT: {
setContentView(R.layout.registration);
break;
}
case Configuration.ORIENTATION_LANDSCAPE: {
setContentView(R.layout.registration_horizontal);
break;
}
}
findViewById(R.id.backtohomepage).setOnClickListener(this);
findViewById(R.id.backtologinpage).setOnClickListener(this);
findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
findViewById(R.id.termsandconditions_id).setOnClickListener(this);
findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.registration);
findViewById(R.id.termsandconditions_id).setOnClickListener(this);
findViewById(R.id.backtohomepage).setOnClickListener(this);
findViewById(R.id.backtologinpage).setOnClickListener(this);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.registration_horizontal);
}
findViewById(R.id.backtologinpage).setOnClickListener(this);
findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
findViewById(R.id.termsandconditions_id).setOnClickListener(this);
findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
};
Put android:configChanges="orientation" for that activity node in the manifest.
EDIT:
Check this question : Activity restart on rotation Android
<activity
android:name="com.Dashboard"
android:configChanges="keyboardHidden|orientation"
>
</activity>
paste this in your manifest...
http://developer.android.com/guide/topics/resources/runtime-changes.html .. go through this link..