Reputation: 695
I am populating a ListView
using my custom adapter as I need to do some special formatting on every TextView that I send over. The problem is that no matter how small I set the textSize of each TextView, they are separated by the same amount of space in the list as shown in the image
I want the spacing between these views reduced. This is my getView() method.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setGravity(Gravity.CENTER);
textView.setText( getItem(position).getName() );
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
textView.setTextColor( getItem(position).getClr() );
return textView;
}
edit1: What I want actually is that "Blue" and "Green" should be closer together. I want to reduce the spacing between them.
edit2: xml:
<ListView
android:id="@+id/answerlist"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/question" >
</ListView>
Upvotes: 0
Views: 675
Reputation: 695
Well... The problem was that I was using the default layout android.R.layout.simple_list_item_1 to generate views for rows of the ListView. This layout uses the text appearance "?android:attr/textAppearanceLarge". I dont know what kind of padding/margins this sets but no matter what I do in the adapter, the spacing did not reduce.
So what I did was that I made a custom xml layout named "row.xml" in res/layout and put a simple TextView in there. I used this layout to inflate rows in my ListView and the space was gone. Here is the code of the constructor:
public ColoredAdapter(Context context, ArrayList<ColorElement> values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
Upvotes: 0
Reputation: 83
As jyotiprakash mentioned it may need to give a more detailed description of what you are trying to do.
However, from what I can see you are trying to set the margin. If so I would go about it in the following way.
.....
View someView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.yourViewLayout, null, false);
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(4, 15, 4, 0);
//Now you can use your view to set the layout params
someView.setLayoutParams(layoutParams);
Now the margin for that view will be in line with the above defined layout params
Upvotes: 0
Reputation: 2086
This problem is because of the following line:
textView.setGravity(Gravity.CENTER);
By d way what you need actually can you explain me in detail?
Upvotes: 1