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.ibm.hellotodo.MainActivity.java

/**
 * Launches a dialog for adding a new TodoItem. Called when plus button is tapped.
 *
 * @param view The plus button that is tapped.
 *///from w w  w.java  2s.  co  m
public void addTodo(View view) {

    final Dialog addDialog = new Dialog(this);

    addDialog.setContentView(R.layout.add_edit_dialog);
    addDialog.setTitle("Add Todo");
    TextView textView = (TextView) addDialog.findViewById(android.R.id.title);
    if (textView != null) {
        textView.setGravity(Gravity.CENTER);
    }

    addDialog.setCancelable(true);
    Button add = (Button) addDialog.findViewById(R.id.Add);
    addDialog.show();

    // When done is pressed, send POST request to create TodoItem on Bluemix
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText itemToAdd = (EditText) addDialog.findViewById(R.id.todo);
            final String name = itemToAdd.getText().toString();
            // If text was added, continue with normal operations
            if (!name.isEmpty()) {

                // Create JSON for new TodoItem, id should be 0 for new items
                String json = "{\"text\":\"" + name + "\",\"isDone\":false,\"id\":0}";

                // Create POST request with IBM Mobile First SDK and set HTTP headers so Bluemix knows what to expect in the request
                Request request = new Request(client.getBluemixAppRoute() + "/api/Items", Request.POST);

                HashMap headers = new HashMap();
                List<String> cType = new ArrayList<>();
                cType.add("application/json");
                List<String> accept = new ArrayList<>();
                accept.add("Application/json");

                headers.put("Content-Type", cType);
                headers.put("Accept", accept);

                request.setHeaders(headers);

                request.send(getApplicationContext(), json, new ResponseListener() {
                    // On success, update local list with new TodoItem
                    @Override
                    public void onSuccess(Response response) {
                        Log.i(TAG, "Item created successfully");

                        loadList();
                    }

                    // On failure, log errors
                    @Override
                    public void onFailure(Response response, Throwable t, JSONObject extendedInfo) {
                        String errorMessage = "";

                        if (response != null) {
                            errorMessage += response.toString() + "\n";
                        }

                        if (t != null) {
                            StringWriter sw = new StringWriter();
                            PrintWriter pw = new PrintWriter(sw);
                            t.printStackTrace(pw);
                            errorMessage += "THROWN" + sw.toString() + "\n";
                        }

                        if (extendedInfo != null) {
                            errorMessage += "EXTENDED_INFO" + extendedInfo.toString() + "\n";
                        }

                        if (errorMessage.isEmpty())
                            errorMessage = "Request Failed With Unknown Error.";

                        Log.e(TAG, "addTodo failed with error: " + errorMessage);
                    }
                });
            }

            // Kill dialog when finished, or if no text was added
            addDialog.dismiss();
        }
    });
}

From source file:cn.hollo.www.custom_view.PagerSlidingTabStrip.java

private void addTextTab(final int position, String title) {
    TextView tab = new TextView(getContext());
    tab.setText(title);//  w  ww  .  j  a v a 2s .c  o m
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();

    if (tabTextColor != null)
        tab.setTextColor(tabTextColor);
    else
        tab.setTextColor(defaultTabTextColor);

    tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
    addTab(position, tab);
}

From source file:cn.hollo.www.custom_view.PagerSlidingTabStrip.java

private void addTextTab(final int position, String title, int leftIcon) {
    TextView tab = new TextView(getContext());
    tab.setText(title);//from   ww w  . j  a va2 s.c o  m
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    if (tabTextColor != null)
        tab.setTextColor(tabTextColor);
    else
        tab.setTextColor(defaultTabTextColor);

    tab.setCompoundDrawablesWithIntrinsicBounds(leftIcon, 0, 0, 0);
    tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
    addTab(position, tab);
}

From source file:android.improving.utils.views.PagerSlidingTabStrip.java

