Example usage for android.view Gravity CENTER

List of usage examples for android.view Gravity CENTER

Introduction

In this page you can find the example usage for android.view Gravity CENTER.

Prototype

int CENTER

To view the source code for android.view Gravity CENTER.

Click Source Link

Document

Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.

Usage

From source file:com.afeng.xf.widget.snackbarlight.Light.java

/**
 * Make a customized {@link Snackbar} to display a message without any action.
 *
 * @param view The view to find a parent from.
 * @param text The message to display. Formatted text is supported.
 * @param textIcon The left icon of the message.
 * @param backgroundColor The background color of the Snackbar. It should be a resolved color.
 * @param textColor The color of message text.
 * @param duration How long to show the message.
 *                 Either {@link Light#LENGTH_SHORT} or {@link Light#LENGTH_LONG}.
 *
 * @return The customized Snackbar that will be displayed.
 *///from ww  w  .  jav a  2 s .  com
public static Snackbar make(@NonNull View view, @NonNull CharSequence text, Drawable textIcon,
        @ColorInt int backgroundColor, @ColorInt int textColor, int duration) {
    // Get a usual Snackbar
    Snackbar snackbar = Snackbar.make(view, text, duration);

    // Get the view of it.
    View mView = snackbar.getView();
    // Change the background color.
    mView.setBackgroundColor(backgroundColor);

    // Get the TextView of message.
    TextView textView = (TextView) mView.findViewById(android.support.design.R.id.snackbar_text);
    // Set the left icon of message.
    textView.setCompoundDrawablesWithIntrinsicBounds(textIcon, null, null, null);
    // Set the padding between message and icon.
    textView.setCompoundDrawablePadding(16);
    // To make icon and message aligned.
    textView.setGravity(Gravity.CENTER);
    // Change color of message text.
    textView.setTextColor(textColor);

    return snackbar;
}

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);//from  w ww . ja v  a 2 s .c  om
    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:ac.robinson.mediaphone.activity.NarrativeBrowserActivity.java

private void initialiseNarrativesView() {
    mScanningForNarratives = false;//from   w w  w  . ja  v  a2 s. com
    mNarratives = (NarrativesListView) findViewById(R.id.list_narratives);

    // for API 11 and above, buttons are in the action bar
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        LayoutInflater layoutInflater = getLayoutInflater();
        View headerRow = layoutInflater.inflate(R.layout.narratives_header, null, false);
        mNarratives.addHeaderView(headerRow, null, false); // false = not selectable
        View emptyView = layoutInflater.inflate(R.layout.narratives_empty, null, false);
        ((ViewGroup) mNarratives.getParent()).addView(emptyView);
        mNarratives.setEmptyView(emptyView); // must add separately as the header isn't shown when empty

    } else {
        // initial empty list placeholder - add manually as the < v11 version includes the header row
        TextView emptyView = new TextView(NarrativeBrowserActivity.this);
        emptyView.setGravity(Gravity.CENTER | Gravity.TOP);
        emptyView.setPadding(10,
                getResources().getDimensionPixelSize(R.dimen.narrative_list_empty_hint_top_padding), 10, 10); // temporary
        emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        emptyView.setText(getString(R.string.narrative_list_empty));
        ((ViewGroup) mNarratives.getParent()).addView(emptyView);
        mNarratives.setEmptyView(emptyView);
    }

    // originally used to fix selection highlights when using hardware button to select
    // now done by overriding isEnabled in NarrativeAdapter
    // mNarratives.setFocusable(false);
    // mNarratives.setFocusableInTouchMode(false);

    mNarrativeAdapter = new NarrativeAdapter(this, true, false);
    mNarratives.setAdapter(mNarrativeAdapter);
    getSupportLoaderManager().initLoader(R.id.loader_narratives_completed, null, this);
    mNarratives.setOnScrollListener(new ScrollManager());
    mNarratives.setOnTouchListener(new FingerTracker());
    mNarratives.setOnItemSelectedListener(new SelectionTracker());
    mNarratives.setOnItemClickListener(new NarrativeViewer());

    mPopupPosition = getLayoutInflater().inflate(R.layout.popup_position, null);
    mPopupText = (TextView) mPopupPosition.findViewById(R.id.popup_text);
}

From source file:com.poomoo.edao.activity.UploadPicsActivity.java

private void select_pics() {
    // SelectPicPopupWindow
    upload_Pics_PopupWindow = new Upload_Pics_PopupWindow(UploadPicsActivity.this, itemsOnClick);
    // ?/*from   www  .j  av a 2s  . c  om*/
    upload_Pics_PopupWindow.showAtLocation(
            UploadPicsActivity.this.findViewById(R.id.activity_uploadpics_layout), Gravity.CENTER, 0, 0); // layoutPopupWindow?
}

