Skip to content

Commit 8664caa

Browse files
author
Rygel Louv
committed
Build the actual library
1 parent 217ecc4 commit 8664caa

File tree

8 files changed

+326
-1
lines changed

8 files changed

+326
-1
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package com.github.rygelouv.androidloadingbuttonlib;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.graphics.drawable.Drawable;
6+
import android.support.annotation.NonNull;
7+
import android.text.TextUtils;
8+
import android.util.AttributeSet;
9+
import android.view.View;
10+
import android.widget.Button;
11+
import android.widget.LinearLayout;
12+
import android.widget.ProgressBar;
13+
import android.widget.RelativeLayout;
14+
15+
/**
16+
* Created by rygelouv on 9/21/17.
17+
* <p>
18+
* AndroidLoadingButton
19+
* Copyright (c) 2017 Rygel Louv All rights reserved.
20+
*/
21+
22+
public class LoadingButton extends LinearLayout
23+
{
24+
private static final int TEXT_DIFF = 14;
25+
View mRootView;
26+
private int textColor;
27+
private String text;
28+
private int backgroundColor;
29+
private Drawable background;
30+
private int textSize;
31+
private int progressColor;
32+
private Button mTextButton;
33+
private ProgressBar mProgressBar;
34+
35+
public LoadingButton(Context context)
36+
{
37+
super(context);
38+
init(context);
39+
}
40+
41+
public LoadingButton(Context context, AttributeSet attrs)
42+
{
43+
super(context, attrs);
44+
getAttributes(context, attrs);
45+
init(context);
46+
}
47+
48+
public LoadingButton(Context context, AttributeSet attrs, int defStyleAttr)
49+
{
50+
super(context, attrs, defStyleAttr);
51+
getAttributes(context, attrs);
52+
init(context);
53+
}
54+
55+
private void getAttributes(Context context, AttributeSet attrs)
56+
{
57+
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LoadingButton, 0, 0);
58+
this.text = ta.getString(R.styleable.LoadingButton_text);
59+
this.textColor = ta.getColor(R.styleable.LoadingButton_textColor,
60+
getResources().getColor(android.R.color.black));
61+
this.textSize = ta.getDimensionPixelOffset(R.styleable.LoadingButton_textSize, 0);
62+
this.backgroundColor = ta.getColor(R.styleable.LoadingButton_backgroundColor,
63+
getResources().getColor(android.R.color.background_dark));
64+
this.background = ta.getDrawable(R.styleable.LoadingButton_background);
65+
this.progressColor = ta.getColor(R.styleable.LoadingButton_progressColor,
66+
getResources().getColor(android.R.color.black));
67+
68+
ta.recycle();
69+
}
70+
71+
private void init(Context context)
72+
{
73+
this.mRootView = inflate(context, R.layout.loading_button_layout, this);
74+
this.mTextButton = mRootView.findViewById(R.id.button);
75+
this.mProgressBar = mRootView.findViewById(R.id.progress);
76+
77+
if (!TextUtils.isEmpty(text))
78+
mTextButton.setText(text);
79+
80+
if (textColor != 0)
81+
mTextButton.setTextColor(textColor);
82+
83+
if (textSize != 0)
84+
mTextButton.setTextSize(textSize - (textSize - TEXT_DIFF));
85+
86+
if (backgroundColor != 0)
87+
this.mRootView.setBackgroundColor(backgroundColor);
88+
89+
if (background != null)
90+
this.mRootView.setBackground(background);
91+
92+
if (progressColor != 0)
93+
this.mProgressBar.getIndeterminateDrawable().setColorFilter(progressColor,
94+
android.graphics.PorterDuff.Mode.MULTIPLY);
95+
96+
this.mRootView.setOnClickListener(new OnClickListener()
97+
{
98+
@Override
99+
public void onClick(View view)
100+
{
101+
startLoading("Loading...");
102+
}
103+
});
104+
this.mTextButton.setOnClickListener(new OnClickListener()
105+
{
106+
@Override
107+
public void onClick(View view)
108+
{
109+
startLoading("Loading...");
110+
}
111+
});
112+
}
113+
114+
public void startLoading(String loadingText)
115+
{
116+
this.setEnabled(false);
117+
this.mProgressBar.setVisibility(VISIBLE);
118+
this.mTextButton.setText(loadingText);
119+
}
120+
121+
public void stopLoading(String loadingDoneText)
122+
{
123+
this.setEnabled(true);
124+
this.mProgressBar.setVisibility(GONE);
125+
this.mTextButton.setText(loadingDoneText);
126+
}
127+
128+
public int getTextColor()
129+
{
130+
return textColor;
131+
}
132+
133+
public void setTextColor(int textColor)
134+
{
135+
this.textColor = textColor;
136+
mTextButton.setTextColor(textColor);
137+
}
138+
139+
public String getText()
140+
{
141+
return text;
142+
}
143+
144+
public void setText(@NonNull String text)
145+
{
146+
this.text = text;
147+
mTextButton.setText(text);
148+
}
149+
150+
public int getCustomBackgroundColor()
151+
{
152+
return backgroundColor;
153+
}
154+
155+
public void setCustomBackgroundColor(int backgroundColor)
156+
{
157+
this.backgroundColor = backgroundColor;
158+
mRootView.setBackgroundColor(backgroundColor);
159+
}
160+
161+
public Drawable getCustomBackground()
162+
{
163+
return background;
164+
}
165+
166+
public void setCustomBackground(@NonNull Drawable background)
167+
{
168+
this.background = background;
169+
mRootView.setBackground(background);
170+
}
171+
172+
public int getTextSize()
173+
{
174+
return textSize;
175+
}
176+
177+
public void setTextSize(int textSize)
178+
{
179+
this.textSize = textSize;
180+
mTextButton.setTextSize(textSize - (textSize - TEXT_DIFF));
181+
}
182+
183+
public int getProgressColor()
184+
{
185+
return progressColor;
186+
}
187+
188+
public void setProgressColor(int progressColor)
189+
{
190+
this.progressColor = progressColor;
191+
mProgressBar.getIndeterminateDrawable().setColorFilter(progressColor,
192+
android.graphics.PorterDuff.Mode.MULTIPLY);
193+
}
194+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:gravity="center_horizontal"
7+
>
8+
9+
<ProgressBar
10+
android:id="@+id/progress"
11+
android:layout_width="30dp"
12+
android:layout_height="30dp"
13+
android:layout_gravity="center_vertical"
14+
android:layout_marginLeft="4dp"
15+
android:visibility="gone"
16+
/>
17+
18+
<Button
19+
android:id="@+id/button"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
tools:text="validate"
23+
android:background="@android:color/transparent"
24+
android:layout_marginRight="4dp"
25+
/>
26+
27+
</LinearLayout>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<declare-styleable name="LoadingButton">
4+
<attr name="text" format="string" localization="suggested" />
5+
<attr name="textColor" format="color" />
6+
<attr name="backgroundColor" format="color"/>
7+
<attr name="background" format="reference"/>
8+
<attr name="textSize" format="dimension"/>
9+
<attr name="progressColor" format="color"/>
10+
</declare-styleable>
11+
</resources>

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
1212
<activity android:name=".MainActivity">
13+
</activity>
14+
<activity android:name=".TestLibActivity">
1315
<intent-filter>
1416
<action android:name="android.intent.action.MAIN"/>
15-
1617
<category android:name="android.intent.category.LAUNCHER"/>
1718
</intent-filter>
1819
</activity>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.rygelouv.androidloadingbutton;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
import android.view.View;
6+
7+
import com.github.rygelouv.androidloadingbuttonlib.LoadingButton;
8+
9+
public class TestLibActivity extends AppCompatActivity
10+
{
11+
12+
@Override
13+
protected void onCreate(Bundle savedInstanceState)
14+
{
15+
super.onCreate(savedInstanceState);
16+
setContentView(R.layout.activity_test_lib);
17+
18+
findViewById(R.id.stop).setOnClickListener(new View.OnClickListener()
19+
{
20+
@Override
21+
public void onClick(View view)
22+
{
23+
((LoadingButton)findViewById(R.id.button_test))
24+
.stopLoading("Done!");
25+
}
26+
});
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
3+
<item android:state_pressed="true" >
4+
<shape android:shape="rectangle" >
5+
<corners android:radius="4dip" />
6+
<stroke android:width="1dip"
7+
android:color="@color/gray_dark" />
8+
<gradient android:angle="-90"
9+
android:startColor="@color/gray_dark"
10+
android:endColor="@color/gray_dark" />
11+
</shape>
12+
</item>
13+
<item android:state_focused="true">
14+
<shape android:shape="rectangle" >
15+
<corners android:radius="4dip" />
16+
<stroke android:width="1dip"
17+
android:color="@color/gray_dark" />
18+
<solid android:color="@color/gray_dark"/>
19+
</shape>
20+
</item>
21+
<item >
22+
<shape android:shape="rectangle" >
23+
<corners android:radius="4dip" />
24+
<stroke android:width="1dip"
25+
android:color="@color/gray_dark" />
26+
<gradient android:angle="-90"
27+
android:startColor="@color/gray_dark"
28+
android:endColor="@color/gray_dark" />
29+
</shape>
30+
</item>
31+
</selector>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
xmlns:custom="http://schemas.android.com/apk/res-auto"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context="com.github.rygelouv.androidloadingbutton.TestLibActivity">
9+
10+
<com.github.rygelouv.androidloadingbuttonlib.LoadingButton
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:id="@+id/button_test"
14+
custom:text="Validate"
15+
custom:textColor="@android:color/white"
16+
custom:background="@drawable/button_accent_full"
17+
custom:textSize="12sp"
18+
android:layout_margin="16dp"
19+
custom:progressColor="@android:color/white"
20+
/>
21+
22+
23+
<Button
24+
android:id="@+id/stop"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:text="stop"
28+
android:layout_below="@+id/button_test"
29+
android:layout_margin="16dp"
30+
/>
31+
32+
</RelativeLayout>

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<color name="colorPrimary">#3F51B5</color>
44
<color name="colorPrimaryDark">#303F9F</color>
55
<color name="colorAccent">#FF4081</color>
6+
<color name="gray_dark">#4e5e69</color>
67
</resources>

0 commit comments

Comments
 (0)