private void addTextTab(final int position, String title) {

    TextView tab = new TextView(getContext());
    tab.setText(title);/*w w  w  .  j ava2s  .com*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    addTab(position, tab);
}

From source file:com.landenlabs.all_devtool.IconBaseFragment.java

/**
 * Show a 'StateListDrawable' information
 *
 * @param imageView//from w w w . ja va 2  s. co m
 * @param row1
 * @param row2
 * @param stateListDrawable
 * @param state
 * @param desc
 * @param stateIcons
 */
private void showStateIcon(final ImageView imageView, TableRow row1, TableRow row2,
        StateListDrawable stateListDrawable, int state, String desc, Set<Drawable> stateIcons) {

    stateListDrawable.setState(new int[] { state });
    Drawable stateD = stateListDrawable.getCurrent();
    if (stateD != null && !stateIcons.contains(stateD)) {
        stateIcons.add(stateD);
        ImageButton stateImageView = new ImageButton(imageView.getContext());
        Drawable[] drawables = new Drawable[] { stateD,
                getResources().getDrawable(R.drawable.button_border_sel) };

        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        stateImageView.setImageDrawable(layerDrawable);
        //   stateImageView.setBackgroundResource(R.drawable.button_border_sel);
        stateImageView.setPadding(10, 10, 10, 10);
        stateImageView.setMinimumHeight(8);
        stateImageView.setMinimumWidth(8);
        stateImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.setImageDrawable(((ImageView) v).getDrawable());
            }
        });

        TextView stateTextView = new TextView(imageView.getContext());
        stateTextView.setText(desc);
        stateTextView.setTextSize(12);
        stateTextView.setGravity(Gravity.CENTER);

        row1.addView(stateTextView);
        row2.addView(stateImageView);
    }
}

From source file:com.ibm.hellotodo.MainActivity.java

/**
 * Launches a dialog for updating the TodoItem name. Called when the list item is tapped.
 *
 * @param view The TodoItem that is tapped.
 *///from w  ww  .  ja  v a 2 s .com
public void editTodoName(View view) {
    // Gets position in list view of tapped item
    final Integer pos = mListView.getPositionForView(view);
    final Dialog addDialog = new Dialog(this);

    addDialog.setContentView(R.layout.add_edit_dialog);
    addDialog.setTitle("Edit Todo");
    TextView textView = (TextView) addDialog.findViewById(android.R.id.title);
    if (textView != null) {
        textView.setGravity(Gravity.CENTER);
    }
    addDialog.setCancelable(true);
    EditText et = (EditText) addDialog.findViewById(R.id.todo);

    final String name = mTodoItemList.get(pos).text;
    final boolean isDone = mTodoItemList.get(pos).isDone;
    final int id = mTodoItemList.get(pos).idNumber;
    et.setText(name);

    Button addDone = (Button) addDialog.findViewById(R.id.Add);
    addDialog.show();

    // When done is pressed, send PUT request to update TodoItem on Bluemix
    addDone.setOnClickListener(new View.OnClickListener() {
        // Save text inputted when done is tapped
        @Override
        public void onClick(View view) {
            EditText editedText = (EditText) addDialog.findViewById(R.id.todo);

            String newName = editedText.getText().toString();

            // If new text is not empty, create JSON with updated info and send PUT request
            if (!newName.isEmpty()) {
                String json = "{\"text\":\"" + newName + "\",\"isDone\":" + isDone + ",\"id\":" + id + "}";

                // Create PUT REST request using the IBM Mobile First SDK and set HTTP headers so Bluemix knows what to expect in the request
                Request request = new Request(client.getBluemixAppRoute() + "/api/Items", Request.PUT);

                HashMap headers = new HashMap();
                List<String> cType = new ArrayList<>();
                cType.add("application/json");
                List<String> accept = new ArrayList<>();
                accept.add("Application/json");

                headers.put("Content-Type", cType);
                headers.put("Accept", accept);

                request.setHeaders(headers);

                request.send(getApplicationContext(), json, new ResponseListener() {
                    // On success, update local list with updated TodoItem
                    @Override
                    public void onSuccess(Response response) {
                        Log.i(TAG, "Item updated successfully");

                        loadList();
                    }

                    // On failure, log errors
                    @Override
                    public void onFailure(Response response, Throwable t, JSONObject extendedInfo) {
                        String errorMessage = "";

                        if (response != null) {
                            errorMessage += response.toString() + "\n";
                        }

                        if (t != null) {
                            StringWriter sw = new StringWriter();
                            PrintWriter pw = new PrintWriter(sw);
                            t.printStackTrace(pw);
                            errorMessage += "THROWN" + sw.toString() + "\n";
                        }

                        if (extendedInfo != null) {
                            errorMessage += "EXTENDED_INFO" + extendedInfo.toString() + "\n";
                        }

                        if (errorMessage.isEmpty())
                            errorMessage = "Request Failed With Unknown Error.";

                        Log.e(TAG, "editTodoName failed with error: " + errorMessage);
                    }
                });

            }
            addDialog.dismiss();
        }
    });
}