From source file:com.acbelter.directionalcarousel.CarouselViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int width = MeasureSpec.getSize(widthMeasureSpec);
    final int height = MeasureSpec.getSize(heightMeasureSpec);

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    if (DEBUG) {/*from w  w w . j  a v a 2  s.com*/
        Log.d(TAG, "w=" + width + " h=" + height);
        Log.d(TAG, "wMode=" + getModeDescription(widthMode) + " hMode=" + getModeDescription(heightMode));
    }

    // FIXME Supported only match_parent and wrap_content attributes
    if (mConfig.orientation == CarouselConfig.VERTICAL) {
        int pageContentWidth = getPageContentWidth();
        int newWidth = width;
        if (widthMode == MeasureSpec.AT_MOST || pageContentWidth + 2 * mWrapPadding > width) {

            newWidth = pageContentWidth + 2 * mWrapPadding;
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, widthMode);
        }

        ViewGroup.LayoutParams lp = getLayoutParams();
        // FIXME Supported only FrameLayout as parent
        if (lp instanceof FrameLayout.LayoutParams) {
            if (!parentHasExactDimensions()) {
                throw new UnsupportedOperationException("Parent layout should have exact " + "dimensions.");
            }

            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) lp;
            if (!mSizeChanged) {
                gravityOffset = 0.0f;
                int hGrav = params.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                if (hGrav == Gravity.CENTER_HORIZONTAL || hGrav == Gravity.CENTER) {
                    gravityOffset = (width - newWidth) * 0.5f;
                }
                if (hGrav == Gravity.RIGHT) {
                    gravityOffset = width - newWidth;
                }
            }

            setRotation(90);
            setTranslationX((newWidth - height) * 0.5f + gravityOffset);
            setTranslationY(-(newWidth - height) * 0.5f);
            params.gravity = Gravity.NO_GRAVITY;
            setLayoutParams(params);
        } else {
            throw new UnsupportedOperationException("Parent layout should be instance of " + "FrameLayout.");
        }

        mSizeChanged = true;
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    } else {
        int pageContentHeight = getPageContentHeight();
        if (heightMode == MeasureSpec.AT_MOST || pageContentHeight + 2 * mWrapPadding > height) {

            int newHeight = pageContentHeight + 2 * mWrapPadding;
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight, heightMode);
        }

        // FIXME Supported only FrameLayout as parent
        if (!(getLayoutParams() instanceof FrameLayout.LayoutParams)) {
            throw new UnsupportedOperationException("Parent layout should be instance of " + "FrameLayout.");
        } else {
            if (!parentHasExactDimensions()) {
                throw new UnsupportedOperationException("Parent layout should have exact " + "dimensions.");
            }
        }

        mSizeChanged = true;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    mViewPagerWidth = getMeasuredWidth();
    mViewPagerHeight = getMeasuredHeight();

    if (calculatePageLimitAndMargin()) {
        setOffscreenPageLimit(mConfig.pageLimit);
        setPageMargin(mConfig.pageMargin);
    }

    if (DEBUG) {
        Log.d(TAG, mConfig.toString());
    }
}

From source file:com.astuetz.PagerSlidingTabStripMenu.java

private void addIconTab2(final int position, String title, int resId) {

    TextView tab = new TextView(getContext());
    tab.setText(title);/*from   w  w  w.j  a  v  a2  s.c om*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    tab.setCompoundDrawablePadding(mTabDrawablePadding);

    switch (mIconPostion) {
    case Left:
        tab.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
        break;
    case Top:
        tab.setCompoundDrawablesWithIntrinsicBounds(0, resId, 0, 0);
        break;
    case Right:
        tab.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
        break;
    case Bottom:
        tab.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, resId);
        break;
    }

    addTab(position, tab);
}

From source file:it.redturtle.mobile.apparpav.MeteogramAdapter.java

/**
 * SINGLE TEXT ROW//  w  ww  .  j a v a 2  s .c  om
 * @param att
 * @param linear
 * @return
 */
public LinearLayout getSingleTextRow(Map<String, String> att, LinearLayout linear) {

    LinearLayout container_layout = new LinearLayout(context);
    container_layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.view_shape_meteo));
    container_layout.setMinimumHeight(46);
    container_layout.setVerticalGravity(Gravity.CENTER);

    LinearLayout.LayoutParams value = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT, 0.5f);
    TextView tx = new TextView(context);
    tx.setText(att.get("title"));
    tx.setTextSize(11);
    tx.setTypeface(null, Typeface.BOLD);
    tx.setGravity(Gravity.LEFT);
    tx.setPadding(3, 0, 0, 2);
    tx.setTextColor(Color.rgb(66, 66, 66));
    container_layout.addView(tx, value);

    LinearLayout.LayoutParams value_params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT, 0.5f);
    TextView t2 = new TextView(context);
    t2.setText(att.get("value").equals("") ? " - " : att.get("value"));
    t2.setTextSize(11);
    t2.setGravity(Gravity.CENTER_HORIZONTAL);
    t2.setPadding(2, 0, 0, 2);
    t2.setTextColor(Color.rgb(66, 66, 66));
    container_layout.addView(t2, value_params);

    linear.addView(container_layout);
    return linear;
}

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 v a2s  . co 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: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.
 *//*  w w  w .j  av a2 s .c om*/
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.mailchat.view.PagerSlidingTabStrip.java

private void addIconTab(final int position, int resId) {

    //layout/*from  w  ww.j  av a2 s .c om*/
    RelativeLayout tabLayout = new RelativeLayout(getContext());
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tabLayout.setLayoutParams(layoutParams);
    tabLayout.setGravity(Gravity.CENTER);

    //tab
    RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    imgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    ImageButton tab = new ImageButton(getContext());
    tab.setId(100 + position);
    tab.setImageResource(resId);
    tabLayout.addView(tab, imgParams);

    //???
    RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParams.addRule(RelativeLayout.RIGHT_OF, tab.getId());
    View view = new View(getContext());
    ViewGroup.LayoutParams vParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    view.setLayoutParams(vParams);
    tabLayout.addView(view, viewParams);

    addTab(position, tabLayout);

    /*BadgeView badgeView = new BadgeView(getContext(), view);
      badgeView.setText("");
      badgeView.setTextSize(10);
      badgeView.setGravity(Gravity.CENTER);
      badgeView.setBackgroundResource(R.drawable.main_tab_new_message_notify);
      badgeView.setBadgePosition(BadgeView.POSITION_VERTICAL_LEFT);
      badgeView.show();*/
}