Example usage for android.widget TextView setTextAlignment

List of usage examples for android.widget TextView setTextAlignment

Introduction

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

Prototype

public void setTextAlignment(@TextAlignment int textAlignment) 

Source Link

Document

Set the text alignment.

Usage

From source file:org.mozilla.focus.utils.ViewUtils.java

/**
 * Create a snackbar with Focus branding (See #193).
 *///from  w ww.j a  va2  s  .  com
public static void showBrandedSnackbar(View view, @StringRes int resId, int delayMillis) {
    final Context context = view.getContext();
    final Snackbar snackbar = Snackbar.make(view, resId, Snackbar.LENGTH_LONG);

    final View snackbarView = snackbar.getView();
    snackbarView.setBackgroundColor(ContextCompat.getColor(context, R.color.snackbarBackground));

    final TextView snackbarTextView = (TextView) snackbarView
            .findViewById(android.support.design.R.id.snackbar_text);
    snackbarTextView.setTextColor(ContextCompat.getColor(context, R.color.snackbarTextColor));
    snackbarTextView.setGravity(Gravity.CENTER);
    snackbarTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

    view.postDelayed(new Runnable() {
        @Override
        public void run() {
            snackbar.show();
        }
    }, delayMillis);
}

From source file:com.example.sam.savemeapp.StatisticsFragment.java

/**
 * Sets up the initial legend with the contained categories shown
 * in the pie chart and bullet points with the corresponding colors.
 *///from   ww w .  ja  v a  2s. c o  m
