jcaruso
jcaruso

Reputation: 2494

Android Spacing on Multiple Columns

I'm trying to evenly space all the images horizontally with rating starts below it but I'm struggling to do it. Can someone show me please?

public class LevelActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(LevelActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.level, menu);
        return true;
    }


    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.zero, R.drawable.one,
                R.drawable.two, R.drawable.three,
                R.drawable.four, R.drawable.five,
                R.drawable.zero, R.drawable.one,
                R.drawable.two, R.drawable.three,
                R.drawable.four, R.drawable.five,
                R.drawable.zero, R.drawable.one,
                R.drawable.two, R.drawable.three,
                R.drawable.four, R.drawable.five,
                R.drawable.zero, R.drawable.one
        };
    }
}

enter image description here New layout, just needs stars now.

Upvotes: 0

Views: 117

Answers (2)

Dinesh Raj
Dinesh Raj

Reputation: 654

use gridview

 <GridView
                android:id="@+id/list_view"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:columnWidth="90dp"
                android:gravity="center"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:horizontalSpacing="10dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:scrollingCache="false"
                android:animationCache="false"
                android:fastScrollEnabled="true"
                android:verticalSpacing="10dp" />

Upvotes: 1

prakash
prakash

Reputation: 654

Try like this, it may help you

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/levelselection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:background="@null"
            android:src="@drawable/icon" />

         <RatingBar
             android:id="@+id/rtbProductRating"
             style="@style/RatingBar" // I dont know what you are using this line
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_below="@+id/levelselection"
             android:numStars="3"
             android:rating="2"
             android:paddingTop="20sp" />

    </RelativeLayout>

Upvotes: 0

Related Questions