6 Grid Layout





Algorithm:
Step 1) Create a new Android Studio Project

Step 2)Goto Project Structure and open activity_main.xml file

Step 3) Remove the default layout i.e Constraint layout and create Grid Layout by using tag or drag and drop from the palette.

Step 3) Create TextViews or ImageViews by using tags in sctivity_main.xml file or drag and drop from the palette.

Step 4) Add Image to the res/drawable folder and supported filenames are lowercase and _ names are allowed. Ex: klu_thins.jpg

Step 5) Run the application from the menu bar select Run and Click on Run App option.


Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<GridLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

<TextView android:layout_width="match_parent" android:layout_height="50dp" android:textAlignment="center" android:textSize="30dp" android:text="@string/app_name"/>

<ImageView android:id="@+id/imageView4" android:layout_width="match_parent" android:layout_height="117dp" android:layout_column="0"
android:layout_row="1" app:srcCompat="@drawable/img1" />







<ImageView android:id="@+id/imageView5" android:layout_width="match_parent" android:layout_height="75dp" android:layout_column="0" android:layout_row="2" app:srcCompat="@drawable/img2" />

</GridLayout>

Programs:
1)GridAdapter(user defined class)

package com.example.basha.gridview;



import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;



public class GridAdapter extends BaseAdapter {

    Context context;

    private final String[] values;

    private final int[] images;

    View view;

    LayoutInflater layoutInflater;



    public GridAdapter(Context context, String[] values, int[] images) {

        this.context = context;

        this.values = values;

        this.images = images;

    }



    @Override

    public int getCount() {

        return values.length;

    }



    @Override

    public Object getItem(int position) {

        return null;

    }



    @Override

    public long getItemId(int position) {

        return 0;

    }



    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        layoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        if(convertView==null){

         view=new View(context);

         view=layoutInflater.inflate(R.layout.single_item1,null);

            ImageView imageView=(ImageView)view.findViewById(R.id.imageview);

            TextView textView=(TextView)view.findViewById(R.id.textview);

            imageView.setImageResource(images[position]);

            textView.setText(values[position]);





        }

        return view;

    }

}















2)MainActivity:
 
package com.example.basha.gridview;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.GridLayout;

import android.widget.GridView;



public class MainActivity extends AppCompatActivity {



    GridView gridView;

    String[] values={"India","South_Africa","Malaysia","Spain","Zimbabwe","Nambia","France","USA","Iceland"};


    int images[]={

       R.drawable.img1,

    R.drawable.img2,

    R.drawable.img3,

    R.drawable.img4,

    R.drawable.img5,

    R.drawable.img6,

    R.drawable.img7,

    R.drawable.img8,

    R.drawable.img9

    };

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        gridView=findViewById(R.id.gridview);

        GridAdapter gridAdapter=new GridAdapter(this,values,images);

        gridView.setAdapter(gridAdapter);

    }

}


OUTPUT:


Comments

Popular posts from this blog

20 Simple Content Provider Example

14 Popup Menu

17 Simple Notification Program