public void setUpPiechart() {
    pieChart.getDescription().setEnabled(false);
    Legend pieL = pieChart.getLegend();
    Log.d("stats", Float.toString(pieL.getEntries().length));
    startLegend = pieL;
    LegendEntry[] legends = pieL.getEntries();
    pieL.setEnabled(false);
    mLegend.removeAllViews();
    //The default legend consist of TextViews and ImageViews which contains a bullet point.
    //The legends are contained inside of a horizontal LinearLayout which is contained in a vertical LinearLayout.
    for (int t = 0; t < legends.length - 1; t++) {
        LegendEntry x = legends[t];
        LinearLayout hLayout = new LinearLayout(getContext());
        hLayout.setLayoutParams(params);
        TextView tv = new TextView(getContext());
        tv.setText(x.label);
        tv.setTextColor(x.formColor);
        tv.setTypeface(fontLight);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                getResources().getDimension(R.dimen.statistics_main_text_size));
        tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
        LinearLayoutCompat.LayoutParams textViewParams = new LinearLayoutCompat.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textViewParams.setMarginStart((int) getResources().getDimension(R.dimen.statistics_text_margin));
        tv.setLayoutParams(textViewParams);
        ImageView iv = new ImageView(getContext());
        Drawable dot = ContextCompat.getDrawable(getContext(), R.drawable.circle_legend);
        dot.setColorFilter(x.formColor, PorterDuff.Mode.SRC);
        iv.setImageDrawable(dot);
        iv.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        hLayout.addView(iv);
        hLayout.addView(tv);
        mLegend.addView(hLayout);
    }

    designPiechart();

    pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        private int color;

        @Override
        public void onValueSelected(Entry e, Highlight h) {
            //it creates new legends with the information of the subcategories when a
            // category in the pi chart is clicked
            ArrayList<LegendEntry> list = new ArrayList<>();
            mLegend.removeAllViews();
            if (e.getData().equals(Color.parseColor("#00a0ae"))) {
                for (LegendEntry x : startLegend.getEntries()) {
                    if (x.label.equals("Food & Beverage")) {
                        color = x.formColor;
                    }
                }
                list.add(new LegendEntry("Food & Beverage: "
                        + u.categories.get("Food & Beverage").get("Food & Beverage") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Restaurant & Caf: "
                        + u.categories.get("Food & Beverage").get("Restaurant & Caf") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Supermarket: " + u.categories.get("Food & Beverage").get("Supermarket") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                mLegend.removeAllViews();
                //This category legends consist of the main category in this case "Food & Beverage" in the top of the legend
                //and the sub categories bellow. The name is also shown together with the amount spend in that category.
                for (LegendEntry x : list) {
                    TextView tv = new TextView(getContext());
                    if (x.equals(list.get(0))) {
                        tv.setTypeface(fontBlack);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_main_text_size));
                    } else {
                        tv.setTypeface(fontLight);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_text_size));
                    }
                    tv.setText(x.label);
                    tv.setTextColor(color);
                    tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                    mLegend.addView(tv);
                }
            } else if (e.getData().equals(Color.parseColor("#be3127"))) {
                for (LegendEntry x : startLegend.getEntries()) {
                    if (x.label.equals("Transportation")) {
                        color = x.formColor;
                    }
                }
                list.add(new LegendEntry("Transportation: "
                        + u.categories.get("Transportation").get("Transportation") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Public transport: "
                        + u.categories.get("Transportation").get("Public transport") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Taxi & Uber: " + u.categories.get("Transportation").get("Taxi & Uber") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Petrol: " + u.categories.get("Transportation").get("Petrol") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Maintenance: " + u.categories.get("Transportation").get("Maintenance") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                mLegend.removeAllViews();
                for (LegendEntry x : list) {
                    TextView tv = new TextView(getContext());
                    if (x.equals(list.get(0))) {
                        tv.setTypeface(fontBlack);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_main_text_size));
                    } else {
                        tv.setTypeface(fontLight);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_text_size));
                    }
                    tv.setText(x.label);
                    tv.setTextColor(color);
                    tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                    mLegend.addView(tv);
                }
            } else if (e.getData().equals(Color.parseColor("#7dc725"))) {
                for (LegendEntry x : startLegend.getEntries()) {
                    if (x.label.equals("Shopping")) {
                        color = x.formColor;
                    }
                }
                list.add(new LegendEntry(
                        "Shopping: " + u.categories.get("Shopping").get("Shopping") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Clothing: " + u.categories.get("Shopping").get("Clothing") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Electronics: " + u.categories.get("Shopping").get("Electronics") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Books: " + u.categories.get("Shopping").get("Books") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Gifts: " + u.categories.get("Shopping").get("Gifts") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Other: " + u.categories.get("Shopping").get("Other") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                mLegend.removeAllViews();
                for (LegendEntry x : list) {
                    TextView tv = new TextView(getContext());
                    if (x.equals(list.get(0))) {
                        tv.setTypeface(fontBlack);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_main_text_size));
                    } else {
                        tv.setTypeface(fontLight);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_text_size));
                    }
                    tv.setText(x.label);
                    tv.setTextColor(color);
                    tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                    mLegend.addView(tv);
                }
            } else if (e.getData().equals(Color.parseColor("#e88300"))) {
                for (LegendEntry x : startLegend.getEntries()) {
                    if (x.label.equals("Entertainment")) {
                        color = x.formColor;
                    }
                }
                list.add(new LegendEntry(
                        "Entertainment: " + u.categories.get("Entertainment").get("Entertainment") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Going out: " + u.categories.get("Entertainment").get("Going out") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Sports: " + u.categories.get("Entertainment").get("Sports") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                mLegend.removeAllViews();
                for (LegendEntry x : list) {
                    TextView tv = new TextView(getContext());
                    if (x.equals(list.get(0))) {
                        tv.setTypeface(fontBlack);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_main_text_size));
                    } else {
                        tv.setTypeface(fontLight);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_text_size));
                    }
                    tv.setText(x.label);
                    tv.setTextColor(color);
                    tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                    mLegend.addView(tv);
                }
            } else if (e.getData().equals(Color.parseColor("#dc006d"))) {
                for (LegendEntry x : startLegend.getEntries()) {
                    if (x.label.equals("Health")) {
                        color = x.formColor;
                    }
                }
                list.add(new LegendEntry("Health: " + u.categories.get("Health").get("Health") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Doctor: " + u.categories.get("Health").get("Doctor") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Pharmacy: " + u.categories.get("Health").get("Pharmacy") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                mLegend.removeAllViews();
                for (LegendEntry x : list) {
                    TextView tv = new TextView(getContext());
                    if (x.equals(list.get(0))) {
                        tv.setTypeface(fontBlack);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_main_text_size));
                    } else {
                        tv.setTypeface(fontLight);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_text_size));
                    }
                    tv.setText(x.label);
                    tv.setTextColor(color);
                    tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                    mLegend.addView(tv);
                }
            } else if (e.getData().equals(Color.parseColor("#1562a4"))) {
                for (LegendEntry x : startLegend.getEntries()) {
                    if (x.label.equals("Bills")) {
                        color = x.formColor;
                    }
                }
                list.add(new LegendEntry("Bills: " + u.categories.get("Bills").get("Bills") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Water: " + u.categories.get("Bills").get("Water") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry(
                        "Electricity: " + u.categories.get("Bills").get("Electricity") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Gas: " + u.categories.get("Bills").get("Gas") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Rent: " + u.categories.get("Bills").get("Rent") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Phone: " + u.categories.get("Bills").get("Phone") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(
                        new LegendEntry("Insurance: " + u.categories.get("Bills").get("Insurance") + u.currency,
                                Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("TV: " + u.categories.get("Bills").get("TV") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                list.add(new LegendEntry("Internet: " + u.categories.get("Bills").get("Internet") + u.currency,
                        Legend.LegendForm.SQUARE, 7f, 7f, null, color));
                mLegend.removeAllViews();
                for (LegendEntry x : list) {
                    TextView tv = new TextView(getContext());
                    if (x.equals(list.get(0))) {
                        tv.setTypeface(fontBlack);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_main_text_size));
                    } else {
                        tv.setTypeface(fontLight);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                                getResources().getDimension(R.dimen.statistics_text_size));
                    }
                    tv.setText(x.label);
                    tv.setTextColor(color);
                    tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                    mLegend.addView(tv);
                }
            }
        }

        @Override
        public void onNothingSelected() {
            //Sets up the default legend when nothing is selected
            pieChart = (PieChart) v.findViewById(R.id.piechart);
            pieChart.setUsePercentValues(true);
            LegendEntry[] legends = startLegend.getEntries();
            startLegend.setEnabled(false);
            mLegend.removeAllViews();
            for (LegendEntry x : legends) {
                LinearLayout hLayout = new LinearLayout(getContext());
                hLayout.setLayoutParams(params);
                TextView tv = new TextView(getContext());
                tv.setText(x.label);
                tv.setTextColor(x.formColor);
                tv.setTypeface(fontLight);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                        getResources().getDimension(R.dimen.statistics_main_text_size));
                tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                LinearLayoutCompat.LayoutParams textViewParams = new LinearLayoutCompat.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                textViewParams
                        .setMarginStart((int) getResources().getDimension(R.dimen.statistics_text_margin));
                tv.setLayoutParams(textViewParams);
                ImageView iv = new ImageView(getContext());
                Drawable dot = ContextCompat.getDrawable(getContext(), R.drawable.circle_legend);
                dot.setColorFilter(x.formColor, PorterDuff.Mode.SRC);
                iv.setImageDrawable(dot);
                iv.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                hLayout.addView(iv);
                hLayout.addView(tv);
                mLegend.addView(hLayout);
            }
        }
    });
}

