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.diplimadoapp.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);//w  ww .  j av a2s .  co  m

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

    switch (position) {
    case 0:

        LinearLayout ll = new LinearLayout(getActivity());
        ll.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);

        ImageView iv = new ImageView(getActivity());
        iv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1020));
        //iv.setLayoutParams(new  LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        iv.setMaxHeight(520);
        iv.setImageResource(R.drawable.contact);
        ll.addView(iv);

        TableLayout tl = new TableLayout(getActivity());
        tl.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        TableRow tr = new TableRow(getActivity());
        tr.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tr.setGravity(Gravity.FILL_HORIZONTAL);
        tr.setPadding(5, 5, 5, 5);

        List<RssItem> lecturaitems = null;
        try {
            // Create RSS reader
            RssReader rssReader = new RssReader(
                    "http://www.senalradionica.gov.co/index.php/home/articulos/itemlist?format=feed");
            // Get a ListView from main view
            //ListView itcItems = (ListView) findViewById(R.id.listMainView);

            lecturaitems = rssReader.getItems();

        } catch (Exception e) {
            Log.e("Diplomado App Reader", e.getMessage());
        }

        TextView v1 = new TextView(getActivity());
        params.setMargins(margin, margin, margin, margin);
        v1.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
        v1.setGravity(Gravity.CENTER);
        v1.setBackgroundResource(R.drawable.background_card);
        if (lecturaitems != null) {
            v1.setText(lecturaitems.get(0).getTitle());
            String urlnoticia = lecturaitems.get(0).getLink();
            v1.setOnClickListener(new NoticiaDestacadaOnClickListener(urlnoticia));
        } else {
            v1.setText("No Registra nuevas noticias");
        }

        tr.addView(v1);

        ImageButton ib = new ImageButton(getActivity());
        ib.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
        ib.setImageResource(R.drawable.facebook);

        ib.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink.setData(Uri.parse("http://www.facebook.com"));
                startActivity(myWebLink);
            }
        });

        tr.addView(ib);

        ImageButton ib2 = new ImageButton(getActivity());
        ib2.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
        ib2.setImageResource(R.drawable.twitter);

        ib2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink.setData(Uri.parse("http://www.twitter.com"));
                startActivity(myWebLink);
            }
        });

        tr.addView(ib2);

        ImageButton ib3 = new ImageButton(getActivity());
        ib3.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
        ib3.setImageResource(R.drawable.youtube);

        ib3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink.setData(Uri.parse("http://www.youtube.com"));
                startActivity(myWebLink);
            }
        });

        tr.addView(ib3);

        tl.addView(tr);

        ll.addView(tl);

        fl.addView(ll);

        break;

    case 1:

        LinearLayout ll2 = new LinearLayout(getActivity());
        ll2.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        ll2.setOrientation(LinearLayout.VERTICAL);

        TableLayout tl2 = new TableLayout(getActivity());
        tl2.setLayoutParams(
                new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        TableRow tr2 = new TableRow(getActivity());
        tr2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tr2.setGravity(Gravity.FILL_HORIZONTAL);
        tr2.setPadding(5, 5, 5, 5);

        TextView v0 = new TextView(getActivity());
        params.setMargins(margin, margin, margin, margin);
        v0.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
        v0.setGravity(Gravity.CENTER);
        v0.setText("Acusticos Radionoca. Lunes a Viernes 8:00 a.m. - 9:00 a.m.");
        tr2.addView(v0);

        ImageButton ib20 = new ImageButton(getActivity());
        ib20.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
        ib20.setImageResource(R.drawable.pacusticos);
        ib20.setBackgroundDrawable(null);

        tr2.addView(ib20);

        tl2.addView(tr2);

        TableRow tr3 = new TableRow(getActivity());
        tr3.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tr3.setGravity(Gravity.FILL_HORIZONTAL);
        tr3.setPadding(5, 5, 5, 5);

        TextView v03 = new TextView(getActivity());
        params.setMargins(margin, margin, margin, margin);
        v03.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
        v03.setGravity(Gravity.CENTER);
        v03.setText("Acusticos Radionoca. Lunes a Viernes 8:00 a.m. - 9:00 a.m.");
        tr3.addView(v03);

        ImageButton ib30 = new ImageButton(getActivity());
        ib30.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
        ib30.setImageResource(R.drawable.pradionica_lacarta);
        ib30.setBackgroundDrawable(null);

        tr3.addView(ib30);

        tl2.addView(tr3);

        TableRow tr4 = new TableRow(getActivity());
        tr4.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tr4.setGravity(Gravity.FILL_HORIZONTAL);
        tr4.setPadding(5, 5, 5, 5);

        TextView v04 = new TextView(getActivity());
        params.setMargins(margin, margin, margin, margin);
        v04.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
        v04.setGravity(Gravity.CENTER);
        v04.setText("Acusticos Radionoca. Lunes a Viernes 8:00 a.m. - 9:00 a.m.");
        tr4.addView(v04);

        tl2.addView(tr4);

        ImageButton ib40 = new ImageButton(getActivity());
        ib40.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
        ib40.setImageResource(R.drawable.prockeros);
        ib40.setBackgroundDrawable(null);

        tr4.addView(ib40);

        ll2.addView(tl2);

        /*
        VideoView videoView = new VideoView(getActivity());
                
        Uri path = Uri.parse("rtmp://cdns840stu0010.multistream.net:80/rtvcRadionicalive/?pass=|radionica|");
                
        videoView.setVideoURI(path);
        videoView.start(); 
                
        TableRow tr5 =new TableRow(getActivity());
        tr5.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tr5.setGravity(Gravity.FILL_HORIZONTAL);
        tr5.setPadding(5,5,5,5);
        tr5.addView(videoView);
                
        ll2.addView(tr5);*/

        fl.addView(ll2);
        break;
    case 2:
        ListView lv = new ListView(getActivity());
        params.setMargins(margin, margin, margin, margin);
        lv.setLayoutParams(params);
        lv.setLayoutParams(params);
        lv.setBackgroundResource(R.drawable.background_card);
        try {
            // Create RSS reader
            RssReader rssReader = new RssReader(
                    "http://www.senalradionica.gov.co/index.php/home/articulos/itemlist?format=feed");
            // Get a ListView from main view
            //ListView itcItems = (ListView) findViewById(R.id.listMainView);

            List<RssItem> lecturaitems2 = rssReader.getItems();
            // Create a list adapter
            ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(getActivity(),
                    android.R.layout.simple_list_item_1, lecturaitems2);
            // Set list adapter for the ListView
            lv.setAdapter(adapter);

            // Set list view item click listener
            lv.setOnItemClickListener(new ListListener(lecturaitems2, getActivity()));

        } catch (Exception e) {
            Log.e("Diplomado App Reader", e.getMessage());
        }
        fl.addView(lv);
        break;
    }

    return fl;
}

