0

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);
}
pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
Chris6647
  • 178
  • 1
  • 2
  • 13
  • have you tried this.. http://stackoverflow.com/a/5727920/1265724 – Ram kiran Pachigolla Jul 09 '12 at 05:28
  • I've done something along those lines, that isnt the issue atm though. The issue is, even if i create only one new textview, to add it the to widget so its actually shown. I'll add an example of one of the things ive tried to my question above in just a moment. Thank your for trying to help though! :) Much appreciated – Chris6647 Jul 09 '12 at 13:17

2 Answers2

0

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.

Chris6647
  • 178
  • 1
  • 2
  • 13
0
Hi Chris below is a resouce I developed to dynamically generate spinners and edit texts which are added to a linear layout hope it helps. This function

gives the elements unique ids that can be used to manipulate the elements properties

 /*Dynamic function*/
    public void dynaGen(LinearLayout tbl, ArrayList<Integer> ida, ArrayList<Integer> idab, ArrayList<Integer> idac,
                          ArrayList<Integer> idad, ArrayList<Integer> idae, ArrayList<Integer> idaf, final double eunica) {
        ArrayList<Integer> uniqid = new ArrayList<>();
        ArrayList<String> products = resocs.getallPRD();

        for (int i = 0; i < 6; ++i) {
            uniqid.add(Resources.generateViewId());
        }


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

        View child = ((ViewGroup) cont).getChildAt(0);
        Spinner prduct = (Spinner) child;
        prduct.setId(Math.abs(uniqid.get(0)));
        prduct.setPadding(0, 10, 0, 10);
        prduct.setAdapter(new ArrayAdapter(ordContext, R.layout.spinresoc, products) {
            public View getDropDownView(int v, View view, ViewGroup viewGroup) {
                TextView textView = (TextView) super.getDropDownView(v, view, viewGroup);
                textView.setTextSize((float) eunica + 5.0f);
                return textView;
            }

            public View getView(int v, View view, ViewGroup viewGroup) {
                TextView textView = (TextView) super.getView(v, view, viewGroup);
                textView.setTextSize((float) eunica + 5.0f);
                return textView;
            }
        });
        prduct.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) ordContext);
        resocs.spinMod(prduct, eunica);

        View childb = ((ViewGroup) cont).getChildAt(1);
        EditText quanty = (EditText) childb;
        quanty.setId(Math.abs(uniqid.get(1)));
        quanty.setHint("Quantity");
        quanty.setTextSize((float) eunica + 5.0f);
        quanty.addTextChangedListener((TextWatcher) ordContext);

        View childc = ((ViewGroup) cont).getChildAt(2);
        EditText price = (EditText) childc;
        price.setId(Math.abs(uniqid.get(2)));
        price.setHint("Price");
        price.setTextSize((float) eunica + 5.0f);

        View childd = ((ViewGroup) cont).getChildAt(3);
        EditText vat = (EditText) childd;
        vat.setId(Math.abs(uniqid.get(3)));
        vat.setHint("VAT");
        vat.setTextSize((float) eunica + 5.0f);

        View childe = ((ViewGroup) cont).getChildAt(4);
        EditText discount = (EditText) childe;
        discount.setId(Math.abs(uniqid.get(4)));
        discount.setHint("Discount");
        discount.setTextSize((float) eunica + 5.0f);
        discount.addTextChangedListener((TextWatcher) ordContext);

        View childf = ((ViewGroup) cont).getChildAt(5);
        EditText total = (EditText) childf;
        total.setId(Math.abs(uniqid.get(5)));
        total.setHint("Total");
        total.setTextSize((float) eunica + 5.0f);


        tbl.addView(cont, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        ida.add(prduct.getId());
        idab.add(quanty.getId());
        idac.add(price.getId());
        idad.add(vat.getId());
        idae.add(discount.getId());
        idaf.add(total.getId());

    }

/*XML resource*/
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:alignContent="flex_start"
    app:alignItems="flex_start"
    app:flexDirection="row"
    app:flexWrap="nowrap">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="40.0dip"
        android:background="@drawable/spintleric"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:popupBackground="@drawable/spinback_resoc"
        app:layout_flexBasisPercent="35.000004%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtdta_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:textColor="#ff000000"
        android:hint="@string/ordestringc"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtdta_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:textColor="#ff000000"
        android:editable="false"
        android:hint="@string/ordestringd"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:id="@+id/ordedtxtb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtdta_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:editable="false"
        android:padding="10.0dip"
        android:textColor="#ff000000"
        android:hint="@string/ordestringe"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtdta_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:textColor="#ff000000"
        android:hint="@string/ordestringf"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtcomb_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:editable="false"
        android:textColor="#ff000000"
        android:hint="@string/ordestringg"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />
</com.google.android.flexbox.FlexboxLayout>