From source file:plugin.google.maps.GoogleMaps.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override//from www  . j ava  2 s  . c o  m
public View getInfoContents(Marker marker) {
    String title = marker.getTitle();
    String snippet = marker.getSnippet();
    if ((title == null) && (snippet == null)) {
        return null;
    }

    JSONObject properties = null;
    JSONObject styles = null;
    String propertyId = "marker_property_" + marker.getId();
    PluginEntry pluginEntry = this.plugins.get("Marker");
    PluginMarker pluginMarker = (PluginMarker) pluginEntry.plugin;
    if (pluginMarker.objects.containsKey(propertyId)) {
        properties = (JSONObject) pluginMarker.objects.get(propertyId);

        if (properties.has("styles")) {
            try {
                styles = (JSONObject) properties.getJSONObject("styles");
            } catch (JSONException e) {
            }
        }
    }

    // Linear layout
    LinearLayout windowLayer = new LinearLayout(activity);
    windowLayer.setPadding(3, 3, 3, 3);
    windowLayer.setOrientation(LinearLayout.VERTICAL);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER;
    windowLayer.setLayoutParams(layoutParams);

    //----------------------------------------
    // text-align = left | center | right
    //----------------------------------------
    int gravity = Gravity.LEFT;
    int textAlignment = View.TEXT_ALIGNMENT_GRAVITY;

    if (styles != null) {
        try {
            String textAlignValue = styles.getString("text-align");

            switch (TEXT_STYLE_ALIGNMENTS.valueOf(textAlignValue)) {
            case left:
                gravity = Gravity.LEFT;
                textAlignment = View.TEXT_ALIGNMENT_GRAVITY;
                break;
            case center:
                gravity = Gravity.CENTER;
                textAlignment = View.TEXT_ALIGNMENT_CENTER;
                break;
            case right:
                gravity = Gravity.RIGHT;
                textAlignment = View.TEXT_ALIGNMENT_VIEW_END;
                break;
            }

        } catch (Exception e) {
        }
    }

    if (title != null) {
        if (title.indexOf("data:image/") > -1 && title.indexOf(";base64,") > -1) {
            String[] tmp = title.split(",");
            Bitmap image = PluginUtil.getBitmapFromBase64encodedImage(tmp[1]);
            image = PluginUtil.scaleBitmapForDevice(image);
            ImageView imageView = new ImageView(this.cordova.getActivity());
            imageView.setImageBitmap(image);
            windowLayer.addView(imageView);
        } else {
            TextView textView = new TextView(this.cordova.getActivity());
            textView.setText(title);
            textView.setSingleLine(false);

            int titleColor = Color.BLACK;
            if (styles != null && styles.has("color")) {
                try {
                    titleColor = PluginUtil.parsePluginColor(styles.getJSONArray("color"));
                } catch (JSONException e) {
                }
            }
            textView.setTextColor(titleColor);
            textView.setGravity(gravity);
            if (VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                textView.setTextAlignment(textAlignment);
            }

            //----------------------------------------
            // font-style = normal | italic
            // font-weight = normal | bold
            //----------------------------------------
            int fontStyle = Typeface.NORMAL;
            if (styles != null) {
                try {
                    if ("italic".equals(styles.getString("font-style"))) {
                        fontStyle = Typeface.ITALIC;
                    }
                } catch (JSONException e) {
                }
                try {
                    if ("bold".equals(styles.getString("font-weight"))) {
                        fontStyle = fontStyle | Typeface.BOLD;
                    }
                } catch (JSONException e) {
                }
            }
            textView.setTypeface(Typeface.DEFAULT, fontStyle);

            windowLayer.addView(textView);
        }
    }
    if (snippet != null) {
        //snippet = snippet.replaceAll("\n", "");
        TextView textView2 = new TextView(this.cordova.getActivity());
        textView2.setText(snippet);
        textView2.setTextColor(Color.GRAY);
        textView2.setTextSize((textView2.getTextSize() / 6 * 5) / density);
        textView2.setGravity(gravity);
        if (VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            textView2.setTextAlignment(textAlignment);
        }

        windowLayer.addView(textView2);
    }
    return windowLayer;
}