allow changing the widget background and text color

This commit is contained in:
tibbi 2016-01-31 20:45:37 +01:00
parent 85b790ff49
commit 4ce5c8f0fc
7 changed files with 143 additions and 60 deletions

View File

@ -34,8 +34,11 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'me.grantland:autofittextview:0.2.1'
compile 'com.github.yukuku:ambilwarna:2.0.1'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.0'
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'

View File

@ -28,4 +28,5 @@ public class Constants {
public static final String PREFS = "prefs";
public static final String CALC_VALUE = "calc_value";
public static final String WIDGET_BG_COLOR = "widget_bg_color";
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
}

View File

@ -8,19 +8,28 @@ import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yuku.ambilwarna.AmbilWarnaDialog;
public class MyWidgetConfigure extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
public class MyWidgetConfigure extends AppCompatActivity {
@Bind(R.id.btn_reset) View resetBtn;
@Bind(R.id.config_seekbar) SeekBar seekBar;
@Bind(R.id.config_bg_color) View bgColorPicker;
@Bind(R.id.config_bg_seekbar) SeekBar bgSeekBar;
@Bind(R.id.config_text_color) View textColorPicker;
@Bind(R.id.config_calc) View background;
@Bind(R.id.config_save) Button saveBtn;
private int widgetId;
private int newBgColor;
private int bgColor;
private int bgColorWithoutTransparency;
private float bgAlpha;
private int textColor;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -28,10 +37,7 @@ public class MyWidgetConfigure extends AppCompatActivity implements SeekBar.OnSe
setResult(RESULT_CANCELED);
setContentView(R.layout.widget_config);
ButterKnife.bind(this);
resetBtn.setVisibility(View.VISIBLE);
seekBar.setOnSeekBarChangeListener(this);
seekBar.setProgress(50);
initVariables();
final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
@ -42,11 +48,37 @@ public class MyWidgetConfigure extends AppCompatActivity implements SeekBar.OnSe
finish();
}
private void initVariables() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 0);
if (bgColor == 0) {
bgColor = Color.BLACK;
bgAlpha = .5f;
} else {
bgAlpha = Color.alpha(bgColor) / (float) 255;
}
resetBtn.setVisibility(View.VISIBLE);
bgColorWithoutTransparency = Color.rgb(Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor));
bgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener);
bgSeekBar.setProgress((int) (bgAlpha * 100));
updateBackgroundColor();
textColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
updateTextColor();
}
private void updateTextColor() {
textColorPicker.setBackgroundColor(textColor);
saveBtn.setTextColor(textColor);
}
@OnClick(R.id.config_save)
public void saveConfig() {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
final RemoteViews views = new RemoteViews(getPackageName(), R.layout.activity_main);
views.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor);
views.setInt(R.id.calculator_holder, "setBackgroundColor", bgColor);
appWidgetManager.updateAppWidget(widgetId, views);
storeWidgetBackground();
@ -60,7 +92,7 @@ public class MyWidgetConfigure extends AppCompatActivity implements SeekBar.OnSe
private void storeWidgetBackground() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, newBgColor).apply();
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, bgColor).apply();
}
private void requestWidgetUpdate() {
@ -69,19 +101,70 @@ public class MyWidgetConfigure extends AppCompatActivity implements SeekBar.OnSe
sendBroadcast(intent);
}
private void updateBackgroundColor() {
bgColor = adjustAlpha(bgColorWithoutTransparency, bgAlpha);
background.setBackgroundColor(bgColor);
bgColorPicker.setBackgroundColor(bgColor);
saveBtn.setBackgroundColor(bgColor);
}
@OnClick(R.id.config_bg_color)
public void pickBackgroundColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, bgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
bgColorWithoutTransparency = color;
updateBackgroundColor();
}
});
dialog.show();
}
@OnClick(R.id.config_text_color)
public void pickTextColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, textColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
textColor = color;
updateTextColor();
}
});
dialog.show();
}
private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
final float percent = (float) progress / (float) 100;
final int alpha = (int) (255 * percent);
newBgColor = Color.argb(alpha, 0, 0, 0);
background.setBackgroundColor(newBgColor);
bgAlpha = (float) progress / (float) 100;
updateBackgroundColor();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
private int adjustAlpha(int color, float factor) {
final int alpha = Math.round(Color.alpha(color) * factor);
final int red = Color.red(color);
final int green = Color.green(color);
final int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
}

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/config_button_pressed"
android:state_pressed="true"/>
<item
android:drawable="@drawable/config_button_normal"/>
</selector>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/dark_grey"/>
</shape>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/dark_grey_pressed"/>
</shape>

View File

@ -4,42 +4,58 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/activity_margin">
android:layout_margin="@dimen/activity_margin"
android:paddingBottom="@dimen/activity_margin">
<include
android:id="@+id/config_calc"
layout="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/config_bottom"
android:layout_above="@+id/config_bg_seekbar_holder"
android:layout_marginBottom="@dimen/activity_margin"/>
<Button
android:id="@+id/config_bg_color"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@+id/config_text_color"/>
<RelativeLayout
android:id="@+id/config_bottom"
android:id="@+id/config_bg_seekbar_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/config_bg_color"
android:layout_alignTop="@+id/config_bg_color"
android:layout_toRightOf="@+id/config_bg_color"
android:background="@android:color/white">
<SeekBar
android:id="@+id/config_bg_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white">
android:layout_centerVertical="true"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"/>
</RelativeLayout>
<Button
android:id="@+id/config_text_color"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/config_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/config_text_color"
android:layout_alignParentRight="true"
android:background="@drawable/config_button"
android:layout_alignTop="@+id/config_text_color"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:text="Save"
android:text="OK"
android:textColor="@android:color/white"
android:textSize="@dimen/config_text_size"/>
<SeekBar
android:id="@+id/config_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/activity_margin"
android:layout_toLeftOf="@id/config_save"/>
</RelativeLayout>
</RelativeLayout>