Android ChipGroup IllegalArgumentException, even though using MaterialComponents theme - android

I'm using a com.google.android.material.chip.ChipGroup in an AppCompatActivity, but when I try to make a Chip, I'm getting java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).. I have tried every possible Theme.MaterialComponents theme (yes, even .Bridge), and I'm still getting the error. Somewhere else in the app, I'm also using a chipgroup and making new chips, and everything works fine. What am I missing here?
Relevant in MyActivity:
Chip chip = new Chip(getApplicationContext());
Relevant in MyActivity xml:
<com.google.android.material.chip.ChipGroup
android:id="#+id/filter_chips"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appname">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:networkSecurityConfig="#xml/network_security_config"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppName">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="MyActivity"/>
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
</application>
</manifest>
Style:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AppName"
parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?
attr/colorPrimaryVariant</item>
</style>
</resources>

Don't use application context, use the activity's context. Application context does not have material theme enabled in it.

Related

How To Set SupportRTL in AndroidManifest for Specifed Activity

i removed Action Bar from all activities Then I put to specific activity an action bar and when i used SupportsRtl (in Specifed Activity) for change title position but title position still no changed
styles :
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="myTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#FFFFFF</item>
</style>
</resources>
Manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.classmanager">
<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>
<activity android:name=".FirstTab" />
<activity android:name=".SecondTab" />
<activity android:name=".ThirdTab" />
<activity android:name=".AddNewClass"
android:theme = "#style/myTheme"
android:supportsRtl="true" <==== DONT WORK
android:label="اضافه کردن کلاس">
</activity>
</application> </manifest>
You can't do such thing in Manifest.xml.
I recommend set android:layoutDirection="rtl" into your activity parent layout to set just that specific activity support rtl.
this article has great information about layout directions support.
When you enable support for RTL layouts like this:
<application
...
android:supportsRtl="true">
You enable the use of this feature for every activity in the app.
This suggests that the problem probably lies in the layout of your AppBarLayout/ Toolbar.
In order for the application to properly reflect the direction, you should use the appropriate attributes - instead of using Left/Right, you should use Start/End ones. To read more check Android Developers Blog.
You can also use the automatic Android Studio option by selecting Refactor > Add RTL Support Where Possible...

Why am I receiving this "You need to use a Theme.AppCompat theme (or descendant) with this activity" error?

I am having the most frustrating issue with a new Activity. I created it in the "normal" way:
Right-Click -> New -> Activity -> Empty Activity
However I am getting this annoying error when I try to launch the activity:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
I feel my question is different from the others on StackOverflow because I've done some poking around on StackOverflow as well as googling in general, but these errors seem to pop up when somebody tries to open a dialog box, not when they are simply trying to launch a new activity.
Here is my Activity Class ViewAllStudents.java:
public class ViewAllStudents extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all_students);
}
}
I am getting the error on the setContentView(R.layout.activity_view_all_students); line.
My layout resource file activity_view_all_students.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_all_students_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/student_table_header" />
<ListView
android:id="#+id/student_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
My include file student_table_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/DARKOLIVEGREEN"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/student_number_header"
style="#style/StdTableHeader"
android:layout_weight="1"
android:text="#string/student_number_header"/>
<TextView
android:id="#+id/student_id_header"
style="#style/StdTableHeader"
android:text="#string/student_id_header"/>
<TextView
android:id="#+id/student_location_header"
style="#style/StdTableHeader"
android:text="#string/student_location_header"/>
<TextView
android:id="#+id/student_status_header"
style="#style/StdTableHeader"
android:text="#string/student_status_header"/>
<TextView
android:id="#+id/student_delete_header"
style="#style/StdTableHeader"
android:text="#string/student_injuries_header"/>
<TextView
android:id="#+id/student_deleted_header"
style="#style/StdTableHeader"
android:text="#string/student_deleted_header"/>
<TextView
android:id="#+id/student_last_modified"
style="#style/StdTableHeader"
android:text="#string/student_last_modified_header"/>
</LinearLayout>
Finally, my styles resource file styles.xml:
<resources>
<style name="StdTableHeader">
<item name="android:textSize">14sp</item>
<item name="android:paddingLeft">4dp</item>
<item name="android:paddingRight">4dp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:layout_weight">10</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/WHITE</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">true</item>
<item name="android:gravity">center</item>
</style>
</resources>
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".main.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".forms.formOne"
android:label="#string/form_one_header"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan|stateHidden" />
<activity
android:name=".forms.formTwo"
android:label="#string/form_two_header"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan|stateHidden" />
<activity
android:name=".forms.formThree"
android:label="#string/form_three_header"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan|stateHidden" />
<activity
android:name=".main.AddStudent"
android:label="#string/title_activity_add_patient"
android:theme="#style/AppTheme.NoActionBar" />
<activity android:name=".main.ViewAllStudents"></activity>
</application>
</manifest>
Add this attribute
android:theme="#style/Theme.AppCompat.Light"
in the activity tag.
Or have the styles.xml like the following
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
and in manifest add this attribute
android:theme="#style/AppTheme"
in the application tag.
Ok ... to make a long answer short, check your AndroidManifest.xml files. For some reason, AndroidStudio neglected to insert critical line:
android:theme="#style/AppTheme.NoActionBar"
AndroidStudio had inserted this line for all of my other activities that I have created, except for this one.
Thanks to all who took the time to help and who tolerated my ignorance.

