Android Grid View with Image and Text - java

I need to show text below every image in grid view. I have been able to put the images in grid view but how to put text below it? Below I'm posting the code snippets.
fragment_facility_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
FacilityAdapter.class
package com.androidbelieve.HIT_APP;
/**
* Created by Akash on 2/13/2016.
*/
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class FacilityAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.bank, R.drawable.bus,
R.drawable.girlshostel , R.drawable.guesthouse,
R.drawable.gym , R.drawable.library,R.drawable.sports
};
public String[] mThumbNames = {
"Bank", "Bus Service","Guest House", "GYM","Fac1","Fac2"
};
// Constructor
public FacilityAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View gridView;
gridView = new View(mContext);
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
return imageView;
}
}
Thank You

I would rather suggest that instead of creating ImageView dynamically at run time and return that from your getView() method, you should create an xml for your each of the list items. Below is the sample xml with ImageView and TextView
<?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"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"/>
</LinearLayout>
Also you should follow ViewHolder Patternto make your GridView memory efficient. Your getView() method will look alike this
#Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.yourxml,
null);
holder.textview= (TextView) convertView.findViewById(R.id.text);
holder.imageview= (ImageView) convertView.findViewById(R.id.image);
holder.imageView.getLayoutParams().width = yourImageWidth;
holder.imageView.getLayoutParams().height = yourImageHeight;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setImageResource(mThumbIds[position]);
holder.textview.setText(mThumbNames[position]);
return convertView;
}
static class ViewHolder {
ImageView imageView;
TextView textView;
}

Try to extend this:
SimpleAdapter
or use it by default like:
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.bank, R.drawable.bus,
R.drawable.girlshostel , R.drawable.guesthouse,
R.drawable.gym , R.drawable.library,R.drawable.sports
};
public String[] mThumbNames = {"Bank", "Bus Service","Guest House", "GYM","Fac1","Fac2"};
Simple adapter= new SimpleAdapter(mContext, List<?> your_list, R.layout.your_layout_file, from, to);
You also have to make a layout file and define how the image and text you would like to look for each gridview item like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/this_image_id_have_to_be_used_in_your_to_array"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/this_text_id_have_to_be_used_in_your_to_array"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"/>
</LinearLayout>
Hope it helps!!!

Related

How can i add favourite (CheckBox/ToggleButton/ImageView) to my ListView?

Please help me.
How can i add favourite (CheckBox/ToggleButton/ImageView) to my ListView? ... I have never added a favourite button before so I would like to add one in listView with CheckBox or ToggleButton or ImageView .
Hope everyone understand my question
My code :
This is my ListAdapter
static class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Context ctx;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
ctx = context;
}
public int getCount() {
return RecipeName.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtRecipeName.setText(RecipeName[position]);
return convertView;
}
static class ViewHolder {
TextView txtRecipeName;
}
}
This is my row xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1"
android:gravity="center_horizontal"
android:background="#drawable/row">
<TextView
android:id="#+id/txtRecipeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/text"
android:textSize="16sp"
android:textStyle="bold"
android:layout_weight="0.04"
android:typeface="normal"
android:background="#drawable/button2"/>
</LinearLayout>
For adding image u can use ImageView same as TextView in the .xml file..
Just add the src of the image after it "android:src="#drawable/beach" like this.
Here "#drawable/beach" is the path of image.
`<ImageView
android:id="#+id/photo_image_view" //for id
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" //this will display image in center
android:src="#drawable/beach" /> //path of image`
For Toggle Button..use the following code in .xml file
<Switch
android:id="#+id/toggle_switch" //give id so can use this in java code later
android:layout_width="wrap_content" //set width
android:layout_height="wrap_content" //set height
android:text="Toggle Button" // string to be displayed
android:textAppearance="?android:textAppearanceSmall" />
How it will look
Toggle Button

Unable to add delete functionality in my list view

