How to control a button in the app from its widget in android












0















In my project i have to control a button of the application from its widget.That is when pressing the punch-in button in widget it should work as the punch-in button in the application,



i have given my code below, so far i have created an application which have two button. And a widget for the application. widget have two button and a text view. By pressing first button of the widget(openApp),it will open the application,the second button lead us to google home page.




MainActivity.java




package com.example.emssh.widget_test_1;

import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
final Button testButton = (Button) findViewById(R.id.btnPunchIn);
Button checkIn = (Button) findViewById(R.id.btnCheckIn);
testButton.setTag(1);

testButton.setText("Punch In");
testButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int status = (Integer) v.getTag();
if (status == 1) {

Toast.makeText(MainActivity.this, "Punched In", Toast.LENGTH_LONG).show();
testButton.setText("Punch Out");
testButton.setBackgroundDrawable(getResources().getDrawable(R.color.colorPrimary));
v.setTag(0); // punch in


} else {

testButton.setText("Punch In");
testButton.setBackgroundDrawable(getResources().getDrawable(R.color.colorAccent));
v.setTag(1); //punch out

}
}
});
}

public void CheckIn(View view) {
Toast.makeText(MainActivity.this, "customer checked in", Toast.LENGTH_LONG).show();
}
}



MywidgetProvider.xml




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin"
>

<Button
android:id="@+id/btnOpenApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="187dp"
android:text="OpenApp" />

<Button
android:id="@+id/btnOpenweb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="204dp"
android:text="goto" />


</RelativeLayout>



MywidgetProvider.java




package com.example.emssh.widget_test_1;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

import java.util.Random;

/**
* Implementation of App Widget functionality.
*/
public class MywidgetProvider extends AppWidgetProvider {

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {



RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mywidget_provider);

String text = "data :" + new Random().nextInt(1000);
views.setTextViewText(R.id.textView2,text);

Intent openApp = new Intent(context,MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context,0,openApp,0);

views.setOnClickPendingIntent(R.id.btnOpenApp,pIntent);

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.googlr.com"));
PendingIntent myPIntent = PendingIntent.getActivity(context,1,intent,0);

views.setOnClickPendingIntent(R.id.btnOpenweb, myPIntent);

appWidgetManager.updateAppWidget(appWidgetId,views);

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);

}
}

@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}


i want to achieve that when i am pressing a button(check-in) from my widget, the app should run the code in the checkIn method in main activity.anyone can help me ?










