Obtain the text of the given component by view id - Android android.view

Android examples for android.view:View

Description

Obtain the text of the given component by view id

Demo Code

import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Main {

  /**//w ww  .  j ava2s . c  o  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