What is better between Activity and AppCompactActivity to use Toolbar? - android

When I created a new activity, it extends ActionBarActivity but it's deprecated.
At that moment, I have two options:
1) Used Activity and android:Theme.Material in my style like that :
<style name="AppTheme" parent="android:Theme.Material">
Or
2) Used AppCompatActivity and Theme.AppCompat :
<style name="AppTheme" parent="Theme.AppCompat">
So my question is, what is better between Activity and AppCompactActivity to use Android Material Design and Toolbar ?

It isn't a matter of which is "better". Which one you should use depends on what versions of Android you are supporting.
Theme.Material is only available on devices running API 21 (Lollipop) and up. If you wish to use the Material theme on devices running API 20 and below, you need to use AppCompat.
When I created a new activity, it extends ActionBarActivity but it's deprecated.
This is a very recent change. As of version 22.1 of AppCompat, ActionBarActivity has been deprecated in favor of AppCompatActivity.

Related

MaterialCardview requires Theme.AppCompat

I'm trying to test my skills on new Google Material components.
But for now I am encountering a problem with MaterialCardView
The building process tells me
The style on this component requires your app theme to be Theme.AppCompat
[..]
at com.google.android.material.card.MaterialCardView.<init>
With this clue I added
style="#style/Theme.AppCompat" & android:theme="#style/Theme.AppCompat" to the MaterialCardView and also to my Activity in the manifest.
I tried also to change my Acitivity to AppCompatActivity but without any success.
I also tried to set styles told by material.io documentation but without success !
Have you some clues?
Thanks
According to Material Components 1.2.1 you need to do this:
Depend on the library implementation 'com.google.android.material:material:1.2.1'
You'll need to have compileSdkVersion 30
Your activity needs to extend AppCompatActivity (or use AppCompatDelegate)
You have to use a Material Components theme
Source: https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md
The easiest way to get started is for your current theme to extend Theme.MaterialComponents.*.Bridge instead of Theme.AppCompat.*.
Additionally you'll need to override the following attribute in your theme, otherwise the card color will be broken:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<item name="elevationOverlayEnabled">false</item>
</style>
Don't set android:theme="#style/Theme.MaterialComponents" on the card. You'll lose color information (primary, secondary, accent,...) from your theme on every widget inside the card.
Don't set style="#style/Theme.MaterialComponents on the card. Don't mix themes and styles.

Extending AppCompatActivity not letting me use Holo or Material themes

I'm trying to create an app with android:Theme.Holo.Light and extending the Activity by AppCompatActivity. Android is throwing errors saying that I must use AppCompat theme.
My question is:
If I don't extend AppCompatActivity, I don't get material look in API level below 21 and if I extend Activity and set Holo theme, I don't get any errors but at the cost of missing the Material feel in the older devices. How do I overcome this limitation?
You need to use Theme.AppCompat if you are extending AppCompactActivity
You can use Theme.AppCompat. It is similar to Theme.Holo

Failure to change Theme in Android Studio

I want to change the Theme to Theme.Holo, but it was crashed when I run the apps. I only made change in Manifest.xml as below, and so far there are no error message shown.
Original:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
Changed:
<style name="AppTheme_Holo" parent="android:style/Theme.Holo"></style>
Here is the logcat:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
logcat message seem to tell me I can only use the Theme.AppCompat, and I think this theme is coming from supporting library appcompat v7:22.
I have tried to search around the link as below from Android developer site, but seem the setting above has no difference with their suggestion
May I know why I can only use the Theme from the supporting library v7:22?
Is there something I missed to change?
Android Developer site:Styling the Action Bar
Yes there is something more. Your activity is extending AppCompatActivity or ActionBarActivity and that needs a Theme.AppCompat theme or any descendant. You just need to change that to Activity or FragmentActivity from support library v4.
You might be using ActionBarActivity which uses Theme.AppCompat.*. Make your activity extend android.app.Activity to get rid of this error.

ActionBar not showing on android < 5.0

I switched from ABS to AppCompat and Material theme(for api 21 only)
<!--manifest: -->
<application
android:theme="#style/AppStyle"
<-- values folder -->
<style name="AppStyle" parent="#style/AudioRecTheme">
<style name="AudioRecTheme" parent="#style/Theme.AppCompat.Light">
<!-- values-v21 folder-->
<style name="AudioRecTheme" parent="#android:style/Theme.Material.Light">
My activity:
public class AudioRecActivity extends FragmentActivity
The action bar is showing only in Android 5.0, but missing otherwise.
First, either use appcompat-v7 or use built-in themes, not both for the same activity. Here, you are trying to use Theme.AppCompat.Light in some cases and Theme.Material.Light in others, which is not only unnecessary but AFAIK will not work. If you are going to use Theme.AppCompat.Light, do so for all API levels.
Second, if you are going to use appcompat-v7 and Theme.AppCompat.Light, you need to inherit from ActionBarActivity.

android:Theme.Material.Light requires API level 21 (current min is 8)

I want to use Material Theme in my application which has minimum sdk version of 8. As per docs - "The material theme is only available in Android 5.0 (API level 21) and above. The v7 Support Libraries provide themes with material design styles for some widgets and support for customizing the color palette." Does it mean I can use it if I add v7 Support Libarary in my project? Because after adding this library I got the following error:
android:Theme.Material.Light requires API level 21 (current min is 8).
Or maybe I understood something wrong? Any suggestion will be appreciated. Thanks in advance.
For this you need to have 2 values folders.
One that exists by default, and another, you have to create in your res folder and name it values-v21.
In the default values folder, in styles.xml, use a theme other than Material theme.
And in the styles.xml of values-v21 folder that you created, use Material theme.
Android phone will automatically pickup the styles.xml which it supports. If the phone supports Material Design (Lollipop devices), your app will use Material theme (values-21 folder).
If it doesn't (in phones running older Android versions), the default values folder will be used.
You need to use android:theme="#style/Theme.AppCompat.Light" theme to get a material design.
Make sure your min is 8 and your target is 21. And you're using build tools/sdk 21.
Pedro Oliveira is right with regards to Theme.AppCompat, but some essential information is missing in that answer.
A blog post titled appcompat v21: material design for pre-Lollipop devices! by Chris Banes from the Android team probably best answers the question of how to get Material Theme for API levels prior to 21.
To summarise, you need appcompat-v7 dependency:
dependencies {
...
compile "com.android.support:appcompat-v7:21.0.3"
}
After that, for dark version as your base theme, use:
<style name="AppTheme" parent="Theme.AppCompat">
</style>
And for light version:
<style name="AppTheme" parent="Theme.AppCompat.Light">
</style>
And if you're new to AppCompat, there are things you need to know, such as:
All of your Activities must extend from ActionBarActivity*. It extends
from FragmentActivity from the v4 support library, so you can continue
to use fragments.
*NB: more recently, ActionBarActivity has been deprecated in favour of AppCompatActivity.
But you really should read the whole Setup section of that blog post! (The information on Toolbar vs Action Bar, and some of the comments are also something you probably shouldn't miss.)
In your NameActivity.java file import the following:
import android.support.v7.widget.Toolbar;
Comment the previous one:
//import android.widget.Toolbar;
With this the problem is solved.

Categories

Resources