Obtain the text of the given component. - Android User Interface

Android examples for User Interface:TextView

Description

Obtain the text of the given component.

Demo Code


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

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

public class Main {
    /**//from  w  w w  .  j av  a  2  s .  co  m
     * Obtain the text of the given component.
     *
     * @param viewId The id of the component.
     * @return The contained text, null otherwise.
     */
    public static String getText(View parent, int viewId) {
        String text = null;
        View view = parent.findViewById(viewId);

        if (view instanceof TextView)
            text = ((TextView) view).getText().toString();
        if (view instanceof EditText)
            text = ((EditText) view).getText().toString();

        return text;
    }

    public static String getText(Activity activity, int viewId) {
        return getText(activity.getWindow().getDecorView().getRootView(),
                viewId);
    }
}

Related Tutorials