FridregJohn
FridregJohn

Reputation: 79

How to use com.google.android.material Objects and change button background

I have a problem I want to both change Buttons color and use com.google.android.material Objects I found out that when I change the line in themes: <style name="Theme.TeoriaLearning" parent="Theme.MaterialComponents.Light.DarkActionBar"> to this line: <style name="Theme.TeoriaLearning" parent="Theme.AppCompat.Light.DarkActionBar"> I can change the background color of Buttons.. etc.

But also when I do that I can't use com.google.android.material.textfield.TextInputLayout and com.google.android.material.textfield.TextInputEditText or any other com.google.android.material Object

(After I change the line) when I try to start the Activity the app crash and I get these errors:

Logcat errors

2021-04-03 19:22:05.558 29774-29774/com.example.teorialearning E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.teorialearning, PID: 29774
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teorialearning/com.example.teorialearning.Activies.RegisterActivity}: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
     Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:92)
        at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:86)
        at com.example.teorialearning.Activies.RegisterActivity.onCreate(RegisterActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:6679)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:187)
        at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:118)
        at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:463)
        at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:422)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:645) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:861) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
        at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:92) 
        at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:86) 
        at com.example.teorialearning.Activies.RegisterActivity.onCreate(RegisterActivity.java:19) 
        at android.app.Activity.performCreate(Activity.java:6679) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

So what can I do to both use these Objects and have the ability to change background color?

If you need any code just ask and I will edit and mention you :)

Upvotes: 1

Views: 202

Answers (2)

SooryaSRajan
SooryaSRajan

Reputation: 64

You can use the setBackground(Drawable background) method on the buttons view object to set a background through code. The Drawable object can either be extracted from the res folder or you can generate a solid or gradient shape with rounded borders if you desire and pass it to the setBackground() method.

It is invoked like this:

buttonObject.setBackground(background);

Upvotes: 1

Shay Kin
Shay Kin

Reputation: 2667

To use the TextInputEditText you must use a Material style. so keep your style like this :

<style name="Theme.TeoriaLearning" parent="Theme.MaterialComponents.Light.DarkActionBar">

And for your first problem to change the button background use this in your xml layout :

    app:backgroundTint="@color/YourColor"

for example :

  <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
        app:backgroundTint="@color/purple_700"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

And to change it programmatically :

MaterialButton button = findViewById(R.id.button);
        
button.setBackgroundColor(ContextCompat.getColor(this,R.color.black));

For normal effect use this style in your button :

style="@style/Widget.MaterialComponents.Button.UnelevatedButton"

EDIT

as @GabrieleMariotti suggested the better way is using setBackgroundTintList() and this is an example how to use it :

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MaterialButton button = findViewById(R.id.button);
    int[][] states = new int[][]{
            new int[]{android.R.attr.state_enabled}, // enabled
            new int[]{-android.R.attr.state_enabled}, // disabled
            new int[]{android.R.attr.state_pressed}  // pressed
    };

    int[] colors = new int[]{
            getColorRes(R.color.purple_700), // enabled color
            getColorRes(R.color.purple_200), // disabled color
            getColorRes(R.color.purple_500) // pressed color
    };
    button.setBackgroundTintList(new ColorStateList(states, colors));

}

private int getColorRes(@ColorRes int colorRes) {
    return ContextCompat.getColor(this, colorRes);
}

Upvotes: 2

Related Questions