0

Can you explain me please why my TextView don't work on gravity to left .

enter image description here

A: LinearLayout | orientation=HORIZONTAL | gravity A = top or center or bottom.

B: LinearLayout | orientation=VERTICAL | gravity B = left or center or right.

C: TextView

i us:

A.setGravity(gravity A);

B.setGravity(gravity B);

Result gravity:

  • top | left :not work (=top center)

  • center | left :not work (=center center)

  • bottom | left :not work (=bottom center)

  • top | center :work

  • center | center :work

  • bottom | center :work

  • top | right :work

  • center | right :work

  • bottom | right :work

        LinearLayout parent = new LinearLayout(mContext);
    
    LinearLayout framelayoutTitle = new LinearLayout(mContext);
    framelayoutTitle.setOrientation(LinearLayout.HORIZONTAL);
    framelayoutTitle.setGravity(vGravityTitle);
    
    LinearLayout layoutTitle = new LinearLayout(mContext);
    layoutTitle.setOrientation(LinearLayout.VERTICAL);
    layoutTitle.setGravity(hGravityTitle);
    
    holder.textTitle = new TextView(mContext);
    layoutTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    
    
    framelayoutTitle.setLayoutParams(new LinearLayout.LayoutParams((widthTitle * (width - dp2px(widthImageLV))) / 100, dp2px(heightTitle)));    
    
    
            holder.textTitle.setText(titleList.get(position));
            holder.textTitle.setGravity(alignmentTextTitle);
            holder.textTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            holder.textTitle.setTextSize(sizeTitle);
            holder.textTitle.setBackgroundColor(Color.WHITE);
    
    layoutTitle.addView(holder.textTitle);
    framelayoutTitle.addView(layoutTitle);
    
    parent.setOrientation(LinearLayout.HORIZONTAL);
    parent.setGravity(Gravity.TOP);
    parent.addView(framelayoutTitle);
    
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
lachguer9
  • 5
  • 7

1 Answers1

0

Try this and give padding and margin according to your needs.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:text="A"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:text="b"
    android:layout_below="@+id/textView"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:text="b"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    />

</RelativeLayout>
  • i don't know your xml code .if it not getting any idea for above code then pl post your code and i make changes and give it. –  May 10 '18 at 11:32
  • NO i need to move the text view on the all position inside the layout A. And i don't us XML i us programmatically code – lachguer9 May 10 '18 at 11:47
  • you want to perform user can change textbox position at runtime. it like animation. –  May 10 '18 at 11:49
  • not annimation but just can change the position of textView : example: if i setGravity like this: A.setGravity(Gravity.TOP); B.setGravity(Gravity.Left); normay i wil have result my textView on left and top – lachguer9 May 10 '18 at 11:52
  • provide your xml code then better idea what you done. –  May 10 '18 at 11:55
  • my code work well just problem with left gravity i can't have textview on left position for other position is work – lachguer9 May 10 '18 at 11:55
  • i have add the simple code i us this inside adapter grid view – lachguer9 May 10 '18 at 12:00
  • OK i found a solution i just have problem on initialization of my variable int: int hGravityTitle = 1; i have change it to 3 // i don't know what is the problem but it work now public void HGravityTitle(int alignment) { if(alignment == 1) hGravityTitle = 3;//Gravity.LEFT; else if(alignment == 2) hGravityTitle = 5;//Gravity.RIGHT; else hGravityTitle = 1;//Gravity.CENTER_HORIZONTAL; } – lachguer9 May 10 '18 at 14:13