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 7888767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:55:11+00:00 2026-06-03T05:55:11+00:00

Im trying to make it so that on a press of a image button

  • 0

Im trying to make it so that on a press of a image button on my widget it will change the text of a textview. This textviews data is a random string from a array (Something i already have code for). I started to have a look at a broadcast reciever but i really dont know why mine isnt working. Any help is really appreciated 🙂

Widget activity

public class Widget extends AppWidgetProvider {




private Random rgenerator = new Random();
private Random rgenerator2 = new Random();
private String[] myString1;






  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
      Resources res = context.getResources();
        myString1 = res.getStringArray(R.array.myArray);


    final int N = appWidgetIds.length;
    Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));
    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
      int appWidgetId = appWidgetIds[i];
      // Create an Intent to launch ExampleActivity
      Intent intent = new Intent();
        intent.setAction(TestReceiver.TEST_INTENT);
        intent.setClassName(TestReceiver.class.getPackage().getName(), TestReceiver.class.getName());
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
      views.setOnClickPendingIntent(R.id.imagewidgeterica, pendingIntent);
      // To update a label
      views.setTextViewText(R.id.widget1label , myString1[rgenerator.nextInt(myString1.length)]);
      // Tell the AppWidgetManager to perform an update on the current app
      // widget
      appWidgetManager.updateAppWidget(appWidgetId, views);


    }

}


         }

Broadcast reciver:

package kevin.erica.box;

public class TestReceiver extends BroadcastReceiver {
private Random rgenerator = new Random();
private Random rgenerator2 = new Random();
private String [] myString1;
private String myStringRandom = myString1[rgenerator.nextInt(myString1.length)];

public static final String TEST_INTENT= "MyTestIntent";

@Override
public void onReceive(Context context, Intent intent) {
 // TODO Auto-generated method stub

 Toast.makeText(context, "Test", Toast.LENGTH_SHORT);

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());

  Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();
  int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
  RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            rViews.setTextViewText(R.id.widget1label, "someupdateddata");
            appWidgetManager.updateAppWidget(appWidgetIds, rViews);


}



 }
  • 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-06-03T05:55:12+00:00Added an answer on June 3, 2026 at 5:55 am

    If the ImageButton and TextView are both in the same layout, then BroadcastReceiver is probably overkill.

    Have you tried something like this in your Activity:

    final Widget w = (Widget)findViewById(R.id.yourWidgetId);
    ImageButton button = (ImageButton) findViewById(R.id.yourImageButtonId);
    
    button.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            w.onUpdate();
        }
    }
    

    http://developer.android.com/reference/android/view/View.OnClickListener.html

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

Sidebar

Related Questions

I'm trying to make a random image appear on the press of a button.
I'm trying to make a Button that will play a animation-list when pressed. I
I'm trying to make a form that will take given text and direct you
I am trying to make something that will enable all controls if a SubString
Im trying to make a function that will return an element of type point:
Im trying to make multiple quizes that will keep track of each quiz on
Im trying to make a JDialog that will show the user a dynamic message
So I'm trying to make a button that launches the OpenFeint achievements page, but
I'm trying to make a regular button that functions like a toggle button, but
I'm trying to make an app that will respond to your command when inserted.

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.