Issue
I want to add a background to a button, as I have already done several times. I have a drawable resource file with the appropriate background. However, if I link this, it will not be accepted, instead only one color will be used as the background.
I had the suspicion that it could be a mistake in my Styles.xml but nothing is called too colored in these. What can be the reasons why Android Studio does not recognize a background or where could I have defined a general theme of how buttons should look? I can’t remember having put on something like that. I tried something similar with an EditText, I had no problems here, so I think it is a specific button problem.
Also if I change the background color to a plain whit it is not changing anything and still showing a dark blue.
Update: If I change the preview style (upper middel of the preview) to DialogStyle the button background works. How can that be if there is nothing defined in der styles?
Update: I added a new version of my button this time initializing the background in a extra style. Result is that it is still not showing the right color. I also tried it with black but nothing happend.
Thanks for any help.
Button
<Button
android:id="@+id/buttonRegistrieren"
android:layout_width="232dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="@drawable/btnbackground"
android:onClick="WriteData"
android:text="@string/Registrieren"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:textSize="16dp"></Button>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">@android:color/black</item>
</style>
<style
name="SplashTheme"
parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="ButtonStyle" parent="android:style/Widget.Button">
<item name="android:textSize">19sp</item>
<item name="android:textColor">@color/colorWhite</item>
<item name="android:background">@color/colorBlack</item>
</style>
<style
name="SecondTheme"
parent="AppTheme">
<item name="android:windowFullscreen">false</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
</resources>
btnbackground.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#FF5256AC"
android:endColor="#FF662D91"
android:startColor="#FF29ABE2"
android:type="linear" />
<corners android:radius="150dp">
</corners>
</shape>
Button with Theme
<Button
android:id="@+id/buttonLogin"
android:layout_width="160dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Anmelden"
android:textAllCaps="true"
android:textColor="@color/colorWhite"
android:textSize="16sp"
android:theme="@style/ButtonStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="426dp"
/>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="edmt.dev.androideatitv2client">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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"
tools:replace="android:theme">
<activity android:name=".PasswordActivity"></activity>
<activity android:name=".LoginActivity" />
<activity android:name=".RegistrationActivity" />
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UpdatePasswordActivity" />
<activity android:name=".UpdateProfileActivity" />
<activity android:name=".ProfileActivity" />
<activity
android:name=".HomeActivity"
android:label="@string/title_activity_home"
android:theme="@style/AppTheme.NoActionBar" />
<service android:name=".services.MyFCMServices">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
Solution
You have to add Width
and Height
to your Shape
in
btnbackground.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#FF5256AC"
android:endColor="#FF662D91"
android:startColor="#FF29ABE2"
android:type="linear" />
<corners android:radius="150dp">
</corners>
If you don’t use AndroidX, you have to change your Theme from Theme.MaterialComponents.Light.NoActionBar
to Theme.AppCompat.Light.NoActionBar
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">@android:color/black</item>
</style>
<style
name="SplashTheme"
parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="ButtonStyle" parent="android:style/Widget.Button">
<item name="android:textSize">19sp</item>
<item name="android:textColor">#fff</item>
<item name="android:background">#000</item>
</style>
<style
name="SecondTheme"
parent="AppTheme">
<item name="android:windowFullscreen">false</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DialogStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
Button
<Button
android:id="@+id/buttonLogin"
android:layout_width="160dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Anmelden"
android:textAllCaps="true"
android:textColor="#fff"
android:textSize="16sp"
android:theme="@style/ButtonStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Answered By – Abderazak Amiar
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0