I wrote my layout entirely in java code because it was just more convenient. (I had a lot of textViews and using for statements were more convenient).
However, my TextViews require the day of the week and in portrait mode, I would like to cut the day of the week to a short form. For example, I want "Sunday" to show "Sun" for portrait mode, but "Sunday" for landscape mode.
I understand how to do this in XML files, but how do I do it in code?
I.e. sample code:
LinearLayout parent = new LinearLayout(getApplicationContext());
parent.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
TextView example = new TextView;
example.setLayoutParams(mparams);
example.setText("Sunday"); //<--make this "Sun" when in portrait but "Sunday" in landscape
parent.addView(example);
If your application is getting restarted when it moves to landscape then you can define the strings that need to be expanded in Landscape mode in values-land
And it is never a good idea to use hard coded strings. Even if you are creating layouts programmatically use strings.xml to store strings
You can override the onConfigurationChanged() method in your Activity class, and you can set your text, when orientation will change this method will call... Hope it will helpful to you..:)
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
// Here you can set text **Sun**
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Here you can set text **Sunday**
}
}
Related
I need to show 2 columns in RecyclerView when the orientation is portrait and 4 columns when orientation is land. It works correct on the emulator.
It's RecyclerView in portrait orientation.
Turning the emulator:
As you can see, there is refresh button. And after clicking on it we see 4 columns on screen:
But when I run the project on my mobile phone, there is no refresh button and I see 2 columns in land orientation.
Here's a part of my code:
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
previewImages.setLayoutManager(new GridLayoutManager(this, 4));
}
else {
previewImages.setLayoutManager(new GridLayoutManager(this, 2));
}
}
And I added this line to manifest:
android:configChanges="orientation"
So, what's the problem and how can I solve it?
can you change your code block in onConfigurationChanged method like this. maybe can help.
new GridLayoutManager(context, 1, GridLayoutManager.HORIZONTAL, false)
I am using jiaozi video player library for playing video in my android app. Everything is working fine. Now i am facing an issue, on screen orientation change from portrait to landscape the video should be fullscreen, has anyone done this, if yes then pls share me the code or snippet so that i can implement the same.
Examplifying the code I got from here: https://stackoverflow.com/a/5726776/2232127
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
yourVideoPlayer.setFullscreen(true);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
yourVideoPlayer.setFullscreen(false);
}
}
I don't actually know your video player library, so the setFullscreen method probably doesn't exist. Try to find the equivalent for that.
I'm developing the typical tablet app with fragments: a list of elements on the left side and the content on the right side. For the smartphone version I show only one fragment with the list of elements and for the tablets I show 2 fragments with the list and contents.
In order to handle the screen rotation, I'm handling the rotations on my app. When the user rotates the screen and I'm showing an image or some text on the right side, the I show it fullscreen:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged");
if(MainFragmentActivity.isTablet)
{
FragmentManager fm = getSupportFragmentManager();
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE && !frameTabs.isShown()) //Show again the tabs fragment
{
Utils.goNormalScreen(this);
frameTabs.setVisibility(View.VISIBLE);
}
else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT && (fm.findFragmentByTag(Utils.SHOW_IMAGE_TAG) != null || fm.findFragmentByTag(Utils.SHOW_TEXT_TAG) != null
|| fm.findFragmentByTag(Utils.SHOW_WEB_TAG) != null)) //Hide tabs fragment
{
Log.i(TAG, "Detected showing image");
Utils.goFullScreen(this);
frameTabs.setVisibility(View.GONE);
}
}
}
The problem is the 7 inch tablet:on portrait mode I want to show the smartphone version (1 fragment with a list) and on landscape the tablet version with 2 fragments.
So in the onConfigurationChanged() I need to detect the 7 inch tablet and recreate the activity in order to change the layout, isn't it? how to do that? is there any other better way to make it work?
Should I recreate the activity getting all the data again from the server (ListView) or is it better to save the data on a bundle and reuse it when rotated?
First, stop handling configuration changes, then put the 2 fragment layout in layout-sw600dp-land and leave the others as they are. Now Android will determine to use that layout automatically upon rotation.
I'm developing an Android app that uses Internationalization.
So i have the folders values-language (e.g. values-en, values-br) in my solution. The internationalization is working well.
The problem is when i combine it with orientation elements.
I have also one xml file for landscape and other for portrait. I have the folders layout-orientation (e.g layout-port). The orientation is working well too. I accomplish this by overhiding the following method:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
But i'm having problems with the string values (labels). When i change the orientation, all the string values loose data and appers as ids. For example, I have a button that has a label "SAVE". When i rotate my phone, the label of the button changes to "#23232324" which is the number of the resource.
What can I do? I've tried to create portrait and landscape folders for each language that I have but didn't work (e.g values-br-port, values-br-land).
I need to keep both working, orientation and internationalization. I need to have two different layouts based on the orientation and also different languages.
BTW, I got it solved!!
The problem was that i was not calling the method setLocale before reloading the view.
So i had to do,
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLocale();
setContentView(R.layout.main);
}
And the method setLocale:
public void setLocale(){
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
How do I determine whether or not the accelerometer would report a roll of zero when the bottom long side of the screen is facing the ground?
I need to do this without instructing the user to hold the phone in a certain position. I am hoping to be able to do something like:
Context.getResources().getConfiguration().getNaturalOrientation == Orientation.LANDSCAPE
IMPORTANT: The above line of code is not possible, it's just an example of what I would like to do.
I found the answer for this. It's very simple using API8 and higher, simply use Display.getRotation()
this may help you to detect the configuration change
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//onConfigurationChanged
if (newConfig.equals(Configuration.ORIENTATION_LANDSCAPE)) {
//ORIENTATION_LANDSCAPE
} else if (newConfig.equals(Configuration.ORIENTATION_PORTRAIT)) {
//ORIENTATION_PORTRAIT
}
}