Skip to content

Commit 2aaa4ba

Browse files
committed
Buttons added in all empty layouts
1 parent 5505c1b commit 2aaa4ba

File tree

11 files changed

+208
-42
lines changed

11 files changed

+208
-42
lines changed

EmptyLayout/res/layout/view_empty.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@
1616
android:textAppearance="?android:attr/textAppearanceMedium"
1717
android:gravity="center_horizontal" />
1818

19+
<Button
20+
android:id="@+id/buttonEmpty"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:layout_gravity="center_horizontal"
24+
android:layout_marginTop="16dp"
25+
android:text="@string/empty_button" />
26+
1927
</LinearLayout>

EmptyLayout/res/layout/view_error.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@
1515
android:textAppearance="?android:attr/textAppearanceMedium"
1616
android:gravity="center_horizontal" />
1717

18+
<Button
19+
android:id="@+id/buttonError"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_gravity="center_horizontal"
23+
android:text="@string/error_button"
24+
android:layout_marginTop="16dp" />
25+
1826
</LinearLayout>

EmptyLayout/res/layout/view_loading.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@
2222
android:gravity="center_horizontal"
2323
android:layout_gravity="center_horizontal" />
2424

25+
<Button
26+
android:id="@+id/buttonLoading"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:layout_gravity="center_horizontal"
30+
android:text="@string/loading_button" />
31+
2532
</LinearLayout>

EmptyLayout/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
<string name="empty_message">No items yet</string>
99
<string name="loading_message">Please wait</string>
1010
<string name="error_message">Oops! Something wrong happened</string>
11+
12+
<string name="empty_button">Try again</string>
13+
<string name="loading_button">Cancel</string>
14+
<string name="error_button">Try again</string>
1115

1216
</resources>

EmptyLayout/src/com/kanak/emptylayout/EmptyLayout.java

