Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #36 from zeoflow/material-button
Browse files Browse the repository at this point in the history
Updated Material Button
  • Loading branch information
teogor authored Oct 2, 2020
2 parents 842f606 + 93f853c commit ef97fa3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (C) 2020 ZeoFlow
*
/**
* Copyright 2020 ZeoFlow SRL
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -16,12 +16,14 @@

package com.zeoflow.material.elements.button;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
Expand All @@ -47,6 +49,7 @@
import androidx.annotation.RestrictTo;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import androidx.core.widget.TextViewCompat;
Expand All @@ -73,7 +76,7 @@
* <p>This class supplies updated Material styles for the button in the constructor. The widget will
* display the correct default Material styles without the use of the style flag.
*
* <p>All attributes from {@link com.zeoflow.material.elements.R.styleable#MaterialButton} are
* <p>All attributes from {@link com.zeoflow.material.elements} are
* supported. Do not use the {@code android:background} attribute. MaterialButton manages its own
* background drawable, and setting a new background means {@link MaterialButton} can no longer
* guarantee that the new attributes it introduces will function properly. If the default background
Expand Down Expand Up @@ -167,6 +170,7 @@ public class MaterialButton extends AppCompatButton implements Checkable, Shapea
private boolean broadcasting = false;
@IconGravity
private int iconGravity;
private int zFont;

public MaterialButton(@NonNull Context context)
{
Expand All @@ -178,11 +182,13 @@ public MaterialButton(@NonNull Context context, @Nullable AttributeSet attrs)
this(context, attrs, R.attr.materialButtonStyle);
}

private Context zContext;
public MaterialButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)
{
super(MaterialThemeOverlay.wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
// Ensure we are using the correctly themed context rather than the context that was passed in.
context = getContext();
zContext = context;

TypedArray attributes =
ThemeEnforcement.obtainStyledAttributes(
Expand All @@ -203,6 +209,13 @@ public MaterialButton(@NonNull Context context, @Nullable AttributeSet attrs, in
ShapeAppearanceModel shapeAppearanceModel =
ShapeAppearanceModel.builder(context, attrs, defStyleAttr, DEF_STYLE_RES).build();

zFont = attributes.getResourceId(R.styleable.MaterialButton_mbFontFamily, 0);
if (zFont != 0)
{
Typeface typeface = ResourcesCompat.getFont(context, zFont);
setTypeface(typeface, Typeface.BOLD);
}

// Loads and sets background drawable attributes
materialButtonHelper = new MaterialButtonHelper(this, shapeAppearanceModel);
materialButtonHelper.loadFromAttributes(attributes);
Expand All @@ -213,6 +226,13 @@ public MaterialButton(@NonNull Context context, @Nullable AttributeSet attrs, in
updateIcon(/*needsIconUpdate=*/icon != null);
}

public void setFont(int font)
{
this.zFont = font;
Typeface typeface = ResourcesCompat.getFont(zContext, font);
setTypeface(typeface, Typeface.BOLD);
}

