Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main_graph.xml
- <?xml version="1.0" encoding="utf-8"?>
- <navigation 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/main_graph"
- app:startDestination="@id/blankFragment">
- <fragment
- android:id="@+id/blankFragment"
- android:name="com.example.navigationcomponent.BlankFragment"
- android:label="blank_fragment"
- tools:layout="@layout/blank_fragment" />
- <fragment
- android:id="@+id/threeFragment"
- android:name="com.example.navigationcomponent.ThreeFragment"
- android:label="three_fragment"
- tools:layout="@layout/three_fragment" />
- <fragment
- android:id="@+id/newFragment"
- android:name="com.example.navigationcomponent.NewFragment"
- android:label="new_fragment"
- tools:layout="@layout/new_fragment" />
- </navigation>
- activity_main.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=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="116dp"
- android:text="actBtn1"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="actBtn2"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toStartOf="@+id/button"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <androidx.fragment.app.FragmentContainerView
- android:id="@+id/nav_host_fragment"
- android:name="androidx.navigation.fragment.NavHostFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:defaultNavHost="true"
- app:navGraph="@navigation/main_graph" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java
- public class MainActivity extends AppCompatActivity {
- NavController navController;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- navController = Navigation.findNavController(this, R.id.nav_host_fragment);
- Button button1 = findViewById(R.id.button);
- Button button2 = findViewById(R.id.button2);
- }
- }
Add Comment
Please, Sign In to add comment