Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update app widget on click in android

I am working on Android Widget my widget is like following diagram, [prev] event name [next]

prev, next are the buttons on the widget.So i want to update the event name on click of next and prev button.On start of the service i loaded the data and stored it in array list. Now i want to iterate through the array list by clicking the next and prev buttons on the widget.

So how can i achieve this.Please help me with proper solution. Here is my xml layout

<LinearLayout
    android:id="@+id/lytWidget2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnPreviousView"
        android:layout_width="43dp"
        android:layout_height="fill_parent"
        android:layout_gravity="left|center"
        android:background="@drawable/ic_leftarrow"
        android:padding="5dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="52dp"
        android:layout_weight="0.97"
        android:text="Event Name" />

    <Button
        android:id="@+id/btnNextView"
        android:layout_width="43dp"
        android:layout_height="fill_parent"
        android:layout_gravity="right|center_vertical"
        android:background="@drawable/ic_rightarrow"
        android:padding="5dip" />
</LinearLayout>
like image 753
VK.Dev Avatar asked Dec 21 '22 04:12

VK.Dev


1 Answers

So, just handle the click events of your buttons in the AppWidgetProvider class like this. See this good tutorial on how to create simple widget. There are click handlers added too.

  • Tutorial – Creating a Custom Analog Clock Widget

Sample code

 @Override 
 public void onReceive(Context context, Intent intent) 
 { 
      super.onReceive(context, intent); 

      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
      // find your TextView here by id here and update it.

      Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show(); 
 } 
like image 61
Tomislav Markovski Avatar answered Dec 23 '22 17:12

Tomislav Markovski