Set text of a child view. - Android User Interface

Android examples for User Interface:View Child

Description

Set text of a child view.

Demo Code


//package com.java2s;
import android.app.Activity;
import android.view.View;

import android.widget.TextView;

public class Main {
    /**//from  w  w w  .j a v a  2s. c  o  m
     * Set text of a child view.
     *
     * @param v Parent view.
     * @param childId Child view id whose text should be set.
     * @param text
     * @return TextView the child whose text was set.
     */
    public static TextView setText(View v, int childId, CharSequence text) {
        TextView tv = view(v, childId);
        tv.setText(text);
        return tv;
    }

    /**
     * Set text of a child view.
     *
     * @param a Parent activity.
     * @param childId Child view id whose text should be set.
     * @param text
     * @return TextView the child whose text was set.
     */
    public static TextView setText(Activity a, int childId,
            CharSequence text) {
        TextView tv = view(a, childId);
        tv.setText(text);
        return tv;
    }

    /**
     * Find a child view of any type.
     *
     * @param v Parent view
     * @param childId Child view id.
     * @return View
     */
    public static <V extends View> V view(View v, int childId) {
        return (V) v.findViewById(childId);
    }

    /**
     * Find a child view of any type.
     *
     * @param a Parent activity.
     * @param childId Child view id.
     * @return View
     */
    public static <V extends View> V view(Activity a, int childId) {
        return (V) a.findViewById(childId);
    }
}

Related Tutorials