Lalit Jadiya
Lalit Jadiya

Reputation: 213

Text in TextView gravity not working

I written the following function to create text view.But some the text not visible completely so i get gravity to center_horizontal|center|vertical but it is not working.I the following images IN DATE & OUT DATE not visible completely.So i want to set all text to center vertical and center horizontal

protected TextView createDefaultTabView(Context context) {
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        //textView.setPadding(0,0,0,25);

        textView.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    [![enter image description here][1]][1]    TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
                outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
        textView.setAllCaps(true);

        int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
        textView.setPadding(padding, padding, padding, padding + 10);

        return textView;
    }

enter image description here

Upvotes: 3

Views: 1103

Answers (1)

Cruceo
Cruceo

Reputation: 6824

Your TextView's height is set to WRAP_CONTENT, so it's internal Gravity has no effect on it's vertical positioning (because there's no extra bounds to position itself in).

If you want to get it vertically aligned, you need to either set the TextView's PARENT gravity to CENTER_VERTICAL, or set the TextView's height to MATCH_PARENT.

Either of those will give you the effect you seek.

Upvotes: 2

Related Questions