Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5996477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:06:31+00:00 2026-05-23T00:06:31+00:00

So I have a bit of experience building android applications. But now i would

  • 0

So I have a bit of experience building android applications. But now i would like to build a widget for android that would sit on the home screen and display a button, and when the button is pressed it plays a sound. I’ve been looking at tutorials online on how to set up an android widget but i cant seem to figure it out. Are there any good tutorials out there on how to make a standalone widget this simple or somewhere i can start?
Thanks in advance,
Peter

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:06:32+00:00Added an answer on May 23, 2026 at 12:06 am

    first Create a new layout file inside res/layout, under the project structure, that will define the widget layout (widgetlayout.xml) according to the following structure.

    <TextView android:text="@string/widgettext" 
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="0.8"
                  android:layout_gravity="center_vertical"
                  android:textColor="#000000"></TextView>
    <TextView android:text="@string/widgetmoodtext"
                  android:id="@+id/widgetMood" android:layout_width="0dp" 
                  android:layout_height="wrap_content" 
                  android:layout_weight="0.3" 
                  android:layout_gravity="center_vertical" 
                  android:textColor="#000000"></TextView>
    <ImageButton android:id="@+id/widgetBtn" 
                 android:layout_width="0dp" 
                 android:layout_height="wrap_content" 
                 android:layout_weight="0.5"  
                 android:src="@drawable/smile_icon" 
                 android:layout_gravity="center_vertical">
     </ImageButton>
    

    Create the res/xml folder under the project structure
    Create a xml file (widgetproviderinfo.xml) with the following parameters:

     <appwidget-provider
               xmlns:android="http://schemas.android.com/apk/res/android" 
               android:minWidth="220dp" 
               android:minHeight="72dp"
               android:updatePeriodMillis="86400000" 
               android:initialLayout="@layout/widgetlayout">
           </appwidget-provider>
    

    Now you should create the service that reacts to the user interaction with the smiley image button (CurrentMoodService.java).

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStart(intent, startId);
                updateMood(intent);
        stopSelf(startId);
        return START_STICKY;
    }
    
    private void updateMood(Intent intent) {
           if (intent != null){
            String requestedAction = intent.getAction();
            if (requestedAction != null &&  requestedAction.equals(UPDATEMOOD)){
                this.currentMood = getRandomMood();
                int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
                AppWidgetManager appWidgetMan = AppWidgetManager.getInstance(this);
                RemoteViews views = new RemoteViews(this.getPackageName(),R.layout.widgetlayout);
                views.setTextViewText(R.id.widgetMood, currentMood);
                appWidgetMan.updateAppWidget(widgetId, views);  
            }
           }
       }
    

    After defining the service, it is time to implement the widget provider class (CurrentMoodWidgetProvider.java).

        @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    
        for (int i=0; i<appWidgetIds.length; i++) {
            int appWidgetId = appWidgetIds[i];
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            Intent intent = new Intent(context, CurrentMoodService.class);
            intent.setAction(CurrentMoodService.UPDATEMOOD);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
            views.setOnClickPendingIntent(R.id.widgetBtn, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }   
    

    Finally it is necessary to declare the Service and the AppWidgetProvider in the Manifest (AndroidManifest.xml).

        <service android:name=".CurrentMoodService">
        </service>
    <receiver android:name=".CurrentMoodWidgetProvider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/widgetproviderinfo" />
    </receiver>
    

    and if you want to download the whole source code then have a look at the url below…

    http://sites.google.com/site/androidsourcecode/src/CurrentMoodWidgetProject.rar?attredirects=0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a bit new with android but I have some java experience. So
I have a bit of experience in Python but I'm taking the Udacity computer
I have a bit of experience writing OpenGL 2 applications and want to learn
I have a bit of experience setting up online payment systems that accept credit
I'm new to programming androids but I have quite a bit of experience programming
I have a bit of experience in creating web applications using asp.net however I
I have quite a bit experience in Python , but I haven't yet used
Does anybody have any experience/ knowledge in installing the aspImage.dll on a 64-bit Windows
i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have a bit of code that fits theoretical prediction to experimental data, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.