Set different constraints for each orientation in Android Studio - java

The question sounds way harder than it is. Simply all I want to do is to be able to set a different layout for, I already added a landscape orientation, and recreated the whole design the way I desire in landscape. However, the app restarts each time I rotate the phone, if I add android:configChanges="orientation" in manifest, it does save the state but not the way I set in landscape. The app is 4 layouts in each orientation. So if I could just make different constraints for landscape the doesn't interrupt the original portrait it could solve my problem.
sorry for the poor explanation. It's my first week with Android.

You are correct in designing different layouts for different orientations.
For the state, you should override the onSaveInstanceState() method in Java.
Using that, you can save the state of the application and then inflate the layout the way you like in Java.

Related

What's the most efficient way to change the overall theme of the app?

So I got an idea for an app that would change visual theme based on user's selection, something like how a Sub-Reddit would have the option for users to switch between themes. In this case, I would utilize at least 4 themes, each theme changing the color of certain views, such as the background, buttons, image, and etc. I would like to know the the best approach to this. Do I need to keep a list of views that would be affected or something?
I've tried keeping different button backgrounds with different color since setting background color programmatically would reset the background shape back to the default design, but I'm afraid that it will cause the app to be bloated with numerous files. I've tried using the color filter to change the views.
Color id still keeps the filter applied to it, causing it be unusable if user switch to different color, then back.
Hopefully, this question was directed at native development. If so, then you should take a look at the guidelines. You can create your themes in XML and reference them in your layout XML files.
Personally, I would store the theme as a SharePreference somewhere and then when laying out each Activity/Fragment, utilize the user's saved theme.

How to handle different initialized variables in layout and layout-landscape for android activity.java file?

Situation:
In an Android Studio project in app/src/main/res you have the folders layout, and layout-land (landscape version of layout). Each of these has the folders has a file named activity_quiz.xml in them.
You also have QuizActivity.java that uses activity_quiz.xml.
Layout has 4 variables that it initializes within its xml file.
Layout-land has 3 variables that it initializes within its xml file. Layout-land does not have one of the variables that layout has. (You might not always want to use the exact same variables in portrait vs landscape)
QuizActivity.java sets listeners on all of the variables. However, because layout-land does not have a variable that is initialized in layout, the app will crash due to QuizActivity setting listeners on all of the variables (including the ones that are now not initialized).
In this situation, what is the best way to handle this? Should you have an if statement to check which type of layout the app is in, before setting listeners on each variable? If you do this, how do you check for what layout the app is in?
Or should you initialize all the variables through overriding onSaveInstanceState(Bundle)? (Or initialize the variables somewhere else?) -- This seems like bad coding practice.
I know some work arounds, but I wanted to know what would be considered the best coding standard to implement. Any help is appreciated!
Extra question: If for some reason the QuizActivity.java implementation for landscape mode is significantly different than portrait mode, what would be best in this situation? Keep all the code in one QuizActivity.java file with an if statement separating them, or separate activity files for each landscape and portrait?
Should you have an if statement to check which type of layout the app
is in, before setting listeners on each variable? If you do this, how
do you check for what layout the app is in?
Yes, that's a valid approach. Referencing this question, you could use the following snippet to check the orientation:
if(getResources().getConfiguration().orientation) == Configuration.ORIENTATION_LANDSCAPE){
//In landscape
}else if(getResources().getConfiguration().orientation) == Configuration.ORIENTATION_PORTRAIT){
//In portrait
}
Using this check in your onCreate method allows you to do different variable initialization depending on your screen orientation.
If for some reason the QuizActivity.java implementation for landscape
mode is significantly different than portrait mode, what would be best
in this situation? Keep all the code in one QuizActivity.java file
with an if statement separating them, or separate activity files for
each landscape and portrait?
I would say that separating the logic with an if/else statement would do. If your code tends to differentiate too much when toggling between landscape and portrait, I'd say that your design is flawed. Imo, only the graphical interface (your xml layout) should adapt to fit the screen when the orientation change, the user would be confused if there would be drastic changes to application behaviour just because of the orientation change.
You can use an if-statement and check the orientation in this way:
if (getResources().getConfiguration().orientation == Confirguration.ORIENTATION_LANDSCAPE){
...
} else {
// it is ORIENTATION_PORTRAIT
}
http://developer.android.com/reference/android/content/res/Configuration.html

