Advertisement
praymes

Fragment

May 16th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. package com.example.eurynomus.fragments;
  2.  
  3. import android.graphics.Color;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v4.app.FragmentManager;
  6. import android.support.v4.app.FragmentTransaction;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.FrameLayout;
  12. import android.widget.TextView;
  13.  
  14. import java.util.Random;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. TextView textView, textView2;
  19. FrameLayout frameLayout;
  20. FrameLayout frameLayout2;
  21. Button button;
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27.  
  28. textView = findViewById(R.id.textView);
  29. textView2 = findViewById(R.id.textView2);
  30. frameLayout = findViewById(R.id.frameLayout);
  31. frameLayout2 = findViewById(R.id.frameLayout2);
  32. button = findViewById(R.id.button);
  33.  
  34.  
  35. // Generate random color
  36. Random rnd = new Random();
  37. Integer color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
  38. Random rnd2 = new Random();
  39. Integer color2 = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
  40.  
  41. textView.setText( String.format("#%06X", (0xFFFFFF & color)) );
  42. textView2.setText( String.format("#%06X", (0xFFFFFF & color2)) );
  43.  
  44. frameLayout.setBackgroundColor(color);
  45. frameLayout2.setBackgroundColor(color2);
  46.  
  47. final Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
  48. fragment.getView().setBackgroundColor(Color.WHITE);
  49.  
  50.  
  51.  
  52. button.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View view) {
  55.  
  56. // create a FragmentManager
  57. FragmentManager fm = getSupportFragmentManager();
  58. // create a FragmentTransaction to begin the transaction and replace the Fragment
  59. FragmentTransaction fragmentTransaction = fm.beginTransaction();
  60. // replace the FrameLayout with new Fragment
  61. fragmentTransaction.replace(R.id.frameLayout, fragment);
  62. fragmentTransaction.commit(); // save the changes
  63. }
  64.  
  65. });
  66. }
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. <?xml version="1.0" encoding="utf-8"?>
  75. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  76. xmlns:app="http://schemas.android.com/apk/res-auto"
  77. xmlns:tools="http://schemas.android.com/tools"
  78. android:layout_width="match_parent"
  79. android:layout_height="match_parent"
  80. android:orientation="horizontal"
  81. tools:context=".MainActivity">
  82.  
  83. <FrameLayout
  84. android:id="@+id/frameLayout"
  85. android:layout_width="405dp"
  86. android:layout_height="154dp"
  87. android:layout_marginStart="3dp"
  88. android:layout_marginEnd="3dp"
  89. android:layout_marginBottom="136dp"
  90. app:layout_constraintBottom_toTopOf="@+id/frameLayout2"
  91. app:layout_constraintEnd_toEndOf="parent"
  92. app:layout_constraintHorizontal_bias="0.0"
  93. app:layout_constraintStart_toStartOf="parent"
  94. app:layout_constraintTop_toTopOf="parent">
  95.  
  96. <TextView
  97. android:id="@+id/textView"
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:text="TextView"
  101. app:layout_constraintTop_toBottomOf="@+id/frameLayout"/>
  102.  
  103. </FrameLayout>
  104.  
  105. <FrameLayout
  106. android:id="@+id/frameLayout2"
  107. android:layout_width="405dp"
  108. android:layout_height="154dp"
  109. android:layout_marginTop="48dp"
  110. app:layout_constraintEnd_toEndOf="parent"
  111. app:layout_constraintStart_toStartOf="parent"
  112. app:layout_constraintTop_toBottomOf="@+id/frameLayout">
  113.  
  114. <TextView
  115. android:id="@+id/textView2"
  116. android:layout_width="wrap_content"
  117. android:layout_height="wrap_content"
  118. android:text="TextView"
  119. app:layout_constraintTop_toBottomOf="@+id/frameLayout2" />
  120.  
  121. </FrameLayout>
  122.  
  123. <Button
  124. android:id="@+id/button"
  125. android:layout_width="wrap_content"
  126. android:layout_height="wrap_content"
  127. android:layout_marginStart="27dp"
  128. android:layout_marginEnd="30dp"
  129. android:layout_marginBottom="39dp"
  130. android:text="Add"
  131. app:layout_constraintBottom_toBottomOf="parent"
  132. app:layout_constraintEnd_toStartOf="@+id/button2"
  133. app:layout_constraintStart_toStartOf="parent" />
  134.  
  135. <Button
  136. android:id="@+id/button2"
  137. android:layout_width="wrap_content"
  138. android:layout_height="wrap_content"
  139. android:layout_marginStart="30dp"
  140. android:layout_marginEnd="22dp"
  141. android:layout_marginBottom="39dp"
  142. android:text="Transfer"
  143. app:layout_constraintBottom_toBottomOf="parent"
  144. app:layout_constraintEnd_toStartOf="@+id/button3"
  145. app:layout_constraintStart_toEndOf="@+id/button" />
  146.  
  147. <Button
  148. android:id="@+id/button3"
  149. android:layout_width="wrap_content"
  150. android:layout_height="wrap_content"
  151. android:layout_marginStart="7dp"
  152. android:layout_marginEnd="53dp"
  153. android:layout_marginBottom="39dp"
  154. android:text="Remove"
  155. app:layout_constraintBottom_toBottomOf="parent"
  156. app:layout_constraintEnd_toEndOf="parent"
  157. app:layout_constraintStart_toEndOf="@+id/button2" />
  158.  
  159. <fragment
  160. android:id="@+id/fragment"
  161. android:layout_width="405dp"
  162. android:layout_height="154dp"
  163. android:layout_marginStart="3dp"
  164. android:layout_marginEnd="3dp"
  165. app:layout_constraintEnd_toEndOf="parent"
  166. app:layout_constraintHorizontal_bias="0.0"
  167. app:layout_constraintStart_toStartOf="parent"
  168. app:layout_constraintTop_toTopOf="parent" />
  169.  
  170.  
  171.  
  172. </android.support.constraint.ConstraintLayout>
  173.  
  174.  
  175.  
  176.  
  177.  
  178. !!!!! FragmentOne.java !!!!!!
  179.  
  180. package com.example.eurynomus.fragments;
  181.  
  182. import android.graphics.Color;
  183. import android.os.Bundle;
  184. import android.support.v4.app.Fragment;
  185. import android.view.LayoutInflater;
  186. import android.view.View;
  187. import android.view.ViewGroup;
  188. import android.widget.FrameLayout;
  189.  
  190. import java.util.Random;
  191.  
  192. public class FragmentOne extends Fragment {
  193.  
  194. FrameLayout frameLayout;
  195.  
  196. @Override
  197. public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
  198. return inflater.inflate(R.layout.activity_main, parent, false);
  199.  
  200.  
  201. }
  202.  
  203. Random rnd = new Random();
  204. Integer color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement