Example usage for android.widget TextView setGravity

List of usage examples for android.widget TextView setGravity

Introduction

In this page you can find the example usage for android.widget TextView setGravity.

Prototype

public void setGravity(int gravity) 

Source Link

Document

Sets the horizontal alignment of the text and the vertical gravity that will be used when there is extra space in the TextView beyond what is required for the text itself.

Usage

From source file:Main.java

static void preDraw(TextView view, Canvas canvas) {
    Drawable[] drawables = view.getCompoundDrawables();
    if (drawables[0] != null) {
        view.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
        onCenterDraw(view, canvas, drawables[0], Gravity.START);
    } else if (drawables[1] != null) {
        view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        onCenterDraw(view, canvas, drawables[1], Gravity.TOP);
    } else if (drawables[2] != null) {
        view.setGravity(Gravity.CENTER_VERTICAL | Gravity.END);
        onCenterDraw(view, canvas, drawables[2], Gravity.END);
    } else if (drawables[3] != null) {
        view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        onCenterDraw(view, canvas, drawables[3], Gravity.BOTTOM);
    }/*from   w w  w .  j  a v a2  s  .c  o  m*/
}

From source file:Main.java

public static void showOkDialog(String title, String msg, Activity act) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(act);
    if (title != null) {

        TextView dialogTitle = new TextView(act);
        dialogTitle.setText(title);//w  w  w . j a v  a 2  s  .  co m
        dialogTitle.setPadding(10, 10, 10, 10);
        dialogTitle.setGravity(Gravity.CENTER);
        dialogTitle.setTextColor(Color.WHITE);
        dialogTitle.setTextSize(20);
        dialog.setCustomTitle(dialogTitle);

    }
    if (msg != null) {
        dialog.setMessage(msg);
    }
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });
    AlertDialog dlg = dialog.show();
    TextView messageText = (TextView) dlg.findViewById(android.R.id.message);
    messageText.setGravity(Gravity.CENTER);

}

From source file:Main.java

public static void preDraw(TextView view, Canvas canvas) {
    Drawable[] drawables = view.getCompoundDrawables();
    if (drawables != null) {
        if (drawables[0] != null) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            onCenterDraw(view, canvas, drawables[0], Gravity.LEFT);
        } else if (drawables[1] != null) {
            view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
            onCenterDraw(view, canvas, drawables[1], Gravity.TOP);
        } else if (drawables[2] != null) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
            onCenterDraw(view, canvas, drawables[2], Gravity.RIGHT);
        } else if (drawables[3] != null) {
            view.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            onCenterDraw(view, canvas, drawables[3], Gravity.BOTTOM);
        }//from  w  w  w  . j  av  a 2 s.  c  o  m
    }
}

From source file:cn.foxnickel.listentome.view.ViewUtil.java

public static View getWordsView(Context context, String word, @ColorRes int color, boolean isTextSelectable) {
    TextView tv = new TextView(context);
    tv.setTextColor(ContextCompat.getColor(context, color));
    tv.setPadding(0, 6, 0, 6);/*w w w. j a  v  a 2s .co m*/
    tv.setTextSize(16);
    tv.setTextIsSelectable(isTextSelectable);
    tv.setGravity(Gravity.LEFT);
    tv.setText(word);
    return tv;
}

From source file:Main.java

protected static Bitmap creatCodeBitmap(String contents, int width, int height, Context context) {
    TextView tv = new TextView(context);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(layoutParams);// w  ww. j  ava  2s  .  c om
    tv.setText(contents);
    tv.setHeight(height);
    tv.setGravity(Gravity.CENTER_HORIZONTAL);
    tv.setWidth(width);
    tv.setDrawingCacheEnabled(true);
    tv.setTextColor(Color.BLACK);
    tv.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());

    tv.buildDrawingCache();
    Bitmap bitmapCode = tv.getDrawingCache();
    return bitmapCode;
}

From source file:com.jamesgiang.aussnowcam.Utils.java