From source file:com.example.fan.horizontalscrollview.PagerSlidingTabStrip.java

public TextView getTextTab(final int position, String title) {

    TextView tab = new TextView(getContext());
    tab.setText(title);//from w w  w.ja  v a2s.com
    tab.setFocusable(true);
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    tab.setTextColor(getResources().getColorStateList(
            mTextChangeable ? R.color.pst_tab_changeable_text_selector : R.color.pst_tab_text_selector));
    tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.0f);
    tab.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (null != mOnPageClickedLisener) {
                mOnPageClickedLisener.onPageClicked(position);
            }
            pager.setCurrentItem(position);
        }
    });

    return tab;
}

From source file:com.app.plugins.childBrowser.ChildBrowser.java

/**
 * Display a new browser with the specified URL.
 *
 * @param url           The url to load.
 * @param jsonObject /*from   w  ww .ja v  a 2  s. c  o m*/
 */
public String showWebPage(final String url, JSONObject options) {
    // Determine if we should hide the location bar.
    if (options != null) {
        showLocationBar = options.optBoolean("showLocationBar", false);
    }

    final WebView parent = this.webView;

    // Create dialog in new thread 
    Runnable runnable = new Runnable() {
        public void run() {
            dialog = new Dialog(cordova.getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    try {
                        JSONObject obj = new JSONObject();
                        obj.put("type", CLOSE_EVENT);

                        sendUpdate(obj, false);
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "Should never happen");
                    }
                }
            });

            //LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            //LinearLayout.LayoutParams forwardParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            //LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 1.0f);
            LinearLayout.LayoutParams closeParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams wvParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);

            LinearLayout main = new LinearLayout(cordova.getActivity());
            main.setOrientation(LinearLayout.VERTICAL);

            LinearLayout toolbar = new LinearLayout(cordova.getActivity());
            toolbar.setOrientation(LinearLayout.HORIZONTAL);

            /*
            ImageButton back = new ImageButton((Context) ctx);
            back.getBackground().setAlpha(0);
            back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                goBack();
            }
            });
            back.setId(1);
            try {
            back.setImageBitmap(loadDrawable("plugins/childbrowser/icon_arrow_left.png"));
            } catch (IOException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
            }
            back.setLayoutParams(backParams);
                    
            ImageButton forward = new ImageButton((Context) ctx);
            forward.getBackground().setAlpha(0);
            forward.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                goForward();
            }
            });
            forward.setId(2);
            try {
            forward.setImageBitmap(loadDrawable("plugins/childbrowser/icon_arrow_right.png"));
            } catch (IOException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
            }               
            forward.setLayoutParams(forwardParams);
            */

            /*
            edittext = new EditText((Context) ctx);
            edittext.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                  navigate(edittext.getText().toString());
                  return true;
                }
                return false;
            }
            });
            edittext.setId(3);
            edittext.setSingleLine(true);
            edittext.setText(url);
            edittext.setLayoutParams(editParams);
            */
            //edittext = new EditText((Context) ctx);
            //edittext.setVisibility(View.GONE);

            LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.FILL_PARENT, 1.0f);
            TextView title = new TextView(cordova.getActivity());
            title.setId(1);
            title.setLayoutParams(titleParams);
            title.setGravity(Gravity.CENTER_VERTICAL);
            title.setTypeface(null, Typeface.BOLD);

            ImageButton close = new ImageButton(cordova.getActivity());
            close.getBackground().setAlpha(0);
            close.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    closeDialog();
                }
            });
            close.setId(4);
            try {
                close.setImageBitmap(loadDrawable("plugins/childbrowser/icon_close.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            close.setLayoutParams(closeParams);

            childWebView = new WebView(cordova.getActivity());
            childWebView.getSettings().setJavaScriptEnabled(true);
            childWebView.getSettings().setBuiltInZoomControls(true);
            WebViewClient client = new ChildBrowserClient(parent, ctx, title/*, edittext*/);
            childWebView.setWebViewClient(client);
            childWebView.loadUrl(url);
            childWebView.setId(5);
            childWebView.setInitialScale(0);
            childWebView.setLayoutParams(wvParams);
            childWebView.requestFocus();
            childWebView.requestFocusFromTouch();

            //toolbar.addView(back);
            //toolbar.addView(forward);
            //toolbar.addView(edittext);
            toolbar.addView(close);
            toolbar.addView(title);

            if (getShowLocationBar()) {
                main.addView(toolbar);
            }
            main.addView(childWebView);

            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;
            lp.verticalMargin = 0f;
            lp.horizontalMargin = 0f;

            dialog.setContentView(main);
            dialog.show();
            dialog.getWindow().setAttributes(lp);
        }

        private Bitmap loadDrawable(String filename) throws java.io.IOException {
            InputStream input = cordova.getActivity().getAssets().open(filename);
            return BitmapFactory.decodeStream(input);
        }
    };
    this.cordova.getActivity().runOnUiThread(runnable);
    return "";
}