share|improve this question





























    0















    In my project i have to control a button of the application from its widget.That is when pressing the punch-in button in widget it should work as the punch-in button in the application,



    i have given my code below, so far i have created an application which have two button. And a widget for the application. widget have two button and a text view. By pressing first button of the widget(openApp),it will open the application,the second button lead us to google home page.




    MainActivity.java




    package com.example.emssh.widget_test_1;

    import android.annotation.SuppressLint;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setContentView(R.layout.activity_main);
    final Button testButton = (Button) findViewById(R.id.btnPunchIn);
    Button checkIn = (Button) findViewById(R.id.btnCheckIn);
    testButton.setTag(1);

    testButton.setText("Punch In");
    testButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    final int status = (Integer) v.getTag();
    if (status == 1) {

    Toast.makeText(MainActivity.this, "Punched In", Toast.LENGTH_LONG).show();
    testButton.setText("Punch Out");
    testButton.setBackgroundDrawable(getResources().getDrawable(R.color.colorPrimary));
    v.setTag(0); // punch in


    } else {

    testButton.setText("Punch In");
    testButton.setBackgroundDrawable(getResources().getDrawable(R.color.colorAccent));
    v.setTag(1); //punch out

    }
    }
    });
    }

    public void CheckIn(View view) {
    Toast.makeText(MainActivity.this, "customer checked in", Toast.LENGTH_LONG).show();
    }
    }



    MywidgetProvider.xml




    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#09C"
    android:padding="@dimen/widget_margin"
    >

    <Button
    android:id="@+id/btnOpenApp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="187dp"
    android:text="OpenApp" />

    <Button
    android:id="@+id/btnOpenweb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="204dp"
    android:text="goto" />


    </RelativeLayout>



    MywidgetProvider.java




    package com.example.emssh.widget_test_1;

    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.widget.RemoteViews;

    import java.util.Random;

    /**
    * Implementation of App Widget functionality.
    */
    public class MywidgetProvider extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
    int appWidgetId) {



    RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mywidget_provider);

    String text = "data :" + new Random().nextInt(1000);
    views.setTextViewText(R.id.textView2,text);

    Intent openApp = new Intent(context,MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(context,0,openApp,0);

    views.setOnClickPendingIntent(R.id.btnOpenApp,pIntent);

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.googlr.com"));
    PendingIntent myPIntent = PendingIntent.getActivity(context,1,intent,0);

    views.setOnClickPendingIntent(R.id.btnOpenweb, myPIntent);

    appWidgetManager.updateAppWidget(appWidgetId,views);

    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
    updateAppWidget(context, appWidgetManager, appWidgetId);

    }
    }

    @Override
    public void onEnabled(Context context) {
    // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
    // Enter relevant functionality for when the last widget is disabled
    }
    }


    i want to achieve that when i am pressing a button(check-in) from my widget, the app should run the code in the checkIn method in main activity.anyone can help me ?










    share|improve this question



























      0












      0








      0








      In my project i have to control a button of the application from its widget.That is when pressing the punch-in button in widget it should work as the punch-in button in the application,



      i have given my code below, so far i have created an application which have two button. And a widget for the application. widget have two button and a text view. By pressing first button of the widget(openApp),it will open the application,the second button lead us to google home page.




      MainActivity.java




      package com.example.emssh.widget_test_1;

      import android.annotation.SuppressLint;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.Toast;

      public class MainActivity extends AppCompatActivity {


      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setContentView(R.layout.activity_main);
      final Button testButton = (Button) findViewById(R.id.btnPunchIn);
      Button checkIn = (Button) findViewById(R.id.btnCheckIn);
      testButton.setTag(1);

      testButton.setText("Punch In");
      testButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      final int status = (Integer) v.getTag();
      if (status == 1) {

      Toast.makeText(MainActivity.this, "Punched In", Toast.LENGTH_LONG).show();
      testButton.setText("Punch Out");
      testButton.setBackgroundDrawable(getResources().getDrawable(R.color.colorPrimary));
      v.setTag(0); // punch in


      } else {

      testButton.setText("Punch In");
      testButton.setBackgroundDrawable(getResources().getDrawable(R.color.colorAccent));
      v.setTag(1); //punch out

      }
      }
      });
      }

      public void CheckIn(View view) {
      Toast.makeText(MainActivity.this, "customer checked in", Toast.LENGTH_LONG).show();
      }
      }



      MywidgetProvider.xml




      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#09C"
      android:padding="@dimen/widget_margin"
      >

      <Button
      android:id="@+id/btnOpenApp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="187dp"
      android:text="OpenApp" />

      <Button
      android:id="@+id/btnOpenweb"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="204dp"
      android:text="goto" />


      </RelativeLayout>



      MywidgetProvider.java




      package com.example.emssh.widget_test_1;

      import android.app.PendingIntent;
      import android.appwidget.AppWidgetManager;
      import android.appwidget.AppWidgetProvider;
      import android.content.Context;
      import android.content.Intent;
      import android.net.Uri;
      import android.widget.RemoteViews;

      import java.util.Random;

      /**
      * Implementation of App Widget functionality.
      */
      public class MywidgetProvider extends AppWidgetProvider {

      static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
      int appWidgetId) {



      RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mywidget_provider);

      String text = "data :" + new Random().nextInt(1000);
      views.setTextViewText(R.id.textView2,text);

      Intent openApp = new Intent(context,MainActivity.class);
      PendingIntent pIntent = PendingIntent.getActivity(context,0,openApp,0);

      views.setOnClickPendingIntent(R.id.btnOpenApp,pIntent);

      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.googlr.com"));
      PendingIntent myPIntent = PendingIntent.getActivity(context,1,intent,0);

      views.setOnClickPendingIntent(R.id.btnOpenweb, myPIntent);

      appWidgetManager.updateAppWidget(appWidgetId,views);

      }

      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds) {
      // There may be multiple widgets active, so update all of them
      for (int appWidgetId : appWidgetIds) {
      updateAppWidget(context, appWidgetManager, appWidgetId);

      }
      }

      @Override
      public void onEnabled(Context context) {
      // Enter relevant functionality for when the first widget is created
      }

      @Override
      public void onDisabled(Context context) {
      // Enter relevant functionality for when the last widget is disabled
      }
      }


      i want to achieve that when i am pressing a button(check-in) from my widget, the app should run the code in the checkIn method in main activity.anyone can help me ?










      share|improve this question
















      In my project i have to control a button of the application from its widget.That is when pressing the punch-in button in widget it should work as the punch-in button in the application,



      i have given my code below, so far i have created an application which have two button. And a widget for the application. widget have two button and a text view. By pressing first button of the widget(openApp),it will open the application,the second button lead us to google home page.




      MainActivity.java




      package com.example.emssh.widget_test_1;

      import android.annotation.SuppressLint;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.Toast;

      public class MainActivity extends AppCompatActivity {


      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setContentView(R.layout.activity_main);
      final Button testButton = (Button) findViewById(R.id.btnPunchIn);
      Button checkIn = (Button) findViewById(R.id.btnCheckIn);
      testButton.setTag(1);

      testButton.setText("Punch In");
      testButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      final int status = (Integer) v.getTag();
      if (status == 1) {

      Toast.makeText(MainActivity.this, "Punched In", Toast.LENGTH_LONG).show();
      testButton.setText("Punch Out");
      testButton.setBackgroundDrawable(getResources().getDrawable(R.color.colorPrimary));
      v.setTag(0); // punch in


      } else {

      testButton.setText("Punch In");
      testButton.setBackgroundDrawable(getResources().getDrawable(R.color.colorAccent));
      v.setTag(1); //punch out

      }
      }
      });
      }

      public void CheckIn(View view) {
      Toast.makeText(MainActivity.this, "customer checked in", Toast.LENGTH_LONG).show();
      }
      }



      MywidgetProvider.xml




      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#09C"
      android:padding="@dimen/widget_margin"
      >

      <Button
      android:id="@+id/btnOpenApp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="187dp"
      android:text="OpenApp" />

      <Button
      android:id="@+id/btnOpenweb"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="204dp"
      android:text="goto" />


      </RelativeLayout>



      MywidgetProvider.java




      package com.example.emssh.widget_test_1;

      import android.app.PendingIntent;
      import android.appwidget.AppWidgetManager;
      import android.appwidget.AppWidgetProvider;
      import android.content.Context;
      import android.content.Intent;
      import android.net.Uri;
      import android.widget.RemoteViews;

      import java.util.Random;

      /**
      * Implementation of App Widget functionality.
      */
      public class MywidgetProvider extends AppWidgetProvider {

      static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
      int appWidgetId) {



      RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mywidget_provider);

      String text = "data :" + new Random().nextInt(1000);
      views.setTextViewText(R.id.textView2,text);

      Intent openApp = new Intent(context,MainActivity.class);
      PendingIntent pIntent = PendingIntent.getActivity(context,0,openApp,0);

      views.setOnClickPendingIntent(R.id.btnOpenApp,pIntent);

      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.googlr.com"));
      PendingIntent myPIntent = PendingIntent.getActivity(context,1,intent,0);

      views.setOnClickPendingIntent(R.id.btnOpenweb, myPIntent);

      appWidgetManager.updateAppWidget(appWidgetId,views);

      }

      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds) {
      // There may be multiple widgets active, so update all of them
      for (int appWidgetId : appWidgetIds) {
      updateAppWidget(context, appWidgetManager, appWidgetId);

      }
      }

      @Override
      public void onEnabled(Context context) {
      // Enter relevant functionality for when the first widget is created
      }

      @Override
      public void onDisabled(Context context) {
      // Enter relevant functionality for when the last widget is disabled
      }
      }


      i want to achieve that when i am pressing a button(check-in) from my widget, the app should run the code in the checkIn method in main activity.anyone can help me ?







      java android android-studio android-layout user-interface






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 11:08







      mohammed shefeeq

















      asked Nov 26 '18 at 7:00









      mohammed shefeeqmohammed shefeeq

      36




      36
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476102%2fhow-to-control-a-button-in-the-app-from-its-widget-in-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476102%2fhow-to-control-a-button-in-the-app-from-its-widget-in-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)