Lines changed: 171 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ public class EmptyLayout {
2727
private int mErrorMessageViewId;
2828
private int mEmptyMessageViewId;
2929
private int mLoadingMessageViewId;
30-
31-
private View.OnClickListener mLoadingViewClickListener;
32-
private View.OnClickListener mEmptyViewClickListener;
33-
private View.OnClickListener mErrorViewClickListener;
30+
private LayoutInflater mInflater;
31+
private boolean mViewsAdded;
32+
private int mLoadingAnimationViewId;
33+
private View.OnClickListener mLoadingButtonClickListener;
34+
private View.OnClickListener mEmptyButtonClickListener;
35+
private View.OnClickListener mErrorButtonClickListener;
3436

3537
// ---------------------------
3638
// static variables
@@ -55,9 +57,12 @@ public class EmptyLayout {
5557
private String mErrorMessage = "Oops! Something wrong happened";
5658
private String mEmptyMessage = "No items yet";
5759
private String mLoadingMessage = "Please wait";
58-
private LayoutInflater mInflater;
59-
private boolean mViewsAdded;
60-
private int mLoadingAnimationViewId;
60+
private int mLoadingViewButtonId = R.id.buttonLoading;
61+
private int mErrorViewButtonId = R.id.buttonError;
62+
private int mEmptyViewButtonId = R.id.buttonEmpty;
63+
private boolean mShowEmptyButton = true;
64+
private boolean mShowLoadingButton = true;
65+
private boolean mShowErrorButton = true;
6166

6267
// ---------------------------
6368
// getters and setters
@@ -283,69 +288,161 @@ public int getLoadingAnimationViewId() {
283288
*/
284289
public void setLoadingAnimationViewId(int loadingAnimationViewId) {
285290
this.mLoadingAnimationViewId = loadingAnimationViewId;
286-
}
291+
}
292+
293+
/**
294+
* Gets the OnClickListener which perform when LoadingView was click
295+
* @return
296+
*/
297+
public View.OnClickListener getLoadingButtonClickListener() {
298+
return mLoadingButtonClickListener;
299+
}
287300

288301
/**
289302
* Sets the OnClickListener to LoadingView
290-
* @param loadingViewClickListener OnClickListener Object
303+
* @param loadingButtonClickListener OnClickListener Object
304+
*/
305+
public void setLoadingButtonClickListener(View.OnClickListener loadingButtonClickListener) {
306+
this.mLoadingButtonClickListener = loadingButtonClickListener;
307+
}
308+
309+
/**
310+
* Gets the OnClickListener which perform when EmptyView was click
311+
* @return
291312
*/
292-
public void setLoadingViewClickListener(View.OnClickListener loadingViewClickListener) {
293-
this.mLoadingViewClickListener = loadingViewClickListener;
294-
if(mLoadingView != null)
295-
mLoadingView.setOnClickListener(mLoadingViewClickListener);
313+
public View.OnClickListener getEmptyButtonClickListener() {
314+
return mEmptyButtonClickListener;
296315
}
297316

298317
/**
299318
* Sets the OnClickListener to EmptyView
300-
* @param emptyViewClickListener OnClickListener Object
319+
* @param emptyButtonClickListener OnClickListener Object
320+
*/
321+
public void setEmptyButtonClickListener(View.OnClickListener emptyButtonClickListener) {
322+
this.mEmptyButtonClickListener = emptyButtonClickListener;
323+
}
324+
325+
/**
326+
* Gets the OnClickListener which perform when ErrorView was click
327+
* @return
301328
*/
302-
public void setEmptyViewClickListener(View.OnClickListener emptyViewClickListener) {
303-
this.mEmptyViewClickListener = emptyViewClickListener;
304-
if(mEmptyView != null)
305-
mEmptyView.setOnClickListener(mEmptyViewClickListener);
329+
public View.OnClickListener getErrorButtonClickListener() {
330+
return mErrorButtonClickListener;
306331
}
307332

308333
/**
309334
* Sets the OnClickListener to ErrorView
310-
* @param errorViewClickListener OnClickListener Object
335+
* @param errorButtonClickListener OnClickListener Object
311336
*/
312-
public void setErrorViewClickListener(View.OnClickListener errorViewClickListener) {
313-
this.mErrorViewClickListener = errorViewClickListener;
314-
if(mErrorView != null)
315-
mErrorView.setOnClickListener(mErrorViewClickListener);
337+
public void setErrorButtonClickListener(View.OnClickListener errorButtonClickListener) {
338+
this.mErrorButtonClickListener = errorButtonClickListener;
316339
}
317340

341+
/**
342+
* Gets if a button is shown in the empty view
343+
* @return if a button is shown in the empty view
344+
*/
345+
public boolean isEmptyButtonShown() {
346+
return mShowEmptyButton;
347+
}
318348

319349
/**
320-
* Gets the OnClickListener which perform when LoadingView was click
321-
* @return
350+
* Sets if a button will be shown in the empty view
351+
* @param showEmptyButton will a button be shown in the empty view
322352
*/
323-
public View.OnClickListener getLoadingViewClickListener() {
324-
return mLoadingViewClickListener;
325-
}
353+
public void setEmptyButtonShown(boolean showEmptyButton) {
354+
this.mShowEmptyButton = showEmptyButton;
355+
}
326356

357+
/**
358+
* Gets if a button is shown in the loading view
359+
* @return if a button is shown in the loading view
360+
*/
361+
public boolean isLoadingButtonShown() {
362+
return mShowLoadingButton;
363+
}
327364

328-
/**
329-
* Gets the OnClickListener which perform when EmptyView was click
330-
* @return
365+
/**
366+
* Sets if a button will be shown in the loading view
367+
* @param showEmptyButton will a button be shown in the loading view
331368
*/
332-
public View.OnClickListener getEmptyViewClickListener() {
333-
return mEmptyViewClickListener;
334-
}
369+
public void setLoadingButtonShown(boolean showLoadingButton) {
370+
this.mShowLoadingButton = showLoadingButton;
371+
}
335372

373+
/**
374+
* Gets if a button is shown in the error view
375+
* @return if a button is shown in the error view
376+
*/
377+
public boolean isErrorButtonShown() {
378+
return mShowErrorButton;
379+
}
336380

337-
/**
338-
* Gets the OnClickListener which perform when ErrorView was click
339-
* @return
381+
/**
382+
* Sets if a button will be shown in the error view
383+
* @param showEmptyButton will a button be shown in the error view
340384
*/
341-
public View.OnClickListener getErrorViewClickListener() {
342-
return mErrorViewClickListener;
343-
}
385+
public void setErrorButtonShown(boolean showErrorButton) {
386+
this.mShowErrorButton = showErrorButton;
387+
}
388+
389+
/**
390+
* Gets the ID of the button in the loading view
391+
* @return the ID of the button in the loading view
392+
*/
393+
public int getmLoadingViewButtonId() {
394+
return mLoadingViewButtonId;
395+
}
396+
397+
/**
398+
* Sets the ID of the button in the loading view. This ID is required if you want the button the loading view to be click-able.
399+
* @param loadingViewButtonId the ID of the button in the loading view
400+
*/
401+
public void setLoadingViewButtonId(int loadingViewButtonId) {
402+
this.mLoadingViewButtonId = loadingViewButtonId;
403+
}
404+
405+
/**
406+
* Gets the ID of the button in the error view
407+
* @return the ID of the button in the error view
408+
*/
409+
public int getErrorViewButtonId() {
410+
return mErrorViewButtonId;
411+
}
412+
413+
/**
414+
* Sets the ID of the button in the error view. This ID is required if you want the button the error view to be click-able.
415+
* @param errorViewButtonId the ID of the button in the error view
416+
*/
417+
public void setErrorViewButtonId(int errorViewButtonId) {
418+
this.mErrorViewButtonId = errorViewButtonId;
419+
}
420+
421+
/**
422+
* Gets the ID of the button in the empty view
423+
* @return the ID of the button in the empty view
424+
*/
425+
public int getEmptyViewButtonId() {
426+
return mEmptyViewButtonId;
427+
}
428+
429+
/**
430+
* Sets the ID of the button in the empty view. This ID is required if you want the button the empty view to be click-able.
431+
* @param emptyViewButtonId the ID of the button in the empty view
432+
*/
433+
public void setEmptyViewButtonId(int emptyViewButtonId) {
434+
this.mEmptyViewButtonId = emptyViewButtonId;
435+
}
436+
437+
438+
439+
440+
344441

345442
// ---------------------------
346443
// private methods
347444
// ---------------------------
348-
445+
349446
private void changeEmptyType() {
350447

351448
setDefaultValues();
@@ -419,15 +516,48 @@ private void setDefaultValues() {
419516
if (mEmptyView==null) {
420517
mEmptyView = (ViewGroup) mInflater.inflate(R.layout.view_empty, null);
421518
if (!(mEmptyMessageViewId>0)) mEmptyMessageViewId = R.id.textViewMessage;
519+
if (mShowEmptyButton && mEmptyViewButtonId>0 && mEmptyButtonClickListener!=null) {
520+
View emptyViewButton = mEmptyView.findViewById(mEmptyViewButtonId);
521+
if (emptyViewButton != null) {
522+
emptyViewButton.setOnClickListener(mEmptyButtonClickListener);
523+
emptyViewButton.setVisibility(View.VISIBLE);
524+
}
525+
}
526+
else if (mEmptyViewButtonId>0) {
527+
View emptyViewButton = mEmptyView.findViewById(mEmptyViewButtonId);
528+
emptyViewButton.setVisibility(View.GONE);
529+
}
422530
}
423531
if (mLoadingView==null) {
424532
mLoadingView = (ViewGroup) mInflater.inflate(R.layout.view_loading, null);
425533
mLoadingAnimationViewId = R.id.imageViewLoading;
426534
if (!(mLoadingMessageViewId>0)) mLoadingMessageViewId = R.id.textViewMessage;
535+
if (mShowLoadingButton && mLoadingViewButtonId>0 && mLoadingButtonClickListener!=null) {
536+
View loadingViewButton = mLoadingView.findViewById(mLoadingViewButtonId);
537+
if (loadingViewButton != null) {
538+
loadingViewButton.setOnClickListener(mLoadingButtonClickListener);
539+
loadingViewButton.setVisibility(View.VISIBLE);
540+
}
541+
}
542+
else if (mLoadingViewButtonId>0) {
543+
View loadingViewButton = mLoadingView.findViewById(mLoadingViewButtonId);
544+
loadingViewButton.setVisibility(View.GONE);
545+
}
427546
}
428547
if (mErrorView==null) {
429548
mErrorView = (ViewGroup) mInflater.inflate(R.layout.view_error, null);
430549
if (!(mErrorMessageViewId>0)) mErrorMessageViewId = R.id.textViewMessage;
550+
if (mShowErrorButton && mErrorViewButtonId>0 && mErrorButtonClickListener!=null) {
551+
View errorViewButton = mErrorView.findViewById(mErrorViewButtonId);
552+
if (errorViewButton != null) {
553+
errorViewButton.setOnClickListener(mErrorButtonClickListener);
554+
errorViewButton.setVisibility(View.VISIBLE);
555+
}
556+
}
557+
else if (mErrorViewButtonId>0) {
558+
View errorViewButton = mErrorView.findViewById(mErrorViewButtonId);
559+
errorViewButton.setVisibility(View.GONE);
560+
}
431561
}
432562
}
433563

EmptyLayoutSample/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33

4-
<string name="app_name">Empty View Test</string>
4+
<string name="app_name">Empty View Sample</string>
55
<string name="action_settings">Settings</string>
66
<string name="hello_world">Hello world!</string>
77

EmptyLayoutSample/src/com/kanak/emptylayoutsample/MainActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@
88

99
import android.os.Bundle;
1010
import android.view.View;
11+
import android.view.View.OnClickListener;
1112
import android.widget.ArrayAdapter;
13+
import android.widget.Toast;
1214
import android.app.ListActivity;
1315

1416
public class MainActivity extends ListActivity {
1517

1618
private EmptyLayout mEmptyLayout; // this is required to show different layouts (loading or empty or error)
1719
private ArrayAdapter<String> mAdapter;
20+
private View.OnClickListener mErrorClickListener = new OnClickListener() {
21+
@Override
22+
public void onClick(View v) {
23+
Toast.makeText(MainActivity.this, "Try again button clicked", Toast.LENGTH_LONG).show();
24+
}
25+
};
1826

1927
// the list items
2028
static final String[] MOVIES = new String[] {
@@ -45,6 +53,7 @@ protected void onCreate(Bundle savedInstanceState) {
4553

4654
// initialize the empty view
4755
mEmptyLayout = new EmptyLayout(this, getListView());
56+
mEmptyLayout.setErrorButtonClickListener(mErrorClickListener);
4857

4958
// populate the list view
5059
populateList();

Screenshots/Screen01.png

63.5 KB
Loading

Screenshots/Screen02.png

56.4 KB
Loading

Screenshots/Screen03.png

56.7 KB
Loading

0 commit comments

Comments
 (0)