so I want to add an option to delete a particular entry in a list, so I added an Image view into my list's adapter. But for some reason, when I try to create the list in my app now, it crashes. This is the error line :
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ImageView
Here is the code for my list adapter :
package com.example.taskmasterv3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class SubtaskAdapter extends ArrayAdapter<subtask> {
private final Context context;
private ArrayList<subtask> values;
public SubtaskAdapter(Context context, ArrayList<subtask> list) {
//since your are using custom view,pass zero and inflate the custom view by overriding getview
super(context, 0 , list);
this.context = context;
this.values = list;
}
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
//check if its null, if so inflate it, else simply reuse it
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subtask_item, parent, false);
}
//use convertView to refer the childviews to populate it with data
TextView tvSubtaskName = convertView.findViewById(R.id.tvSubtaskName);
ImageView ivPri = convertView.findViewById(R.id.ivPri);
ImageView ivTime = convertView.findViewById(R.id.ivTime);
ImageView ivDelete = convertView.findViewById(R.id.ivDelete);
tvSubtaskName.setText(values.get(position).getSubtaskName());
if (values.get(position).isPriHigh()) {
ivPri.setImageResource(R.drawable.priority_high);
} else if (values.get(position).isPriMed()) {
ivPri.setImageResource(R.drawable.priority_med);
} else if (values.get(position).isPriLow()) {
ivPri.setImageResource(R.drawable.priority_low);
}
if (values.get(position).isTimeMore()) {
ivTime.setImageResource(R.drawable.time_symbol_more);
} else if (values.get(position).isTimeMed()) {
ivTime.setImageResource(R.drawable.time_symbol_med);
} else if (values.get(position).isTimeLess()) {
ivTime.setImageResource(R.drawable.time_symbol_less);
}
// Delete button for subtasks
ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
values.remove(position);
notifyDataSetChanged();
}
});
//return the view you inflated
return convertView;
}
//to keep adding the new subtasks try the following
public void addANewSubTask(subtask newSubTask){
ArrayList<subtask> newvalues = new ArrayList<>(this.values);
newvalues.add(newSubTask);
this.values = newvalues;
notifyDataSetChanged();
}
}
This is the xml code for the list item :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ivDelete"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="16dp"
android:background="#color/white"
android:clickable="true"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
app:srcCompat="#drawable/delete" />
<TextView
android:id="#+id/tvSubtaskName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3"
android:fontFamily="#font/roboto"
android:gravity="center_horizontal"
android:text="subtask_name"
android:textColor="#color/black"
android:textSize="15sp" />
<ImageView
android:id="#+id/ivPri"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_weight="1"
app:srcCompat="#drawable/priority_high" />
<ImageView
android:id="#+id/ivTime"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_weight="1"
app:srcCompat="#drawable/time_symbol_more" />
</LinearLayout>
You are trying to reference a LinearLayout as an ImageView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ivDelete"
...
ImageView ivDelete = convertView.findViewById(R.id.ivDelete);
Do you see the ids here?
Just replace this last line with the following:
LinearLayout ivDelete = convertView.findViewById(R.id.ivDelete);
Your issue is here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ivDelete"
and in your getView()
ImageView ivDelete = convertView.findViewById(R.id.ivDelete);
Use:
ImageView ivDelete = convertView.findViewById(R.id.imageView);
since in your layout:
<ImageView
android:id="#+id/imageView"
app:srcCompat="#drawable/delete"
yet another answer with fastest fix: replace id of your deleting ImageView, from:
ImageView ivDelete = convertView.findViewById(R.id.ivDelete);
to
ImageView ivDelete = convertView.findViewById(R.id.imageView);
btw. clean up your resources naming, currently your whole row have ivDelete id

Spinner's style is changed once i open it for the second

