funkycookie
funkycookie

Reputation: 226

Adding more elements to a GridView using adapters in Android

This is the desired output:

https://i.sstatic.net/H3xgw.jpg

So basically, I'm using a GridView to generate all those 18 buttons and I am trying to add some TextViews on top of every Button, and 2 on the bottom side of the last buttons.

This is my Adapter implementation:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by pop on 9/15/2014.
 */
public class CustomGridAdapter extends BaseAdapter {

    private Context _context;
    private String[] _items;
    LayoutInflater _inflater;

    public CustomGridAdapter(Context context, String[] items) {
        this._context = context;
        this._items = items;
        _inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return _items.length;
    }

    @Override
    public Object getItem(int position) {
        return _items[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = _inflater.inflate(R.layout.screen_gridcell_time, null);
        }
        Button button = (Button) convertView.findViewById(R.id.calendar_day_gridcell);
        button.setText(_items[position]);

        return convertView;
    }
}

My gridcell:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/transparent"
    android:padding="4dp">

    <ToggleButton
        android:id="@+id/calendar_day_gridcell"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/wampWhite"
        android:background="@drawable/time_input_button_selector"
        android:layout_marginLeft="2dp"
        android:textOff=""
        android:textOn="\u2713"
        />

</RelativeLayout>

And the Java file using these two:

public class TimeActivity extends Activity  {

    private GridView gridView;
    private final String[] items = new String[] {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time);
        setFonts();

        gridView = (GridView) this.findViewById(R.id.timeInputView);
        CustomGridAdapter adapter = new CustomGridAdapter(TimeActivity.this, items);
        gridView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.time, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setFonts() {
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Medium.ttf");
        TextView dateOutput = (TextView) findViewById(R.id.dateOutput);
        dateOutput.setTypeface(tf);
    }
}

I tried adding in the gridcell a TextView, and afterwards giving it android:layout_above(id of buttons) but it was just inside the button, on the top side of it. Any suggestions? Cheers!

Upvotes: 0

Views: 322

Answers (2)

funkycookie
funkycookie

Reputation: 226

@bwegs - It works, thanks a lot! The only problem right now is that I have 18 buttons and need 21 TextViews.

As I mentioned above, each button has a textView on top of it, but the last buttons from the bottom, have 3 textViews below the buttons. Due to my implementation, I'm not really sure how will I add those 3 textViews...

Upvotes: 0

bwegs
bwegs

Reputation: 3767

This is one way to get your RelativeLayout to look like you want:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/transparent"
android:padding="4dp">

<TextView
    android:id="@+id/gridcell_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="6:00" />

<ToggleButton
    android:id="@+id/calendar_day_gridcell"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_below="@id/gridcell_time"
    android:layout_gravity="center"
    android:textColor="@color/wampWhite"
    android:background="@drawable/time_input_button_selector"
    android:layout_marginLeft="2dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textOff=""
    android:textOn="\u2713" />

</RelativeLayout>

All I've done is added the TextView before the ToggleButton and set the TextView's layout_centerHorizontal property to true. I also changed the below property of the ToggleButton to the id of the TextView (in this case I called it gridcell_time).

Upvotes: 1

Related Questions