I have a Base Activity which sets up Navigation drawer. My Main activity extends this BaseActivtity
. The navigation drawer works perfectly fine but when I add any buttons on Main Activity, the onClick
of the button doesn't get called.
Here is the Base Activity.
private void addDrawerItems(){
mArrayAdapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, getResources().getStringArray(R.array.menu_list));
mDrawerList.setAdapter(mArrayAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
protected void onCreateDrawer() {
setContentView(R.layout.activity_base);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarBase);
setSupportActionBar(toolbar);
mActivityTitle = getTitle().toString();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_drawer);
addDrawerItems();
setUpDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView parent, View view, int position, long id){
selectItem(position);
Intent intent;
if (position==getResources().getInteger(R.integer.uploadPos)){
intent = new Intent(getApplicationContext(), UploadActivity.class);
}
else if (position == getResources().getInteger(R.integer.homePos)) {
intent = new Intent(getApplicationContext(), MainActivity.class);
}
else if (position == getResources().getInteger(R.integer.browsePos)){
intent = new Intent(getApplicationContext(), BrowseActivity.class);
}
else if (position == getResources().getInteger(R.integer.myUploadsPos)){
intent = new Intent(getApplicationContext(), MyUploads.class);
}
else if (position == getResources().getInteger(R.integer.logoutPos)){
// destroying user session in the shared preferences table
sharedPreferences = getSharedPreferences(getResources().getString(R.string.preferences_table_name), 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("UserID");
editor.commit();
intent = new Intent(getApplicationContext(), LoginActivity.class);
}
else {
intent = new Intent(getApplicationContext(), LoginActivity.class);
}
startActivity(intent);
}
}
The MainActivity class
public class MainActivity extends BaseActivity{
private String LOG_TAG = "Main Activity";
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
checkForLogin();
Button button = (Button) findViewById(R.id.test_button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "on click test button");
Toast toast = Toast.makeText(v.getContext(), "test toast", Toast.LENGTH_SHORT);
toast.show();
}
});
button.bringToFront();
super.onCreateDrawer();
}
I can see the button but, on clicking it, the toast doesn't show up when I click it. If instead of extending BaseActivity
, I extend AppCompatActivity
(and remove super.onCreateDrawer
), the toast works.
I tried bringing the button to the front with button.bringToFront()
, that didn't work either.
The NavigationDrawer
somehow is interfering with the clicks on MainActivity
and I can't figure it out.
Any suggestions would be appreciated
activity_base.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarBase"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 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/list_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>
<include layout="@layout/content_base" />
content_base.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.nikhil.lecturenotessharing.BaseActivity"
tools:showIn="@layout/activity_base">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:id="@+id/test_button"
/>
Please ask if more information or any XML layouts are needed.
Try to remove setContentView(R.layout.activity_base);
from onCreateDrawer()
, it will works.
Because what you've initialized in onCreate()
of MainActivity.class
will be removed by calling setContentView(R.layout.activity_base);
in onCreateDrawer()
of BaseActivity.class
.
Keep in touch @nikhil vavs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.