Android Widget Tutorial 2017 – Android API 25 Supported

Android widget tutorial android studio 2.3.3 API 25 supported

First of all you may face that code is not working or you unable to find widget in your mobile. Reason you must have to install widget in your mobile internal memory. Learn new Android code with NextGenEarn get latest code snippets without deprecated code. Here below the simple Android widget tutorial. Create your own widget today. From widget you can open a link to your mobile browser along with show a TextView. Here from code change your package name with your own android project package.

android widget

Android Widget MainActivity extends AppWidgetProvider Code

package attraction.mywidget;

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;

public class MainActivity extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {

        for(int i=0; i<appWidgetIds.length; i++) {
            int currentWidgetId = appWidgetIds[i];
            String url = "http://www.google.com";

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse(url));

            PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
            RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.activity_main);

            views.setOnClickPendingIntent(R.id.button, pending);
            appWidgetManager.updateAppWidget(currentWidgetId,views);
            Toast.makeText(context, "SUCCESS TO GROW QUICK", Toast.LENGTH_SHORT).show();
        }
    }
}

Android Widget Layout activity_main.xml Code

<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity”
android:transitionGroup=”true”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/attraction”
android:textStyle=”bold”
android:id=”@+id/textView”
android:textColor=”#00b7ff”
android:textSize=”35sp”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/widget”
android:id=”@+id/button”
android:layout_below=”@+id/textView”
android:layout_centerHorizontal=”true” />

</RelativeLayout>

Android Widget AndroidManifest.xml Code

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”attraction.mywidget”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme”>
<receiver android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.appwidget.action.APPWIDGET_UPDATE”></action>
</intent-filter>

<meta-data
android:name=”android.appwidget.provider”
android:resource=”@xml/mywidge”></meta-data>
</receiver>

<activity android:name=”.MyWidgetConfigureActivity”>
<intent-filter>
<action android:name=”android.appwidget.action.APPWIDGET_CONFIGURE” />
</intent-filter>
</activity>
</application>

</manifest>

Get many more latest feature example code from NextGenEarn guys Bookmark our website for future visit. Support us in spreading world wide and solve your problem. Android Studio simple Audio Recorder example. Visit NextGenEarn Blog for more Android latest code and online money earnings tips. Get more widget update information from Official Android Page. Learn with latest android Studio version 2.3.3 so less chances of code deprecation alert. Happy coding keep visiting for more new codes.

Add a Comment

Your email address will not be published. Required fields are marked *