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:com.bryan.example.widget.slidingtablayout.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab
 * view is not set via {@link #setCustomTabView(int, int)}.
 *///  w  w  w.ja v a 2  s.c  om
@SuppressLint("NewApi")
protected TextView createDefaultTabView(Context context) {
    /**
     * ?Holo??
     */
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    // textView.setTypeface(Typeface.DEFAULT_BOLD);//
    textView.setTypeface(Typeface.DEFAULT);

    // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    // {
    // // If we're running on Honeycomb or newer, then we can use the
    // // Theme's
    // // selectableItemBackground to ensure that the View has a pressed
    // // state
    // TypedValue outValue = new TypedValue();
    // getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
    // outValue, true);
    // textView.setBackgroundResource(outValue.resourceId);
    // }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the
        // Action Bar tab style
        textView.setAllCaps(true);
    }

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

    return textView;
}

From source file:com.development.jaba.view.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//  w  w w.java2s  .  c  o m
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    // By default use the unselected title color.
    textView.setTextColor(mTitleColor);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

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

    return textView;
}

From source file:com.leo.tablayout.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//from w w  w. j a va2  s  .co  m
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    //        TypedValue outValue = new TypedValue();
    //        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
    //                outValue, true);
    //        textView.setBackgroundResource(outValue.resourceId);
    //        textView.setAllCaps(true);

    if (colorStateList != null) {
        textView.setTextColor(colorStateList);
    }
    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

From source file:com.aazamnawlakha.eac_android.view.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//  www  .  j  a  v a2 s .  co  m
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(context.getResources().getDimension(R.dimen.dimen_9_sp));
    textView.setTypeface(Typeface.DEFAULT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        // textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(40, 20, 50, 20);

    return textView;
}

From source file:com.cookbeans.boredapp.ui.view.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*from  w  w  w. j  a  va  2 s  .c  o  m*/
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextColor(Color.WHITE);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    //        textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setTypeface(Typeface.DEFAULT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

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

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,
            ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
    textView.setLayoutParams(layoutParams);

    return textView;
}

From source file:com.example.alex.helloworld.SlidingTabLayout.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//from ww w  .  j ava2 s.c  om
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setTextColor(getResources().getColor(R.color.weiss));
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    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);

    return textView;
}

From source file:com.consumer.widget.SlidingTabLayoutSpend.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*from   w  w w  .  j a  va 2  s .  c  om*/
 */
@SuppressLint("NewApi")
protected TextView createDefaultTabView(Context context) {

    Resources res = context.getResources();

    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    //textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    //textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTextSize(TAB_VIEW_TEXT_SIZE_SP);

    //textView.setTypeface(Typeface.DEFAULT_BOLD);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT, 1.0f);
    textView.setLayoutParams(param);

    // int unSelColorBg= R.color.TabPager;
    int unSelColorBg = R.color.White;

    textView.setTextColor(context.getResources().getColor(unSelColorBg));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    //textView.setPadding(padding,padding, padding, padding);
    textView.setPadding(28, 15, 28, 15);

    return textView;
}

From source file:com.example.android.MainActivity.java

/**Display JSON data in table format on the user interface of android app
 * by clicking on the button 'Start'*/
@Override/* ww  w .  j a v a 2s.  com*/
protected void onCreate(Bundle savedInstanceState) {
    /**
     * Declares TextView, Button and Tablelayout to retrieve the widgets 
     * from User Interface. Insert the TableRow into Table and set the 
     * gravity, font size  and id of table rows and columns.
     * 
     * Due to great amount of JSON data, 'for' loop method is used to insert 
     * the new rows and columns in the table. In each loop, each of rows and 
     * columns are set to has its own unique id. This purpose of doing this 
     * is to allows the user to read and write the text of specific rows and 
     * columns easily.
     */
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    progress = new ProgressDialog(this);
    StartDisplay = (Button) findViewById(R.id.btnDisplay);
    profile = (TableLayout) findViewById(R.id.tableLayout1);
    profile.setStretchAllColumns(true);
    profile.bringToFront();

    for (int i = 1; i < 11; i++) {
        TableRow tr = new TableRow(this);
        TextView c1 = new TextView(this);
        TextView c2 = new TextView(this);
        c1.setId(i * 10 + 1);
        c1.setTextSize(12);
        c1.setGravity(Gravity.CENTER);
        c2.setId(i * 10 + 2);
        c2.setTextSize(12);
        c2.setGravity(Gravity.CENTER);
        tr.addView(c1);
        tr.addView(c2);
        tr.setGravity(Gravity.CENTER_HORIZONTAL);
        profile.addView(tr);
    }

    /**
    * onClick: Executes the DownloadWebPageTask once OnClick event occurs. 
    * When user click on the "Start" button, 
    * 1)the JSON data will be read from URL 
    * 2)Progress bar will be shown till all data is read and displayed in
    * table form. Once it reaches 100%, it will be dismissed. 
    * 
    * Progress Bar: The message of the progress bar is obtained from strings.xml.
    * New thread is created to handle the action of the progress bar. 
    */
    StartDisplay.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            final String TAG = "MyActivity";
            progress.setMessage(getResources().getString(R.string.ProgressBar_message));
            progress.setCancelable(true);
            progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progress.setProgress(0);
            progress.setMax(100);
            progress.show();
            new Thread(new Runnable() {

                public void run() {
                    while (ProgressBarStatus < 100) {

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            Log.e(TAG, Log.getStackTraceString(e));
                        }
                        progressBarbHandler.post(new Runnable() {
                            public void run() {
                                progress.setProgress(ProgressBarStatus);
                            }
                        });
                    }

                    if (ProgressBarStatus >= 100) {

                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            Log.e(TAG, Log.getStackTraceString(e));
                        }

                        progress.dismiss();
                    }
                }
            }).start();
            DownloadWebPageTask task = new DownloadWebPageTask();
            task.execute("http://private-ae335-pgserverapi.apiary.io/user/profile/234");
            StartDisplay.setClickable(false);
        }
    });

}

From source file:com.android.firewall.FirewallListPage.java

private TextView getTabView(int width, int height, String title) {
    TextView view = new TextView(this);
    view.setMinimumWidth(width);/*from   w  w w  .jav a2 s  . co m*/
    view.setMinimumHeight(height);
    view.setGravity(Gravity.CENTER);
    view.setBackgroundResource(R.drawable.tab_indicator_holo);
    view.setText(title);
    return view;
}

From source file:com.itime.team.itime.fragments.InputDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    // Get the layout inflater
    final LayoutInflater inflater = getActivity().getLayoutInflater();

    TextView title = new TextView(getActivity());
    // You Can Customise your Title here
    String titleText = getArguments().getString(INPUT_DIALOG_TITLE);
    title.setText(titleText);/*from  www . j  a  v  a 2s .  c o  m*/
    title.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    title.setPadding(10, 10, 10, 10);
    title.setGravity(Gravity.CENTER);
    title.setTextColor(Color.WHITE);
    title.setTextSize(20);

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.fragment_input_dialog, null)).setCustomTitle(title)
            // Add action buttons
            .setPositiveButton(R.string.Save, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    TextView inputArea = (TextView) getDialog().findViewById(R.id.input_area);
                    mListener.onDialogPositiveClick(inputArea.getText().toString());

                }
            }).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onDialogNegativeClick(InputDialogFragment.this);
                }
            });
    return builder.create();
}