Saturday 21 November 2020

Android - How to check if any item has been clicked in Drawer Layout?

I am displaying all the files of a folder inside a DrawerLayout as a SubMenu. I am able to do it but I wanna know how to handle the click event if I click on any file inside DrawerLayout.

Here is the Code

public class HTMLEditor extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar;

TextView textViewprojectName;
String projectName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_html_editor);

    drawer = findViewById(R.id.drawer);
    navigationView = findViewById(R.id.navigation);
    toolbar = findViewById(R.id.toolbar);

    Intent intent = getIntent();
    projectName = intent.getStringExtra("projectName");

    navigationView.setNavigationItemSelectedListener(this);

    View view = navigationView.getHeaderView(0);
    textViewprojectName = view.findViewById(R.id.projectName);
    textViewprojectName.append(" " + projectName);


    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_open, R.string.navigation_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    File root = new File(Environment.getExternalStorageDirectory(),
            getString(R.string.app_name) + "/HTML Projects/" + projectName);
    File[] files = root.listFiles();
    Menu m = navigationView.getMenu();
    SubMenu subMenu = m.addSubMenu("All Files");
    if (files != null && files.length > 0) {
        for (File file : files) {
            subMenu.add(file.getName());
        }
    }
    getSupportFragmentManager().beginTransaction().replace(R.id.frame,
            new EditorFragment()).commit();

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
    // How to handle click event here.
    return false;
}

All the files are getting displayed as SubMenu. If I click on that file nothing happens. So, I want to get the name of that file (for further use) when I click on any SubMenu item.

So, How can I achieve that?



from Android - How to check if any item has been clicked in Drawer Layout?

No comments:

Post a Comment