I have this video view. It has a fullscreen button as a controller. The auto rotation works normally at first, but whenever the fullscreen button is clicked, the screen rotate to be landscape, and when I try to auto rotate, it doesn't auto rotate to portrait.
Here is a screenshot of my video view and controller.
Here is my code for fullscreen button onClickEvent
mFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//To fullscreen
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mFullScreen.setImageResource(R.drawable.fullscreen);
toolbar.setVisibility(View.VISIBLE);
updateMetadata(false);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getSupportActionBar().hide();
mFullScreen.setImageResource(R.drawable.normal_screen);
toolbar.setVisibility(View.INVISIBLE);
updateMetadata(true);
}
}
});
Here is my onConfiguration changed method
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getSupportActionBar().show();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
getSupportActionBar().hide();
toolbar.setVisibility(View.INVISIBLE);
try {
channelNameText.setVisibility(View.GONE);
channelImage.setVisibility(View.GONE);
} catch (NullPointerException ne) {
ne.printStackTrace();
}
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
updateMetadata(false);
} else {
Logger.e("Orientation", "ORIENTATION_POTRAIT");
try {
channelNameText.setVisibility(View.VISIBLE);
channelImage.setVisibility(View.VISIBLE);
} catch (NullPointerException ne) {
ne.printStackTrace();
}
mFullScreen.setImageResource(R.drawable.fullscreen);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
updateMetadata(true);
}
}
The onConfiguration method is called but only when we press the fullscreen button. How do I enable auto rotation after setRequestedOrientation?
If you want to change your activity’s orientation mode at any time as per your requirement, then below is the java code for doing the same. As we discuss all the Android screen orientation modes above, we can set any of the mode at any time from the java code. Below is the example for both landscape and portrait mode. But you can set all available modes as the same way. put the below lines of code anywhere in your activity java file, where you want to change the screen orientation of your activity. For example if want to set a particular orientation before showing the screen, then you can add the code in onCreate() function of your activity.
For Landscape mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
For Portrait Mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Note: While adding this code, don’t forget to import the package
import android.content.pm.ActivityInfo; in to your code.
Sometime, you want to get the screen orientation of the current screen programmatically at runtime . To get the same you can try the below java code in your activity.java file.
//Get current screen orientation
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
switch(orientation) {
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
Note: Don’t forget to import below packages for the above code.
import android.view.Display;
import android.content.res.Configuration;
import android.view.WindowManager;
Related
I am working on a Tuner app in Android Studio.
I want to have a button that displays "Start Tuning" & when you click it, the ToggleButton above (which has the image of a mic on & mic off) switches from the Muted Mic image to the Unmuted to show that the app is "listening".
As of right now, I have a button that onClick changes the text view from off to on, however there is no way to go back from on to off, and so far I have found no way to make the mic "button" change as well. Is there a way to do this? Here is the Java code I have so far:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beginTuning = findViewById(R.id.toggleButton);
mic = findViewById(R.id.muteButton);
mic_status = findViewById(R.id.micState);
mic_status.setText("Mic off");
tester = findViewById(R.id.test_button);
tester.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (tester.isPressed() && mic.isChecked()) {
mic_status.setText("on");
} else {
mic_status.setText("off");
}
}
});
you have stored mute status in variable 'mic' so when volume is turned on' set mute-check-status to 'false': You just need to add statements marked 1.. and 2.. to your code;
public void onClick(View view) {
if (tester.isPressed() && mic.isChecked()) {
mic_status.setText("on");
mic.setChecked(false); // 1..add just this line so 'else' part of condition will execute next time
} else {
mic_status.setText("off");
mic.setChecked(ture); // 2..also set mute checked..
}
}
});
I'm totally new to android and I got my first task which is supposed to be simple, so the task is that I should add a password toggle to the password input field so that it can be shown/hidden when pressed and that was easy I only added this line in the TextInputLayout and it worked:
app:passwordToggleEnabled="true"
Now the thing is when I rotate the phone I lose the state of the toggle, so if I'm showing the password and I rotated the phone by default it hides the text, I was reading about Bundle savedInstanceState but I can't reference the default password toggle from inside the java class
You should use savedInstanceState for the best practice. But you can also set configChanges to your activity. Your activity won't re-create again when rotate.
android:configChanges="orientation|screenLayout|screenSize|navigation"
You can also handle screen rotate event in your activity.
#Override
public void onConfigurationChanged(#NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// portrait
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// landscape
}
}
I'm allowing all possible orientation for my app both for portrait and landscape (small-normal-large-xlarge) but after testing it on a small screen, I just didn't like how it appears so, what I'm trying to do is disable landscape for small layouts. Is there a way to do this ?
All I found was changes to do on manifest file but I believe that by reconfiguring the manifest I will apply the changes to all layouts.
The easiest way is to put this in the onCreate() method of all your Activities (better yet, put it in a BaseActivity class and extend all your Activities from it)
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
if (isLargeDevice(getBaseContext())) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
} else {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
You can use this method to detect if the device is a phone or tablet:
private boolean isLargeDevice(Context context) {
int screenLayout = context.getResources().getConfiguration().screenLayout;
screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
switch (screenLayout) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
return false;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
return true;
default:
return false;
}
}
Check this link you can check the type of device and set orientation as needed
Android: allow portrait and landscape for tablets, but force portrait on phone?
You can programatically handle runtime configuration changes like that
in your manifest:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
in your activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
///check the screen size and change it to potrait
}
}
or check this answer to see how to check the screen size and change it
for example 480 screen size devices:Apply in oncreate method:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
if(width==480){
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
I have a ListActivity which I want to show when the phone's orientation is portrait and I have another normal Activity which I want to show when the user rotates the phone to landscape mode.
Some code:
public class ListActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_PORTRAIT) {
//Do stuff
}else if (this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_LANDSCAPE) {
Intent myIntent = new Intent(getApplicationContext(), AnotherActivity.class);
startActivity(myIntent);
}
}
}
This approach works, but has some problems. Is there a more correct way? The problem with this method is that when you rotate your phone and press the back button, the screen just becomes black since the phone is not rotated. So should I implement the rotation in another way, or modify the back button in some way so that the user cant return to the previous Activity?
It is a normal way. But it is long - 5 sec of waiting.
There is another way:
register orientation change as a configuration change
A.
activity android:name=".SomeActivity"
android:label="#string/app_name"
android:configChanges="orientation"
And process it as a config change.
B.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
SetupActivity();
}
How do you detect that the user has changed the orientation of the display screen from portrait to landscape and vice versa?
The Android application must adjust to the new orientation. The system should be sending a change of orientation message to all open applications so that they can adjust. I could not find anything about this in the docs? Sorry if i copy these statement from somewhere else but i'm kinna new ina android/java, can someone guide me along?
Its just one of the Preference screen in multiple tabs i have, but i just want mere change of screen orientation when the phone position changes...
public class Tab2Activity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.tab2);
}
}
Android will automatically recreate (destroy and then create) your activity when the orientation changes. If you want a special layout for landscape, you can create a special resource file in the layout-land folder, which will be loaded after the activity is recreated.
On the other hand, if you want to override the default behavior so that you can do something custom when the orientation changes, you can override the onConfigurationChanged method like so:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//do something if portrait...
}
}
http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged%28android.content.res.Configuration%29
EDIT: in code (add this to your Activity):
#Override
public void onConfigurationChanged(Configuration _newConfig){
super.onConfigurationChanged(_newConfig);
int height = getWindowManager().getDefaultDisplay().getHeight();
int width = getWindowManager().getDefaultDisplay().getWidth();
if(width > height){
// landscape
}else{
// portrait
}
}