From source file:com.asc_ii.bangnote.bigbang.BigBangLayout.java

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

From source file:com.example.fan.horizontalscrollview.PagerSlidingTabStrip.java

private void addRedDotTab(final int position, String title, int resId) {
    LinearLayout tabLayout = new LinearLayout(getContext());
    tabLayout.setOrientation(LinearLayout.HORIZONTAL);
    tabLayout.setGravity(Gravity.CENTER);
    TextView tab = new TextView(getContext());
    tab.setText(title);//from w  w w . j  a  va 2  s  .  c  o  m
    tab.setFocusable(true);
    tab.setGravity(Gravity.CENTER);

    tab.setSingleLine();
    tab.setTextColor(getResources().getColorStateList(R.color.pst_tab_text_selector));
    tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.0f);
    tabLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (null != mOnPageClickedLisener) {
                mOnPageClickedLisener.onPageClicked(position);
            }
            pager.setCurrentItem(position);
        }
    });

    tabLayout.addView(tab, 0);
    ImageView tabImg = new ImageView(getContext());
    LinearLayout.LayoutParams tabImgParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    tabImgParams.setMargins(DeviceUtils.dip2px(getContext(), 5), DeviceUtils.dip2px(getContext(), 10), 0, 0);
    tabImgParams.gravity = Gravity.TOP;
    tabImg.setLayoutParams(tabImgParams);

    tabLayout.addView(tabImg, 1);
    tabsContainer.addView(tabLayout);
}

