My main java code :
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.shrikanthravi.customnavigationdrawer2.data.MenuItem;
import com.shrikanthravi.customnavigationdrawer2.widget.SNavigationDrawer;
import java.util.ArrayList;
import java.util.List;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
SNavigationDrawer sNavigationDrawer;
Class fragmentClass;
public static Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sNavigationDrawer = findViewById(R.id.navigationDrawer);
// Menu add
List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(new MenuItem("Home",R.drawable.ic_home));
menuItems.add(new MenuItem("About us",R.drawable.ic_about));
menuItems.add(new MenuItem("Subscribe Us",R.drawable.ic_subscribe));
menuItems.add(new MenuItem("Follow Us",R.drawable.ic_follow));
menuItems.add(new MenuItem("Rate Us",R.drawable.ic_rate));
sNavigationDrawer.setMenuItemList(menuItems);
fragmentClass = HomeFragment.class;
try {
fragment = (Fragment)fragmentClass.newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
if (fragment!= null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out).replace(R.id.frameLayout, fragment).commit();
}
sNavigationDrawer.setDrawerListener(new NavigationDrawer.DrawerListener() {
@Override
public void onDrawerOpening() {
}
@Override
public void onDrawerClosing() {
try {
fragment = (Fragment)fragmentClass.newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
if (fragment!= null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out).replace(R.id.frameLayout, fragment).commit();
}
}
@Override
public void onDrawerOpened() {
}
@Override
public void onDrawerClosed() {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
}
My homefragment code :
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class HomeFragment extends Fragment
{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,container,false);
return view;
}
}
This code uses fragments. I want to open an URL (example www.google.com ) from click on navigation drawer item. Please help, I am new on this android development.
When a navigation item is clicked, I do not want this fragment page to open, instead I want to open an URL.
Add this inside your listener:
if(fragment instanceof HomeFragment)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com")); startActivity(browserIntent);
}
EDIT::
I found the library code that you're using. It has a onClickListener. Try this :
sNavigationDrawer.setOnMenuItemClickListener(new SNavigationDrawer.OnMenuItemClickListener() {
@Override
public void onMenuItemClicked(int position) {
if(position==0){
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
}
}
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.