Search code examples
javaandroidandroid-fragmentsstack-overflow

Why item on menu drawer can't click ? Android Navigation


I have a really interesting problem with my Navigator menu. I have no idea why... But I can click on any item from my menu, I don't want to say I click and nothing happened. I really want to say I can't click on any item, all my menu it's like a big image. I've try to make a new project witch already have Navigation Drawer Activity, of course it works.. but when I've try to copy that code and put on mine.. I have the same problem and vice versa, I've try to put my code into a new project with Navigation Drawer Activity, but again... I can't click on any item.

image - this trouble

image - can't click

this my code :

menu_bar.java

package com.database.m.lyburan;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.database.m.lyburan.adapter.EventAdapter;
import com.database.m.lyburan.app.AppController;
import com.database.m.lyburan.data.EventData;
import com.database.m.lyburan.util.Server;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.database.m.lyburan.Buddies.TAG_MESSAGE;
import static com.database.m.lyburan.Login.TAG_USERNAME;
import static com.database.m.lyburan.R.id.parent;

public class menu_bar extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener {

Toolbar toolbar;
DrawerLayout drawer;
NavigationView navigationView;
FragmentManager fragmentManager;
Fragment fragment = null;

ListView list;
SharedPreferences sharedpreferences;
SwipeRefreshLayout swipe;
List<EventData> eventList = new ArrayList<EventData>();

private static final String TAG = menu_bar.class.getSimpleName();

private static String url_list   = Server.URL + "event.php?offset=";

private int offSet = 0;

int no;

EventAdapter adapter;

public static final String TAG_NO       = "no";
public static final String TAG_ID       = "id";
public static final String TAG_JUDUL    = "judul";
public static final String TAG_TGL      = "tgl";
public static final String TAG_ISI      = "isi";
public static final String TAG_GAMBAR   = "gambar";

Handler handler;
Runnable runnable;

private Boolean isFabOpen = false;
private FloatingActionButton fab, fab1, fab2;
private CardView card1, card2;
private Animation fab_open, fab_close, rotate_forward, rotate_backward, card_open, card_close;

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



    sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layouts);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    if (savedInstanceState == null) {
        fragment = new Root();
        callFragment(fragment);
    }

    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    list = (ListView) findViewById(R.id.list_event);
    eventList.clear();

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent intent = new Intent(menu_bar.this, Buddies.class);
            intent.putExtra(TAG_ID, eventList.get(position).getId());
            startActivity(intent);
        }
    });

    adapter = new EventAdapter(menu_bar.this, eventList);
    list.setAdapter(adapter);

    swipe.setOnRefreshListener(this);

    swipe.post(new Runnable() {
                   @Override
                   public void run() {
                       swipe.setRefreshing(true);
                       eventList.clear();
                       adapter.notifyDataSetChanged();
                       callEvent(0);
                   }
               });

    list.setOnScrollListener(new AbsListView.OnScrollListener() {

        private int currentVisibleItemCount;
        private int currentScrollState;
        private int currentFirstVisibleItem;
        private int totalItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            this.currentScrollState = scrollState;
            this.isScrollCompleted();
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            this.currentFirstVisibleItem = firstVisibleItem;
            this.currentVisibleItemCount = visibleItemCount;
            this.totalItem = totalItemCount;
        }

        private void isScrollCompleted() {
            if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                    && this.currentScrollState == SCROLL_STATE_IDLE) {

                swipe.setRefreshing(true);
                handler = new Handler();

                runnable = new Runnable() {
                    public void run() {
                        callEvent(offSet);
                    }
                };

                handler.postDelayed(runnable, 3000);
            }
        }

    });

    Button meet_buddies = (Button) findViewById(R.id.meet_buddies);
    meet_buddies.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(menu_bar.this,Buddies.class);
            startActivity(intent);
        }
    });

    Button upcoming = (Button) findViewById(R.id.upcoming);
    upcoming.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(menu_bar.this,asd.class);
            startActivity(intent);
        }
    });


    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab1 = (FloatingActionButton) findViewById(R.id.fab1);
    fab2 = (FloatingActionButton) findViewById(R.id.fab2);
    card1 = (CardView) findViewById(R.id.card1);
    card2 = (CardView) findViewById(R.id.card2);

    /*kenalkan animasi*/
    fab_open = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
    fab_close = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close);
    rotate_forward = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_forward);
    rotate_backward = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_backward);
    card_open=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.card_open);
    card_close=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.card_close);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            animateFAB();
        }
    });

    fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(menu_bar.this,Addhome.class);
            startActivity(intent);
        }
    });

    fab2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(menu_bar.this, "New Call", Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@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_bar, menu);
    return true;
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_profile) {
        fragment = new Import();
        callFragment(fragment);
    } else if (id == R.id.nav_payment) {
        fragment = new Import();
        callFragment(fragment);
    } else if (id == R.id.nav_help) {
        fragment = new Import();
        callFragment(fragment);
    } else if (id == R.id.btn_logout) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layouts);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Toast.makeText(getApplicationContext(), "Action Settings", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void callFragment(Fragment fragment) {
    fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.frame_containers, fragment)
            .commit();
}