I am developing an app where I show a dropdownlist using a spinner.
I used a custom adapter, here is the code:
public class DemandeCongeAdapter extends BaseAdapter {
public static final String TAG = "DemandeCongeAdapter";
private final Context mContext;
private List<DemandeCongeType> mData;
protected LayoutInflater mInflater;
public DemandeCongeAdapter(Context mContext, List<DemandeCongeType> mData) {
this.mContext = mContext;
this.mData = mData;
this.mInflater = LayoutInflater.from(mContext);
}
static class ViewHolder {
public ViewHolder(View v) {
typedeDemandeName = (TextView) v.findViewById(R.id.type_demande_name);
}
protected final TextView typedeDemandeName;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public DemandeCongeType getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.layout_create_demande_conge_custom_spinner, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (position != 0) {
DemandeCongeType item = getItem(position);
String name = item.getTypdeDemandeName() == null ? "" : item.getTypdeDemandeName();
viewHolder.typedeDemandeName.setText(name);
}
return convertView;
}
}
And for the activity code, here is it:
private List<DemandeCongeType> congeTypes;
private DemandeCongeAdapter spinnerCongeTypeArrayAdapter;
congeTypes.addAll(new ArrayList<>((List<DemandeCongeType>) responseBody));
congeTypes.add(0, new DemandeCongeType());
spinnerCongeTypeArrayAdapter = new
DemandeCongeAdapter(DemandeCongeNewActivity.this, congeTypes);
congeTypeSpinner.setAdapter(spinnerCongeTypeArrayAdapter);
congeTypeSpinner.setSelection(Adapter.NO_SELECTION, false);
Everything works fine, but when I select the spinner for the first time, it shows the list correctly like this:
Once I click outside the spinner and I click for the second time on the spinner, I got it like this:
So I can't the find the problem, please if anyone can help me with that.
Here is the XML code:
<Spinner
android:id="#+id/spinner_type_conge"
fontPath="fonts/light.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#null"
android:dropDownWidth="match_parent"
android:ellipsize="end"
android:lines="1"
android:paddingRight="2dp"
android:spinnerMode="dropdown"
android:textSize="13sp" />
And for the spinner item xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/type_demande_name"
fontPath="fonts/light.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:lines="1"
android:padding="10dp"
android:textColor="#color/annuaire_hint_color"
android:textSize="13sp" />
Try This way i have use this in my Application.
XML Design Code.
Spinner set in your Activity
<Spinner
android:id="#+id/sp_pen_Category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:padding="#dimen/margin_10" />
This file XML File sp_list.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:padding="15dp"
android:singleLine="true"
android:textColor="#color/black"
android:textSize="#dimen/sub_title">
</CheckedTextView>
This file XML File sp_items.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#color/black">
</CheckedTextView>
This is Java code when you fill spinner.
ArrayAdapter<String> adp_sp = new ArrayAdapter<String>(Activity.this, R.layout.sp_items, arr_spinner);
adp_sp.setDropDownViewResource(R.layout.sp_list);
sp_pen_Category.setAdapter(adp_sp);

How to insert checkbox on custom listview

