Android how to set ActionBar in just one Activity - android

I am new to Android and I'm facing this problem.
I got MainActivity with items list, when one of them is clicked then the DetailActivity starts.
I disabled ActionBar with
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
so the MainActivity no longer have actionBar. But I want this ActionBar in the DetailActivity ( i need the basic one, with the left arrow to get back to MainActivity ), so I created another style
<style name="DetailTheme" parent="android:Theme.Material.Light.DarkActionBar">
and used it in activity_detail.xml like this
android:theme="#style/DetailTheme"
But it seems this is not the right way, because there is no ActionBar in that Activity .
I'm currently not sure if DetailActivity will be the only one with ActionBar, so I would like to know how to activate it on just this one Activity.
What is the right solution for this ?

Not sure but maybe try assigning it to the activity in the manifest
<activity android:name=".DetailActivity"
android:theme="#style/DetailTheme"/>

used it in activity_detail.xml like this android:theme="#style/DetailTheme"
My best guess is that you mean you added the android:theme attribute to the root view tag in your layout file. Maybe something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="#style/DetailTheme"
...
/>
This doesn't actually apply the theme to your Activity, it only applies the theme to your Activity's "content view". Content views don't have action bars, so those attributes of the theme will be ignored.
To apply a theme to an Activity, you have to specify it in AndroidManifest.xml
<activity
android:name=".DetailActivity"
android:theme="#style/DetailTheme"/>

Related

Have trouble setting up Toolbar in android studio

I am new to android development, (Rather of a PHP background) and tried adding a toolbar to an app I am building. The toolbar works but requires me to initialize it in every activity! Surely there must be a better way than that? I tried creating another class SetupActivity extending AppCompactActivity and moved all the repeated code in there. But no activity calls the onCreate in SetupActivity.Then I tried using a fragment but it is not a subclass of Context.Please help me find a way to fix it. Thanks!
Edit: I have to use the setSupportActionBar in every activity before I can get it to display.
Also, The app bar has a button that sends the user to another activity. I have to create the button using Java and then add a click listener to it in every activity.
One way of doing this is through xml:-
What you can do is first create a style in your app/src/main/res/values/themes.xml file.
In the following example, I'm removing the action bar from my app
Eg:
<style name="NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
Then set this theme as the "android:theme" in the "application" tag of your app/src/main/AndroidManifest.xml file. Eg:
<application
android:allowBackup="true"
android:icon="#mipmap/app_icon"
android:label="#string/app_name"
android:roundIcon="#mipmap/app_icon_round"
android:supportsRtl="true"
android:theme="#style/NoActionBar"/>
This way the style which you have created (containing the new toolbar) will be applied across your entire application.

apply a style to a specific xml layout

I am making an app, that has a lot of text to read, and i want it to have 2 themes. dark and light. So i think the way to do this is to have 2 XML layouts. But I don't know how to set a 1 style to 1 laayout, and a different style to a different layout. There is a field android:theme in the manifest, but that sets a theme to the whole app, and not a specific layout.
So, to be clear, my question is; How do you set a style/theme to a specific activity and not the whole app?
In the manifest, android:theme can be added to the activity tags, and those override the application tag theme.
<application
android:theme="..." <!-- App theme -->
...
<activity
android:theme="..." <!-- Theme for just this Acitivty -->
You can also try to programmatically call setTheme in an Activity class
i think the way to do this is to have 2 XML layouts
You just need one layout that can pick up the attributes of the theme(s) you'll set

How to remove the title bar from Android app

How to remove the title bar from my app.?
I tried by making changes in manifest file by adding "NoTitleBar" at the end of theme and i also followed the answer given in this question
"How to hide the title bar for an Activity in XML with existing custom theme"
But none of them worked for me.Please help
Use this:
requestWindowFeature(Window.FEATURE_NO_TITLE);
in your Activity.
You could add in your activity before you set view in oncreate function.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
It is because your activity is extending ActionBarActivity. You could replace by extending Activity for your activity.
public class MainActivity extends Activity
I hope this will solve your problem.
In the AndroidManifest.xml file you can find a tag <application
That tag contains android:theme attribute, it must be something like "#style/AppTheme". Open the file styles.xml.
Find a tag style name="AppTheme" and change its parent attribute to be Theme.AppCompat.Light.NoActionBar.
In my case it is
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
To hide the title bar on the preview mode click on the button next to the orientation switcher button, that must open a popup where you can choose a style. Choose one with NoActionBar, for example DeviceDefault.Light.NoActionBar

