Multiple TextView selection in GridView

378 views Asked by At

I managed to create a grid of TextView with preview of the chosen number. As you can see from the code , pressing on a number comes out a simple toast. I would need to do a multiple selection of numbers and maybe create an array to postpone them to another activity. You can help me?

XML:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/zaffiro">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center" >

        <GridView
            tools:context=".MainActivity"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:columnWidth="10dp"
            android:numColumns="6"
            android:verticalSpacing="2dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:id="@+id/gridview" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"
        >

        <Button
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="Importo"
            android:textColor="#fff"
            android:background="@drawable/gioca_pressed"/>

        <Button
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="Annulla"
            android:textColor="#fff"
            android:background="@drawable/annulla_pressed"/>

    </LinearLayout>

</LinearLayout>

NUMBERADAPTER:

    package kangel.customtest;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.view.GravityCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Andrea on 22/06/2015.
 */
public class NumberAdapter extends BaseAdapter {

    private Context context;

    public NumberAdapter(Context context){
        this.context = context;
    }
    @Override
    public int getCount() {
        return 60;
    }

    @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 numberView;

        if (convertView == null){

            numberView = new TextView(context);
            numberView.setLayoutParams(new GridView.LayoutParams(110, 110));
            numberView.setPadding(0, 0, 0, 0);
            numberView.setTextColor(Color.WHITE);
            numberView.setBackgroundResource(R.drawable.rounded_edittext_zaffiro);
            numberView.setGravity(Gravity.CENTER);
            numberView.setTextSize(25);

        }
        else {
            numberView = (TextView) convertView;
        }

        numberView.setText(""+(position+1));
        return numberView;

    }
}

MAINACTIVITY:

    package kangel.customtest;

import android.content.Context;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
    GridView gridView;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(new NumberAdapter(this));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                LayoutInflater inflater = getLayoutInflater();
                final View layout = inflater.inflate(R.layout.custom_toast_layout,
                        (ViewGroup) findViewById(R.id.custom_toast_layout));
                TextView selectedNumber = (TextView) layout.findViewById(R.id.selected_number);
                selectedNumber.setText(""+(position+1));

                Context context = getBaseContext();
                String text = ""+(position+1);
                int duration = Toast.LENGTH_SHORT;
                final Toast toast = Toast.makeText(context, text, duration);
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setView(layout);
                toast.show();
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        toast.cancel();
                    }
                }, 1000);
            }
        });
    }

    @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);
    }
}
0

There are 0 answers