From source file:com.junshan.pub.widget.PagerSlidingTabStrip.java

private void addTextTab(final int position, String title) {
    TextView tab = new TextView(getContext());
    tab.setText(title);//from   w  ww  .  jav a  2  s.  c  o m
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    int ps = DimenUtils.dp2px(mContext, paddingSize);
    tab.setPadding(ps, 0, ps, 0);
    addTab(position, tab);
}

From source file:com.baoyz.bigbang.BigBangLayout.java

public void addTextItem(String text) {
    TextView view = new TextView(getContext());
    view.setText(text);//  w w w . ja  v a  2  s  .  c o  m
    view.setBackgroundResource(R.drawable.item_background);
    view.setTextColor(ContextCompat.getColorStateList(getContext(), R.color.bigbang_item_text));
    view.setGravity(Gravity.CENTER);
    addView(view);
}

From source file:ca.mudar.parkcatcher.ui.fragments.DetailsFragment.java

private TextView getPanel(String desc, LayoutParams params, int textColor) {
    desc = desc.trim().toUpperCase();/* w  w  w  .jav  a  2s . c  o  m*/
    final TextView panelUi = new TextView(getSherlockActivity());

    panelUi.setGravity(Gravity.CENTER_HORIZONTAL);
    panelUi.setLayoutParams(params);
    panelUi.setTextColor(textColor);

    String prefix = (String) desc.subSequence(0, 2);
    prefix = prefix.trim();
    try {
        // Start with a figure: authorized duration
        Integer.valueOf(prefix.trim());
        panelUi.setBackgroundResource(R.drawable.bg_panel_parking);
    } catch (NumberFormatException e) {
        if (desc.subSequence(0, 1).equals("P")) {
            // Starts with "P " (trimmed): authorized
            panelUi.setBackgroundResource(R.drawable.bg_panel_parking);
            desc = desc.substring(1);
        } else if (desc.subSequence(0, 2).equals("\\A")) {
            panelUi.setBackgroundResource(R.drawable.bg_panel_no_stopping);
            desc = desc.substring(2);
        } else if (desc.subSequence(0, 2).equals("\\P")) {
            panelUi.setBackgroundResource(R.drawable.bg_panel_no_parking);
            desc = desc.substring(2);
        } else {
            panelUi.setBackgroundResource(R.drawable.bg_panel_no_parking);
        }
    }

    panelUi.setText(desc.trim());

    return panelUi;
}

From source file:com.it520.activity.main.wight.SmartTabLayout.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 va  2s.c o  m
 */
protected TextView createDefaultTabView(CharSequence title) {
    TextView textView = new TextView(getContext());
    textView.setGravity(Gravity.CENTER);
    textView.setText(title);
    textView.setTextColor(tabViewTextColors);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabViewTextSize);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.MATCH_PARENT));

    if (tabViewBackgroundResId != NO_ID) {
        textView.setBackgroundResource(tabViewBackgroundResId);
    } else 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(tabViewTextAllCaps);
    }

    textView.setPadding(tabViewTextHorizontalPadding, 0, tabViewTextHorizontalPadding, 0);

    if (tabViewTextMinWidth > 0) {
        textView.setMinWidth(tabViewTextMinWidth);
    }

    return textView;
}