Append given text String to the provided view (one of TextView or EditText). - Android User Interface

Android examples for User Interface:TextView

Description

Append given text String to the provided view (one of TextView or EditText).

Demo Code


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

import android.util.Log;

import android.view.View;

import android.widget.TextView;

public class Main {
    /**/*from w w  w.  ja  v  a 2 s .  co  m*/
     * Append given text String to the provided view (one of TextView or EditText).
     *
     * @param view     View to update
     * @param toAppend String text
     */
    public static void appendText(TextView view, String toAppend) {
        String currentText = getText(view);
        view.setText(currentText + toAppend);
    }

    /**
     * Get text as String from EditView.
     * <b>Note:</b> returns "" for null EditText, not a NullPointerException
     *
     * @param view EditView to get text from
     * @return the text
     */
    public static String getText(TextView view) {
        String text = "";
        if (view != null) {
            text = view.getText().toString();
        } else {
            Log.e("PercolateAndroidUtils",
                    "Null view given to getText().  \"\" will be returned.");
        }
        return text;
    }

    /**
     * Get text as String from EditView.
     * <b>Note:</b> returns "" for null EditText, not a NullPointerException
     *
     * @param context The current Context or Activity that this method is called from
     * @param id      Id for the TextView/EditView to get text from
     * @return the text
     */
    public static String getText(Activity context, int id) {
        TextView view = findViewById(context, id);

        String text = "";
        if (view != null) {
            text = view.getText().toString();
        } else {
            Log.e("PercolateAndroidUtils",
                    "Null view given to getText().  \"\" will be returned.");
        }
        return text;
    }

    /**
     * Method used to set text for a TextView
     *
     * @param context The current Context or Activity that this method is called from
     * @param field   R.id.xxxx value for the text field.
     * @param text    Text to place in the text field.
     */
    public static void setText(Activity context, int field, String text) {
        View view = context.findViewById(field);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        } else {
            Log.e("PercolateAndroidUtils",
                    "ViewUtils.setText() given a field that is not a TextView");
        }
    }

    /**
     * Method used to set text for a TextView
     *
     * @param parentView The View used to call findViewId() on
     * @param field      R.id.xxxx value for the text field.
     * @param text       Text to place in the text field.
     */
    public static void setText(View parentView, int field, String text) {
        View view = parentView.findViewById(field);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        } else {
            Log.e("PercolateAndroidUtils",
                    "ViewUtils.setText() given a field that is not a TextView");
        }
    }

    /**
     * Utility method to make getting a View via findViewById() more safe & simple.
     * <p/>
     * - Casts view to appropriate type based on expected return value
     * - Handles & logs invalid casts
     *
     * @param context The current Context or Activity that this method is called from
     * @param id      R.id value for view
     * @return View object, cast to appropriate type based on expected return value.
     * @throws ClassCastException if cast to the expected type breaks.
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(Activity context, int id) {
        T view = null;
        View genericView = context.findViewById(id);
        try {
            view = (T) (genericView);
        } catch (Exception ex) {
            String message = "Can't cast view (" + id + ") to a "
                    + view.getClass() + ".  Is actually a "
                    + genericView.getClass() + ".";
            Log.e("PercolateAndroidUtils", message);
            throw new ClassCastException(message);
        }

        return view;
    }

    /**
     * Utility method to make getting a View via findViewById() more safe & simple.
     * <p/>
     * - Casts view to appropriate type based on expected return value
     * - Handles & logs invalid casts
     *
     * @param parentView Parent View containing the view we are trying to get
     * @param id         R.id value for view
     * @return View object, cast to appropriate type based on expected return value.
     * @throws ClassCastException if cast to the expected type breaks.
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(View parentView, int id) {
        T view = null;
        View genericView = parentView.findViewById(id);
        try {
            view = (T) (genericView);
        } catch (Exception ex) {
            String message = "Can't cast view (" + id + ") to a "
                    + view.getClass() + ".  Is actually a "
                    + genericView.getClass() + ".";
            Log.e("PercolateAndroidUtils", message);
            throw new ClassCastException(message);
        }

        return view;
    }
}

Related Tutorials