Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text on a widget's textview

I am trying to set text on a home screen widget. i am using remoteView to archive this, but it does not show the text when i run the application. The text needs to be set on a TextView (labelled 'Update")

public class MyWidgetProvider extends AppWidgetProvider 
{       
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
  {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    views.setTextViewText(R.id.update, "Hellow");
  }
}
like image 516
Ndupza Avatar asked Oct 02 '12 10:10

Ndupza


People also ask

Which method is used to set the text in a TextView?

SetText(String, TextView+BufferType) Sets the text to be displayed using a string resource identifier.

How do you fit text in TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.

Can you set text in EditText?

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.


1 Answers

try below code.

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

            DateFormat format = SimpleDateFormat.getTimeInstance(
                    SimpleDateFormat.MEDIUM, Locale.getDefault());
            CharSequence text = format.format(new Date());


            Intent intent = new Intent(context, UpdateService.class);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0,
                    intent, 0);


            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget);
            remoteViews.setOnClickPendingIntent(R.id.LinearLayout01, pendingIntent);

            remoteViews.setTextViewText(R.id.widget_textview, text); <---- here you can set text

            // Tell the widget manager
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

you have to make a one xml file

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_height="120dp"
    android:layout_width="320dp"
    android:orientation="horizontal"
    >

<TextView  
    android:id="@+id/widget_textview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:textColor="@android:color/white"
    android:text="00:00:00 PM"
    android:textSize="16sp"
    android:background="#FFAADD"
    />


</LinearLayout>

it will print the text in widget

like image 92
Hardik Nadiyapara Avatar answered Oct 12 '22 22:10

Hardik Nadiyapara