Reputation: 1315
My OnClickListener
is never triggered, I have tried to create listener for this button only, then as it is now implemented OnClickListener
. initToolbarAndDrawerWithReadableName
method is invoked in child class as this.initToolbarAndDrawerWithReadableName
. What i did wrong?
Toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/end"
android:gravity="center|end"
android:id="@+id/auth_toolbar"
android:theme="@style/AppTheme">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageButton
android:layout_width="35dp"
android:layout_height="26dp"
android:id="@+id/drawer_button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/menu_active"
android:scaleType="fitXY"
android:clickable="true"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Init toolbar
protected void initToolbarAndDrawerWithReadableName(String title) {
toolbarTv = (TextView)findViewById(R.id.toolbarTv);
toolbarTv.setText(title);
toolbarTv.setOnClickListener(this);
View child = (View)getLayoutInflater().inflate(R.layout.drawer_header, null);
nvDrawer = (NavigationView) findViewById(R.id.nvView);
nvDrawer.addHeaderView(child);
mToolbar = (Toolbar) findViewById(R.id.auth_toolbar);
setSupportActionBar(mToolbar);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
setupDrawerContent(nvDrawer);
imageButton = (ImageButton) mToolbar.findViewById(R.id.drawer_button);
imageButton.setOnClickListener(this);
}
OnClick:
@Override
public void onClick(View v) {
Log.d("click","i am here");
switch (v.getId()){
case R.id.drawer_button:{
mDrawer.openDrawer(GravityCompat.START);
break;
}
case R.id.toolbarTv:{
mDrawer.openDrawer(GravityCompat.START);
break;
}
}
}
Upvotes: 0
Views: 122
Reputation:
Add this line of cod to your xml. It will call the onClick method you made every time the button is pressed
android:onClick="onClick"
try making your xml look like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/end"
android:gravity="center|end"
android:id="@+id/auth_toolbar"
android:theme="@style/AppTheme">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageButton
android:layout_width="35dp"
android:layout_height="26dp"
android:id="@+id/drawer_button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/menu_active"
android:onClick="onClick"
android:scaleType="fitXY"
android:clickable="true"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
make sure you onClick class is in the activity, but not in any other activities. ex
main activity{
create(){}
other(){}
onClick(){}
otherOther(){}
}
also, comment out the method that declares the listener for now and see if it works, it works for me
Upvotes: 0
Reputation: 163
use Butter Knife Link Tutorial and GitHub Simple,
1) add this to your dependencies
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
2) add this to your dependencies in build.gradle
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
befor
apply plugin: 'android-apt'
add
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
3) add this in your Activity
@BindView(R.id.drawer_button) ImageButton drawer_button;
4) then add
@OnClick(R.id.drawer_button)
public void openDrawer() {
mDrawer.openDrawer(GravityCompat.START);
}
Upvotes: 0
Reputation: 387
Looking at your xml file you have only define ImageButton, No TextView for toolbarTv and also no click method which is defined for switch statement.
Try
<TextView
android:id="@+id/toolbarTv"
android:layout_width="35dp"
android:layout_height="26dp"
android:id="@+id/toolbarTv"
android:onClick="onClick"
android:clickable="true"/>
Upvotes: 1
Reputation: 2403
Instead of setting click listener to ImageButton, add it to parent RelativeLayout because here parent is intercepting the click event.
Upvotes: 0