Disable landscape mode for a whole application - java

Is the there a way to set a landscape mode to the whole application, not by adding android:screenOrientation="portrait" to every activity in AndroidManifest?

Here's the only thing I can think of. Write a class that extends Activity and put the following in that class:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Then, instead of extending Activity in your other classes, extend the new class.

One programmatic way of doing this, that I can think of, is to create a super class that extends activity and extend all your classes from there.
Have the below setting in the super class in a protected method and call super.xxx() to initiate this:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In case you what a specific activity in a different way you can simply override this.
[I have not tried this yet, but by the knowledge of OOP this works]

You could try putting that attribute in the node of your manifest. I don't know if that is supported though. And if not Im afraid putting it in each of your is going to be the next easiest way.
You might be able to achieve it by making yourself a CustomActivity that extends activity and sets the window flags to be Portrait in the onCreate. Then with all of your other activities you could extend your CustomActivity instead of plain Activity.

Related

How to extend an activity from the MainActivity class file?

The UI elements in the MainActivity class need to be preserved and the other class file adds a new UI element to the main xml layout that's used by both classes to differing degrees.
It's understood that you can create a base abstract class and two concrete inherited classes however in this case there a third supporting class for the second (other class) and it requires a handler to function.
For perspective, a button (in activity_main) is clicked and it should launch an activity while maintaining the UI elements used MainActivity. Furthermore the button has it's own class file methods and isn't in MainActivity.
What happens now? The button is pressed and nothing happens. Manifest confirmed so its not that. Or I allow the main activity or the other activity and it works, both need to work simultaneously.
Basically MainActivity needs to act as the base abstract activity for the separate class file.
You are messing up activities and views. To reuse the same business logic, you can write common logic in base class for all other activities (i.e. class BaseActivity extends Activity). To reuse different UI parts you should either use fragments or you can use <include>/<merge> tags pair to include a certain layout into another layout.

When is Activity.onBackPressed called in Android? (in relation to other callback functions)

When is Activity.onBackPressed called in my Android application? I'm not looking for the obvious answer of when the user presses the back button. I wan't the answer in relation to other "callback" functions.
Is is possible to be called during the execution of another function within the Activity class?
What is the case if I have my Activity class implement some typical interfaces used for your typical game? For example GLSurfaceView.Rendered? I'm having the feeling onBackPressed is called during GlSurfaceView.Renderer.onDrawFrame but I'm not 100 % sure yet. Even if this isn't the case, I want to know how it works. (It seems difficult to find this kind of simple information anywhere.)
Finally, below is a code example for the layout of my Activity class. The question is, however, not limited to this particular setup.
class MainActivity extends Activity implements Renderer {
onCreate(...) {
layout = new FrameLayout(this);
GLSurfaceHolder glsurface = new GLSurfaceHolder(this, this);
glsurface.setRenderer(this);
layout.addView(glsurface);
setContentView(layout);
GLSurfaceHolder is just a simple dummy class that extends GLSurfaceView. It has the onTouchEvent overloaded and simple passes the data over to the MainActivity class. (The design philosophy in this very, very simple app is just to focus all the sensory and other data to one place and then "make things happen"..)
onbackpressed will be called when you pressed back button. Default behaviour will be destroying the activity. To avoid override the onbackkeypressed or onkeypressed.

Best practise for displaying multiple controls which require an extended class?

If I want to display a MapView inside my Activity I would have to extend my Class from MapActivity. If I want to display a Tabbed interface I would have to extend my Class from TabActivity. Similar is the case with some other controls which require user class to extend from a specific class.
Let's say inside my Activity I want to display both a MapView for displaying Google Map and a TabView to display some other data. I can't do it directly because Java doesn't support multiple inheritance. My question is how can I achieve this scenario? How can I display multiple controls inside my activity where each require your class to extend from a specific class. Is it possible first of all? If yes, what are the best practises to achieve this scenario?
Update I want to achieve this
I am using Map and Tab for sake of an example. I would like to know how you can tackle this scenario in general?
In this case it's simple: You can use the MapActivity within the TabActivity (it's designed to manage activities as tabs).
As general approach I always prefer to use views and nest them in an activity. I never use such things like ListActivity. They should make things easier but often look like a bad design decision to me. I never faced the fact that I had to combine two activities (expect TabActivity).
You can take a look at this question. It seems that activities never meant to be used that way. I think the situation which you describe is the reason why fragments where introduced.
You could build it via object composition. Initially I am not sure how to get the Activity started and add it to the layout, but then I found out about LocalActivityManager which allow you to embed other Activity as your view. Note that this class is deprecated since API Level 11. In any case here are the steps to embed other Activity that require extension as a View:
Create a LocalActivityManager to enable creation of Activity within Activity
Start the activity that you want to embed and get the View via getDecorView()
Add the View in your layout
The following is my test code that I tried within my Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create local activity manager so that I could start my activity
LocalActivityManager localActivityManager = new LocalActivityManager(this, true);
// dispatch the onCreate from this manager
localActivityManager.dispatchCreate(savedInstanceState);
// layout to hold the activity, optionally this could be set through XML file
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
this.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
// start the activity which is in this example is an extension of a TabActivity
Intent tabIntent = new Intent(this, DummyTabActivity.class);
Window tabWindow = localActivityManager.startActivity("tabView", tabIntent);
View tabView = tabWindow.getDecorView();
// start the activity that extends MapActivity
Intent mapIntent = new Intent(this, DummyMapView.class);
Window mapViewWindow = localActivityManager.startActivity("mapView", mapIntent);
View mapView = mapViewWindow.getDecorView();
// dispatch resume to the Activities
localActivityManager.dispatchResume();
// add to the tabView, optionally you could use other layout as well
layout.addView(tabView);
// add to the mapView, optionally you could use other layout as well
layout.addView(mapView);
}
My limited experiments show that object composition via the above method will achieve what you are trying to do. Having said that, I am not sure how common this approach is. Without your question, I wouldn't probably look for the above method, but your use case is interesting and might be applicable for future use. I will look into Fragment and see if I could do the same with it and update my answer if it is applicable.

