0

I have a custom view , I have put some TextView inside that and i have to change the layout property of each child view differently. I won't able to change textview property by xml,

Here is my xml:

<com.squareup.timessquare.CalendarRowView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/calendar_day_headers_paddingbottom">

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
    </com.squareup.timessquare.CalendarRowView>

Above is xml file in which there is 8 textview is defined I want each textview have different width,

and following is the java code for that...

public static MonthView create(ViewGroup parent, LayoutInflater inflater,
      DateFormat weekdayNameFormat, Listener listener, Calendar today) 
  {
    final MonthView view = (MonthView) inflater.inflate(R.layout.month, parent, false);
    final int originalDayOfWeek = today.get(Calendar.DAY_OF_WEEK);

    int firstDayOfWeek = today.getFirstDayOfWeek();
    final CalendarRowView headerRow = (CalendarRowView) view.grid.getChildAt(0);
    for (int offset = 0; offset < 7; offset++) 
    {
      today.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + offset);
      final TextView **textView** = (TextView) headerRow.getChildAt(offset);
      textView.setText(weekdayNameFormat.format(today.getTime()));
    }
    today.set(Calendar.DAY_OF_WEEK, originalDayOfWeek);
    view.listener = listener;
    return view;
  }
Ranco
  • 893
  • 2
  • 13
  • 43
DjP
  • 4,537
  • 2
  • 25
  • 34

1 Answers1

0

Using the Inflater with XML limits your layouts to what was in the XML.

To dynamically change a Layout property you must create the layout dynamically as well, For example:

CalendarRowView calenderViewGroup = new CalendarRowView(thisContext);
int widthNeeded = 300;  // in pixels
TextView someTextView = new TextView(thisContext);
someTextView.setText("this is my text");
someTextView.setWidth(widthNeeded );
someTextView.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));

calenderViewGroup.addView(someTextView);

See Public Methods here http://developer.android.com/reference/android/widget/TextView.html for more available methods.

Jim
  • 36
  • 3