From source file:com.example.fan.horizontalscrollview.PagerSlidingTabStrip.java

private void addWarningTab(final int position, String title, int resId) {
    LinearLayout tabLayout = new LinearLayout(getContext());
    tabLayout.setOrientation(LinearLayout.HORIZONTAL);
    tabLayout.setGravity(Gravity.CENTER);

    ImageView tabImg = new ImageView(getContext());
    int width = DimenUtils.dp2px(getContext(), 18);
    LinearLayout.LayoutParams tabImgParams = new LinearLayout.LayoutParams(width, width);
    tabImgParams.setMargins(0, 0, DimenUtils.dp2px(getContext(), 10), 0);
    tabImgParams.gravity = Gravity.CENTER;
    tabImg.setLayoutParams(tabImgParams);
    if (resId != -1) {
        tabImg.setBackgroundResource(resId);
        tabImg.setVisibility(View.VISIBLE);
    } else {// ww  w .j a v  a2  s.c  om
        tabImg.setVisibility(View.GONE);
    }
    tabLayout.addView(tabImg, 0);

    TextView tab = new TextView(getContext());
    tab.setText(title);
    tab.setFocusable(true);
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    tab.setTextColor(getResources().getColorStateList(
            mTextChangeable ? R.color.pst_tab_changeable_text_selector : R.color.pst_tab_text_selector));
    tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.0f);
    tabLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (null != mOnPageClickedLisener) {
                mOnPageClickedLisener.onPageClicked(position);
            }
            pager.setCurrentItem(position);
        }
    });
    tabLayout.addView(tab, 1);

    tabsContainer.addView(tabLayout);
}

From source file:com.easemob.easeui.widget.viewpagerindicator.PagerSlidingTabStrip.java

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

    TextView tab = new TextView(getContext());
    tab.setText(title);/*from  w  w  w.j a v  a2s.c o  m*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    tab.setPadding(tabMargin, 0, tabMargin, 0);
    tab.setTextColor(tabTextColor);
    //      tab.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.btn_tab_mendian_selector, 0, 0);
    tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
    addTab(position, tab);

}

From source file:com.coolerfall.uiart.PagerSlidingTabStrip.java

/** add one text tab */
private void addTextTab(final int position, String title) {
    TextView tab = new TextView(getContext());
    tab.setText(title);/*  ww w.  jav a 2s . com*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();

    addTab(position, tab);
}

From source file:com.insthub.ecmobile.component.PagerSlidingTabStrip.java

/** add one text tab */
private void addTextTab(final int position, String title, int res) {
    TextView tab = new TextView(getContext());
    tab.setText(title);/*from  w  w w  . jav  a2  s  .c o  m*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    Drawable drawable = getResources().getDrawable(res);
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    tab.setCompoundDrawables(drawable, null, null, null);
    addTab(position, tab);
}

From source file:com.example.linkagescroll.widget.PagerSlidingTabStrip.java

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

    TextView tab = new TextView(getContext());
    tab.setTextSize(14);// ww w.jav  a2  s  .com
    tab.setText(title);
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();

    addTab(position, tab);
}