public void animateFAB() {
    /*jika fab dalam keadaan false*/
    if (isFabOpen) {
        fab.startAnimation(rotate_backward);
        fab1.startAnimation(fab_close);
        fab2.startAnimation(fab_close);
        card1.startAnimation(card_close);
        card2.startAnimation(card_close);
        fab1.setClickable(false);
        fab2.setClickable(false);
        isFabOpen = false;
    } else {
        /*jika dalam keadaan true*/
        fab.startAnimation(rotate_forward);
        fab1.startAnimation(fab_open);
        fab2.startAnimation(fab_open);
        card1.startAnimation(card_open);
        card2.startAnimation(card_open);
        fab1.setClickable(true);
        fab2.setClickable(true);
        isFabOpen = true;
    }
}

@Override
public void onRefresh() {
    eventList.clear();
    adapter.notifyDataSetChanged();
    callEvent(0);
}

private void callEvent(int page){

    swipe.setRefreshing(true);

    JsonArrayRequest arrReq = new JsonArrayRequest(url_list + page,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    if (response.length() > 0) {
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                EventData news = new EventData();

                                no = obj.getInt(TAG_NO);

                                news.setId(obj.getString(TAG_ID));
                                news.setJudul(obj.getString(TAG_JUDUL));

                                if (!Objects.equals(obj.getString(TAG_GAMBAR), "")) {
                                    news.setGambar(obj.getString(TAG_GAMBAR));
                                }

                                news.setDatetime(obj.getString(TAG_TGL));
                                news.setIsi(obj.getString(TAG_ISI));

                                // adding news to news array
                                eventList.add(news);

                                if (no > offSet)
                                    offSet = no;

                                Log.d(TAG, "offSet " + offSet);

                            } catch (JSONException e) {
                                Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                            }

                            adapter.notifyDataSetChanged();
                        }
                    }
                    swipe.setRefreshing(false);
                }

            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            swipe.setRefreshing(false);
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(arrReq);
}
}

activity_menu_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layouts"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_menu_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_menu_bar"
        app:menu="@menu/activity_menu_bar_drawer" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">


        <Button
            android:id="@+id/meet_buddies"
            android:layout_width="fill_parent"
            android:drawableLeft="@drawable/ic_profil_w"
            android:background="@color/ijo"
            android:textColor="@android:color/background_light"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:onClick="meet"
            android:layout_marginTop="70dp"
            android:text="Meet Buddies"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:id="@+id/upcoming"
            android:layout_width="170dp"
            android:background="@color/white"
            android:textColor="@color/ijo"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:text="Notification"
            android:layout_marginTop="135dp" />

        <Button
            android:id="@+id/myshared"
            android:layout_width="190dp"
            android:background="@color/white"
            android:textColor="@color/ijo"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:layout_marginLeft="190dp"
            android:layout_marginTop="135dp"
            android:text="My Shared"
             />



    </RelativeLayout>
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">



        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:stateListAnimator="@null"
            android:src="@drawable/ic_plus_w"
            app:backgroundTint="@color/colorAccent"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="130dp"
            android:stateListAnimator="@null"
            android:layout_marginRight="@dimen/fab_margin"
            android:visibility="invisible"
            android:src="@drawable/ic_menu_camera"
            app:backgroundTint="#ffffff"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp" />

        <android.support.v7.widget.CardView
            android:id="@+id/card2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="140dp"
            android:layout_marginRight="75dp"
            android:visibility="invisible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="Add Event" />
        </android.support.v7.widget.CardView>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="80dp"
            android:layout_marginRight="@dimen/fab_margin"
            android:src="@drawable/ic_beranda"
            android:visibility="invisible"
            app:backgroundTint="#ffffff"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp" />

        <android.support.v7.widget.CardView
            android:id="@+id/card1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="90dp"
            android:layout_marginRight="70dp"
            android:visibility="invisible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="Add Homestay" />
        </android.support.v7.widget.CardView>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Event"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="198dp" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_marginTop="222dp"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/list_event"
            android:layout_width="wrap_content"
            android:layout_height="330dp"
            android:divider="@null"
            android:dividerHeight="2dp"
            android:layout_marginTop="222dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

    </android.support.v4.widget.SwipeRefreshLayout>

    </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>

</android.support.v4.widget.DrawerLayout>

i've tried, how can i fix it ? anyone please help me. thanks.


Solution

  • Anyone else having similar problem. It appears the order in the XML matters too.

    I had my NavigationView and after it I had a layout included and I couldn't click an item on the NavigationView.

    After I switched them, so that my layout was being included AND THEN AFTER IT I had my NavigationView, it worked ...