Layout changes after turning the emulator, but not the device - android

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)

Related

Fullscreen mode on screen orientation change in jiaozi video player library

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.

Create specific layout for 7 inch tablet for portrait /landscape changing the layout

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.

Change Text on Landscape mode with Java Code and not XML (Android)

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**
}
}

weird kindle fire layout issue

I've written an android app with a view within an activity that relies on a callback to the view's wrapper's getHeight() function using an OnGlobalLayoutListener attached to the view's wrapper's viewTreeObserver to determine the amount of space it has to work with, during onCreate. Usually this is less than the 400px requested by chordDisplay in the xml below. This works perfectly in the android emulator, for a variety of screen sizes using android 2.1 and 4.03. However, on my kindle fire, the callback does not initially return the correct height when I launch the app in landscape (less than 400px are available) until I switch the orientation to portrait and back to landscape either manually or by code. This means the contents of my view aren't sized correctly initially.
sample logcat output when I initially launch the app in landscape:
04-22 17:31:28.249: D/onGlobalLayout(12979): chordDisplay.getHeight(): 400
then I switch to portrait and back to landscape:
04-22 17:32:44.546: D/onGlobalLayout(12979): chordDisplay.getHeight(): 350
I don't understand how this could be happening considering that all that happens during an orientation change is another call to onCreate() in the app, right? Which is the same thing that happens when I start the app.
Does anyone have any experience with similar orientation switching / layout bugs in their apps?/Have any ideas why this could be happening? Any help would be appreciated... Thanks.
code:
OnGlobalLayoutListener thelistener;
RelativeLayout chordDisplayWrapper;
TableRow.LayoutParams lp;
private ChordAdapter chordAdapter;
private HorizontalListView chordDisplay
thelistener = new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
chordDisplayWrapper.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.d("onGlobalLayout", "chordDisplay.getHeight(): " + chordDisplay.getHeight());
lp = new TableRow.LayoutParams(chordDisplay.getHeight()/6 -2,chordDisplay.getHeight()/6 -2); //6 just works. I shrink it a tiny bit to compensate for some weird bug.
chordAdapter = (new ChordAdapter(mcontext, R.layout.row, magicbundle, lp));
chordDisplay.setAdapter(chordAdapter);
}
};
chordDisplayWrapper.getViewTreeObserver().addOnGlobalLayoutListener(thelistener);
xml layout for views involved:
<RelativeLayout
android:id="#+id/chordDisplayWrapper"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center" >
<berry.chordsfree.HorizontalListView
android:id="#+id/testerList"
android:layout_width="wrap_content"
android:layout_height="400px"
android:layout_centerInParent="true"
android:gravity="center" />
</RelativeLayout>
You may have solved this by now, but posting for posterity for those that may come across this after -
I'm having the same problem on my Nexus One, running a custom 2.3.7 ROM, but the code works on my Nexus Galaxy on stock 4.1.1.
Similar to you, I'm trying to resize certain views during onCreate using ViewTreeObserver. I've tried manually calling dispatchGlobalLayout and have ascertained it isn't an issue with onGlobalLayout not firing. And I played around with moving elements into the activity's onWindowFocusChanged without success (but I want to avoid having much resizing done there). This has cost me more than a day of work so I'm just using a quick/dirty fix for now. It appears normally if I just tweak the layout/view in question, after onCreate finishes:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!reset) {
LinearLayout frameItem = (LinearLayout) findViewById(R.id.frameItem);
frameItem.setVisibility(LinearLayout.GONE);
frameItem.setVisibility(LinearLayout.VISIBLE);
reset = true;
}
}

How do I determine if an android device is naturally landscape or portrait?

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
}
}

Categories

Resources