I have a trouble with my app. I want to make an app that has WebView
and will change the URL when I click one of the items on the navigation drawer. For example:
Facebook
Twitter
Github
and so on. But I couldn't implement the onClick
event. I am new in Android development. Thanks in advance.
This is my Java File
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolBar;
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolBar = (Toolbar) findViewById(R.id.nav_action_bar);
setSupportActionBar(mToolBar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.facebook.com/groups/276912206003690/");
webView.setWebViewClient(new WebViewClient());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
}
This my Main_activity.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tehedligmail.navigation_drawer.MainActivity"
android:id="@+id/drawer_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout = "@layout/navigation_action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView">
</WebView>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
app:headerLayout="@layout/navigation_header"
android:layout_gravity = "start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is my Menu file menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settings"
android:icon="@mipmap/ic_launcher"
android:title="Settings" />
<item
android:id="@+id/log_out"
android:title="Log out"
android:icon="@mipmap/ic_launcher"/>
<item
android:id="@+id/google"
android:title="google"
android:icon="@mipmap/ic_launcher"/>
</menu>
MainActivity
needs to implement NavigationView.OnNavigationItemSelectedListener
and override onNavigationItemSelected
NavigationView
(e.g. mNavigationView
) and bind it to your view.mNavigationView.setNavigationItemSelectedListener(this);
onNavigationItemSelected()
Here's how that method looks:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.settings) {
//do something
} else if (id == R.id.log_out){
//do something
} else if (id == R.id.google) {
//do something
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}