[Android Studio] ksrea:
First of all, new to coding for android studio, java..

70 year old guy trying to learn new tricks.

I have in the past only coded in Microsoft Access and Visual Studio C#.

I hope I have explained this ok.

thanks fo your help.

Kevin Rea

I want to have one of the menu items such as Gallery or Slideshow, when you click on it, I want it to show another activity i have called : viewcontents_layout.xml and it's associated file: ViewListContents.java

this viewcontents_layout.xml is a page with a ListView on it.

I want this to show when someone clicks on either the Gallery or Slideshow menu item.

code for MainActivity2.java:

```

package com.example.sqliteapp;

import android.os.Bundle;

import android.view.View;

import android.view.Menu;

import com.google.android.material.snackbar.Snackbar;

import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;

import androidx.navigation.Navigation;

import androidx.navigation.ui.AppBarConfiguration;

import androidx.navigation.ui.NavigationUI;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sqliteapp.databinding.ActivityMain2Binding;

public class MainActivity2 extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;

private ActivityMain2Binding binding;

@override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

binding = ActivityMain2Binding.inflate(getLayoutInflater());

setContentView(binding.getRoot());

setSupportActionBar(binding.appBarMain.toolbar);

binding.appBarMain.fab.setOnClickListener(new View.OnClickListener() {

@override

public void onClick(View view) {

Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

.setAction("Action", null).show();

}

});

DrawerLayout drawer = binding.drawerLayout;

NavigationView navigationView = binding.navView;

// Passing each menu ID as a set of Ids because each

// menu should be considered as top level destinations.

mAppBarConfiguration = new AppBarConfiguration.Builder(

R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_contacts)

.setOpenableLayout(drawer)

.build();

NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);

NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);

NavigationUI.setupWithNavController(navigationView, navController);

}

@override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main_activity2, menu);

return true;

}

@override

public boolean onSupportNavigateUp() {

NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);

return NavigationUI.navigateUp(navController, mAppBarConfiguration)

|| super.onSupportNavigateUp();

}

}

```

CODE FOR activity_main2.xml

```

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.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

android:id="@+id/app_bar_main"

layout="@Layout/app_bar_main"

android:layout_width="match_parent"

android:layout_height="match_parent" />

<com.google.android.material.navigation.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/nav_header_main"

app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

```

CODE FOR GALLERYFRAGMENT.JAVA

```

package com.example.sqliteapp.ui.gallery;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.fragment.app.Fragment;

import androidx.lifecycle.ViewModelProvider;

import com.example.sqliteapp.databinding.FragmentGalleryBinding;

public class GalleryFragment extends Fragment {

private FragmentGalleryBinding binding;

public View onCreateView(@NonNull LayoutInflater inflater,

ViewGroup container, Bundle savedInstanceState) {

GalleryViewModel galleryViewModel =

new ViewModelProvider(this).get(GalleryViewModel.class);

binding = FragmentGalleryBinding.inflate(inflater, container, false);

View root = binding.getRoot();

final TextView textView = binding.textGallery;

galleryViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

return root;

}

@override

public void onDestroyView() {

super.onDestroyView();

binding = null;

}

}

```

CODE FOR fragment_gallery.xml

```

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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=".ui.gallery.GalleryFragment">

<TextView

android:id="@+id/text_gallery"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginStart="8dp"

android:layout_marginTop="8dp"

android:layout_marginEnd="8dp"

android:textAlignment="center"

android:textSize="20sp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

```

CODE FOR EXISITING ACTIVITY WITH THE LISTVIEW ON IT IS CALLED: ViewListContents.java

```

package com.example.sqliteapp;

import android.content.Intent;

import android.database.Cursor;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

import java.util.List;

public class ViewListContents extends AppCompatActivity {

DatabaseHelper myDb;

// @override

protected void onCreate(@nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.viewcontents_layout);

ListView listView = (ListView) findViewById(R.id.ListView);

myDb = new DatabaseHelper(this);

ArrayList<String> theList = new ArrayList<>();

Cursor data = myDb.getListContents();

if(data.getCount() == 0) {

Toast.makeText(ViewListContents.this, "No Records in Database :(.", Toast.LENGTH_LONG).show();

}else{

while(data.moveToNext()) {

theList.add(data.getString(1));

ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);

listView.setAdapter(listAdapter);

}

}

}

public void previous(View view) {

startActivity(new Intent(ViewListContents.this,MainActivity.class));

}

}

```

CODE FOR XML FOR EXISTING LISTVIEW IS: viewcontents_layout.xml

```

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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">

<ListView

android:id="@+id/ListView"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_marginStart="1dp"

android:layout_marginTop="1dp"

android:layout_marginEnd="1dp"

android:layout_marginBottom="120dp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="1.0" />

<Button

android:id="@+id/btnBack"

android:onClick="previous"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="24dp"

android:text="Previous"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/ListView" />

</androidx.constraintlayout.widget.ConstraintLayout>

```

I have tried looking at every youtube video I could find searching under

"android studio navigation views drawer activity use existing activity"

and everything close...

but i cannot figure it out..

i tried searching all over you tube for android studio navigation views drawer activity use existing activity

no luck so far