how to set TextView size Programmatically at the runtime

MamadexMamadex USMember
edited November 2014 in Xamarin.Android

Following codes don't work:

//#1:
TextView tv = FindViewById<TextView>(Resource.Id.myTV);
tv.LayoutParameters.Width=10; //there's no Size property in ViewGroup.LayoutParams class which is type of LayoutParameters.

//#2:
LinearLayout l = FindViewById<LinearLayout>(Resource.Layout.Main); 
l.LayoutParameters.Width=10; 

//#3:
ViewGroup.LayoutParams lp = tv.LayoutParameters; //or: ViewGroup.LayoutParams lp = l.LayoutParameters;
 lp.Width=10;

This is Main.axml file if its content of mine helps.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#abcdef">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView
            android:textColor="#FF267F00"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="words-here"
            android:id="@+id/myTV"
            android:layout_weight="1"
            android:textSize="33dp" />
    </LinearLayout>
  </LinearLayout>

What should I do then?

Answers

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    I have done something similar like this:

        var linearLayout = new LinearLayout(Activity);
                linearLayout.LayoutParameters = propertyListLinearLayout.LayoutParameters;
                linearLayout.Orientation = Orientation.Horizontal;
                linearLayout.SetPadding(0, 4, 0, 4);
    
                var valueTextView = new TextView(Activity);
                valueTextView.LayoutParameters = new LinearLayout.LayoutParams(-2, -2, 1);
                valueTextView.Gravity = GravityFlags.Right;
                valueTextView.SetTypeface(null, Android.Graphics.TypefaceStyle.Bold);
                valueTextView.Text = "MyValueText";
    
                linearLayout.AddView(valueTextView);
    

    The trick that worked for me was LinearLayout.LayoutParams.

  • MamadexMamadex USMember
    edited December 2014

    Could you use this:

    TextView tv = FindViewById<TextView>(Resource.Id.myTV);

    then reference it for changing params?

Sign In or Register to comment.