public static void About(Context c) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(c);
    dialogBuilder.setTitle(R.string.app_name);
    dialogBuilder.setIcon(R.drawable.icon);
    TextView textView = new TextView(c);
    SpannableString s = new SpannableString(c.getString(R.string.about_info));
    Linkify.addLinks(s, Linkify.WEB_URLS);
    textView.setText(s);/*  ww  w  .ja  v a  2  s.  c o m*/
    textView.setGravity(Gravity.CENTER);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    dialogBuilder.setView(textView);
    dialogBuilder.show();
}

From source file:com.bhb27.isu.tools.Tools.java

public static void DoAToast(String message, Context context) {
    Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
    TextView view = (TextView) toast.getView().findViewById(android.R.id.message);
    if (view != null)
        view.setGravity(Gravity.CENTER);
    toast.show();/*w ww  . jav a 2  s  .c o m*/
}

From source file:com.afeng.xf.widget.snackbarlight.Light.java

/**
 * Make a customized {@link Snackbar} to display a message without any action.
 *
 * @param view The view to find a parent from.
 * @param text The message to display. Formatted text is supported.
 * @param textIcon The left icon of the message.
 * @param backgroundColor The background color of the Snackbar. It should be a resolved color.
 * @param textColor The color of message text.
 * @param duration How long to show the message.
 *                 Either {@link Light#LENGTH_SHORT} or {@link Light#LENGTH_LONG}.
 *
 * @return The customized Snackbar that will be displayed.
 *///ww w  . j  av  a 2 s .  c  o  m
public static Snackbar make(@NonNull View view, @NonNull CharSequence text, Drawable textIcon,
        @ColorInt int backgroundColor, @ColorInt int textColor, int duration) {
    // Get a usual Snackbar
    Snackbar snackbar = Snackbar.make(view, text, duration);

    // Get the view of it.
    View mView = snackbar.getView();
    // Change the background color.
    mView.setBackgroundColor(backgroundColor);

    // Get the TextView of message.
    TextView textView = (TextView) mView.findViewById(android.support.design.R.id.snackbar_text);
    // Set the left icon of message.
    textView.setCompoundDrawablesWithIntrinsicBounds(textIcon, null, null, null);
    // Set the padding between message and icon.
    textView.setCompoundDrawablePadding(16);
    // To make icon and message aligned.
    textView.setGravity(Gravity.CENTER);
    // Change color of message text.
    textView.setTextColor(textColor);

    return snackbar;
}

From source file:com.fjoglar.etsitnoticias.utils.UiUtils.java

/**
 * Configure the TextViews that will show the attachments of the new.
 *
 * @param textView      TextView to be configured.
 * @param title         Text shown in TextView
 * @param downloadLink  Link attached to TextView.
 * @param fileType      Type of file of the attachment.
 * @param context       The context of activity.
 *//* w w  w .j  av  a  2s  .c o m*/
public static void configureTextView(TextView textView, String title, final String downloadLink,
        Attachment.FILE_TYPE fileType, final Context context) {

    final int TEXT_VIEW_MIN_HEIGHT = 40;
    final int TEXT_VIEW_MARGIN_TOP = 4;

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params.setMargins(0, convertDpToPx(TEXT_VIEW_MARGIN_TOP, context), 0, 0);
    textView.setLayoutParams(params);

    textView.setText(title);
    textView.setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));
    textView.setMinHeight(convertDpToPx(TEXT_VIEW_MIN_HEIGHT, context));
    textView.setGravity(Gravity.CENTER_VERTICAL);

    switch (fileType) {
    case FILE:
        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_file, 0, 0, 0);
        break;
    case IMAGE:
        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_photo, 0, 0, 0);
        break;
    case FOLDER:
        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_folder, 0, 0, 0);
        break;
    default:
        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_link, 0, 0, 0);
        break;
    }

    textView.setCompoundDrawablePadding(convertDpToPx(4, context));

    TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
    textView.setBackgroundResource(typedValue.resourceId);

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Navigator.getInstance().openUrl(context, downloadLink);
        }
    });
}

From source file:com.benli.testdagger2.ui.HomeFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    TextView tv = new TextView(getActivity());
    tv.setGravity(CENTER);
    tv.setText("Hello, World");
    return tv;/*from   w  w w  .j a  v a 2s  . co m*/
}