1

I've looked through a heap of previous answers on this , and tried the code marked as working, but so far nothing I do can convince the TextView I'm creating at runtme from filling the screen completely from left to right. I want to add a margin on both sides similar to a regular Toast. After that I can add a drop shadow to the shape.

Here my form layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rel_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/hello_world"
        android:textColor="@color/holobrightblue"
        android:textSize="48sp"
        android:textStyle="bold"
        tools:context=".MainActivity" />

</RelativeLayout>

.. and my code

textView = new TextView(m_Context);
RoundRectShape rs = new RoundRectShape(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }, null, null);

ShapeDrawable sd =  new ShapeDrawable(rs);
sd.setAlpha(m_opacity);
textView.setBackgroundDrawable(sd);             
textView.setTextColor(m_txtcolor);  
textView.setText(toasttitle+"\n"+toastmessage);
textView.setBackgroundColor(Color.BLACK);
textView.setPadding(10,10,10,10);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);


lp.setMargins(60, 0, 60, 0);          
textView.setLayoutParams(lp);

toastView = new Toast(m_Context);
toastView.setView(textView);
toastView.setDuration(m_toastlen);
toastView.setGravity(m_screengravity, 0,0);
toastView.show();

As mentioned, nothing I've tried from other solutions seems to convince the textview from filling all the horizonal space.

I've tried removing that shape etc...

Any ideas?

user1325094
  • 339
  • 5
  • 13

2 Answers2

4

You will need the holder RelativeLayout which will contain your TextView,

and then you can set the holder's Gravity to CENTER

RelativeLayout head2 = new RelativeLayout(this);
head2.setId(++myid);
RelativeLayout.LayoutParams head2Params = new RelativeLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 25);
head2Params.addRule(RelativeLayout.BELOW, head1.getId());
head2.setLayoutParams(head2Params);
head2.setBackgroundColor(Color.CYAN);

then your create TextView

textView = new TextView(m_Context);
RoundRectShape rs = new RoundRectShape(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }, null, null);

ShapeDrawable sd =  new ShapeDrawable(rs);
sd.setAlpha(m_opacity);
textView.setBackgroundDrawable(sd);             
textView.setTextColor(m_txtcolor);  
textView.setText(toasttitle+"\n"+toastmessage);
textView.setBackgroundColor(Color.BLACK);
textView.setPadding(10,10,10,10);

LinearLayout.LayoutParams lp = new
   LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                                          ^^^^^^^^^^^^
                                          ^^^^^^^^^^^^MUST  EDIT
lp.setMargins(60, 0, 60, 0);          
textView.setLayoutParams(lp);

then add your Textview to holder relativeLayout with Center Gravity

head2.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
head2.addView(textView);

Show it

toastView = new Toast(m_Context);
toastView.setView(textView);
toastView.setDuration(m_toastlen);
toastView.setGravity(m_screengravity, 0,0);
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
  • Padding will not solve this problem as it will be applied to the TEXT inside of the TextView so that the space between the text and the border will get increased.The person wants the complete TextView to get centered or moved as the given Value from the border of the Screen – Haresh Chaudhary Aug 01 '12 at 11:18
  • Thanks for taking my comment +vely.Still,the problem will remain as the padding will be applied to the Text that is in the TextView.Are you getting what I am saying??Because the padding is applied to the TextView,it's text will get padded. – Haresh Chaudhary Aug 01 '12 at 11:22
  • Sorry, I should have mentioned that it's the margins I need applied to the textview, which create whitespace between it and the Layout container. – user1325094 Aug 01 '12 at 12:55
  • you can use setBackground(Color.ANY_One) to your textView – Chintan Raghwani Aug 01 '12 at 12:56
  • This was very close to being correct. Changing the LinearLayout params to LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT and then setting the Gravity to textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); fixed this for me. The logic to create a Relative layout in code and adding the Textview to it, caused an Exception when the Toast tried to disappear. – user1325094 Aug 02 '12 at 09:17
  • O.K. If you appreciate that my answer was helpful to you, then you can up-vote me and can help me.. – Chintan Raghwani Aug 02 '12 at 18:32
-1

This is the easy code to set textview ( text ) to given margins ,

LayoutParams lp = new LayoutParams(text.getWidth(), text.getHeight());
        lp.setMargins(5, 5, 0, 5);
        text.setLayoutParams(lp);

Parameters of setMargins() are as :

LayoutParams.setMargins(int left,int top , int right,int bottom)

setLayoutParams take Object of LayoutParams as parameter

Dilroop Singh
  • 544
  • 6
  • 16