LearningBasics
LearningBasics

Reputation: 680

Android:gravity parameter of layout not showing any effect

I am using to implement CustomAdapter to populate listview data. And for each item I am trying to customize the layoutparams of textview . But layoutparams.gravity is not working in my code. Below is the code snippet which I am using

public View getView(int position, View convertView, ViewGroup parent) {
        User user= getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
        }   
        TextView tvName = (TextView) convertView.findViewById(R.id.userName);
        tvName.setLayoutParams(getLayoutParams());
        tvName.setText(getStringFromXmlByName(user.getTitle()));
        tvName.setGravity(Gravity.CENTER_HORIZONTAL);
        tvName.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.background));

        return convertView;
    } 

private LayoutParams getLayoutParams() {

        LayoutParams layoutParams = new LayoutParams(convertDpIntoPixels(300),
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.RIGHT;
        return layoutParams;

    }

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp" >

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:paddingLeft="10dp"
        android:paddingRight="6dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Any possible solution to this problem?

Upvotes: 0

Views: 63

Answers (2)

Christopher Perry
Christopher Perry

Reputation: 39225

Gravity specifies how the contents of a View is to be laid out within itself.

Layout Gravity specifies how the View is to be laid out within it's parent.

What your code does is sets the text inside the TextView to be laid out in the center, horizontally, within the TextView that wraps it's own content. So essentially there's nothing for gravity to do as the bounds of the TextView completely hug the text.

What I suspect you are trying to do is center the TextView within the parent. For this you need to either set gravity on the parent to center_horizontal or set the layout_gravity of the TextView to center_horizontal.

Upvotes: 1

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="10dp" >

<TextView
    android:id="@+id/userName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:textColor="#ffffff"
    android:paddingLeft="10dp"
    android:paddingRight="6dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Upvotes: 0

Related Questions