Issue
I have an ImageButton on navigation_menu_header.xml
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="@drawable/signin"
android:textAlignment="center"
/>
And here is my main_activity class code for the button
public void signIn() {
ImageButton img = (ImageButton) getLayoutInflater().inflate(R.layout.navigation_menu_header,null);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, WebViewActivity.class));
}
});
}
The WebViewActivity works fine since I tried with a button that is on the mainactivity.xml but because I have the ImageButton on another xml, it does not open the webview.
Edit Added mainactivity.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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
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/navigation_menu_header">
</android.support.v4.widget.DrawerLayout>
Solution
Use this:
NavigationView navigationView=(NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
ImageButton img =(ImageButton)headerView.findViewById(R.id.signIn);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, WebViewActivity.class));
}
});
Answered By - Divyesh Patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.