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,
int appWidgetId) {
CharSequence widgetText = FrameWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// CharSequence widgetText = FrameWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
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
appWidgetManager.updateAppWidget(appWidgetId, views);
@ -35,7 +35,7 @@ public class FrameWidget extends AppWidgetProvider {
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
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.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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 PREF_PREFIX_KEY = "appwidget_";
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) {
final Context context = FrameWidgetConfigureActivity.this;
// When the button is clicked, store the string locally
String widgetText = mAppWidgetText.getText().toString();
saveTitlePref(context, mAppWidgetId, widgetText);
// Test function: change background color
Button selectButton = (Button) findViewById(R.id.select_image_button);
selectButton.setBackgroundColor(Color.GREEN);
// It is the responsibility of the configuration activity to update the app widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
FrameWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
// final Context context = FrameWidgetConfigureActivity.this;
//
// // 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
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
@ -42,30 +59,30 @@ public class FrameWidgetConfigureActivity extends Activity {
super();
}
// Write the prefix to the SharedPreferences object for this widget
static void saveTitlePref(Context context, int appWidgetId, String text) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
prefs.apply();
}
// // Write the prefix to the SharedPreferences object for this widget
// static void saveTitlePref(Context context, int appWidgetId, String text) {
// SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
// prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
// prefs.apply();
// }
// Read the prefix from the SharedPreferences object for this widget.
// If there is no preference saved, get the default from a resource
static String loadTitlePref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
if (titleValue != null) {
return titleValue;
} else {
return context.getString(R.string.appwidget_text);
}
}
// // Read the prefix from the SharedPreferences object for this widget.
// // If there is no preference saved, get the default from a resource
// static String loadTitlePref(Context context, int appWidgetId) {
// SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
// String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
// if (titleValue != null) {
// return titleValue;
// } else {
// return context.getString(R.string.appwidget_text);
// }
// }
static void deleteTitlePref(Context context, int appWidgetId) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.remove(PREF_PREFIX_KEY + appWidgetId);
prefs.apply();
}
// static void deleteTitlePref(Context context, int appWidgetId) {
// SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
// prefs.remove(PREF_PREFIX_KEY + appWidgetId);
// prefs.apply();
// }
@Override
public void onCreate(Bundle icicle) {
@ -76,8 +93,8 @@ public class FrameWidgetConfigureActivity extends Activity {
setResult(RESULT_CANCELED);
setContentView(R.layout.frame_widget_configure);
mAppWidgetText = (EditText) findViewById(R.id.appwidget_text);
findViewById(R.id.add_button).setOnClickListener(mOnClickListener);
findViewById(R.id.select_image_button).setOnClickListener(mSelectClickListener);
findViewById(R.id.finish_button).setOnClickListener(mFinishClickListener);
// Find the widget id from the intent.
Intent intent = getIntent();
@ -93,7 +110,7 @@ public class FrameWidgetConfigureActivity extends Activity {
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"?>
<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_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
<Button
android:id="@+id/select_image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/configure" />
android:text="@string/select_an_image" />
<EditText
android:id="@+id/appwidget_text"
<ImageView
android:layout_width="match_parent"
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
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:id="@+id/finish_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/add_widget" />
android:layout_marginBottom="8dp"
android:text="@string/finish" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/about"/>
</LinearLayout>

View File

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