0

Sorry for my poor English. My purpose now is, displaying one editText, and gridView text in the Snackbar. This Snackbar display when user click FAB, than he can write something in editText. After that, user should click some text generated by GridViewAdapter displayed in gridView. My code success displaying editView and some static button here the code, but it's fail to view the gridView. Here my last code, please help me find the solution

    //grid view properties
GridView gridView;
List<String> ItemsList;
String selectedItem;
TextView GridViewItems,BackSelectedItem;
int backposition = -1;
String[] itemsName = new String[]{
        "Fisika",
        "Kimia",
        "Biologi",
        "Matematika",
};
    private void setupUI() {
    btnFab = (FloatingActionButton) findViewById(R.id.btnFloatingAction);
    btnFab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);

            final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
            Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();

            // Inflate your custom view with an Edit Text
            LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
            // custom_snac_layout is your custom xml

            final EditText txt_event = (EditText) snackView.findViewById(R.id.event_text);
            gridView = (GridView)findViewById(R.id.grid_view);

            ItemsList = new ArrayList<String>(Arrays.asList(itemsName));

            gridView.setAdapter(new GridViewAdapter(ParentActivity.this));

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    // TODO Auto-generated method stub

                    selectedItem = parent.getItemAtPosition(position).toString();

                    GridViewItems = (TextView) view;

                    GridViewItems.setBackgroundColor(Color.parseColor("#814f00"));

                    GridViewItems.setTextColor(Color.parseColor("#fdfcfa"));

                    BackSelectedItem = (TextView) gridView.getChildAt(backposition);

                    if (backposition != -1)
                    {

                        BackSelectedItem.setSelected(false);

                        BackSelectedItem.setBackgroundColor(Color.parseColor("#fbdcbb"));

                        BackSelectedItem.setTextColor(Color.parseColor("#040404"));
                    }

                    backposition = position;

                    String event =  txt_event.getText().toString();
                    Toast.makeText(ParentActivity.this, "Item selected "+ selectedItem +"Text "+event, Toast.LENGTH_LONG).show();

                }
            });

            layout.addView(snackView, 0);
            snackbar.show();

        }
    });

}
public class GridViewAdapter extends BaseAdapter {
    Context context;

    public GridViewAdapter(Context context)
    {
        this.context = context;
    }

    @Override
    public int getCount() {

        return itemsName.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub

        return itemsName[position];
    }

    @Override
    public long getItemId(int position) {

        // TODO Auto-generated method stub

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        TextView text = new TextView(this.context);

        text.setText(itemsName[position]);

        text.setGravity(Gravity.CENTER);

        text.setBackgroundColor(Color.parseColor("#fbdcbb"));

        text.setTextColor(Color.parseColor("#040404"));

        text.setLayoutParams(new GridView.LayoutParams(144, 144));

        return text;

    }

}

and here my custom_snac_layout.xml code

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_alignParentTop="true"
    android:id="@+id/event_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollHorizontally="false"
    android:hint="@string/type_your_message"
    android:inputType="textNoSuggestions"
    android:maxLines="4"
    android:singleLine="false"
    android:textSize="18sp"
    android:paddingLeft="4dp" />

<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/event_text"
    android:layout_centerHorizontal="true"
    android:numColumns="3"/>

These code report "NullPointerException" on the logcat

Community
  • 1
  • 1
fajarra
  • 11
  • 3

1 Answers1

0

Try this,

   gridView = (GridView)snackView.findViewById(R.id.grid_view);
Komal12
  • 3,340
  • 4
  • 16
  • 25