I am trying to add a checkbox in a customized listview but don't know how.
I have the following codes:
ListObject.java
package br.com.eduvm.xurrascalc;
public class ListObject {
private String texto;
private int iconeRid;
public ListObject() {
}
public ListObject(String texto, int iconeRid) {
this.texto = texto;
this.iconeRid = iconeRid;
}
public int getIconeRid() {
return iconeRid;
}
public void setIconeRid(int iconeRid) {
this.iconeRid = iconeRid;
}
public String getTexto() {
return texto;
}
public void setTexto(String texto) {
this.texto = texto;
}
}
ListAdapter.java
package br.com.eduvm.xurrascalc;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<ListObject> itens;
public ListAdapter(Context context, ArrayList<ListObject> itens) {
// Itens que preencheram o listview
this.itens = itens;
// responsavel por pegar o Layout do item.
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return itens.size();
}
public ListObject getItem(int position) {
return itens.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup parent) {
ListObject item = itens.get(position);
view = mInflater.inflate(R.layout.itens_lista, null);
((TextView) view.findViewById(R.id.text)).setText(item.getTexto());
((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());
return view;
}
}
List.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"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5sp" >
<ImageView
android:id="#+id/imagemview"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5sp"
android:gravity="center_vertical"
android:textColor="#FFF" />
</LinearLayout>
</LinearLayout>
How could I do to insert a checkbox in the items in this list?
This would be the layout for each individual row in your list
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/lstChkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstText"
android:textSize="30px"
android:layout_weight="1" />
<ImageView
android:id="#+id/listImage"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
You main list activity layout will look something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:choiceMode="multipleChoice"
/>
Your class which extends BaseAdapter getView method will somethings like this:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vHolder = null;
if (convertView != null)
vHolder = (ViewHolder) convertView.getTag(); // convertView is been recycled
else {
convertView = (View) mInflater.inflate(R.layout.list_item, null); // Set content of new View with list_item.xml
vHolder = new ViewHolder();
vHolder.checkBox = ((CheckBox) convertView.findViewById(R.id.lstChkbox)); // Getting pointers
vHolder.textView = ((TextView) convertView.findViewById(R.id.lstText));
vHolder.imageView = ((ImageView) convertView.findViewById(R.id.listImage));
vHolder.checkBox.setOnCheckedChangeListener(this); // Setting Listeners
convertView.setTag(vHolder);
}
vHolder.checkBox.setId(position); // This is part of the Adapter APi
vHolder.textView.setId(position); // Do not delete !!!
vHolder.imageView.setId(position);
if (mItems.get(position).getChecked()) { // Setting parameters for the View from our mItems list
vHolder.checkBox.setChecked(true);
} else {
vHolder.checkBox.setChecked(false);
}
vHolder.textView.setText(mItems.get(position).getText());
vHolder.imageView.setImageDrawable(mItems.get(position).getmImage());
return convertView;
}
public static class ViewHolder {
CheckBox checkBox;
TextView textView;
ImageView imageView;
}
/*
* Ok for this test but Toast are going to show every time the row comes into View
*/
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "Checked");
int position = buttonView.getId();
if (isChecked) {
mItems.get(position).setChecked(true);
Toast.makeText(context, mItems.get(position).getText(), Toast.LENGTH_LONG).show();
} else {
mItems.get(buttonView.getId()).setChecked(false);
}
}
Just add a checkbox to the layout itens_lista.xml.
Just adding a checkbox is easy. Do you want to do anything more to ti.
Add a CheckBox View to your custom row layout.

How can I set the texts at center of the cells of gridview of android application?

Hello dear programmers,
I am very new to android application development and for practice I tried to develope an application with following code
package com.android.practice;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class CalculatorAdapter extends BaseAdapter {
private Context mContext;
private String[] texts={"1","2","3","+","4","5","6","-","7","8","9","*",".","0","=","/"};
public CalculatorAdapter(Context context) {
mContext=context;
}
#Override
public int getCount() {
return texts.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) {
TextView tv;
if(convertView==null){
tv=new TextView(mContext);
tv.setLayoutParams(new GridView.LayoutParams(85,85));
}else{
tv=(TextView)convertView;
}
tv.setText(texts[position]);
tv.setTextSize(30);
return tv;
}
}
package com.android.practice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
public class MyCalculatorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv=(GridView)findViewById(R.id.gridView);
//gv.setGravity(0x11);
gv.setAdapter(new CalculatorAdapter(this));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:textStyle="bold"
android:textSize="30px"
android:gravity="center_horizontal"
android:background="#aa0000"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:clickable="true"
android:numColumns="4"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
/>
</LinearLayout>
My problem is though I have given the gridview gravity attribute as center, I am not getting the texts at the center of the cells of my gridview.
Please help me, If somebody finds where the problem lies.
Thanks in advance.
Use this
<TextView
android:gravity="center_vertical|center_horizontal" />
You can try infalting a custom layout in the getview section of the adaptor
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.tab_button, null);
}
LinearLayout ll=(LinearLayout) convertView.findViewById(R.id.linear_topbar);
TextView tv= (TextView) convertView.findViewById(R.id.title);
tv.setText(texts[position]);
tv.setTextSize(30);
return convertView;
}
this is the xml files
<TextView android:id="#+id/title" android:text="title"
android:layout_width="wrap_content" android:textColor="#fff"
android:textStyle="bold" android:textSize="30dp" android:layout_height="wrap_content"></TextView>

Categories

Resources