Reconfigured example widget to desired layout

This commit is contained in:
Jaculabilis 2016-10-15 15:04:08 -05:00
parent a82677edc4
commit 21eb41759b
4 changed files with 76 additions and 48 deletions

View File

@ -14,10 +14,10 @@ public class FrameWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) { int appWidgetId) {
CharSequence widgetText = FrameWidgetConfigureActivity.loadTitlePref(context, appWidgetId); // CharSequence widgetText = FrameWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object // Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.frame_widget); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.frame_widget);
views.setTextViewText(R.id.appwidget_text, widgetText); // views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget // Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views); appWidgetManager.updateAppWidget(appWidgetId, views);
@ -35,7 +35,7 @@ public class FrameWidget extends AppWidgetProvider {
public void onDeleted(Context context, int[] appWidgetIds) { public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it. // When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) { for (int appWidgetId : appWidgetIds) {
FrameWidgetConfigureActivity.deleteTitlePref(context, appWidgetId); // FrameWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
} }
} }

View File

@ -5,8 +5,10 @@ import android.appwidget.AppWidgetManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
/** /**
@ -17,19 +19,34 @@ public class FrameWidgetConfigureActivity extends Activity {
private static final String PREFS_NAME = "horse.jaeil.microframe.FrameWidget"; private static final String PREFS_NAME = "horse.jaeil.microframe.FrameWidget";
private static final String PREF_PREFIX_KEY = "appwidget_"; private static final String PREF_PREFIX_KEY = "appwidget_";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
EditText mAppWidgetText;
View.OnClickListener mOnClickListener = new View.OnClickListener() { View.OnClickListener mSelectClickListener = new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
final Context context = FrameWidgetConfigureActivity.this;
// When the button is clicked, store the string locally // Test function: change background color
String widgetText = mAppWidgetText.getText().toString(); Button selectButton = (Button) findViewById(R.id.select_image_button);
saveTitlePref(context, mAppWidgetId, widgetText); selectButton.setBackgroundColor(Color.GREEN);
// It is the responsibility of the configuration activity to update the app widget // final Context context = FrameWidgetConfigureActivity.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); //
FrameWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId); // // When the button is clicked, store the string locally
// String widgetText = mAppWidgetText.getText().toString();
// saveTitlePref(context, mAppWidgetId, widgetText);
//
// // It is the responsibility of the configuration activity to update the app widget
// AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
// FrameWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
// // Make sure we pass back the original appWidgetId
// Intent resultValue = new Intent();
// resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
// setResult(RESULT_OK, resultValue);
// finish();
}
};
View.OnClickListener mFinishClickListener = new View.OnClickListener() {
public void onClick(View v) {
// Make sure we pass back the original appWidgetId // Make sure we pass back the original appWidgetId
Intent resultValue = new Intent(); Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
@ -42,30 +59,30 @@ public class FrameWidgetConfigureActivity extends Activity {
super(); super();
} }
// Write the prefix to the SharedPreferences object for this widget // // Write the prefix to the SharedPreferences object for this widget
static void saveTitlePref(Context context, int appWidgetId, String text) { // static void saveTitlePref(Context context, int appWidgetId, String text) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit(); // SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text); // prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
prefs.apply(); // prefs.apply();
} // }
// Read the prefix from the SharedPreferences object for this widget. // // Read the prefix from the SharedPreferences object for this widget.
// If there is no preference saved, get the default from a resource // // If there is no preference saved, get the default from a resource
static String loadTitlePref(Context context, int appWidgetId) { // static String loadTitlePref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); // SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null); // String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
if (titleValue != null) { // if (titleValue != null) {
return titleValue; // return titleValue;
} else { // } else {
return context.getString(R.string.appwidget_text); // return context.getString(R.string.appwidget_text);
} // }
} // }
static void deleteTitlePref(Context context, int appWidgetId) { // static void deleteTitlePref(Context context, int appWidgetId) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit(); // SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.remove(PREF_PREFIX_KEY + appWidgetId); // prefs.remove(PREF_PREFIX_KEY + appWidgetId);
prefs.apply(); // prefs.apply();
} // }
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
@ -76,8 +93,8 @@ public class FrameWidgetConfigureActivity extends Activity {
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
setContentView(R.layout.frame_widget_configure); setContentView(R.layout.frame_widget_configure);
mAppWidgetText = (EditText) findViewById(R.id.appwidget_text); findViewById(R.id.select_image_button).setOnClickListener(mSelectClickListener);
findViewById(R.id.add_button).setOnClickListener(mOnClickListener); findViewById(R.id.finish_button).setOnClickListener(mFinishClickListener);
// Find the widget id from the intent. // Find the widget id from the intent.
Intent intent = getIntent(); Intent intent = getIntent();
@ -93,7 +110,7 @@ public class FrameWidgetConfigureActivity extends Activity {
return; return;
} }
mAppWidgetText.setText(loadTitlePref(FrameWidgetConfigureActivity.this, mAppWidgetId)); // mAppWidgetText.setText(loadTitlePref(FrameWidgetConfigureActivity.this, mAppWidgetId));
} }
} }

View File

@ -1,27 +1,37 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
android:padding="16dp"> android:padding="16dp">
<TextView <Button
android:id="@+id/select_image_button"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:text="@string/configure" /> android:text="@string/select_an_image" />
<EditText <ImageView
android:id="@+id/appwidget_text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="text" /> android:layout_marginBottom="8dp"
app:srcCompat="@android:drawable/ic_menu_gallery"
android:id="@+id/imageView"
tools:ignore="ContentDescription" />
<Button <Button
android:id="@+id/add_button" android:id="@+id/finish_button"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginBottom="8dp"
android:text="@string/add_widget" /> android:text="@string/finish" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/about"/>
</LinearLayout> </LinearLayout>

View File

@ -1,6 +1,7 @@
<resources> <resources>
<string name="app_name">MicroFrame</string> <string name="app_name">MicroFrame</string>
<string name="appwidget_text">EXAMPLE</string> <string name="appwidget_text">EXAMPLE</string>
<string name="configure">Configure</string> <string name="select_an_image">Select an Image</string>
<string name="add_widget">Add widget</string> <string name="finish">Finish</string>
<string name="about">Version: 0.2.3\nAuthor: Tim Van Baak</string>
</resources> </resources>