I have an activity that contains a toolbar with a back button like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
style="#style/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />
</LinearLayout>
and here is the toolbar style used
<style name="toolbar">
<item name="navigationIcon">?attr/homeAsUpIndicator</item>
</style>
The back button is working as expected, but I am facing a weird error where the back button disappears only when I kill the app from recent apps or the app crashes and I reopen the app.
like the following picture
but if I click on the place where the button is supposed to be is looks like the following picture
Was it ever a darker color?
Because now I see that you may have set a toolbar back button drawable to the same color of your toolbar. You can check if you have the same problem when you change the background of the toolbar view.
One solution is to change the android:homeAsUpIndicator icon drawable in your app theme:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:homeAsUpIndicator">#drawable/dark_back_button_drawable</item>
</style>
When you apply a theme to a view you have to specify the parent theme if you want the normal behavior with a specific part modified.
<style name="AppTheme.BackNavigationToolbar" parent="Widget.AppCompat.Toolbar">
<item name="navigationIcon">?attr/homeAsUpIndicator</item>
</style>
If you decide to use the Toolbar which you get from a activity and not declare it in xml. You can just use the setDisplayHomeAsUpEnabled(true). If you want to read more on this check this page out: https://developer.android.com/training/appbar/up-action
supportActionBar?.setDisplayHomeAsUpEnabled(true)
If it is two activities you are navigating between then you just have to specifiy parent activity in the AndroidManifest.xml
Exampel
<activity android:name=".SecondActivity"
android:label = "Second Activity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
More resources:
https://developer.android.com/training/appbar/setting-up
Related
Goal: I am attempting to apply two different themes in one Fragment. One for the fragment's overall xml and the other for a particular view within that fragment's xml.
Reason/Problem: The reason for this is that it doesn't seem possible to change the entire background of a Floating Action Button to a vector using MaterialComponents, but it does work with appCompat.
Initially, I tried to change the theme in the xml using style="..." as shown below, but it appears to be reverting back to the theme declared in the manifest which is AppTheme1. I know this because I tested it by switching the theme declared there. That is, when I switched it to AppTheme2, the vector loaded properly in the FAB, but it crashed elsewhere since I have MaterialComponents throughout the app.
Solution: I think the obvious solution for this would be to change the theme for just the Floating Action Button in question, but I don't know how to do that. Any help much appreciated. Thank you!
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/nationality"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/flag_united_states_of_america"
app:borderWidth="0dp"
style="#style/AppTheme2"
app:maxImageSize="56dp" />
Themes:
<style name="AppTheme1" parent="Theme.MaterialComponents.Light.NoActionBar" >
&&
<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar" >
With MaterialComponents:
With AppCompat:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fabtest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So when the theme is changed from AppCompat to Material Components, the image resource no longer applied properly to the Floating Action Button. So I just want to apply AppCompat for Floating Action Button but keep the Material Components as the main style.
In your case you can use:
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:tint="#null"
app:srcCompat="#drawable/flag_united_states_of_america"
..>
The FloatingActionButton in the Material Components uses the colorOnSecondary attribute defined in your app theme to tint its icon (and the default value is #000000). If you don't want to tint the icon you have to use app:tint="#null".
In general if you want to use a custom style in your FAB you can use:
<com.google.android.material.floatingactionbutton.FloatingActionButton
style="#style/MyCustomFab"
..>
with:
<style name="MyCustomFab" parent="#style/Widget.MaterialComponents.FloatingActionButton>
.....
<item name="backgroundTint">#color/....</item>
<item name="tint">#color/....</item>
</style>
If you want to override the attribute defined in your app theme you can use:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:theme="#style/fab_themeoverlay"
..>
with:
<style name="fab_themeoverlay" parent="#style/Widget.MaterialComponents.FloatingActionButton>
<item name="colorPrimary">#color/...</item>
</style>
If you want to change the theme of the button, which has implications for how it is inflated, you should use android:theme instead of android:style.
Both Ben P. and Gabrielle were semi-correct. The answer required both a change in the theme via:
android:theme="#style/SecondTheme"
and setting the tint to null:
app:tint="#null"
Both were required in this case
All together, given two themes, this is the correct XML for a FloatingActionButton if you want to change it's entire background to a vector while your primary theme is MaterialComponents as referenced in your app's manifest:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:borderWidth="0dp"
app:srcCompat="#drawable/your_vector"
app:tint="#null"
android:theme="#style/SecondTheme"
app:maxImageSize="56dp" />
Thank you Gabrielle and Ben for your help!
I used Android Studio to create basic activity using New -> Activity -> Basic Activity. But the activity background colour is black. I know it is to reduce battery consumption. But is there a way to change it to white colour, without changing style.
I tried to change the background colour of the root layout, but then it won't show hint text of edit text field. Because it's colour is white.
Then I tried to change theme from Material to Material Lite, then it wouldn't show the change when I run the app in the device.
Here is the activity declaration in AndroidManifest.xml
<activity
android:name=".SearchBus"
android:label="#string/title_activity_search_bus"
android:parentActivityName=".MapsActivity"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="uk.co.stableweb.iroute.MapsActivity" />
</activity>
In your styles.xml set the android:windowBackground attribute for whatever theme you are using for application or activity. You can find the theme you are using in your AndroidManifest.xml.
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">#color/somecolor</item>
</style>
</resources>
Note: If you have views in your layout that aren't transparent, it will block you from seeing the color you set here.
Did you try :android:background="#ffffff" i? It works for me
I' developing an android app and I need to use my custom title bar using a picture and two buttons. The thing is, immediately when I launch my app, during 1 or 2 seconds before my custom title bar appears, there is the ugly default one with "my application" displayed. The minimum targeted API is 15.
All the answers found on stack overflow didn't work, or succeed to make it disappear but was doing the same to my custom title bar.
Here is how I call it from my activity:
supportRequestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.topbarclassic);
Since my first view is a fragment I dont call SetContentView
And this is my custom styles.xml:
<resources>
<style name="theme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowNoTitle">false</item>
</style>
</resources>
Once again my custom title bar works properly. I just need to get rid of the default one displayed quickly when the app starts. Thanks a lot!
if you use this style the activity will load without an action bar
<style name="theme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Then you should really be using a toolbar to set the action bar. For example :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />
</RelativeLayout>
Then in your activity you can set the action bar like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Dont forget to set your them in your AndroidManifest.xml
<activity
android:name=".path.MyActivity"
android:theme="#style/theme"/>
If found a workaround for my problem using Modge's link about splashscreen.
It doesn't solve the problem itself but remains a good workaround.
I created a small activity in charge of the splash screen, avoiding the white first screen to stay for too long. Then this splash redirect on my main activity.
This is also useful in my case since I can start some connection process during the splash screen. You can follow this example: http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
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.
I can't seem to get past this error:
android.util.AndroidRuntimeException: You cannot combine custom titles
with other title features
I am running API > 14.
Manifest is as follows:
<activity
android:name=".activity.ActivityWelcome"
android:label="#string/app_label_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:theme="#style/My.Theme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My activity is doing the following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.welcome);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title_main);
...
Where R.layout.window_title_main is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_title"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dp" >
<TextView
android:id="#+id/textview_title"
android:drawableLeft="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="-1"/>
</LinearLayout>
Why is this not working when it seems to work for others?
This is the kind of error that will drive you to insanity and beyond.
So I happen to be using Theme.Holo as my parent theme. (The theme that my own theme extends)
The documentation says:
...the action bar is included in all activities that use the Theme.Holo
theme (or one of its descendants), which is the default theme when
either the targetSdkVersion or minSdkVersion attribute is set to "11"
or greater.
http://developer.android.com/guide/topics/ui/actionbar.html#Adding (first paragraph)
Ok, so when I try to go through the process above (my question) I get the error:
You cannot combine custom titles with other title features
Well, that's because, by default action bar is setup already and it won't allow you to use a custom title bar as well.
Documentation reveals that "each activity includes the action bar":
This way, when the application runs on Android 3.0 or greater, the
system applies the holographic theme to each activity, and thus, each
activity includes the action bar.
So, I will now focus my time on altering the action bar and not the title bar!
FEATURE_CUSTOM_TITLE
and
<item name="android:windowNoTitle">true</item>
Are mutially exclusive. Remove
<item name="android:windowNoTitle">true</item>
and it will work again.
As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>