Have trouble setting up Toolbar in android studio - android

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.

Related

Cordova Android set windowBackground programmatically at runtime

When the Cordova android app launches, a blank screen is briefly visible before cordova-plugin-splashscreen kicks in. I have learned that this is the windowBackground colour and can be changed by making a custom styles.xml and referencing it within AndroidManifest.xml though the activity's android:theme property. Example:
From AndroidManifest.xml:
<activity android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize" android:label="#string/activity_name" android:launchMode="singleTask" android:name="MainActivity" android:screenOrientation="portrait" android:theme="#style/CustomStyle" android:windowSoftInputMode="adjustPan">
From styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomStyle" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:windowBackground">#drawable/init_splash</item>
</style>
</resources>
styles.xml references another file just containing a drawable colour.
This works. It allows me to change the colour that appears before the splash screen.
However, I am now looking to allow the user to optionally change to a dark theme. I have already figured out how to modify cordova-plugin-splashscreen to change the splashscreen based on the user preference, but I'm having trouble changing the windowBackground/theme programatically at runtime.
I have tried adding the following within MainActivity.java or CordovaActivity.java:
setTheme(R.style.CustomDarkStyle);
getWindow().setBackgroundDrawableResource(getResources().getIdentifier("init_splash_dark", "drawable", getPackageName()));
getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
I placed these in the onCreate before super.onCreate() or setContentView(). The window background colour does indeed change, but the initial blank screen before the splash stays at whatever colour was set in the manifest.
How can I change the activity/window background colour programmatically when the application is started?
Some have suggested changing the app theme to a transparent one to prevent the blank screen entirely, but that causes a delay in opening the app. I'm fine with the blank screen, I just want to change it's colour programmatically.
As of April 22nd, I am yet to find a solution to this problem.
Create a class with the same name as of your project and it will extend Application not Activity. Put this code inside this class as this will be automatically initialized as soon as your application starts. It will be acting as a constructor for your application.
Hope it helps!
I have a project named "slot" so i created a class named slot like this
package com.xyz.slot;
import android.app.Application;
public class slot extends Application {
#Override
public void onCreate() {
setTheme(R.style.CustomDarkStyle);
super.onCreate();
}
}
But make sure there is no theme set in manifest as it will not be overridden.

Android how to set ActionBar in just one Activity

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"/>

Android - Disable dummy starting window of application

I want to know how to achieve this effect.
My app (default everything), when launcher icon is clicked, right away display some kind of a empty dummy window, where nothing is happening and then loads layout into it.
"Heavier" apps like YouTube, Drive, Dropbox etc. when started, seem to wait after launch, without showing that dummy window and load right into ready layout.
Any idea how to do this or where should I look into?
Thanks
//EDIT: this has nothing to do with something like loading database, where I should display progressBar, imho this has to do with stuff before activity exists.
I suggest you don't disable the dummy loading window using:
<style name="Theme.MyTheme" parent="android:style/Theme" >
....
<item name="android:windowDisablePreview">true</item>
....
</style>
because you could run into several problems. For example, if you try to animate an AlertDialog, enter animation will not work (personal experience).
The right solution is to set the theme of your application like to your main activity screen (same ActionBar style, same background color).
In my application after severals experiments i found the right solution for my needs.
(I have a main activity with blank background, no ActionBar, just full screen personal layout)
1 - styles.xml: add new style like this (remove ActionBar and set the same background color of my main activity)
<style name="LoadingTheme" parent="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#color/white_smoke</item>
</style>
2 - AndroidManifest.xml: set my theme for main activity to "LoadingTheme"
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.company.appname.MainActivity"
android:label="#string/app_name"
android:theme="#style/LoadingTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
</application>
And finally i have full blank dummy loading window without ActionBar with soft MainActivity load and working animations.
Hope to be helpful.
<style name="Theme.MyTheme" parent="android:style/Theme" >
....
<item name="android:windowDisablePreview">true</item>
....
</style>
but use with caution
This will create a custom preview window for your activity.
Create a style in styles.xml like
<style name="MyTheme" parent="#android:style/Theme.Holo.NoActionBar">
<item name="android:windowBackground">#color/myBackgroundColor</item>
</style>
In your Manifest file use the theme created above.
For eg:
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/MyTheme"
/>
In your activity change the background of the window back to null in onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(null);
setContentView(R.layout.main);
}
You can call the setContentView method anywhere you'd like in your activity. Before calling this method it will display a blank screen. If you wanted a splash screen, you can call the setContentView method within the onCreate method to display the splash screen view. Then when you're ready to display the "main" view you can use a LayoutInflater to display it. I also suggest using a ProgressDialog to show the user that information is loading.
Load your resources before setting the contentview. There will be a dummy window until you would not set view using setContentView.
EDIT
To avoid this screen. Make there changes to your code.
1) Do not do any heavy work in activity onCreate method. Run a separate thread for it.
2) Set content view right after the super.onCreate() line
super.onCreate(savedInstanceState);
setContentView (R.layout.splash_screen);
3) Apply following theme to your application.
Define theme in style
<resources>
<style name="Theme.MyTheme" parent="android:style/Theme" >
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#drawable/any_default_background</item>
</style>
</resources>
In your Manifest
<application
....
android:theme="#style/Theme.MyTheme"
....
>
Thats It.

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

How to hide title bar from the beginning

I am using following code to replace title bar.
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
And it's working fine once UI loaded. Problem is however when I start the app, the ugly gray bar appears for 1-2 seconds until UI loaded. Is there any way to specify not showing the default title bar at all?
If you want the titlebar to be gone in every activity within your app, then add
<application android:name=".YourAppNameHere"
android:label="#string/app_name"
android:icon="#drawable/icon"
android:theme="#android:style/Theme.NoTitleBar">
to your manifest. Not 100% sure though that this will prevent the titlebar from showing up momentarily, but it should work.
In the Manifest file, add this line inside the application tag
android:theme="#android:style/Theme.NoTitleBar"
It will hide the bar from all the activities. If you want to hide it from a specific activity, add the same line to that activity's tag.
Good Luck !
You should add a line to your AndroidManifest which states that you use a theme (standard android or extended)
<application android:name=".YourAppNameHere"
android:label="#string/app_name"
android:icon="#drawable/icon"
android:theme="#style/MyTheme">
and then you can have a themes.xml in your res/values/ folder where you extend the: Theme.NoTitleBar and add custom rules to them (for example like windowBackground)
<resources>
<style name="MyTheme" parent="android:Theme.NoTitleBar">
<item name="android:windowBackground">#drawable/my_background</item>
</style>
<resources>
Have fun

Categories

Resources