Skip to content

Commit

Permalink
now can switch background color between light and dark.
Browse files Browse the repository at this point in the history
  • Loading branch information
vancebs committed Aug 26, 2015
1 parent 90bdf78 commit cdfd0a4
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hf.resreader.R;
Expand All @@ -27,7 +28,7 @@ public View read(Context context, View convertView, String pkg, String type, Str
}

TextView resIdView = (TextView) view.findViewById(R.id.res_id);
FrameLayout blockView = (FrameLayout) view.findViewById(R.id.color_block);
ImageView blockView = (ImageView) view.findViewById(R.id.color_block);
TextView valueView = (TextView) view.findViewById(R.id.color_value);

// get package context
Expand Down
45 changes: 37 additions & 8 deletions app/src/main/java/com/hf/resreader/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
Expand All @@ -29,6 +30,7 @@ public class MainActivity extends Activity {
private static final String EXTRA_PKG = "extra-pkg";
private static final String EXTRA_TYPE_INDEX = "extra-type-index";
private static final String EXTRA_KEY = "extra-key";
private static final String EXTRA_IS_DARK = "extra-is-dark";

private String[] mTypes = null;

Expand All @@ -49,6 +51,11 @@ public class MainActivity extends Activity {

private ResReaderManager mResReaderManager;

private ViewGroup mContainer;

private MenuItem mThemeBtn;
private boolean mIsDark = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -59,8 +66,8 @@ protected void onCreate(Bundle savedInstanceState) {
mKey = (AutoCompleteTextView) findViewById(R.id.key);

// create reader manager
ViewGroup container = (ViewGroup) findViewById(R.id.value_container);
mResReaderManager = new ResReaderManager(this, container);
mContainer = (ViewGroup) findViewById(R.id.value_container);
mResReaderManager = new ResReaderManager(this, mContainer);

// get pkg auto complete
setPkgAutoComplete();
Expand Down Expand Up @@ -93,6 +100,7 @@ public void onNothingSelected(AdapterView<?> arg0) {
mPkg.setText(savedInstanceState.getString(EXTRA_PKG, ""));
mType.setSelection(savedInstanceState.getInt(EXTRA_TYPE_INDEX, 0));
mKey.setText(savedInstanceState.getString(EXTRA_KEY, ""));
mIsDark = savedInstanceState.getBoolean(EXTRA_IS_DARK, false);

onReadClicked();
}
Expand All @@ -101,17 +109,23 @@ public void onNothingSelected(AdapterView<?> arg0) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
mThemeBtn = menu.findItem(R.id.theme);
updateThemeButton();
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.read) {
onReadClicked();
return true;
} else if (item.getItemId() == R.id.clear) {
onClearClicked();
return true;
switch (item.getItemId()) {
case R.id.read:
onReadClicked();
return true;
case R.id.clear:
onClearClicked();
return true;
case R.id.theme:
onThemeClicked();
return true;
}

return super.onOptionsItemSelected(item);
Expand All @@ -124,6 +138,7 @@ protected void onSaveInstanceState(Bundle outState) {
outState.putString(EXTRA_PKG, mValuePkg);
outState.putInt(EXTRA_TYPE_INDEX, mValueTypeIndex);
outState.putString(EXTRA_KEY, mValueKey);
outState.putBoolean(EXTRA_IS_DARK, mIsDark);
}

private void onReadClicked() {
Expand All @@ -138,6 +153,20 @@ private void onClearClicked() {
mResReaderManager.clear();
}

private void onThemeClicked() {
mIsDark = !mIsDark;
updateThemeButton();
}

private void updateThemeButton() {
Resources res = getResources();
String title = mIsDark ? res.getString(R.string.btn_theme_bright) : res.getString(R.string.btn_theme_dark);
mThemeBtn.setTitle(title);
mThemeBtn.setTitleCondensed(title);

mContainer.setBackgroundResource(mIsDark ? R.color.background_color_dark : R.color.background_color_light);
}

private boolean isNativePkg(String pkg) {
if (pkg == null || pkg.isEmpty()) {
return true;
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/res/drawable/edge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- color -->
<solid android:color="@android:color/transparent"/>
<!-- line size -->
<stroke
android:width="1px"
android:color="@color/edge_color" />

<padding
android:left="1px"
android:top="1px"
android:right="1px"
android:bottom="1px" />

<size
android:width="@dimen/color_block_size"
android:height="@dimen/color_block_size" />"

</shape>
7 changes: 4 additions & 3 deletions app/src/main/res/layout/color_value_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
android:layout_height="wrap_content"
android:textSize="@dimen/result_text_size"
android:text="@string/label_value" />
<FrameLayout
<ImageView
android:id="@+id/color_block"
android:layout_width="@dimen/color_block_size"
android:layout_height="@dimen/color_block_size" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/edge" />
<TextView
android:id="@+id/color_value"
android:layout_width="wrap_content"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/theme"
android:showAsAction="always"
android:titleCondensed="@string/btn_theme_dark"
android:visible="true"
android:title="@string/btn_theme_dark">
</item>

<item android:id="@+id/read"
android:showAsAction="always"
android:titleCondensed="@string/btn_read"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="edge_color">#FF000000</color>
<color name="background_color_dark">#FF666666</color>
<color name="background_color_light">#00FFFFFF</color>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<string name="hint_package">Package name</string>
<string name="hint_key">i.e. app_name</string>

<string name="btn_theme_bright">Bright</string>
<string name="btn_theme_dark">Dark</string>
<string name="btn_read">Read</string>
<string name="btn_clear">Clear</string>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textColor">@android:color/black</item>
</style>

</resources>

0 comments on commit cdfd0a4

Please sign in to comment.