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.example.xdai.demoapplication.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
}

From source file:com.alibaba.akita.samples.TestFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
        mContent = savedInstanceState.getString(KEY_CONTENT);
    }/* w  w w  . j av a  2s  .  c o  m*/

    TextView text = new TextView(getActivity());
    text.setGravity(Gravity.CENTER);
    text.setText(mContent);
    text.setTextSize(20 * getResources().getDisplayMetrics().density);
    text.setPadding(20, 20, 20, 20);

    LinearLayout layout = new LinearLayout(getActivity());
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    layout.setGravity(Gravity.CENTER);
    layout.addView(text);

    return layout;
}

From source file:com.manning.androidhacks.hack005.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);// w  w w .  ja va 2  s  .c om

    mTextSwitcher = (TextSwitcher) findViewById(R.id.your_textview);

    mTextSwitcher.setFactory(new ViewFactory() {
        @Override
        public View makeView() {
            TextView t = new TextView(MainActivity.this);
            t.setGravity(Gravity.CENTER);
            return t;
        }
    });

    mTextSwitcher.setInAnimation(this, android.R.anim.fade_in);
    mTextSwitcher.setOutAnimation(this, android.R.anim.fade_out);

    onSwitchText(null);
}

From source file:com.actionbarsherlock.sample.demos.app.ActionBarActionItemCustomView.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem item = menu.add(0, android.R.id.copy, 0, "Test");

    final int twentyDp = (int) (20 * getResources().getDisplayMetrics().density);

    TypedArray a = getTheme().obtainStyledAttributes(R.styleable.SherlockTheme);
    final int abHeight = a.getLayoutDimension(R.styleable.SherlockTheme_abHeight, LayoutParams.FILL_PARENT);
    a.recycle();//from  ww w  . j a v a  2  s.  c  om

    LinearLayout l = new LinearLayout(this);
    l.setPadding(twentyDp, 0, twentyDp, 20);
    l.setBackgroundColor(0x55FF0000);

    TextView tv = new TextView(this);
    tv.setText("HI!!");
    tv.setGravity(Gravity.CENTER);
    tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, abHeight));
    l.addView(tv);

    l.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ActionBarActionItemCustomView.this, "Got custom action item click!",
                    Toast.LENGTH_SHORT).show();
        }
    });

    item.setActionView(l);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    return super.onCreateOptionsMenu(menu);
}

From source file:com.amazonaws.demo.messageboard.MessageQueueAdapter.java

public View getView(int pos, View convertView, ViewGroup parent) {
    TextView messageText = new TextView(parent.getContext());
    messageText.setText(this.getMessageText(pos));
    messageText.setGravity(Gravity.LEFT);
    messageText.setPadding(10, 10, 10, 10);
    messageText.setMaxWidth(200);/*w ww  .j a v a2 s.c  o  m*/
    messageText.setMinWidth(200);
    messageText.setTextSize(16);

    return messageText;
}

From source file:com.btmura.android.reddit.app.MarkdownTableFragment.java

private void populateTable(final View view, final LayoutInflater inflater) {
    final TableLayout table = (TableLayout) view.findViewById(R.id.table);
    MarkdownTableScanner.scan(getTableDataExtra(), new OnTableScanListener<TableRow>() {
        @Override/* ww w . ja v  a  2s .  c o m*/
        public TableRow onRowStart() {
            return new TableRow(getActivity());
        }

        @Override
        public void onCell(TableRow container, Cell cell) {
            int layout = cell.isHeader ? R.layout.markdown_table_cell_header : R.layout.markdown_table_cell;

            TextView tv = (TextView) inflater.inflate(layout, container, false);
            tv.setGravity(cell.gravity);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
            tv.setText(formatter.formatAll(getActivity(), cell.contents));
            container.addView(tv);

            table.setColumnShrinkable(cell.column, true);
            table.setColumnStretchable(cell.column, true);
        }

        @Override
        public void onRowEnd(TableRow row) {
            table.addView(row);
        }
    });
}