Android app theme - difference when using theme from style xml file

Why is there a difference between theme defined in AndroidManifest.xml and theme taken from styles.xml?
1) AndroidManifest.xml:
<application ... android:theme="#android:style/Theme.Black">
2) AndroidManifest.xml
<application ... android:theme="#style/AppTheme">
styles.xml
<resources>
<style name="AppTheme" parent="#android:style/Theme.Black" />
</resources>
1st setting gives black theme and no action bar. 2nd has dark action bar and light menu.
EDIT : options 1) and 2) - notice Menu and ActionBar
EDIT 2:
Why doesn't the 2nd option actually use the AppTheme (Theme.Black) ? (tested on SGS3)
You probably have another styles.xml file, perhaps under a directory like "values-v11", that is defining the #style/AppTheme differently than #android:style/Theme.Black and taking precedence over the file you're viewing/modifying.
#android:style/Theme.Black implements the exact theme implemented by Android (or device manufacturer). However, #style/AppTheme allows you to perform custom modification in your theme which actually extends the original Theme.Black from android, and in order to perform custom modifications, you use style resources.
In simple words, its just like using Activity class or YourOwnActivity class which extends Activity with extra features inside.
Styles.xml enables you to create your own themes. In AndroidManifest, you set the theme you want for an app or activity. You may want to use a system theme or your own. You can also extend other themes as you're doing setting "parent" attribute. For further information, check this out:
http://developer.android.com/guide/topics/ui/themes.html
You should try to put:
<resources>
<style name="AppTheme" parent="#android:style/Theme.Black" />
</resources>
in a xml file called res/themes.xml

Custom titlebar - system titlebar being shown for a brief moment?

I've got a custom layout I want to use as the titlebar of my android app. The technique found (linked at the bottom) works, but the system titlebar is displayed before onCreate() is called. Obviously that looks pretty jarring, as for a moment the system titlebar is shown, then my custom titlebar is shown:
// styles.xml
<resources>
<style name="MyTheme">
<item name="android:windowTitleSize">40dip</item>
</style>
</resources>
// One of my activities, MyTheme is applied to it in the manifest.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.my_activity);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_header);
}
I could always hide the system titlebar and display my own in-line perhaps with each and every layout, but, that's not very friendly.
Thanks
http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/
I think it's a framework limitation. I had the same problem in some of my applications and the ultimate solution was for me to tell the framework I didn't want a title bar at all and then create my own in my layouts. The include directive made it bearable for me, e.g.:
<include layout="#layout/title" />
When I used requestWindowFeature(Window.FEATURE_NO_TITLE) in my activities, I would have the same issue, I'd see the system title bar briefly while the activity was being build when it first loaded.
When I switched to using a theme to tell the framework I didn't want a title, the problem went away and I now see my own title directly on first load. The styling is easy for that:
<style name="FliqTheme" parent="#android:Theme.Black">
<item name="android:windowNoTitle">true</item>
</style>
I know this doesn't apply to your issue with the custom title, but like ptc mentioned, if you move your custom title into style/theme definitions (which you do by overriding the system title styles in your theme), I think you'll be on the right track.
The same problem happened to me today when I was trying to custom the title. I solved it by set the android:theme to android:style/Theme.NoTitleBar in the AndroidManifest.xml, and call setTheme() to the actual theme I want in my activity's onCreate callback function.
Try creating a custom theme style in XML and then set your activity's theme attribute in the AndroidManifest.xml file.
http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme
The method through setting android:theme does not work for me, I have made it by adding the following code in onCreate() of my Activity subclass:
getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);

Categories

Resources