3

I need to do a simple widget that presents a Toast when clicking on it. My problem is that I cant find how to get or create an "oncreate" action. i see the examples with the pending intent that opens the web browser. But how do i simply create this: Toast.makeText(context, "activated", Toast.LENGTH_LONG).show(); and make it happen every time a user clicks on the widget? just to be clear, I mean a widget on the launcher of the phone. not a regular "button" widget etc...

public class Widget extends AppWidgetProvider {
NotificationManager mNotificationManager;
Notification notification;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);



    appWidgetManager.updateAppWidget(appWidgetIds, view);
}

}

Thanks!

roiberg
  • 13,629
  • 12
  • 60
  • 91

6 Answers6

4

Just call Toast.makeText(context, "activated", Toast.LENGTH_LONG).show(); in widget's onClick() method of View.OnClickListener

Update:

If you use AppWidgetProvider so check this and this one posts

Community
  • 1
  • 1
teoREtik
  • 7,886
  • 15
  • 46
  • 65
1

I think you are missing to show the created Toast. For example:

Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();

Moreover, override the onClick method of your custom View class and pop the toast there.


public class TestButton extends Button {

    public TestButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "activated", Toast.LENGTH_LONG)
                  .show();
            }
        });
    }

}
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Thanks, i added that a second ago. its not that. – roiberg Sep 20 '12 at 11:51
  • 1
    how do i set "view.setonclicklistener" in a class that extends BroadcastReceiver. its imposible. – roiberg Sep 20 '12 at 12:00
  • I'm talking about your custom View class, see my updated answer – waqaslam Sep 20 '12 at 12:06
  • i guess its not halping me couse im kinda new to android but i dont see how i relate the "TestButton" to the widget on the home screen? but ill try that. thanks. – roiberg Sep 20 '12 at 12:17
  • That `TestButton` is an exemplary class. The purpose is to show you how to override `onClick` of your button whean dealing with custom Views. – waqaslam Sep 20 '12 at 13:46
0
Button btn=(Button) findViewById(R.id.button);

            btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
                   Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();


  }
}
});
  • 1
    there is no "findviewbyid" in a class that extends BroadcastReceiver. its imposible. – roiberg Sep 20 '12 at 11:59
  • if you are using BroadcastReceiver then send broadcast message on button click event in Activity.And toast in onRecive() method. –  Sep 21 '12 at 11:09
0

Try this:

button = (Button) findViewById(R.id.buttonToast);

        button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        // Create a piece of toast.
        Toast pieceToast = Toast.makeText(getApplicationContext(), "Test Message", Toast.LENGTH_SHORT);

        // Show the toast.
        pieceToast.show();
    }

This is my BroadcastReciever Class:

public class IncomingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if(null == bundle)
            return;
        Log.i("IncomingCallReceiver",bundle.toString());
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        String registredPhoneNumber;
        Log.i("IncomingCallReceiver","State: "+ state);
        String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        System.out.println("*****Mobile Ringing*******"+phonenumber);
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
        {
            registredPhoneNumber = PreferenceConnector.getPhoneNumber(context);
            System.out.println("registredPhoneNumber:  "+registredPhoneNumber);
            System.out.println("phonenumber:   "+phonenumber);
            Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
            if(phonenumber.equals(registredPhoneNumber)){
                System.out.println("Entered...");
                String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
                if(isMyServiceRunning(context)){
                    context.stopService(new Intent(context,com.visiomaticamericas.visitormobile.services.LaunchServiceActivity.class));
                    System.out.println("******Service Stopped*********");
                }
                Intent i = new Intent(context,com.services.LaunchServiceActivity.class);
                i.putExtra("delay",500L);
                context.startService(i);
                System.out.println("*****Service Started*****");
                Toast.makeText(context, info, Toast.LENGTH_LONG).show();
            }

        }
    }


}

Here, I called my activity class.. and I did my widget design in My activity class..

  • 1
    how do i set "view.setonclicklistener" in a class that extends BroadcastReceiver. its imposible. – roiberg Sep 20 '12 at 11:58
  • you are still doing button.setOnClickListener!! you can not do that in a AppWidgetProvider! non of you read the comments :) – roiberg Sep 20 '12 at 12:03
  • We are not able do this.. So, you just call your activity class from OnRecievemethod of your broadcastReciever extended class. You just design you UI under Activity class.. See My question edited.. –  Sep 20 '12 at 12:17
0

this may helps , here need to pass context and get Onclick Event with your widget

Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();

Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
0

folks my VS environment is VS 2017 with Xamarin .

   private void DisplayMessage(string DisplayMsgText)
    {
        Toast.MakeText(this.ApplicationContext, DisplayMsgText, ToastLength.Short).Show();
    }

and use this function to show message at device screen like below:

DisplayMessage("Vibrate button pressed");