android: own class extending activity and/or listactivity

I have a couple of things which are the same in all my Activities throughout my application, e.g. an optionsmenu and some code which needs to run onresume, onrestart and onpause. I figured it would be a smart approach to put them in my a class MyListActivity extends ListActivity and then have all my activities extending MyListActivity.
This worked out just fine until I created an activity which didn't have a ListView. the App crashes because ListActivity expects a ListView. However, this new activity does not need a ListView, but would still need all my functions / Overrides in MyListActivity .
Right now I can think of two solutions. One: add a dummy listview to the layout with visibility = false, height & width = 0 (haven't tried this, but i guess it should work). And Two: copy/paste the contents of the MyListActivity class into a MyActivity extends Activity class. I feel very silly doing this, but I don't have any other ideas on how to solve this issue.
Any ideas on how to handle this nicer?
Thanks
I think you can implement all the features in an Activity subclass (e.g., MyActivity) and make the MyListActivity class a subclass of the MyActivity class.
Other approach is to make a helper class which contains all the features in static methods with an Activity object as the first argument. In this case you don't need to create MyActivity or MyListActivity classes, but you need to call methods of the helper class in every Activity subclass you want to inherit these features.

extend 2 classes problem android

i'm a junior programmer. I have a base class that extends activity, i use this class to set up the menu in my app. All my activity classes extends this base class. Now, i have a new class which extend ExpandableListActivity this class's purpose is to show an expandable list and of course when the user is viewing this activity the menu becomes unavailable. So how can i do to be able to use the menu while viewing the expandable list. I know that it's impossible to extend more than one class in Java, so what's the trick?
Thanks in advance
You can use delegation. Extract all methods related to Menu in separate MenuDelegate class.
Your new class will extend ExpandableListActivity, and delegate all methods related with Menu to MenuDelegate. It's useful solution if your Menu logic is simple.
In this case, your other activities will be free, and will be able to extend ListActivity, TabActivity or something else..
http://en.wikipedia.org/wiki/Delegation_pattern
You don't have to extend ExpandableListActivity to have an ExpandableList in your layout. You can extend your BaseActivity and handle the ExpandableList on your own. The ExpandableListActivity is just a helper class.

Categories