@NonNull
private String getA11yClassName()
{
Expand Down Expand Up @@ -267,6 +287,7 @@ public void onRestoreInstanceState(@Nullable Parcelable state)
*
* @hide
*/
@SuppressLint("RestrictedApi")
@RestrictTo(LIBRARY_GROUP)
@Override
@Nullable
Expand All @@ -290,6 +311,7 @@ public ColorStateList getSupportBackgroundTintList()
*
* @hide
*/
@SuppressLint("RestrictedApi")
@RestrictTo(LIBRARY_GROUP)
@Override
public void setSupportBackgroundTintList(@Nullable ColorStateList tint)
Expand All @@ -311,6 +333,7 @@ public void setSupportBackgroundTintList(@Nullable ColorStateList tint)
*
* @hide
*/
@SuppressLint("RestrictedApi")
@RestrictTo(LIBRARY_GROUP)
@Override
@Nullable
Expand All @@ -333,6 +356,7 @@ public PorterDuff.Mode getSupportBackgroundTintMode()
*
* @hide
*/
@SuppressLint("RestrictedApi")
@RestrictTo(LIBRARY_GROUP)
@Override
public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
Expand Down Expand Up @@ -130,7 +131,6 @@ private void loadBtn()
rippleDefault = attributes.getBoolean(R.styleable.MaterialLoadingButton_mlbRippleDefault, true);
zFont = attributes.getResourceId(R.styleable.MaterialLoadingButton_mlbFontFamily, 0);


zButton = findViewById(R.id.btn);
if (zFont != 0)
{
Expand Down Expand Up @@ -161,27 +161,32 @@ public void setFont(int font)
zButton.setTypeface(typeface, Typeface.BOLD);
}

public void setText(String text) {
public void setText(String text)
{
this.text = text;
zButton.setText(text);
}

public void setTextColor(int textColor) {
public void setTextColor(int textColor)
{
this.textColor = textColor;
zButton.setTextColor(textColor);
}

public void setRippleColor(int rippleColor) {
public void setRippleColor(int rippleColor)
{
this.rippleColor = rippleColor;
zButton.setRippleColor(ColorStateList.valueOf(rippleColor));
}

public void setRippleColor(ColorStateList rippleColor) {
public void setRippleColor(ColorStateList rippleColor)
{
this.rippleColor = rippleColor.getDefaultColor();
zButton.setRippleColor(rippleColor);
}

public void setAccentColor(int accentColor) {
public void setAccentColor(int accentColor)
{
this.accentColor = accentColor;
if (buttonDesign == BUTTON_DESIGN_OUTLINED)
{
Expand All @@ -202,17 +207,20 @@ public void setAccentColor(int accentColor) {
}
}

public void setRippleDeafult(boolean rippleDefault) {
public void setRippleDeafult(boolean rippleDefault)
{
this.rippleDefault = rippleDefault;
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void setLoadingColor(int loadingColor) {
public void setLoadingColor(int loadingColor)
{
zPbar.setIndeterminateTintList(ColorStateList.valueOf(loadingColor));
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void setLoadingColor(ColorStateList loadingColor) {
public void setLoadingColor(ColorStateList loadingColor)
{
zPbar.setIndeterminateTintList(loadingColor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<attr name="mlbLoadingColor" format="color" />
<attr name="mlbAccentColor" format="color" />
<attr name="mlbRippleDefault" format="boolean" />
<attr name="mlbFontFamily" format="reference" />
<attr name="mlbFontFamily" format="boolean" />
</declare-styleable>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,4 @@
<!-- Menu/item attributes -->
<style name="Theme.MaterialElements.CompactMenu" parent="Base.Theme.MaterialElements.CompactMenu" />

<!-- Deprecated. Use Theme.MaterialElements instead. -->
<style name="Theme.Design" parent="Theme.AppCompat"></style>

<!-- Deprecated. Use Theme.MaterialElements.Light instead. -->
<style name="Theme.Design.Light" parent="Theme.AppCompat.Light"></style>

<!-- Deprecated. Use Theme.MaterialElements.NoActionBar instead. -->
<style name="Theme.Design.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<!-- Deprecated. Use Theme.MaterialElements.Light.NoActionBar instead. -->
<style name="Theme.Design.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

</resources>
25 changes: 11 additions & 14 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,24 @@
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:mlbAccentColor="#2575fc"
app:mlbBackgroundColor="#FFFFFF"
app:mlbButtonDesign="outlined"
app:mlbButtonDesign="filled"
app:mlbLoadingColor="#2575fc"
app:mlbLoadingDesign="circle"
app:mlbRippleDefault="true"
app:mlbStrokeWidth="2dp"
app:mlbText="Login"
app:mlbTextColor="#2575fc"
app:mlbTextColor="#FFFFFF"
app:mlbFontFamily="@font/zeoflow_regular"/>

<com.zeoflow.material.elements.button.MaterialButtonLoading
<com.zeoflow.material.elements.button.MaterialButton
style="@style/MaterialElements.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:mlbAccentColor="#2575fc"
app:mlbButtonDesign="filled"
app:mlbLoadingColor="#2575fc"
app:mlbLoadingDesign="circle"
app:mlbRippleDefault="true"
app:mlbText="Login"
app:mlbTextColor="#FFFFFF"
app:mlbFontFamily="@font/zeoflow_regular"/>
android:text="login_btn"
android:textAllCaps="false"
android:textColor="#2575fc"
app:strokeColor="#2575fc"
app:strokeWidth="2dp"
app:textAllCaps="false"
app:mbFontFamily="@font/zeoflow_regular" />

</LinearLayout>

0 comments on commit ef97fa3

Please sign in to comment.