Meena Ramesh
Meena Ramesh

Reputation: 1

How to keep Image View perfectly in GridView in Android Studio

I am new to Android Mobile App development. Please help. Trying to keep Image View in a Grid View. In image view, the image is perfect as in xml. But when the app runs, the images are so tiny in grid view. Here are the codes..

        Imageadapter:- `package com.fishy.meena.hindudevotionalsongs;

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

/**
 * Created by Meena on 8/1/2015.
 */
public class ImageAdapter extends BaseAdapter {

    public Integer[] imageids = {
        R.drawable.shiva, R.drawable.perumal,
                R.drawable.ganapati, R.drawable.muruga,R.drawable.gayathri};

    private Context icontext;
    public ImageAdapter (Context ctx){
        icontext = ctx;
    }

    @Override
    public int getCount() {
        return imageids.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 inflater = (LayoutInflater) icontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.griditem, null);
        }
        ImageView imageview = (ImageView)convertView.findViewById(R.id.gridimageid);
        imageview.setImageResource(imageids[position]);
        imageview.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageview.setPadding(2, 2, 2, 2);
        return  imageview;
    }
}`


***************
[![Screen Shot here][1]][1]

    Main Activity :-
**************

package com.fishy.meena.hindudevotionalsongs;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    GridView gv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gv = (GridView)findViewById(R.id.gvmainid);
        gv.setAdapter(new ImageAdapter(this));
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this,MediaService.class);
                startService(intent);
            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public  void stop(View v){
        Intent intent = new Intent(MainActivity.this,MediaService.class);
        stopService(intent);
    }
}

****************
Imageview xml:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/gridimageid"
        android:scaleType="centerCrop"
        android:layout_width="150dp"
        android:layout_height="120dp"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"/>
    //android:src="@drawable/perumal"/
</LinearLayout>
**********************
main xml:-
************
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#001334">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="100dp"
        android:numColumns="auto_fit"
        android:id="@+id/gvmainid"
        android:gravity="center"
        android:verticalSpacing="120dp"
        android:horizontalSpacing="120dp">
    </GridView>


</LinearLayout>
***************

Upvotes: 0

Views: 576

Answers (1)

Nivedh
Nivedh

Reputation: 961

Imageview width is 150 *120 right? Then why ate you giving params in adapter as 85*85 .Remove that statement and have a run .

Upvotes: 1

Related Questions