Skip to content Skip to sidebar Skip to footer

Grid In Android Studio

Can someone please tell me how do I create an empty grid with black border on android studio ? It should allow widgets inside the grid. I've tried but the outcome is different to w

Solution 1:

In this code you can place any widget by using grid layout..Try this code...

//Xml:

<?xml version="1.0" encoding="utf-8"?><GridViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/gridview"android:layout_width="fill_parent"android:layout_height="fill_parent"android:columnWidth="90dp"android:numColumns="auto_fit"android:verticalSpacing="10dp"android:horizontalSpacing="10dp"android:stretchMode="columnWidth"android:gravity="center"
/>

//Sample.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;


publicclassSampleextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    GridViewgridview= (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(newImageAdapter(Sample.this));
}
}

//ImageAdapter.java

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

publicclassImageAdapterextendsBaseAdapter {
    private Context mContext;

// ConstructorpublicImageAdapter(Context c) {
    mContext = c;
}

publicintgetCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    returnnull;
}

publiclonggetItemId(int position) {
    return0;
}

// create a new ImageView for each item referenced by the Adapterpublic View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;

    if (convertView == null) {
        imageView = newImageView(mContext);
        imageView.setLayoutParams(newGridView.LayoutParams(170, 170));

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// Keep all Images in arraypublic Integer[] mThumbIds = {
 //here you can place the image
    R.drawable.ic_radio_2, R.drawable.ic_radio_3,
    R.drawable.ic_radio_4, R.drawable.ic_radio_5,
    R.drawable.ic_radio_4, R.drawable.ic_radio_6,

};
}

Post a Comment for "Grid In Android Studio"