Search code examples
androiddynamicandroid-linearlayouttextviewandroid-appwidget

How to add X number of TextViews to my widget dynamically?


I've searched and searched and for whatever reason I just can't seem to get it working.

What I'm doing currently is that I have a config class in which I, based on selected items on spinners, want to create x amount of TextViews which I want to set their text as well. So I cant just have these TextViews created in the .xml file, I have to create them in runtime, since I won't know until then, how many I need. These TextViews must be added to the widget so that they are shown when the widget is created upon pushing the configuration class' add widget button. I'd like to add the textviews to my Widgets vertical linearlayout that's contained within a horizontal linearlayout within a relativelayout.

So far I've been able to create the TextViews and set the proper text, but whatever I seem to try, I just can't figure out how to add them to the widget so they're actually shown. I've tried inflating the widgets layout, getting the id's and adding them, tried to fool around with RemoteViews since I realized this is a widget and that's prolly how its supposed to be done. Nothing seems to make them want to be added, not even when I just try to create 1 TextView from code. I just can't get it to display itself and the setText I've given it.

I'm working in android 2.2 btw, and this is my first android app / widget, so please don't be too brutal, since I'm prolly making a stupid mistake somewhere...

If you need code examples in order to help me better, lemme know, and ill try to add it later. Right now I'm asking for help on a computer that doesn't have access to the code :)

Thank you so much for taking the time to read this! Hope someone can tell me what I have to do in order to get this done. Or whether its even possible :S /Chris

edit:

This is one of the things ive tried to get it to work, but i guess this doesnt work since im working with a widget, and i ought to use a remoteview and somehow do it through that? From things ive read, it seems like remote views only works with xml generated codes. If thats truly the case, how would i then go about creating, and adding textviews to my widget? :S

//the context c is defined as this, elsewhere:
c = WidgetConfig.this;

public void createPopulateTable(RemoteViews rv){

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

LinearLayout vll1 = (LinearLayout)layout.findViewById(R.id.wVertLinearL1);

// Create a TextView for the left column
TextView wLabelTv = new TextView(c);
wLabelTv.setId(200);
wLabelTv.setTextColor(Color.BLACK);
wLabelTv.setLayoutParams(new LayoutParams(
    LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT));
wLabelTv.setText("No Classes Today");
vll1.addView(wLabelTv);
}

Solution

  • I ended up dropping the idea of dynamically creating the amount of TextViews i needed, and instead created a basic layout of sorts, in xml, on which i could (through iterations) set the text and formatting i wanted, and simply add the entire xml file over and over again :)

    Each row i needed, was always gonna contain a textview and 3 imageviews. I made that skeleton in the new xml file. And in the code i set them to what i wanted, and added the row to the main layout. I did this for each row i needed.