From source file:com.android.browser.GearsPermissionsDialog.java

public void setup() {
    inflate(R.layout.gears_dialog_permission, R.id.panel_content);
    setupButtons(R.string.permission_button_alwaysdeny, R.string.permission_button_allow,
            R.string.permission_button_deny);

    try {/*from   www. ja v a  2 s . c o m*/
        JSONObject json = new JSONObject(mDialogArguments);

        if (json.has("dialogType")) {
            mDialogType = json.getString("dialogType");
            setupDialog();
        }

        if (!json.has("customName")) {
            setLabel(json, "origin", R.id.origin_title);
            View titleView = findViewById(R.id.origin_title);
            if (titleView != null) {
                TextView title = (TextView) titleView;
                title.setGravity(Gravity.CENTER);
            }
        } else {
            setLabel(json, "customName", R.id.origin_title);
            setLabel(json, "origin", R.id.origin_subtitle);
            setLabel(json, "customMessage", R.id.origin_message);
        }

        if (json.has("customIcon")) {
            String iconUrl = json.getString("customIcon");
            mChoosenIconSize = 32;
            downloadIcon(iconUrl);
        }

        View msg = findViewById(R.id.permission_dialog_message);
        if (msg != null) {
            TextView dialogMessage = (TextView) msg;
            if (mDialogType.equalsIgnoreCase(LOCAL_DATA_STRING)) {
                dialogMessage.setText(R.string.query_data_message);
            } else if (mDialogType.equalsIgnoreCase(LOCATION_DATA_STRING)) {
                dialogMessage.setText(R.string.location_message);
            }
        }

    } catch (JSONException e) {
        Log.e(TAG, "JSON exception ", e);
    }
}

From source file:cc.metapro.openct.myclass.TableFragment.java

private void addSeqViews() {
    final int DailyClasses = Integer
            .parseInt(PrefHelper.getString(getContext(), R.string.pref_daily_class_count, "12"));
    for (int i = 1; i <= DailyClasses; i++) {
        TextView textView = new TextView(getContext());
        textView.setText(i + "");
        textView.setGravity(Gravity.CENTER);
        textView.setMinHeight(Constants.CLASS_BASE_HEIGHT * Constants.CLASS_LENGTH);
        textView.setMaxHeight(Constants.CLASS_BASE_HEIGHT * Constants.CLASS_LENGTH);
        textView.setTextSize(10);/*from  w w  w .j  a v  a 2 s.  co  m*/
        mSeq.addView(textView);
    }
}

From source file:com.marshalchen.common.demoofui.pagerSlidingTabStrip.SuperAwesomeCardFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    params.gravity = Gravity.CENTER;//from  w ww .java2  s .c  om

    FrameLayout fl = new FrameLayout(getActivity());
    fl.setLayoutParams(params);

    final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,
            getResources().getDisplayMetrics());

    TextView v = new TextView(getActivity());
    params.setMargins(margin, margin, margin, margin);
    v.setLayoutParams(params);
    v.setGravity(Gravity.CENTER);
    v.setBackgroundResource(R.drawable.view_sliding_tab_background_card);
    v.setText("CARD " + (position + 1));

    fl.addView(v);
    return fl;
}

From source file:com.jia.blossom.viewpageindicator.SuperAwesomeCardFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    FrameLayout fl = new FrameLayout(getActivity());
    fl.setLayoutParams(params);//from  w ww  . j  a  v a2s .  c  o  m
    final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,
            getResources().getDisplayMetrics());
    TextView v = new TextView(getActivity());
    params.setMargins(margin, margin, margin, margin);
    v.setLayoutParams(params);
    v.setLayoutParams(params);
    v.setGravity(Gravity.CENTER);
    v.setBackgroundResource(R.drawable.background_card);
    v.setText("CARD " + (position + 1));
    fl.addView(v);
    return fl;
}