Skip to content

Commit 443fa5b

Browse files
author
michal_szwarc
committed
Update TextItem to show icon on left side
1 parent 4e56b47 commit 443fa5b

File tree

5 files changed

+95
-15
lines changed

5 files changed

+95
-15
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@ Easiest way to save/load settings
5858
* SimpleStorageInterface - in memory
5959
* or write own interface to save data (sqlite, content provider or to file)
6060

61+
Interface:
62+
63+
```java
64+
65+
public abstract class StorageInterface {
66+
public StorageInterface() {}
67+
68+
public abstract void save(String key, Boolean value);
69+
public abstract boolean load(String key, Boolean defaultValue);
70+
71+
public abstract void save(String key, String value);
72+
public abstract String load(String key, String defaultValue);
73+
74+
public abstract void save(String key, Integer value);
75+
public abstract Integer load(String key, Integer defaultValue);
76+
77+
public abstract void save(String key, Float value);
78+
public abstract Float load(String key, Float defaultValue);
79+
80+
public abstract void save(String key, Long value);
81+
public abstract Long load(String key, Long defaultValue);
82+
83+
public abstract Map<String, ?> getAll();
84+
}
85+
86+
```
87+
88+
Extend and write own methods.
89+
6190
# Screens
6291
![Default](/screens/theme_default2.png)
6392
![Rec](/screens/theme_red2.png)

library/src/main/java/com/kenumir/materialsettings/items/TextItem.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.kenumir.materialsettings.items;
22

33
import android.content.Context;
4+
import android.graphics.drawable.Drawable;
45
import android.view.View;
6+
import android.widget.ImageView;
57
import android.widget.TextView;
68

79
import com.kenumir.materialsettings.MaterialSettingsItem;
@@ -15,11 +17,12 @@ public class TextItem extends MaterialSettingsItem {
1517
public static interface OnClickListener {
1618
public void onClick(TextItem item);
1719
}
18-
19-
2020
private String title, subtitle;
2121
private OnClickListener onclick;
2222
private TextView titleView, subtitleView;
23+
private ImageView image;
24+
private Drawable icon;
25+
private int iconRes = 0;
2326

2427
public TextItem(Context ctx, String name) {
2528
super(ctx, name);
@@ -32,11 +35,19 @@ public int getViewResource() {
3235

3336
@Override
3437
public void setupView(View v) {
35-
titleView = (TextView) v.findViewById(R.id.material_dialog_item_title);
36-
subtitleView = (TextView) v.findViewById(R.id.material_dialog_item_subtitle);
38+
titleView = (TextView) v.findViewById(R.id.material_settings_text_title);
39+
subtitleView = (TextView) v.findViewById(R.id.material_settings_text_subtitle);
40+
image = (ImageView) v.findViewById(R.id.material_settings_text_icon);
3741

3842
updateTitle(title);
3943
updateSubTitle(subtitle);
44+
if (iconRes > 0) {
45+
updateIcon(iconRes);
46+
} else if (icon != null) {
47+
updateIcon(icon);
48+
} else {
49+
image.setVisibility(View.GONE);
50+
}
4051

4152
v.setOnClickListener(new View.OnClickListener() {
4253
@Override
@@ -47,6 +58,16 @@ public void onClick(View v) {
4758
});
4859
}
4960

61+
public TextItem setIcon(int icon) {
62+
iconRes = icon;
63+
return this;
64+
}
65+
66+
public TextItem setIcon(Drawable icon) {
67+
this.icon = icon;
68+
return this;
69+
}
70+
5071
@Override
5172
public void save() {
5273
// NOP
@@ -66,6 +87,20 @@ public TextItem updateSubTitle(String newSubTitle) {
6687
return this;
6788
}
6889

90+
public TextItem updateIcon(int icon) {
91+
this.iconRes = icon;
92+
this.image.setImageResource(icon);
93+
this.image.setVisibility(View.VISIBLE);
94+
return this;
95+
}
96+
97+
public TextItem updateIcon(Drawable icon) {
98+
this.icon = icon;
99+
this.image.setImageDrawable(icon);
100+
this.image.setVisibility(View.VISIBLE);
101+
return this;
102+
}
103+
69104
public String getTitle() {
70105
return title;
71106
}
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout
2+
<RelativeLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
4-
style="?attr/msItem"
5-
android:orientation="vertical">
4+
style="?attr/msItem">
5+
6+
<ImageView
7+
android:id="@+id/material_settings_text_icon"
8+
android:layout_width="wrap_content"
9+
android:layout_height="match_parent"
10+
android:scaleType="centerInside"
11+
android:src="@drawable/abc_btn_radio_to_on_mtrl_000"
12+
android:layout_marginRight="24dp"
13+
android:layout_centerVertical="true"
14+
android:visibility="gone"/>
615

716
<TextView
8-
android:id="@+id/material_dialog_item_title"
9-
style="?attr/msTextStyle"/>
17+
android:id="@+id/material_settings_text_title"
18+
style="?attr/msTextStyle"
19+
android:layout_toRightOf="@+id/material_settings_text_icon"/>
1020

1121
<TextView
12-
android:id="@+id/material_dialog_item_subtitle"
13-
style="?attr/msSubTextStyle"/>
22+
android:id="@+id/material_settings_text_subtitle"
23+
style="?attr/msSubTextStyle"
24+
android:layout_toRightOf="@+id/material_settings_text_icon"
25+
android:layout_below="@+id/material_settings_text_title"/>
1426

15-
</LinearLayout>
27+
</RelativeLayout>

sampleapp/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ android {
88
applicationId "com.wt.sampleapp"
99
minSdkVersion 9
1010
targetSdkVersion 21
11-
versionCode 2
12-
versionName "1.2"
11+
versionCode 3
12+
versionName "1.2.1"
1313
}
1414
buildTypes {
1515
release {
16-
minifyEnabled false
16+
minifyEnabled true
1717
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1818
}
1919
}

sampleapp/src/main/java/com/wt/sampleapp/Settings1Activity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public void onClick(TextItem v) {
5151
}));
5252
addItem(new DividerItem(this));
5353
addItem(new TextItem(this, "key5").setTitle("Simple text item 3 - no subtitle"));
54+
addItem(new DividerItem(this));
55+
addItem(new TextItem(this, "key5a").setTitle("Simple text item with icon - no subtitle").setIcon(R.drawable.ic_check_circle_grey600_24dp));
56+
addItem(new DividerItem(this));
57+
addItem(new TextItem(this, "key5b").setTitle("Simple text item with icon - no subtitle").setSubtitle("Subtitle of item with icon").setIcon(R.drawable.ic_check_circle_grey600_24dp));
5458
addItem(new HeaderItem(this).setTitle("Same usage with dialogs"));
5559
addItem(new TextItem(this, "key6").setTitle("Simple message dialog").setSubtitle("Clck to show message and change subtext").setOnclick(new TextItem.OnClickListener() {
5660
@Override

0 commit comments

Comments
 (0)