set Text to TextView - Android User Interface

Android examples for User Interface:TextView Value

Description

set Text to TextView

Demo Code


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

import android.support.annotation.IdRes;

import android.support.annotation.StringRes;
import android.view.View;

import android.widget.TextView;

public class Main {
    public static void setText(CharSequence text, View parent,
            @IdRes int viewId) {
        TextView view = findView(parent, viewId);
        view.setText(text);/*from  w w  w.  j  av  a  2 s  . c  o  m*/
    }

    public static void setText(CharSequence text, Activity activity,
            @IdRes int viewId) {
        TextView view = findView(activity, viewId);
        view.setText(text);
    }

    public static void setText(@StringRes int stringRes, Activity activity,
            @IdRes int viewId) {
        TextView view = findView(activity, viewId);
        view.setText(stringRes);
    }

    public static <T extends View> T findView(View parent, @IdRes int viewId) {
        return (T) parent.findViewById(viewId);
    }

    public static <T extends View> T findView(Activity activity,
            @IdRes int viewId) {
        return (T) activity.findViewById(viewId);
    }
}

Related Tutorials