Programmatically adding linear layouts and textviews

PeterDavisPeterDavis USMember ✭✭✭

I have an activity that has a number of items listed. Each item in the list is inside a horizontal LinearLayout with a WeightSum of 2. Here's an example:

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">
            <TextView
                android:text="Ticket Expiration"
                style="@style/greytext"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_weight="1"
                android:gravity="right"
                android:minWidth="25dp"
                android:minHeight="25dp"
                android:layout_marginRight="5dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <TextView
                android:textAppearance="?android:attr/textAppearanceMedium"
                style="@style/whitetext"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:minWidth="25dp"
                android:minHeight="25dp"
                android:layout_width="0dp"
                android:id="@+id/txtTicketExpiration" />
        </LinearLayout>

These are all inside of a vertical LinearLayout. This part works perfectly. The TextView on the left is right-justified and takes up half of the screen width. The TextView on the right is left-justified and takes up half the screen width.

Then I have an empty one that I'm trying to fill in programmatically:

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/permissions"
        android:weightSum="2" />

        for (int index = 0; index < _controller.Permissions.Length; index++)
        {
            LinearLayout ll = FindViewById<LinearLayout>(Resource.Id.permissions);
            LinearLayout llInner = new LinearLayout(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
            llInner.Orientation = Orientation.Horizontal;
            llInner.WeightSum = 2;
            ll.AddView(llInner);

            TextView tv = new TextView(this);
            if (index == 0)
            {
                tv.Text = "Permissions";
            }
            tv.Id = index + 100;
            tv.SetTextColor(global::Android.Graphics.Color.ParseColor("#FFFFFF"));
            tv.SetTextAppearance(this, global::Android.Resource.Style.TextAppearanceMedium);
            lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WrapContent);
            lp.Gravity = GravityFlags.Right;
            lp.Weight = 1;
            llInner.AddView(tv);

            tv = new TextView(this);
            tv.Text = _controller.Permissions[index];
            tv.Id = index + 120;
            tv.SetTextColor(global::Android.Graphics.Color.ParseColor("#FFFFFF"));
            tv.SetTextAppearance(this, global::Android.Resource.Style.TextAppearanceMedium);
            lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WrapContent);
            lp.Weight = 1;
            llInner.AddView(tv);
        }

Everything gets added, but there are problems.

1> The text in the textviews is black, not white.
2> Everything is left-justified. The first TextView should be right justified and the second one should be left justified, in each row. Instead the first TextView is left justified and the second TextView is bumped up right next to it, left-justified.

Posts

  • PeterDavisPeterDavis USMember ✭✭✭
    edited August 2014

    This just doesn't make sense to me: SetTextColor() does not seem to work. I've tried using constants:

    tv.SetTextColor(global::Android.Graphics.Color.LightGray)
    

    But that didn't work.

    What's odd is, a lot of the methods are working. SetBackgroundColor() works, SetTextAppearance() works. In fact:

     tv.SetTextColor(global::Android.Graphics.Color.LightGray);
     tv.SetBackgroundColor(global::Android.Graphics.Color.Blue);
    

    Sets the background color to blue but leaves the text color as black.

    As for the layout, I got that mostly fixed (it's still not right-justifying the first textviews). I was neglecting to add the LayoutParameters to their appropriate views. But I still can't get the color issue figured out.

    Anyone?

  • PeterDavisPeterDavis USMember ✭✭✭

    Okay, I've now fixed everything except the textcolor. If someone could please help me get around the SetTextColor() not working, that would be fantastic.

  • PhilRyanPhilRyan AUMember ✭✭

    Wouldn't you need to set the Text color after setting the Appearance?

    And alternatively, create your own text Appearance xml that has its own textColor? Like

    <style name="MyStyle" parent="@android:style/Widget.TextView"> <item name="android:textStyle">bold</item> <item name="android:textColor">blue</item> </style>

  • PeterDavisPeterDavis USMember ✭✭✭

    @PhilRyan‌ Yes, that's exactly what I'd need to do. I figured it out just before I went home last night. Well, either way. I ended up moving the set text color after setting the appearance and that fixed it. I didn't realize that setting the appearance set the color. Not sure why. Seems stunningly obvious in retrospect...

  • hey @PeterDavis what is _controller.Permissions you are using for setting ID's?

  • PeterDavisPeterDavis USMember ✭✭✭

    @ChristopherDrosos It's not setting the IDs, it's setting the text and it's merely an array of strings.

Sign In or Register to comment.