Skip to content

Commit

Permalink
Fixed custom text, added attributes for setting image
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkinst committed Aug 9, 2015
1 parent fa1b1f0 commit c6b9f10
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
34 changes: 29 additions & 5 deletions library/src/main/java/cz/kinst/jakub/view/StatefulView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Created by jakubkinst on 05/08/15.
*/
public class StatefulView extends FrameLayout {
private int mCustomEmptyDrawableId = 0;
private int mCustomOfflineDrawableId = 0;
private int mTextAppearance;
private State mInitialState;
private String mCustomEmptyText;
Expand Down Expand Up @@ -58,6 +60,14 @@ public StatefulView(Context context, AttributeSet attrs, int defStyleAttr) {
mInitialState = State.fromId(a.getInt(R.styleable.StatefulView_state, State.CONTENT.id));
}

if(a.hasValue(R.styleable.StatefulView_offlineImageDrawable)) {
mCustomOfflineDrawableId = a.getResourceId(R.styleable.StatefulView_offlineImageDrawable, State.CONTENT.id);
}

if(a.hasValue(R.styleable.StatefulView_emptyImageDrawable)) {
mCustomEmptyDrawableId = a.getResourceId(R.styleable.StatefulView_emptyImageDrawable, State.CONTENT.id);
}


}

Expand All @@ -75,8 +85,10 @@ public void setEmptyText(CharSequence emptyText) {

public void setEmptyImageDrawable(Drawable drawable) {
TintableImageView image = ((TintableImageView) mEmptyView.findViewById(R.id.state_image));
image.setVisibility(drawable != null ? VISIBLE : GONE);
image.setImageDrawable(drawable);
if(image != null) {
image.setVisibility(drawable != null ? VISIBLE : GONE);
image.setImageDrawable(drawable);
}
}


Expand All @@ -98,8 +110,10 @@ public void setOfflineText(CharSequence offlineText) {

public void setOfflineImageDrawable(Drawable drawable) {
TintableImageView image = ((TintableImageView) mOfflineView.findViewById(R.id.state_image));
image.setVisibility(drawable != null ? VISIBLE : GONE);
image.setImageDrawable(drawable);
if(image != null) {
image.setVisibility(drawable != null ? VISIBLE : GONE);
image.setImageDrawable(drawable);
}
}


Expand Down Expand Up @@ -171,6 +185,8 @@ protected void onAttachedToWindow() {


private void initialize() {

// build layout structure
mContent = getChildAt(0);
addView(LayoutInflater.from(getContext()).inflate(R.layout.view_stateful, this, false));
mContainerProgress = (FrameLayout) findViewById(R.id.container_progress);
Expand All @@ -180,20 +196,28 @@ private void initialize() {
mContainerEmpty = (FrameLayout) findViewById(R.id.container_empty);
mContainerEmpty.addView(mEmptyView);

// set custom empty text
mDefaultEmptyText = ((TextView) mEmptyView.findViewById(R.id.state_text));
if(mDefaultEmptyText != null) {
mDefaultEmptyText.setTextAppearance(getContext(), mTextAppearance);
if(mCustomEmptyText != null)
setEmptyText(mCustomEmptyText);
}

// set custom offline text
mDefaultOfflineText = ((TextView) mOfflineView.findViewById(R.id.state_text));
if(mDefaultOfflineText != null) {
mDefaultOfflineText.setTextAppearance(getContext(), mTextAppearance);
if(mCustomOfflineText != null)
setEmptyText(mCustomOfflineText);
setOfflineText(mCustomOfflineText);
}

// set custom drawables
if(mCustomOfflineDrawableId != 0)
setOfflineImageResource(mCustomOfflineDrawableId);
if(mCustomEmptyDrawableId != 0)
setEmptyImageResource(mCustomEmptyDrawableId);

if(mInitialState != null)
setState(mInitialState);
}
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs_stateful_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<attr format="reference" name="offlineLayout"/>
<attr format="reference" name="emptyLayout"/>
<attr format="reference" name="progressLayout"/>
<attr format="reference" name="offlineImageDrawable"/>
<attr format="reference" name="emptyImageDrawable"/>
<attr format="string" name="emptyText"/>
<attr format="string" name="offlineText"/>
</declare-styleable>
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:emptyText="No items here..."
app:offlineText="Oops... No Internet connection"
app:emptyText="@string/custom_empty_text"
app:offlineText="@string/custom_offline_text"
app:state="offline">

<!--Your Content Here-->
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

<string name="placeholder_empty">No data</string>
<string name="placeholder_offline">No network</string>
<string name="custom_empty_text">No items here...</string>
<string name="custom_offline_text">Oops... No Internet connection</string>
</resources>

0 comments on commit c6b9f10

Please sign in to comment.