managing Persistent Visual Data (event driven buttons and ImageViews) throughout Activities of an application

I need some advice for those who are experienced making Android applications. What I would really like to have, for my application's appearance: at the top, a title-bar which is a ImageView (content is a png), and at the bottom a series of custom buttons which make up a tab-bar like thing. In between the title and the tab-bar is the Content, which may be anything... (most likely buttons)
I have been doing this by making a RelativeLayout which specifies LeftMargin and UpperMargin for x,y coordinates--
Currently all of my activities are inheriting a custom MyActivity class, which rebuilds the title and the tab-bar at the time of onCreate. This seems bad to me!
PART1)
---A solution to Persistent data
Since the "tab-bar" and the title are persistent no matter what screen you're on during this application's run-time, it makes the most sense to store them somewhere... How should I do this? Make a singleton object that the Activity's ask for?
I thought a little about the singleton object, and I'm not even sure what I would store, since the Views that are on displayed during Activity A have activity A as context, and not Activity B.
PART2)
---Animation Aesthetics
I would really like to have the "Content" (the view in the middle between title and tabbar) slide out to the left, and the new content slide in from the right. I.e, I'd like the tab-bar and the title to remain fixed while the "activities" change. is this at all possible? What should I do to achieve it?
one idea I had, was to make all of the program in one activity! I would create an animation for the Custom View in the middle, and I would override the "back" button to navigate correctly to the previous Custom View. Is that a horrible idea?
Anyone have any advice?
Read http://developer.android.com/design. Most of the design principles can be applied to apps that run on legacy releases; it's not just limited to Honeycomb and Ice Cream Sandwich. Do consider the Action Bar and Dashboard design patterns.
I don't really recommend using just one Activity -- generally, an Activity should be a separate, encapsulated, pretty well defined chunk of functionality that can execute independently of other Activities.
To avoid duplication of your UI, consider reusing XML layouts.
To avoid duplication of your logic, consider using Fragments. You should be able to mix and match them in your activities.
To achieve the animation you describe, consider implementing a ViewPager.
Using the ActionBarCompat sample app and Android Support Library, you can enjoy modern goodies like Action Bar, fragments, tabs, and horizontal sliding transitions on devices running Android all the way back to Donut (1.6).

How can I save state between orientations?

In a previous question I asked how I could display two different layouts for portrait and landscape orientations:
Problem switching to landscape orientation
Now I would like to know how I can save the state of my application before it goes into another orientation? I seem to lose this data any time orientation changes.
This kind of quesiton has been answered before.
But there are also other ways not listed in the previous answer. For example using the onRestoreInstanceState() method, as explained here. This method is the advised way of storing state between configuration changes, and was poorly documented until recently.

Animation support for Android SDK 1.5?

I'm trying to add some animations to my application. I've essentially got a few menu screens, that all eventually lead to the main application that is a surface view. I want to add some nice animations between screen like fading in and out between screens. What's the easiest way to do this that is supported by SDK1.5 and above (I want to target most users)?
I'm confused by what is and isn't supported in SDK1.5. My belief at the moment is that animations between different activities is not supported in 1.5 but animations in things like ViewFlipper are. It seems the easiest way is to set up a ViewFlipper, put each of my screens in that, set the animation settings and then use this to get nice transitions.
Also, is there a way to override the "no animations" setting that can be found in the phone's main settings screen under display? I'm making a game, so presentation is important, so I want to be sure that whatever I use will cause an animation regardless of this global setting.
My belief at the moment is that
animations between different
activities is not supported in 1.5 but
animations in things like ViewFlipper
are.
Correct.
It seems the easiest way is to set up
a ViewFlipper, put each of my screens
in that, set the animation settings
and then use this to get nice
transitions.
Either that or just directly apply AlphaAnimation and kin to your Views.
is there a way to override the "no
animations" setting that can be found
in the phone's main settings screen
under display?
No, but bear in mind that is only for inter-activity animations.
I'm making a game, so presentation is
important, so I want to be sure that
whatever I use will cause an animation
regardless of this global setting.
Then do not rely upon global settings. Which, in this case, means do not rely upon inter-activity animations.

Categories

Resources