lilott8
lilott8

Reputation: 1116

NavigationDrawer layout elements not clickable

I use the exact example from the android tutorial: http://developer.android.com/training/implementing-navigation/nav-drawer.html

to build a navigation drawer. In this activity, I want allow for a button that is clickable, which I have the code for. But when I go to click the button, nothing happens. I don't understand why this isn't working.

I've tried to inflate another view on top of the activity with no luck and I'm not quite sure what else to try to do get my button clickable!

My navigation drawer works great, it's just the button not being clickable that is problematic. As such, the code that is given is minimal:

the minimalist activity(HomeActivity.java)

public class HomeActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * The order must be maintained:
         * 1) setContext
         * 2) initActivityManager
         * 3-N) anything else
         */
        Singletons.ContextManager.setContext(getApplicationContext());
        Singletons.AppManager.initActivityManager();
        // This has to happen before the services start
        Singletons.Coordinator.setScreenState(true);
        // Inflate our layout
        setContentView(R.layout.activity_app_drawer);

        // Its always useful to keep this saved somewhere
        this.appContext = this;

        // Initialize our drawer
        this.title = "Home";
        // These two must always align
        this.fragmentTitles = new String[]{"Home", "View App", "View History", "View Location", "Manage Services"};
        this.fragmentClasses = new String[]{
                "edu.ucr.aum.fragments.HomeFragment",
                "edu.ucr.aum.fragments.AppStatsFragment",
                "edu.ucr.aum.fragments.BrowserStatsFragment",
                "edu.ucr.aum.fragments.LocationFragment",
                "edu.ucr.aum.fragments.ServicesFragment"};
        this.drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        this.drawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for our drawer
        this.drawerList.setAdapter(
                new ArrayAdapter<String>(
                        this,
                        R.layout.ad_text_view,
                        this.fragmentTitles
                )
        );
        // Set the click listener
        this.drawerList.setOnItemClickListener(new DrawerItemClickListener());

        this.drawerToggle = new ActionBarDrawerToggle(
                this,
                this.drawerLayout,
                R.drawable.ic_drawer,
                R.string.drawer_open,
                R.string.drawer_close
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(title);
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(title);
            }
        };

        this.drawerLayout.setDrawerListener(this.drawerToggle);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        /** Section for my button **/
        Button exitButton = (Button) findViewById(R.id.btnStartExitSurvey);
        long installDate = this.preferences.getLong("INSTALL_TIME", System.currentTimeMillis());

        TextView tv = (TextView) findViewById(R.id.tvExitSurveyInstructions);
        tv.setText(Html.fromHtml(getString(R.string.homeInstructions)));
        if(Singletons.Coordinator.displayExitSurvey(installDate)) {
            exitButton.setVisibility(View.VISIBLE);
            exitButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(appContext, SurveyActivity.class);
                    startActivity(i);
                }
            });
        } else {
            exitButton.setVisibility(View.INVISIBLE);
        }
    }
}

My xml file (activity_app_drawer.xml):

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="fill_parent"
    android:layout_width="wrap_content">
<TextView
    android:id="@+id/tvExitSurveyInstructions"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/homeInstructions"
    />

    <Button
        android:id="@+id/btnStartExitSurvey"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/homeButtonStartExitSurvey"
        android:layout_below="@+id/tvExitSurveyInstructions"
        />

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

</RelativeLayout>

Upvotes: 1

Views: 1535

Answers (1)

rolandl
rolandl

Reputation: 1887

Your layout seems to be wrong. The DrawerLayout should be the parent and content 2 or 3 children :

  • the navigation drawer left ;
  • the navigation drawer right ;
  • a container that can contain children - the FrameLayout in your layout.

So, your layout should be something like :

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
>
    <!-- The main content view -->
    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
    >
        <TextView
            android:id="@+id/tvExitSurveyInstructions"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/homeInstructions"
        />

        <Button
            android:id="@+id/btnStartExitSurvey"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/homeButtonStartExitSurvey"
            android:layout_below="@+id/tvExitSurveyInstructions"
        />
    </RelativeLayout>

    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"
    />
</android.support.v4.widget.DrawerLayout>

Upvotes: 3

Related Questions