Ahad Porkar
Ahad Porkar

Reputation: 1698

dynamic TextView in android appwidget

I want to add dynamic TextView to my appwidget but cant find the solution to do it. In normal view, I use findViewById, but i guess its not supported in appwidget.

Here is my code:

List<String> list2 = new ArrayList<String>();
this.list2 = populatelist();             
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);

AppWidgetManager appWidgetMan = AppWidgetManager.getInstance(this);
RemoteViews views = new RemoteViews(this.getPackageName(),R.layout.mywidgetlayout);
LinearLayout lin = (LinearLayout) findViewById(R.id.mylinear);
for (int i = 0; i <= list2.size() ; i++)
{
    TextView myText = new TextView(this);
    myText.setText(list2.get(i));
    lin.addView(myText);
}
appWidgetMan.updateAppWidget(widgetId, views);

and my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/mylinear"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:padding="0.1dp">

    <ImageButton 
        android:id="@+id/widgetBtn" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.1" 
        android:src="@drawable/smile_icon" 
        android:layout_gravity="bottom">
    </ImageButton>

</LinearLayout>

This line gives error on findViewById:

LinearLayout lin = (LinearLayout) findViewById(R.id.mylinear);

Upvotes: 0

Views: 319

Answers (2)

Ketan Ahir
Ketan Ahir

Reputation: 6728

you can get linearlayout for remoteviews like this

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout1_1);

and then settext to textview within that layout

remoteViews.setTextViewText(R.id.textviewId, "YOUR TEXT HERE");

hope this will help!!

Upvotes: 0

nitesh goel
nitesh goel

Reputation: 6406

try this...

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.mywidgetlayout, null);

    LinearLayout lin = (LinearLayout)layout.findViewById(R.id.mylinear);

Upvotes: 1

Related Questions