Guest User

Untitled

a guest
Nov 3rd, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1.  
  2. main_graph.xml
  3.  
  4. <?xml version="1.0" encoding="utf-8"?>
  5. <navigation xmlns:android="http://schemas.android.com/apk/res/android"
  6. xmlns:app="http://schemas.android.com/apk/res-auto"
  7. xmlns:tools="http://schemas.android.com/tools"
  8. android:id="@+id/main_graph"
  9. app:startDestination="@id/blankFragment">
  10.  
  11. <fragment
  12. android:id="@+id/blankFragment"
  13. android:name="com.example.navigationcomponent.BlankFragment"
  14. android:label="blank_fragment"
  15. tools:layout="@layout/blank_fragment" />
  16. <fragment
  17. android:id="@+id/threeFragment"
  18. android:name="com.example.navigationcomponent.ThreeFragment"
  19. android:label="three_fragment"
  20. tools:layout="@layout/three_fragment" />
  21. <fragment
  22. android:id="@+id/newFragment"
  23. android:name="com.example.navigationcomponent.NewFragment"
  24. android:label="new_fragment"
  25. tools:layout="@layout/new_fragment" />
  26. </navigation>
  27.  
  28.  
  29.  
  30.  
  31. activity_main.xml
  32.  
  33.  
  34. <?xml version="1.0" encoding="utf-8"?>
  35. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  36. xmlns:app="http://schemas.android.com/apk/res-auto"
  37. xmlns:tools="http://schemas.android.com/tools"
  38. android:layout_width="match_parent"
  39. android:layout_height="match_parent"
  40. tools:context=".MainActivity">
  41.  
  42. <TextView
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="Hello World!"
  46. app:layout_constraintLeft_toLeftOf="parent"
  47. app:layout_constraintRight_toRightOf="parent"
  48. app:layout_constraintTop_toTopOf="parent" />
  49.  
  50. <Button
  51. android:id="@+id/button"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:layout_marginEnd="116dp"
  55. android:text="actBtn1"
  56. app:layout_constraintBottom_toBottomOf="parent"
  57. app:layout_constraintEnd_toEndOf="parent"
  58. app:layout_constraintTop_toTopOf="parent" />
  59.  
  60. <Button
  61. android:id="@+id/button2"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:text="actBtn2"
  65. app:layout_constraintBottom_toBottomOf="parent"
  66. app:layout_constraintEnd_toStartOf="@+id/button"
  67. app:layout_constraintStart_toStartOf="parent"
  68. app:layout_constraintTop_toTopOf="parent" />
  69.  
  70.  
  71. <androidx.fragment.app.FragmentContainerView
  72. android:id="@+id/nav_host_fragment"
  73. android:name="androidx.navigation.fragment.NavHostFragment"
  74. android:layout_width="match_parent"
  75. android:layout_height="match_parent"
  76. app:defaultNavHost="true"
  77. app:navGraph="@navigation/main_graph" />
  78.  
  79.  
  80.  
  81. </androidx.constraintlayout.widget.ConstraintLayout>
  82.  
  83.  
  84.  
  85.  
  86. MainActivity.java
  87.  
  88.  
  89. public class MainActivity extends AppCompatActivity {
  90.  
  91. NavController navController;
  92.  
  93. @Override
  94. protected void onCreate(Bundle savedInstanceState) {
  95. super.onCreate(savedInstanceState);
  96. setContentView(R.layout.activity_main);
  97. navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  98.  
  99. Button button1 = findViewById(R.id.button);
  100. Button button2 = findViewById(R.id.button2);
  101.  
  102. }
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
Add Comment
Please, Sign In to add comment