Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom text styles, buttons text size and background color #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,11 @@ gradle-app.setting
.DS_Store
/build
/captures
.idea/

## VScode files
.project
.settings/

###
projectFilesBackup/
22 changes: 0 additions & 22 deletions .idea/compiler.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

19 changes: 0 additions & 19 deletions .idea/gradle.xml

This file was deleted.

46 changes: 0 additions & 46 deletions .idea/misc.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Grab the latest version on gradle using

```groovy
compile 'com.cepheuen.elegant-number-button:lib:1.0.2'
implementation 'com.cepheuen.elegant-number-button:lib:1.0.2'
```
or on maven

Expand Down Expand Up @@ -62,17 +62,31 @@ For Working implementation of this library check ElegantNumberButtonSample App

## Customization

`backgroundColor` : Set button Background color
`backgroundColor` : Set the view Background color.

`minusBackgroundColor` : Set the `-` control Background color.

`plusBackgroundColor` : Set the `+` control Background color.

`initialNumber`: Set initial number for the button.

`finalNumber` : Set final number range for button.

`textColor`: Modify the text color of the button.
`textColor`: Modify the text color of the counter.

`minusTextColor`: Modify the text color of the `-` control.

`plusTextColor`: Modify the text color of the `+` control.

`textStyle`: Modify the text style of the counter to one of [Bold, Italic, Normal].

`buttonTextStyle`: Modify the text style of both controls to one of [Bold, Italic, Normal].

`textSize`: Change text size of the counter.

`textSize`: Change text size of the button.
`buttonTextSize`: Change text size of both the controls.

`backgroundDrawable`: Add drawable background for the button.
`backgroundDrawable`: Add drawable background for the view.

## Methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.view.GravityCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
Expand All @@ -19,6 +22,7 @@

/**
* Created by Ashik Vetrivelu on 10/08/16.
* Modified by Dennis Murage on 24/03/2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove meta data

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

*/
public class ElegantNumberButton extends RelativeLayout {
private Context context;
Expand All @@ -31,7 +35,6 @@ public class ElegantNumberButton extends RelativeLayout {
private int finalNumber;
private TextView textView;
private Button addBtn, subtractBtn;
private View view;
private OnValueChangeListener mOnValueChangeListener;

public ElegantNumberButton(Context context) {
Expand All @@ -56,7 +59,6 @@ public ElegantNumberButton(Context context, AttributeSet attrs, int defStyleAttr
}

private void initView() {
this.view = this;
inflate(context, R.layout.layout, this);
final Resources res = getResources();
final int defaultColor = res.getColor(R.color.colorPrimary);
Expand All @@ -68,22 +70,47 @@ private void initView() {
initialNumber = a.getInt(R.styleable.ElegantNumberButton_initialNumber, 0);
finalNumber = a.getInt(R.styleable.ElegantNumberButton_finalNumber, Integer.MAX_VALUE);
float textSize = a.getDimension(R.styleable.ElegantNumberButton_textSize, 13);
float buttonTextSize = a.getDimension(R.styleable.ElegantNumberButton_buttonTextSize, 13);
String textStyle = "normal";
textStyle = a.getString(R.styleable.ElegantNumberButton_textStyle);
String buttonTextStyle = "normal";
buttonTextStyle = a.getString(R.styleable.ElegantNumberButton_buttonTextStyle);
int color = a.getColor(R.styleable.ElegantNumberButton_backGroundColor, defaultColor);
int minusBackgroundColor = a.getColor(R.styleable.ElegantNumberButton_minusBackgroundColor, defaultColor);
int plusBackgroundColor = a.getColor(R.styleable.ElegantNumberButton_plusBackgroundColor, defaultColor);
int textColor = a.getColor(R.styleable.ElegantNumberButton_textColor, defaultTextColor);
int minusTextColor = a.getColor(R.styleable.ElegantNumberButton_minusTextColor, defaultTextColor);
int plusTextColor = a.getColor(R.styleable.ElegantNumberButton_plusTextColor, defaultTextColor);
Drawable drawable = a.getDrawable(R.styleable.ElegantNumberButton_backgroundDrawable);

subtractBtn = (Button) findViewById(R.id.subtract_btn);
addBtn = (Button) findViewById(R.id.add_btn);
textView = (TextView) findViewById(R.id.number_counter);
LinearLayout mLayout = (LinearLayout) findViewById(R.id.layout);

subtractBtn.setTextColor(textColor);
addBtn.setTextColor(textColor);
subtractBtn.setTextColor(minusTextColor);
addBtn.setTextColor(plusTextColor);
textView.setTextColor(textColor);
subtractBtn.setTextSize(textSize);
addBtn.setTextSize(textSize);

subtractBtn.setBackgroundColor(minusBackgroundColor);
addBtn.setBackgroundColor(plusBackgroundColor);
mLayout.setBackgroundColor(color);

subtractBtn.setTextSize(buttonTextSize);
addBtn.setTextSize(buttonTextSize);
textView.setTextSize(textSize);

int mButtonStyle = buttonTextStyle.equals("bold") ? Typeface.BOLD :
buttonTextStyle.equals("italic") ? Typeface.ITALIC : Typeface.NORMAL;

int mTextStyle = textStyle.equals("bold") ? Typeface.BOLD :
textStyle.equals("italic") ? Typeface.ITALIC : Typeface.NORMAL;

addBtn.setTypeface(addBtn.getTypeface(), mButtonStyle);
subtractBtn.setTypeface(subtractBtn.getTypeface(), mButtonStyle);

textView.setTypeface(textView.getTypeface(), mTextStyle);

if (drawable == null) {
drawable = defaultDrawable;
}
Expand Down
9 changes: 7 additions & 2 deletions lib/src/main/res/layout/layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/subtract_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:gravity="center"
android:background="@android:color/transparent"
android:text="-" />

<TextView
android:id="@+id/number_counter"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center" />
Expand All @@ -27,6 +30,8 @@
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textAlignment="center"
android:background="@android:color/transparent"
android:text="+" />

Expand Down
7 changes: 7 additions & 0 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
<declare-styleable name="ElegantNumberButton">

<attr name="backGroundColor" format="color"/>
<attr name="minusBackgroundColor" format="color"/>
<attr name="plusBackgroundColor" format="color"/>
<attr name="initialNumber" format="integer"/>
<attr name="finalNumber" format="integer" />
<attr name="textColor" format="color"/>
<attr name="minusTextColor" format="color"/>
<attr name="plusTextColor" format="color"/>
<attr name="textStyle" format="string"/>
<attr name="buttonTextStyle" format="string"/>
<attr name="textSize" format="dimension"/>
<attr name="buttonTextSize" format="dimension"/>
<attr name="backgroundDrawable" format="reference"/>
</declare-styleable>
</resources>
Loading