make android app fullscreen in landscape mode only - android

Normally, to make my app fullscreen, inside the onCreate, before setContentView I do
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
But I have a situation where I need to enable fullscreen in landscape orientation -- but not in portrait. However, doing as below throws an exception concerning setContentView. So how do I do this correctly?
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
attachViewPager();
}else{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
detachViewPager();
}
}

try this maybe it will help you
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

Related

android activity hide too bar, full screen

I would like to explain what i am doing now. I developed an aar(sdk) and is running fine. However, i would like my sdk to occupy full screen when being called, even the caller has a toolbar. i tried the sample from this link
[How to set activity to fullscreen mode in Android?
However, if i put the code in android Manifest of the sdk, my app will crashed. If i do it when sdk activity OnCreate as below code. The toolbar still there.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_sdk_main);
May I know what do i need to do to hide the toolbar. Thanks.
If you want full screen mode for particular activity then use this
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
and for all activities Create a BaseActivity and write its onCreate method as shown above and extend this base activity for all other activities.
public void setFullScreenView() {//Hiding status/navigation bar
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
setContentView(R.layout.activity_splash_screen);
}
getSupportActionBar().hide();
works.
Additional explaination, my class is an extension of AppCompatActivity, below is the code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.d(SdkMainActivity.class, "onCreate");
getSupportActionBar().hide(); //---> this works
//requestWindowFeature(Window.FEATURE_NO_TITLE); //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); // ---> this the toolbar still appear
setContentView(R.layout.activity_sdk_main);
}

Change activity when orientation changes

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"

Change Screen Orientation programmatically using a Button

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

Android Fullscreen Not Working: Gingerbread

I am trying to make a fullscreen activity, but it doesn't seem to work in GingerBread, here is my code
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eyes);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
ActionBar actionBar = getActionBar();
actionBar.hide();
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD){
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
In Gingerbread there is still a title.
Does anyone know how to fix this?
First of all you should set the activity as full screen in the manifest.
In the desired activity, add this code
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
and your onCreate should be like this
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD){
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
ActionBar actionBar = getActionBar();
actionBar.hide();
}
This might solve the problem.
Remember that you must call requestWindowFeature before call setContentView
Try this to make a full screen activity by java:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
and in Manifest
android:theme="#android:style/Theme.Translucent.NoTitleBar"
requestWindowFeature has to be called before super.onCreate. Like so:
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD){
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eyes);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
ActionBar actionBar = getActionBar();
actionBar.hide();
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// ...
If this doesn't work, move the getWindow().setFlags(...) call to the top as well.
Unless you have a reason to not setting it in your manifest, add this attribute to your tag in the Manifest:
<application
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
.....
>
And I believe you wouldn't need the if-conditions code in the onCreate().
isn't just android:theme ="#android:style/Theme.NoTitleBar.Fullscreen" both hides the titlebar and the notification area?
it equals to:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
I learn this from https://github.com/pocorall/scaloid-apidemos/blob/master/src/main/java/com/example/android/apis/graphics/CameraPreview.java

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());
}

Categories

Resources