Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.eurynomus.fragments;
- import android.graphics.Color;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.TextView;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity {
- TextView textView, textView2;
- FrameLayout frameLayout;
- FrameLayout frameLayout2;
- Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView = findViewById(R.id.textView);
- textView2 = findViewById(R.id.textView2);
- frameLayout = findViewById(R.id.frameLayout);
- frameLayout2 = findViewById(R.id.frameLayout2);
- button = findViewById(R.id.button);
- // Generate random color
- Random rnd = new Random();
- Integer color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
- Random rnd2 = new Random();
- Integer color2 = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
- textView.setText( String.format("#%06X", (0xFFFFFF & color)) );
- textView2.setText( String.format("#%06X", (0xFFFFFF & color2)) );
- frameLayout.setBackgroundColor(color);
- frameLayout2.setBackgroundColor(color2);
- final Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
- fragment.getView().setBackgroundColor(Color.WHITE);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // create a FragmentManager
- FragmentManager fm = getSupportFragmentManager();
- // create a FragmentTransaction to begin the transaction and replace the Fragment
- FragmentTransaction fragmentTransaction = fm.beginTransaction();
- // replace the FrameLayout with new Fragment
- fragmentTransaction.replace(R.id.frameLayout, fragment);
- fragmentTransaction.commit(); // save the changes
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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"
- android:orientation="horizontal"
- tools:context=".MainActivity">
- <FrameLayout
- android:id="@+id/frameLayout"
- android:layout_width="405dp"
- android:layout_height="154dp"
- android:layout_marginStart="3dp"
- android:layout_marginEnd="3dp"
- android:layout_marginBottom="136dp"
- app:layout_constraintBottom_toTopOf="@+id/frameLayout2"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent">
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView"
- app:layout_constraintTop_toBottomOf="@+id/frameLayout"/>
- </FrameLayout>
- <FrameLayout
- android:id="@+id/frameLayout2"
- android:layout_width="405dp"
- android:layout_height="154dp"
- android:layout_marginTop="48dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/frameLayout">
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView"
- app:layout_constraintTop_toBottomOf="@+id/frameLayout2" />
- </FrameLayout>
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="27dp"
- android:layout_marginEnd="30dp"
- android:layout_marginBottom="39dp"
- android:text="Add"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toStartOf="@+id/button2"
- app:layout_constraintStart_toStartOf="parent" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="30dp"
- android:layout_marginEnd="22dp"
- android:layout_marginBottom="39dp"
- android:text="Transfer"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toStartOf="@+id/button3"
- app:layout_constraintStart_toEndOf="@+id/button" />
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="7dp"
- android:layout_marginEnd="53dp"
- android:layout_marginBottom="39dp"
- android:text="Remove"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/button2" />
- <fragment
- android:id="@+id/fragment"
- android:layout_width="405dp"
- android:layout_height="154dp"
- android:layout_marginStart="3dp"
- android:layout_marginEnd="3dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
- !!!!! FragmentOne.java !!!!!!
- package com.example.eurynomus.fragments;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.FrameLayout;
- import java.util.Random;
- public class FragmentOne extends Fragment {
- FrameLayout frameLayout;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.activity_main, parent, false);
- }
- Random rnd = new Random();
- Integer color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement