Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android TextView setting margin and background using code

In an Android activity....how do I do the following using just code instead of XML?

<TextView android:background="#0000ff" android:layout_margin="2dip"/>

I also need text align right...

stuck on it for a while =( if someone could help

thanks

UPDATE

I tried the code below

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 
            RelativeLayout.LayoutParams.FILL_PARENT,                
            RelativeLayout.LayoutParams.FILL_PARENT);                
            //250);

    lp.setMargins(5,5,5,5);

and i did

row.addView(t1, lp);
row.addView(t2);
row.addView(t3);
row.addView(t4);

I also tried LinearLayout

but t1 for some reason doesn't show at all anymore....

like image 727
khan Avatar asked Apr 02 '26 17:04

khan


1 Answers

here is how to code a textView:

    mTextView = new TextView(this);
    mTextView.setGravity(Gravity.CENTER_VERTICAL);       
    mTextView.setText(R.string.instructions);
    mTextView.setTextColor(0xFF000000);
    mTextView.setPadding(20, 8, 8, 20);

    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);

    mScroll = new ScrollView(this);
    mScroll.setScrollbarFadingEnabled(false);
    mTextPane = new RelativeLayout(this);
    mTextPane.setVisibility(View.GONE);
    //mScroll.setVisibility(View.GONE);

    mScroll.addView(mTextView);
    mTextPane.addView(mScroll);

    Resources res = getResources();
    //Drawable drawable = res.getDrawable(R.drawable.text_pane_feather2);
    Drawable drawable = res.getDrawable(R.drawable.text);
    mTextPane.setBackgroundDrawable(drawable);

    //RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 420, 420 );
    RelativeLayout.LayoutParams lp = 
        new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
                //RelativeLayout.LayoutParams.WRAP_CONTENT);
                250);

    lp.setMargins(0,0,0,30);
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL );
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    layout.addView(mTextPane, lp);
like image 185
Chris Briscoe Avatar answered Apr 04 '26 05:04

Chris Briscoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!