Created
October 3, 2016 07:45
-
-
Save anonymous/3fa540cc1b7450b7e5b63c5f39563276 to your computer and use it in GitHub Desktop.
App widget on Android. test by benznest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.benzneststudios.mywidgetapp.MainActivity"> | |
<TextView | |
android:id="@+id/tv_title" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Title" | |
android:textSize="40sp"/> | |
<!--<Button--> | |
<!--android:id="@+id/btn_save"--> | |
<!--android:layout_width="wrap_content"--> | |
<!--android:layout_height="wrap_content"--> | |
<!--android:text="Save"/>--> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.benzneststudios.mywidgetapp"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
</activity> | |
<receiver | |
android:name=".MyWidgetProvider" | |
android:label="My Widget"> | |
<intent-filter> | |
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> | |
</intent-filter> | |
<meta-data | |
android:name="android.appwidget.provider" | |
android:resource="@xml/app_widget_info"/> | |
</receiver> | |
<receiver | |
android:name=".MyWidgetListViewProvider" | |
android:label="My WidgetListView"> | |
<intent-filter> | |
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> | |
</intent-filter> | |
<meta-data | |
android:name="android.appwidget.provider" | |
android:resource="@xml/app_widget_listview"/> | |
</receiver> | |
<service | |
android:name=".MyWidgetListViewService" | |
android:permission="android.permission.BIND_REMOTEVIEWS"/> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:padding="16dp" | |
android:background="@drawable/dialog_rounded_corner_bg"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Are you ready for sending your phone to the moon?" | |
android:textSize="28sp" | |
android:id="@+id/textView" | |
android:textColor="#FFFFFF" | |
android:textStyle="bold"/> | |
<Button | |
android:id="@+id/btn_start" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Start now !!" | |
android:textSize="20sp" | |
android:padding="30dp" | |
android:textColor="@color/colorAccent"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:padding="16dp" | |
android:background="@drawable/dialog_rounded_corner_bg"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="This is 2 column. " | |
android:textSize="24sp" | |
android:id="@+id/textView" | |
android:textColor="#FFFFFF" | |
android:textStyle="bold"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" | |
android:minWidth="50dp" | |
android:minHeight="50dp" | |
android:updatePeriodMillis="86400000" | |
android:previewImage="@mipmap/ic_launcher" | |
android:initialLayout="@layout/app_widget_default" | |
android:resizeMode="horizontal|vertical" | |
android:widgetCategory="home_screen"> | |
</appwidget-provider> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@drawable/dialog_rounded_corner_bg" | |
android:orientation="vertical" | |
android:padding="16dp"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="My favorite food" | |
android:textColor="#FFFFFF" | |
android:textSize="24sp"/> | |
<ListView | |
android:id="@+id/listView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/empty_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:text="No data" | |
android:textColor="#ffffff" | |
android:textSize="28sp" | |
android:textStyle="bold"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benzneststudios.mywidgetapp; | |
import android.appwidget.AppWidgetManager; | |
import android.content.ComponentName; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
// Button btnSave; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Bundle bundle = getIntent().getExtras(); | |
if (bundle != null) { | |
String str = bundle.getString("MY_KEY"); | |
TextView tv = (TextView) findViewById(R.id.tv_title); | |
tv.setText(str); | |
} | |
// btnSave = (Button) findViewById(R.id.btn_save); | |
// btnSave.setOnClickListener(new View.OnClickListener() { | |
// @Override | |
// public void onClick(View v) { | |
// updateWidget(); | |
// } | |
// }); | |
} | |
private void updateWidget() { | |
Intent intent = new Intent(this, MyWidgetProvider.class); | |
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); | |
int[] ids = AppWidgetManager.getInstance(getApplication()) | |
.getAppWidgetIds(new ComponentName(getApplication(), MyWidgetProvider.class)); | |
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); | |
sendBroadcast(intent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benzneststudios.mywidgetapp; | |
import android.appwidget.AppWidgetManager; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.widget.RemoteViews; | |
import android.widget.RemoteViewsService; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by benznest on 02-Oct-16. | |
*/ | |
public class MyRemoteViewFactory implements RemoteViewsService.RemoteViewsFactory { | |
private static final int mCount = 20; | |
private List<String> mFoodName = new ArrayList<String>(); | |
private Context mContext; | |
private int mAppWidgetId; | |
public MyRemoteViewFactory(Context context, Intent intent) { | |
mContext = context; | |
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, | |
AppWidgetManager.INVALID_APPWIDGET_ID); | |
} | |
@Override | |
public void onCreate() { | |
for (int i = 0; i < mCount; i++) { | |
mFoodName.add("Padthai "+i); | |
} | |
} | |
public RemoteViews getViewAt(int position) { | |
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.row_food); | |
rv.setTextViewText(R.id.tv_food_name, mFoodName.get(position)); | |
// add onclick. | |
Bundle extras = new Bundle(); | |
extras.putString("MY_KEY", mFoodName.get(position)); | |
Intent fillInIntent = new Intent(); | |
fillInIntent.putExtras(extras); | |
rv.setOnClickFillInIntent(R.id.my_row, fillInIntent); | |
return rv; | |
} | |
@Override | |
public RemoteViews getLoadingView() { | |
return null; | |
} | |
@Override | |
public int getViewTypeCount() { | |
return 1; | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public boolean hasStableIds() { | |
return false; | |
} | |
@Override | |
public void onDataSetChanged() { | |
} | |
@Override | |
public void onDestroy() { | |
} | |
@Override | |
public int getCount() { | |
return mCount; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benzneststudios.mywidgetapp; | |
import android.app.PendingIntent; | |
import android.appwidget.AppWidgetManager; | |
import android.appwidget.AppWidgetProvider; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.widget.RemoteViews; | |
import android.widget.Toast; | |
/** | |
* Created by benznest on 02-Oct-16. | |
*/ | |
public class MyWidgetListViewProvider extends AppWidgetProvider { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
int viewIndex = intent.getIntExtra("MY_KEY", 0); | |
Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show(); | |
super.onReceive(context, intent); | |
} | |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { | |
for (int i = 0; i < appWidgetIds.length; ++i) { | |
Intent intent = new Intent(context, MyWidgetListViewService.class); | |
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); | |
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); | |
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_listview); | |
rv.setRemoteAdapter(R.id.listView, intent); | |
rv.setEmptyView(R.id.listView, R.id.empty_view); | |
Intent startActivityIntent = new Intent(context, MainActivity.class); | |
PendingIntent startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT); | |
rv.setPendingIntentTemplate(R.id.listView, startActivityPendingIntent); | |
appWidgetManager.updateAppWidget(appWidgetIds[i], rv); | |
} | |
super.onUpdate(context, appWidgetManager, appWidgetIds); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benzneststudios.mywidgetapp; | |
import android.content.Intent; | |
import android.widget.RemoteViewsService; | |
/** | |
* Created by benznest on 02-Oct-16. | |
*/ | |
public class MyWidgetListViewService extends RemoteViewsService { | |
@Override | |
public RemoteViewsFactory onGetViewFactory(Intent intent) { | |
return new MyRemoteViewFactory(this.getApplicationContext(), intent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benzneststudios.mywidgetapp; | |
import android.appwidget.AppWidgetManager; | |
import android.appwidget.AppWidgetProvider; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.widget.RemoteViews; | |
import android.widget.Toast; | |
/** | |
* Created by benznest on 02-Oct-16. | |
*/ | |
public class MyWidgetProvider extends AppWidgetProvider { | |
@Override | |
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { | |
int[] appWidgetIds = {appWidgetId}; | |
onUpdate(context,appWidgetManager, appWidgetIds); | |
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions); | |
} | |
private RemoteViews getRemoteViews(Context context, int minWidth, int minHeight) { | |
// First find out rows and columns based on width provided. | |
int rows = getCellsForSize(minHeight); | |
int columns = getCellsForSize(minWidth); | |
// Now you changing layout base on you column count | |
// In this code from 1 column to 4 | |
// you can make code for more columns on your own. | |
switch (columns) { | |
case 2: | |
return new RemoteViews(context.getPackageName(), R.layout.app_widget_default_2_columns); | |
case 3: | |
return new RemoteViews(context.getPackageName(), R.layout.app_widget_default_3_columns); | |
default: | |
return new RemoteViews(context.getPackageName(), R.layout.app_widget_default_4_columns); | |
} | |
} | |
private static int getCellsForSize(int size) { | |
int n = 2; | |
while (70 * n - 30 < size) { | |
n++; | |
} | |
return n - 1; | |
} | |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { | |
// loop each App Widget. | |
for (int i = 0; i < appWidgetIds.length; i++) { | |
Bundle options = null; | |
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { | |
options = appWidgetManager.getAppWidgetOptions(appWidgetIds[i]); | |
// Get min width and height. | |
int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); | |
int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); | |
// Obtain appropriate widget and update it. | |
RemoteViews remoteViews = getRemoteViews(context, minWidth, minHeight); | |
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); | |
} | |
Toast.makeText(context, "Update appWidgetId = " + appWidgetIds[i], Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/my_row" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:padding="16dp"> | |
<ImageView | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:src="@mipmap/ic_launcher"/> | |
<TextView | |
android:id="@+id/tv_food_name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Food name" | |
android:textSize="20sp" | |
android:layout_gravity="center_vertical" | |
android:layout_marginLeft="16dp" | |
android:textColor="#FFFFFF"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" | |
android:minWidth="250dp" | |
android:minHeight="200dp" | |
android:updatePeriodMillis="86400000" | |
android:previewImage="@mipmap/ic_launcher" | |
android:initialLayout="@layout/app_widget_listview" | |
android:resizeMode="horizontal|vertical" | |
android:widgetCategory="home_screen"> | |
</appwidget-provider> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment