-1

I wanted to create a widget with a button. When it's clicked, how can make it perform a function like displaying a dialog box or a Toast?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • You'll have to show a bit more effort than that. The answer can surely be found here on SO - digging for the answer (not just asking for it) is part of the fun. Enjoy! – Teisman Jul 20 '13 at 19:33

2 Answers2

0

Code

Button button= (Button) findViewById(R.id.button1); // id which is defined inside your XML file below
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Your Toast or dialog code goes here
    }
});

XML

Inside the XML file, define the button as:

 <Button
    android:id="@+id/button1"
    android:layout_height = "wrap_content"
    android:layout_width ="wrap_content"
    android:text = "my Button"
 />

Remember

The user defined id should be defined with @+id, like android:id="@+id/button1", not with @id, like android:id="@id/button1".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
0

A widget works with remote views that send intents.
You need to set a PendingIntent see the next example.

After your context (Activity, Service,...) recieve the intent you can make a Toast or a Dialog.

Community
  • 1
  • 1
NickF
  • 5,637
  • 12
  • 44
  • 75