ActionBar Styles being ignored, even the icon is missing

I have my activity with an action bar which I want to customize, however Android is ignoring all my styles...
This is how it looks now:
It should look with a blue background instead and the application logo should be there also...
My manifest is the following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="greensmartcampus.eu.smartcampususerfeedbackapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:logo="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/SmartCampusTheme">
<!-- Splash screen while connecting to the web-server -->
<activity
android:name=".HomeScreen"
android:label="#string/title_activity_home_screen"
android:parentActivityName=".AbstractPortraitActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="greensmartcampus.eu.smartcampususerfeedbackapp.AbstractPortraitActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- The activity where all actions take place -->
<activity
android:name=".ControlActivity"
android:label="#string/title_activity_control"
android:parentActivityName=".AbstractPortraitActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="greensmartcampus.eu.smartcampususerfeedbackapp.AbstractPortraitActivity" />
</activity>
</application>
</manifest>
My activity xml is the following:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="greensmartcampus.eu.smartcampususerfeedbackapp.ControlActivity" />
My styles.xml is the following:
<resources>
<!-- Base application theme. -->
<style name="SmartCampusTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/CustomActionBarTheme</item>
</style>
<style name="GenericProgressIndicator" parent="#android:style/Widget.ProgressBar.Small">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:indeterminate">true</item>
</style>
<style name="CustomActionBarTheme" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/actionbar_bg_color</item>
</style>
</resources>
I also want to hide those 3 dots in the action bar which open the menu to the settings, which I don't have...
What am I doing wrong?
Thanks!
I believe your issue is related to using the app compatibility library. Try removing the android: prefix from your Actionbar styles. See Styling the Action Bar and appcompat v21.
AppCompat v21 brings the Material theme to all devices. Two changes with this is the use of the new color palette for theming your action bar and activity. In the new system, coloring your action bar is accomplished via a theme such as:
<style name="SmartCampusTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/actionbar_bg_color</item>
</style>
In addition, the lack of the app icon is expected. Per the Toolbar documentation:
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.
The action bar showing a 'Settings' option is part of the initial template - you can delete the onCreateOptionsMenu method to remove all items from the options menu.

custom action bar keeps disappearing when i switch between fragments using the navDrawer

I wanted to add my own custom action bar to the drawer navigation by Google which i have figured out, the problem is, that when I try to open the drawer the Action bar then disappears, along with the icon that allows me to toggle the drawer is also gone for good. it again disappears when i click on my next fragment activity. I also noticed that if i leave the theme of the app back to its default theme everything works fine.
here's my custom theme style class
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:background">#f0f0</item>
</style>
</resources>
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.navigationdrawerexample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When it works i use #android:style/Theme.Holo.Light.DarkActionBar for the theme

Incorrect theme is applied to AlertDialog

I am trying to create a new alertbox with a loading symbol in my android application. Working fine. Below is my code.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.custom, null));
progress = new ProgressBar(this);
builder.setMessage("Message");
builder.setView(progress);
//builder.setCancelable(false);
builder.create().show();
My Custom.XML looks like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"/>
</RelativeLayout>
My res/styles.xml is
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="maxWid">
<item name="android:textColor">#000000</item>
<item name="android:layout_marginTop">50dp</item>
<item name="android:layout_marginBottom">50dp</item>
<item name="android:layout_marginLeft">10dp</item>
</style>
<style name="apBtn">
<item name="android:textColor">#000</item>
<item name="android:layout_alignParentLeft">true</item>
</style>
</resources>
res/values-v11/styles.xml is
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>
res/values-v14/styles.xml is
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
My AndroidManifest.cml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.kmc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="com.myapp.kmc.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.myapp.kmc.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.myapp.kmc.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.myapp.kmc.LoginActivity"
android:label="#string/title_activity_login"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name="com.myapp.kmc.ListActivity"
android:label="#string/title_activity_list"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name="com.myapp.kmc.CompleteActivity"
android:label="#string/title_activity_complete"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp.kmc" />
</intent-filter>
</receiver>
<service android:name="com.myapp.kmc.GCMIntentService" />
<activity
android:name="com.myapp.kmc.RegisterDevice"
android:theme="#android:style/Theme.NoTitleBar"
android:label="#string/title_activity_register_device" >
</activity>
</application>
</manifest>
Testing in android 4.1.2
But when I create a new application and follow this code I am getting an alert box like this
But When I implement the same code in my existing application. I am getting an alert box like this
Since I want the first one in my existing application. Its ruining my time..
Can any one please help me. I am new to android
Thanks in advance
Your existing application uses Theme.Black, that's why dialog has black background. Change theme to Theme.Holo.Light to fix it.
For recommended way to do this, take a look at my other answer.
EDIT: Looks like you're applying the correct theme to your <application> tag, but then you're overriding it by setting Theme.NoTitleBar (which is a descendant of Theme.Black) to each of your activities.
Remove android:theme attribute from <activity> tags and it will work.
If you want to remove title and action bar, apply windowNoTitle and windowActionBar attributes to your theme definition:
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
If you want to use Holo Theme you must set your API level to 14 (that has hardware acceleration enabled as default);
Try to insert:
<uses-sdk android:minSdkVersion="14" />
At your AndroidManifest.xml.

Categories

Resources