Skip to content

Commit

Permalink
Buttons added in all empty layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
alamkanak committed Oct 13, 2013
1 parent 5505c1b commit 2aaa4ba
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 42 deletions.
8 changes: 8 additions & 0 deletions EmptyLayout/res/layout/view_empty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_horizontal" />

<Button
android:id="@+id/buttonEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:text="@string/empty_button" />

</LinearLayout>
8 changes: 8 additions & 0 deletions EmptyLayout/res/layout/view_error.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_horizontal" />

<Button
android:id="@+id/buttonError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/error_button"
android:layout_marginTop="16dp" />

</LinearLayout>
7 changes: 7 additions & 0 deletions EmptyLayout/res/layout/view_loading.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" />

<Button
android:id="@+id/buttonLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/loading_button" />

</LinearLayout>
4 changes: 4 additions & 0 deletions EmptyLayout/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
<string name="empty_message">No items yet</string>
<string name="loading_message">Please wait</string>
<string name="error_message">Oops! Something wrong happened</string>

<string name="empty_button">Try again</string>
<string name="loading_button">Cancel</string>
<string name="error_button">Try again</string>

</resources>
212 changes: 171 additions & 41 deletions EmptyLayout/src/com/kanak/emptylayout/EmptyLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ public class EmptyLayout {
private int mErrorMessageViewId;
private int mEmptyMessageViewId;
private int mLoadingMessageViewId;

private View.OnClickListener mLoadingViewClickListener;
private View.OnClickListener mEmptyViewClickListener;
private View.OnClickListener mErrorViewClickListener;
private LayoutInflater mInflater;
private boolean mViewsAdded;
private int mLoadingAnimationViewId;
private View.OnClickListener mLoadingButtonClickListener;
private View.OnClickListener mEmptyButtonClickListener;
private View.OnClickListener mErrorButtonClickListener;

// ---------------------------
// static variables
Expand All @@ -55,9 +57,12 @@ public class EmptyLayout {
private String mErrorMessage = "Oops! Something wrong happened";
private String mEmptyMessage = "No items yet";
private String mLoadingMessage = "Please wait";
private LayoutInflater mInflater;
private boolean mViewsAdded;
private int mLoadingAnimationViewId;
private int mLoadingViewButtonId = R.id.buttonLoading;
private int mErrorViewButtonId = R.id.buttonError;
private int mEmptyViewButtonId = R.id.buttonEmpty;
private boolean mShowEmptyButton = true;
private boolean mShowLoadingButton = true;
private boolean mShowErrorButton = true;

// ---------------------------
// getters and setters
Expand Down Expand Up @@ -283,69 +288,161 @@ public int getLoadingAnimationViewId() {
*/
public void setLoadingAnimationViewId(int loadingAnimationViewId) {
this.mLoadingAnimationViewId = loadingAnimationViewId;
}
}

/**
* Gets the OnClickListener which perform when LoadingView was click
* @return
*/
public View.OnClickListener getLoadingButtonClickListener() {
return mLoadingButtonClickListener;
}

/**
* Sets the OnClickListener to LoadingView
* @param loadingViewClickListener OnClickListener Object
* @param loadingButtonClickListener OnClickListener Object
*/
public void setLoadingButtonClickListener(View.OnClickListener loadingButtonClickListener) {
this.mLoadingButtonClickListener = loadingButtonClickListener;
}

/**
* Gets the OnClickListener which perform when EmptyView was click
* @return
*/
public void setLoadingViewClickListener(View.OnClickListener loadingViewClickListener) {
this.mLoadingViewClickListener = loadingViewClickListener;
if(mLoadingView != null)
mLoadingView.setOnClickListener(mLoadingViewClickListener);
public View.OnClickListener getEmptyButtonClickListener() {
return mEmptyButtonClickListener;
}

/**
* Sets the OnClickListener to EmptyView
* @param emptyViewClickListener OnClickListener Object
* @param emptyButtonClickListener OnClickListener Object
*/
public void setEmptyButtonClickListener(View.OnClickListener emptyButtonClickListener) {
this.mEmptyButtonClickListener = emptyButtonClickListener;
}

/**
* Gets the OnClickListener which perform when ErrorView was click
* @return
*/
public void setEmptyViewClickListener(View.OnClickListener emptyViewClickListener) {
this.mEmptyViewClickListener = emptyViewClickListener;
if(mEmptyView != null)
mEmptyView.setOnClickListener(mEmptyViewClickListener);
public View.OnClickListener getErrorButtonClickListener() {
return mErrorButtonClickListener;
}

/**
* Sets the OnClickListener to ErrorView
* @param errorViewClickListener OnClickListener Object
* @param errorButtonClickListener OnClickListener Object
*/
public void setErrorViewClickListener(View.OnClickListener errorViewClickListener) {
this.mErrorViewClickListener = errorViewClickListener;
if(mErrorView != null)
mErrorView.setOnClickListener(mErrorViewClickListener);
public void setErrorButtonClickListener(View.OnClickListener errorButtonClickListener) {
this.mErrorButtonClickListener = errorButtonClickListener;
}

/**
* Gets if a button is shown in the empty view
* @return if a button is shown in the empty view
*/
public boolean isEmptyButtonShown() {
return mShowEmptyButton;
}

/**
* Gets the OnClickListener which perform when LoadingView was click
* @return
* Sets if a button will be shown in the empty view
* @param showEmptyButton will a button be shown in the empty view
*/
public View.OnClickListener getLoadingViewClickListener() {
return mLoadingViewClickListener;
}
public void setEmptyButtonShown(boolean showEmptyButton) {
this.mShowEmptyButton = showEmptyButton;
}

/**
* Gets if a button is shown in the loading view
* @return if a button is shown in the loading view
*/
public boolean isLoadingButtonShown() {
return mShowLoadingButton;
}

/**
* Gets the OnClickListener which perform when EmptyView was click
* @return
/**
* Sets if a button will be shown in the loading view
* @param showEmptyButton will a button be shown in the loading view
*/
public View.OnClickListener getEmptyViewClickListener() {
return mEmptyViewClickListener;
}
public void setLoadingButtonShown(boolean showLoadingButton) {
this.mShowLoadingButton = showLoadingButton;
}

/**
* Gets if a button is shown in the error view
* @return if a button is shown in the error view
*/
public boolean isErrorButtonShown() {
return mShowErrorButton;
}

/**
* Gets the OnClickListener which perform when ErrorView was click
* @return
/**
* Sets if a button will be shown in the error view
* @param showEmptyButton will a button be shown in the error view
*/
public View.OnClickListener getErrorViewClickListener() {
return mErrorViewClickListener;
}
public void setErrorButtonShown(boolean showErrorButton) {
this.mShowErrorButton = showErrorButton;
}

/**
* Gets the ID of the button in the loading view
* @return the ID of the button in the loading view
*/
public int getmLoadingViewButtonId() {
return mLoadingViewButtonId;
}

/**
* 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.
* @param loadingViewButtonId the ID of the button in the loading view
*/
public void setLoadingViewButtonId(int loadingViewButtonId) {
this.mLoadingViewButtonId = loadingViewButtonId;
}

/**
* Gets the ID of the button in the error view
* @return the ID of the button in the error view
*/
public int getErrorViewButtonId() {
return mErrorViewButtonId;
}

/**
* 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.
* @param errorViewButtonId the ID of the button in the error view
*/
public void setErrorViewButtonId(int errorViewButtonId) {
this.mErrorViewButtonId = errorViewButtonId;
}

/**
* Gets the ID of the button in the empty view
* @return the ID of the button in the empty view
*/
public int getEmptyViewButtonId() {
return mEmptyViewButtonId;
}

/**
* 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.
* @param emptyViewButtonId the ID of the button in the empty view
*/
public void setEmptyViewButtonId(int emptyViewButtonId) {
this.mEmptyViewButtonId = emptyViewButtonId;
}






// ---------------------------
// private methods
// ---------------------------

private void changeEmptyType() {

setDefaultValues();
Expand Down Expand Up @@ -419,15 +516,48 @@ private void setDefaultValues() {
if (mEmptyView==null) {
mEmptyView = (ViewGroup) mInflater.inflate(R.layout.view_empty, null);
if (!(mEmptyMessageViewId>0)) mEmptyMessageViewId = R.id.textViewMessage;
if (mShowEmptyButton && mEmptyViewButtonId>0 && mEmptyButtonClickListener!=null) {
View emptyViewButton = mEmptyView.findViewById(mEmptyViewButtonId);
if (emptyViewButton != null) {
emptyViewButton.setOnClickListener(mEmptyButtonClickListener);
emptyViewButton.setVisibility(View.VISIBLE);
}
}
else if (mEmptyViewButtonId>0) {
View emptyViewButton = mEmptyView.findViewById(mEmptyViewButtonId);
emptyViewButton.setVisibility(View.GONE);
}
}
if (mLoadingView==null) {
mLoadingView = (ViewGroup) mInflater.inflate(R.layout.view_loading, null);
mLoadingAnimationViewId = R.id.imageViewLoading;
if (!(mLoadingMessageViewId>0)) mLoadingMessageViewId = R.id.textViewMessage;
if (mShowLoadingButton && mLoadingViewButtonId>0 && mLoadingButtonClickListener!=null) {
View loadingViewButton = mLoadingView.findViewById(mLoadingViewButtonId);
if (loadingViewButton != null) {
loadingViewButton.setOnClickListener(mLoadingButtonClickListener);
loadingViewButton.setVisibility(View.VISIBLE);
}
}
else if (mLoadingViewButtonId>0) {
View loadingViewButton = mLoadingView.findViewById(mLoadingViewButtonId);
loadingViewButton.setVisibility(View.GONE);
}
}
if (mErrorView==null) {
mErrorView = (ViewGroup) mInflater.inflate(R.layout.view_error, null);
if (!(mErrorMessageViewId>0)) mErrorMessageViewId = R.id.textViewMessage;
if (mShowErrorButton && mErrorViewButtonId>0 && mErrorButtonClickListener!=null) {
View errorViewButton = mErrorView.findViewById(mErrorViewButtonId);
if (errorViewButton != null) {
errorViewButton.setOnClickListener(mErrorButtonClickListener);
errorViewButton.setVisibility(View.VISIBLE);
}
}
else if (mErrorViewButtonId>0) {
View errorViewButton = mErrorView.findViewById(mErrorViewButtonId);
errorViewButton.setVisibility(View.GONE);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion EmptyLayoutSample/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Empty View Test</string>
<string name="app_name">Empty View Sample</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.app.ListActivity;

public class MainActivity extends ListActivity {

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

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

// initialize the empty view
mEmptyLayout = new EmptyLayout(this, getListView());
mEmptyLayout.setErrorButtonClickListener(mErrorClickListener);

// populate the list view
populateList();
Expand Down
Binary file modified Screenshots/Screen01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/Screen02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/Screen03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/Screen04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2aaa4ba

Please sign in to comment.