Skip to content

Commit

Permalink
add orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
Far5had70 committed Nov 6, 2018
1 parent 10ddc36 commit 2152bf0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Add it in your root build.gradle at the end of repositories:

```gradle
dependencies {
implementation 'com.github.Far5had70:GradientButton:1.0.0'
implementation 'com.github.Far5had70:GradientButton:1.1.0'
}
```

Expand All @@ -50,7 +50,7 @@ Step 2. Add the dependency

## Demo

You can try it out here [Sample Application](https://github.com/Far5had70/GradientButton/blob/master/app/src/main/res/layout/activity_main.xml)
You can try it out here [Sample View](https://github.com/Far5had70/GradientButton/blob/master/app/src/main/res/layout/activity_main.xml)



Expand All @@ -62,7 +62,7 @@ You can try it out here [Sample Application](https://github.com/Far5had70/Gradie
**Example:**

```xml
<com.waspar.gradientbutton.GradientButton
<com.waspar.gradientbutton.GradientButton
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
Expand All @@ -74,6 +74,7 @@ You can try it out here [Sample Application](https://github.com/Far5had70/Gradie
app:startColor="@color/colorBlue_A400"
app:endColor="@color/colorBlue_900"
app:textColor="#fff"
app:orientation="right_left"
/>
```

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
app:startColor="@color/colorBlue_A400"
app:endColor="@color/colorBlue_900"
app:textColor="#fff"
app:orientation="right_left"
/>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ public class GradientButton extends FrameLayout {
private TextView textView;
private View root;

public static int RIGHT_LEFT = 1;
public static int LEFT_RIGHT = 2;
public static int BOTTOM_TOP = 3;
public static int TOP_BOTTOM = 4;

public GradientButton(@NonNull Context context) {
super(context);
initView();
}

public GradientButton(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context , attrs);
initView(context, attrs);
}

public GradientButton(Context context, AttributeSet attrs, int defStyleAttr) {
Expand Down Expand Up @@ -61,21 +66,23 @@ private void initView(Context context, AttributeSet attrs) {
mEndColor = context.getResources().getColor(mEndColor);
}

int orientation = ta.getInteger(R.styleable.GradientButton_orientation, 1);

String Font = ta.getString(R.styleable.GradientButton_typeface);

if (Font != null){
setFont(context , Font);
if (Font != null) {
setFont(context, Font);
}

ColorStateList color = ta.getColorStateList(R.styleable.GradientButton_textColor);

setRadius(Radius*2 , mStartColor , mEndColor);
setRadius(Radius * 2, mStartColor, mEndColor, orientation);

setText(Text);

setTextSize((float) (textSize*0.4));
setTextSize((float) (textSize * 0.4));

if (color != null){
if (color != null) {
setTextColor(color);
}

Expand All @@ -87,12 +94,12 @@ private void initView(Context context, AttributeSet attrs) {
addView(view);
}

public void setFont(Context context , String font) {
textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));
public void setFont(Context context, String font) {
textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/" + font));
}

public void setTextColor(ColorStateList color){
if (color != null){
public void setTextColor(ColorStateList color) {
if (color != null) {
textView.setTextColor(color);
}
}
Expand All @@ -105,9 +112,29 @@ public void setText(String Text) {
textView.setText(Text);
}

public void setRadius(float Radius , int start , int end) {
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.RIGHT_LEFT, new int[] {start, end });
drawable.setCornerRadii(new float[] { Radius, Radius, Radius, Radius, Radius, Radius, Radius, Radius });
public void setRadius(float Radius, int start, int end, int Orientation) {

GradientDrawable.Orientation orientation = null;

switch (Orientation) {
case 1:
orientation = GradientDrawable.Orientation.RIGHT_LEFT;
break;
case 2:
orientation = GradientDrawable.Orientation.LEFT_RIGHT;
break;
case 3:
orientation = GradientDrawable.Orientation.BOTTOM_TOP;
break;
case 4:
orientation = GradientDrawable.Orientation.TOP_BOTTOM;
break;
default:
orientation = GradientDrawable.Orientation.RIGHT_LEFT;
}

GradientDrawable drawable = new GradientDrawable(orientation, new int[]{start, end});
drawable.setCornerRadii(new float[]{Radius, Radius, Radius, Radius, Radius, Radius, Radius, Radius});
root.setBackground(drawable);
}
}
8 changes: 8 additions & 0 deletions gradientbutton/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
<attr name="endColor" format="reference"/>
<attr name="textColor" format="color"/>
<attr name="typeface" format="string"/>
<attr name="orientation"/>
</declare-styleable>

<attr name="orientation">
<flag name="right_left" value="1" />
<flag name="left_right" value="2" />
<flag name="bottom_top" value="3" />
<flag name="top_bottom" value="4" />
</attr>

</resources>

0 comments on commit 2152bf0

Please sign in to comment.