-1

I have a widget, and I would like to open a new window, when the user clicks on the widget. Can someone please tell me how to do this, and can you please give me some code? I'm currently learning how to make android apps in Eclipse using java. Thanks in advance!

1
  • Search properly, you will get many tutorials.
    – Ravi Bhatt
    Sep 28, 2013 at 5:17

3 Answers 3

0

You can make any view clickable by adding

android:clickable="true"

to the xml or by calling

view.setClickable(true);

in java. See documentation

Then you can assign an on click listener to do any actions you desire.

0

Change your Class which extends AppWidgetProvider .you can see basic example for ceating simple widget.So you can detect click event of your widget in class which extends AppWidgetProvider. Here, MyWidgetProvider is an Service which will run in Background .

public class MyWidgetProvider extends AppWidgetProvider {

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

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetview);
            remoteViews.setOnClickPendingIntent(R.id.widgetlayout1, buildButtonPendingIntent(context));
        pushWidgetUpdate(context, remoteViews);
        }

    public static PendingIntent buildButtonPendingIntent(Context context) {

      Log.d("MyWidgetProvider", "click");
     Intent intent = new Intent(context,secondactivity.class);// Add secondactivity to Manifest
     context.startActivity(intent);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        }

I have seen your link which you are using.. You can put above code in your LovelyBatteryWidget Class.

EDIT

public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
        ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, remoteViews);     
    }

Check this Tutorial for your reference.

6
  • I have 1 errors: pushWidgetUpdate(context, remoteViews); //Can't find the method Sep 28, 2013 at 11:41
  • If I create a new pushWidgetUpdate() method, what should I place inside the method? Sep 28, 2013 at 11:48
  • Should I place the whole code, with the class declaration, inside the LovelyBatteryWidget Class? What should I place inside the pushWidgetUpdate Method, that I have to create, so I don't get the can't find method error? Thanks in advance! Sep 29, 2013 at 22:10
  • I was looking at the tutorial and they use a a widget button, and I also looked at the new boston tutorial and they also use a button, so my question is: How can I convert the widget to a button, so I can use the listener on that button? Please look at the code on the website I'm using. This is the line where I need to use this button:remoteViews.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context)); //Instead of thewidget_button this code is from the tutorial link you gave me Sep 30, 2013 at 14:47
  • Is there a way I could add the drawable that is used as a widget to a button? Sep 30, 2013 at 14:55
-1

Make another activity for new window in my code it is seconhdActivity

In onclickevent of your widget use the Intent That i use in my code

Intent mIntent = new Intent(MainActivity.this,
                    SecondActivity.class);
            startActivity(mIntent);

==========================================================================

public class MainActivity extends Activity {

Button btnButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnButton = (Button) findViewById(R.id.button1);
    btnButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent mIntent = new Intent(MainActivity.this,
                    SecondActivity.class);
            startActivity(mIntent);

        }
    });

}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.