How can I add Landscape (Rotation) in all activity in one shot? - java

I've one major issue. I built one chat app from scratch using Firebase, Java and Android, which contains many activities and class. Now suddenly I found that I forgot to set UI for Landscape mode (which is like default tablet mode). I opened my app and rotate to landscape, and UI looks very bad, even some part is not visible. I'm actually planning to publish on play store just for learning purpose. So is there any easy way to do this?
Should I restrict, so that user not able to rotate the screen for every activity like below is mation in either java or xml through?
In XML:
<activity android:name=".SomeActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
In Java:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
I also don't know that how can I do this for all activity? Should I have to crate duplicate activities for all activity? and how can I do this? where can I attach? I really don't have any idea about this. Please help me.

Studio generates layout for you just adjust the generated layout as per need and that layout will be used automatically
Should I restrict, so that user not able to rotate the screen for
every activity like below is mation in either java or xml through?
You shouldn't restrict user to one orientation, If design specs of you layout is ideal for landscape then go for it
I also don't know that how can I do this for all activity? Should I
have to crate duplicate activities for all activity? and how can I do
this? where can I attach? I really don't have any idea about this.
Please help me.
With manifest solution, yes, you have to do this for every activity entry in manifest file.
either do it via manifest or via xml.
To set orientation of all activities, you can create a base activity and extend all activities from it where your base activity set the orientation. e.g
// no need to set orientation in manifest
class BaseActivity extends AppCompactActivity{
oncreate(...){
super.oncreate(..);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
class YourOtherActivities extends BaseActivity{
oncreate(...){
super.oncreate(..);
setContentView(..);
}
}

Related

For supporting different screen size why should i use fragment in android instead of activity

I am beginner to android..I am started new android project..for supporting
different screen size..in fragment documentation they given to use fragment..but
why cant i use activity in android..if i use activity or fragment..which i should i use in this both..please dont give link of activity or fragment..please anyone answer me..i dont know which to use?...i want about all documentation they given about activity and fragment but i dint understand which to use..below is the link i read about fragment..if i use activity i should do more codings?
https://developer.android.com/guide/components/fragments.html
In fact you can't use a Fragment alone, Fragments are inside the Activity.
One point of using the Fragments for supporting different screen sizes is the ability to implement some views like a "Master/Detail" view.
A Fragment, as its name says, is a part of a bigger controller "the Activity", its reference can be removed and it's cleaner than having a big massive Activity to handle all the states of a view.
So the use case is completely depends on your project and its User Interface. I'd be glad to help you if you give me more information about your project and its design.
I think you will need at least one activity. And then for better handling different device rotations and screen sizes you can use one or more fragments inside this activity.
I try to explain this with an example:
You want to create a nice music player app which should look nice in portrait and landscape mode.
You split your app up into three fragments:
Here you can see how the app looks in portrait mode. The activity shows two fragments: The first fragment only consists of a listview. There the song titles are listed. On the bottom you can see the second fragment, which displays the song title of the current playing song and got a button for pausing the music.
When your user uses the music player app on a tablet in landscape mode you have more space for displaying stuff. Then the activity shows the list fragment (which also gets displayed in portrait mode) and it shows a third fragment which shows detailed information about the current playing song (e.g. the album image) and a progress bar.
By using fragments you only need to write the code for the list once.
Sweet and Simple thing, What i recommend is always use Fragments,
But for Fragment you will require Activity.
Take it in this way , Activity is a Canvas on which you can put any number of Fragments.
Whatever your UI is always use Fragment present on a activity if you want to show one screen even then also, So that you will always have Flexibilty to use all those cool things which fragments provides,maybe in future or in current.
If you use activity it has limits,FOR EXAMPLE, LIKE in INSTAGRAM AT BOTTOM, It has FIVE OPTIONS, Suppose THOSE OPTIONS ARE ON A ACTIVITY AND BY CLICKING ON THEM YOU CAN SWITCH TO DIFFERENT FRAGMENTS.
For more info:
Here is the most accepted answer for this topic.

Android Development, Google Tutorial

I am following the Google tutorial for building your first android application. I got to the point where I needed to implement the actionbar actions with the functions openSearch() and openSettings().
I implemented all of this in the MainActivity.java file.
My question is this:
In the example app you can type a message and then send it and it displays it in a second activity. In the second activity, the top action bar changes and does not display my Search icon or perform the action when the settings button is clicked. In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
My other somewhat related curiosity is this. The method openSettings() is called when I click the 3 vertical dots and then settings. These 3 vertical dots show up on every activity, and settings is always in the list. However clicking settings obviously doesn't perform the call to openSettings() when in the DisplayMessageActivity and not MainActivity. How is it that settings and the vertical dots are carried over?
Second to last, how can I add other selections to the drop down list from the options/vertical dots in the action bar? Settings is always there although it responds differently in each activity which was my first question. But I would like to add certain things to the options menu that are on all activities, and some things that are unique to some activities. I assume there must be a better way than repeating switch statements and methods in every Activity.java file.
And finally, what is the best practice to implement an action bar over multiple activities?
Obviously different activities will often have different icons/actions in the action bar, however some things like the 3 vertical dots(options) and settings within that would obviously be acceptable to have in every Activity, while it would be nice to add other things to the options list I don't see why settings should ever change across activities. Yet as I stated before the method is not called in DisplayMessageActivity unless I repeat the code in DisplayMessageActivity.java that I had added to MainActivity.java. I'm confused as to where I can add these so that they are displayed on all activities without repeating code. And I'm confused as to how the actionbar's options/vertical dots are carried over to all activities while others require the repeating of code in each activities' java file that I want them to show up in.
I know this was a bit of a long winded quesiton, I will clarify if necessary. I'm just a bit confused. I was able to make it through the tutorial fine as I have a decent understanding of java. However google's guide isn't written that well and the Android environment is very confusing to a beginner.
I do understand how things work to a degree, I just want to ensure that I'm actually doing it in a way that when my app grows in complexity it won't be a mess of unnecessarily repeated statements and methods.
Thanks in advance for any assistance and tips.
In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
That is certainly one solution, but as you obviously know, it's not a very good one. There are at least two alternative solutions:
Create a MenuActivity class which implements all the logic for common menu items and then extend this class from all of your activities, rather than extending the standard Activity class.
Use fragments to implement your UI. Fragments are similar to activities in that they create UI elements from an XML layout. One difference is that they live inside a "host activity". In this particular case, the host activity will provide the common menu functionality and each fragment can customize it further depending on your needs.
How is it that settings and the vertical dots are carried over?
Most likely your DisplayMessageActivity overrides onCreateOptionsMenu() and inflates a menu XML layout which was created by Android Studio (or Eclipse?) when you created the activity class.

How to avoid titlebar of activity loading before actual activity loads and make it load with the activity load using java code?

.
I am doing reverse-engineering of applications.While reverse-engineering an app i needed to change the titles of each activities in an app that i got .I have a problem regarding activity titlebar.
Actually the developers of this app have set many of the titles from androidManifest.xml file using
<activity android:name="com.example.dmo.MainActivity" android:label="first"></Activity>
I changed the title of each activity by putting setTitle(" say abc"); in onCreate() of each activity and title is successfully changed, But problem is that while loading activity it shows the titlebar with the title from androidManifest.xml file i.e. android:label="first".
This title appears for some miliseconds (time increases if activity takes more tome to load), and then when my activity loads completely, title changes to the text i passed in setTitle() in onCreate().
This is somewhat similar to my problem.
I am not able to find solution since i have a strict restrictions that i can not make changes in any of the .xml fiels in an applicaion. I only allowed to change the java code and add some new java code if necessary.
I searched a lot but all solutions i found suggested changes in xml files; and some other solutions those suggested using code did not worked.
So is there any solution that does not require manual hard code xml changes directly. Changing xml using code is acceptable but not hard code changes like editing their original manifest file to :`<activity android:name="com.example.dmo.MainActivity" android:label="say abc"></Activity>`
Any AspectJ solution is also acceptable(i.e. some pointcut to some method that can do the work for me), if someone knows for the same.
I also want to know that, which class or api is responsible to load title bar before activity loads? ,so that if possible i can make changes in that .
Any help would be very appreciable . .Thank u in advance . .
Then the answer is you can't. This is the Application theme that is shown and it can be changed only in manifest. The screen you see is the Activity screen in state when the Application is not yet loaded and before any code in Activity onCrete() is executed.
Did you try apktool? You should be able to get editable manifest using it.

Disable Screen Orientation Dynamically In Android(Java)

I want to lock my application to portrait mode for all the activities. I know this can be done by setting android:screenOrientation="landscape" in activity tag in manifest. but I have lots of activities, so I was wondering is there any way to do it on application level either by some xml tag in manifest or in code anyhow, so I wont have to define it in all the activity tags.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Use this method in the BaseActivity. And extend this activity for every other activity in your application.

Android App Development : How do change content on screen depending on event

Just starting out developing some android apps. Coming from a web development background I'm wondering if the idea behind changing whats displayed on screen is similar to linking html files.
Say I had a button that once clicked would then display a completely new page, button gone and completely new content in its place. I had thought at first that this was done just by having conditional statements in the main activity class. But I don't see how this would work with the xml layout file.
So I have come to the conclusion that you have to define multiple xml files and switch between them using logic in the main class.
If this is correct whats the best way to go about this, and if not could some suggest how this is achieved normally?
Thanks.
I think it wise to follow the following tutorial: http://developer.android.com/training/basics/firstapp/index.html
Have you tried visiting Android developers' website?.The solution to your question can be obtained taking the Android training module in that website. You have said you want to go to a new page, you can use Activities here.
Let me explain you this in simple terms.
In Android for every page(Activity) you need to make a separate xml file. for example main_activity.xml.
And for each page(Activity) there is a java class. For ex MainActivity.java. This class may contain event handling and business logic part.
Now let's go to your question about switching between multiple pages.
Suppose you have 2 activities: MainActivity and SecondActivity.
Now in MainActivity you have a button then you set its onClick attribute to its event handling method. This can be done in xml file.
android:onClick="goToSecond"
Now in MainActivity.java you need to create a method which looks like this.
public void goToSecond(View v)
{
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
}
This is a code snippet for switching to second activity.
And I also agree with other answers that you should check out developers.android.com
Hope it helps.
There is no need to switch between the XML files for portrait and landscape mode. Android does that for you. If you are writing an app that uses both orientation, then you have to write separate layout files and keep them in layout(for portrait), layout-land(for landscape) folders. This is not at all necessary if your design looks same in both orientation.

Categories

Resources