0
I have a project that has a Gridview with several cells and I need the cells that make up Gridview to fill the entire useful area of the Smartphone screen. With the following XML code it did not work:
<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:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e4c5c5">
<GridView
android:id="@+id/gridView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:numColumns="7" />
</LinearLayout>
</LinearLayout>
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class CelulasAdapter extends BaseAdapter {
private Context context;
private List<DiasMesApoio> arrayListDiasMes;
private int width;
private int height;
// Construtor:
public CelulasAdapter(Context context, List<DiasMesApoio> arrayListDiasMes, int width, int height) {
this.context = context;
this.arrayListDiasMes = arrayListDiasMes;
this.width = width;
this.height = height;
}
@Override
public int getCount() {
return arrayListDiasMes.size();
}
@Override
public Object getItem(int position) {
return arrayListDiasMes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View viewLayout;
ViewHolder viewholder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewLayout = inflater.inflate(R.layout.layout_celulas_gridview, null);
viewholder = new ViewHolder();
viewholder.textView1 = (TextView) viewLayout.findViewById(R.id.textView1);
viewLayout.setTag(viewholder);
convertView = viewLayout;
} else {
viewholder = (ViewHolder) convertView.getTag();
viewLayout = convertView;
}
viewLayout.setMinimumHeight(GridViewActivity.height/6);
DiasMesApoio diasMesApoio = arrayListDiasMes.get(position);
viewholder.textView1.setText(String.valueOf(diasMesApoio.getDia()));
return viewLayout;
}
private class ViewHolder {
TextView textView1;
}
}
Are you using a custom Adapter? Or how are you adding the items.
– Luiz
I am using an Adapter class to build Gridview items: gridView.setAdapter(new Celulasadapter(Mainactivity.this, arrayList));
– Vitor Mendanha
I test here with your XML, and if you add the items dynamically without using a custom Adapter, everything works as expected, occupies the entire screen. Have you tried so, to test? Maybe it’s some problem with the size of your.
– Luiz
Meu layout para os itens da GridView: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <TextView
 android:layout_width="match_parent" android:layout_height="match_parent" android:text="Dia" android:id="@+id/textView1" android:background="#fefdba" /> </Linearlayout>
– Vitor Mendanha
Could you show me the code you used to add the items dynamically without Adapter?
– Vitor Mendanha
It was not without Adapter, it was without using a custom Adapter, as is your case. I did so:
ArrayAdapter<Integer> adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line);
 for (int i = 0; i < 42; i++) {
 adapter.add(i);
 }

 ((GridView)findViewById(R.id.gridView2)).setAdapter(adapter);
– Luiz
I will post an answer, you try to do to see what it gives. Using your Custom Adapter.
– Luiz
Try this solution: http://stackoverflow.com/a/11049803/4744158
– Luiz
I tried the solution you sent me, but in my Adapter Class I cannot import the Gridviewactivity Class? Could you help me?
– Vitor Mendanha
The Gridviewactivity class is actually your acvitivy, where you arrow the Adapter. You should recover the screen size there, put it in a static variable and publish, before setting the Adapter and then, on your Adapter, you recover that value you got on your Activity.
– Luiz
Right. But the "view lines = Inflater.inflate(R.layout.item, null);" and "view.setMinimumHeight(Gridviewactivity.height/6);" has to be inserted into the Adapter Class and my Adapter class is a new file?
– Vitor Mendanha
Exactly, if you don’t know how to do, edit your answer and add Adapter.
– Luiz
Luiz, I added my Adapter Class above in the question and in the line "viewLayout.setMinimumHeight(Gridviewactivity.height/6);" I cannot import the Gridviewactivity Class...
– Vitor Mendanha
As I said, Gridviewactivity you have to replace by the name of your acvitivy, where you arrow the Adapter.
– Luiz
OK. I hadn’t noticed. It worked more or less, IE, created a huge horizontal spacing between the items but occupied the entire screen... I’ll try to find a way to make the layout more beautiful...
– Vitor Mendanha
Luiz, I added a new image to show how the Gridview items are now. I can’t find a way to put the size of the horizontal spacing of all items equal, IE, to be equal to your vertical spacing! Can you help me?
– Vitor Mendanha