package com.example.labsix; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.navigation.fragment.NavHostFragment; import java.util.Random; public class RelativeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) { // set up the initial layout RelativeLayout ll = new RelativeLayout(getContext()); ll.setId(View.generateViewId()); ll.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return ll; } /** * Generate a random color * @return the random color */ private int randomColor() { Random r = new Random(); return Color.rgb(r.nextInt(255), r.nextInt(255), r.nextInt(255)); } public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); doRL((RelativeLayout)view); } /** * fill the interface with some pattern * @param ll the interface to fill */ private void doRL(RelativeLayout ll) { //do1(ll); //do1i(ll); //doN(ll, 20); //doNRule(ll,20); //doTurn(ll, 20); //doSSS1(ll); doSSS2(ll); } /** * Put a single square down in the interface * Problem, you only see part of the square * @param ll the interface */ void do1(RelativeLayout ll) { float density = getResources().getDisplayMetrics().density; TextView v = new TextView(getContext()); v.setBackgroundColor(randomColor()); v.setText("" + 0); v.setGravity(Gravity.CENTER); v.setId(View.generateViewId()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (100 * density), (int) (100 * density)); ll.addView(v, lp); } /** * Put a single square down in the interface. * Here set teh top pad of the interface to the height of the actionbar. This pushed the * square down so we see it all. * @param ll the interface */ void do1i(RelativeLayout ll) { float density = getResources().getDisplayMetrics().density; TypedValue tv = new TypedValue(); if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); ll.setPadding(0, actionBarHeight, 0, 0); } TextView v = new TextView(getContext()); v.setBackgroundColor(randomColor()); v.setText("" + 0); v.setGravity(Gravity.CENTER); v.setId(View.generateViewId()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (100 * density), (int) (100 * density)); ll.addView(v, lp); } /** * Put a bunch of squared down in the interface * Problem, they all end up on top of each other. * @param rl the interface * @param maxx the max number of squares to put down */ void doN(RelativeLayout rl, int maxx) { float density = getResources().getDisplayMetrics().density; TypedValue tv = new TypedValue(); if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); rl.setPadding(0, actionBarHeight, 0, 0); } View pp = null; for (int i = 0; i < maxx; i++) { TextView v = new TextView(getContext()); v.setBackgroundColor(randomColor()); v.setText("" + i); v.setGravity(Gravity.CENTER); v.setId(View.generateViewId()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (100 * density), (int) (100 * density)); rl.addView(v, lp); pp = v; } } /** * Put a bunch of squares down in the interface starting at the bottom and working up. * Uses alignment rules to get this behavior. Note that the while they stack, becase after first no rule for * R-L, then default to far L. * @param rl the layout * @param maxx max number in stack */ void doNRule(RelativeLayout rl, int maxx) { float density = getResources().getDisplayMetrics().density; TypedValue tv = new TypedValue(); if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); rl.setPadding(0, actionBarHeight, 0, 0); } View pp = null; for (int i = 0; i < maxx; i++) { TextView v = new TextView(getContext()); v.setBackgroundColor(randomColor()); v.setText("" + i); v.setGravity(Gravity.CENTER); v.setId(View.generateViewId()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (100 * density), (int) (100 * density)); if (pp!=null) { lp.addRule(RelativeLayout.ABOVE, pp.getId()); } else { lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); } rl.addView(v, lp); pp = v; } } /** * Make a square in a square, in a square .... * @param rl the interface */ void doSSS1(RelativeLayout rl) { float density = getResources().getDisplayMetrics().density; float width = getResources().getDisplayMetrics().widthPixels/density; TypedValue tv = new TypedValue(); if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); rl.setPadding(0, actionBarHeight, 0, 0); } int step = 10; for (int i=(int)width; i>(step-1); i=i-step) { View v = new View(getContext()); v.setBackgroundColor(randomColor()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (i * density), (int) (i * density)); lp.addRule(RelativeLayout.CENTER_IN_PARENT); rl.addView(v, lp); } } /** * Lots of things in a sort of diamond layout. * @param rl the interface */ void doSSS2(RelativeLayout rl) { float density = getResources().getDisplayMetrics().density; int siz = 20; View vCenter = new View(getContext()); vCenter.setId(View.generateViewId()); vCenter.setBackgroundColor(Color.GREEN); { RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (siz * density), (int) (siz * density)); lp.addRule(RelativeLayout.CENTER_IN_PARENT); rl.addView(vCenter, lp); } for (int i=2; i<11; i=i+2) { View vInvis = new View(getContext()); vInvis.setId(View.generateViewId()); vInvis.setBackgroundColor(Color.TRANSPARENT); { RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (siz * density), (int) (siz * density)); lp.addRule(RelativeLayout.RIGHT_OF, vCenter.getId()); lp.addRule(RelativeLayout.ALIGN_BOTTOM, vCenter.getId()); rl.addView(vInvis, lp); } View vPrev = new View(getContext()); vPrev.setId(View.generateViewId()); vPrev.setBackgroundColor(randomColor()); { RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (siz * density), (int) (siz * density)); lp.addRule(RelativeLayout.RIGHT_OF, vInvis.getId()); lp.addRule(RelativeLayout.ALIGN_BOTTOM, vInvis.getId()); rl.addView(vPrev, lp); Log.i("THISS", "Here" + vPrev); } vCenter=vPrev; for (int j=0; j<4; j++) { for (int k=0; k<(j==3?(i-1):i); k++) { View v = new View(getContext()); v.setId(View.generateViewId()); v.setBackgroundColor(randomColor()); { RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (siz * density), (int) (siz * density)); switch (j) { case 0: lp.addRule(RelativeLayout.LEFT_OF, vPrev.getId()); lp.addRule(RelativeLayout.ABOVE, vPrev.getId()); break; case 1: lp.addRule(RelativeLayout.LEFT_OF, vPrev.getId()); lp.addRule(RelativeLayout.BELOW, vPrev.getId()); break; case 2: lp.addRule(RelativeLayout.RIGHT_OF, vPrev.getId()); lp.addRule(RelativeLayout.BELOW, vPrev.getId()); break; case 3: lp.addRule(RelativeLayout.RIGHT_OF, vPrev.getId()); lp.addRule(RelativeLayout.ABOVE, vPrev.getId()); break; } rl.addView(v, lp); vPrev=v; } } } } } /** * Stack from the bottom for a while, then turn right. * Note the effect of not having a rule for vertical position after 5. * @param rl * @param maxx */ void doTurn(RelativeLayout rl, int maxx) { float density = getResources().getDisplayMetrics().density; TypedValue tv = new TypedValue(); if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); rl.setPadding(0, actionBarHeight, 0, 0); } View pp = null; for (int i = 0; i < maxx; i++) { TextView v = new TextView(getContext()); v.setBackgroundColor(randomColor()); v.setText("" + i); v.setGravity(Gravity.CENTER); v.setId(View.generateViewId()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (100 * density), (int) (100 * density)); if (pp != null) { if (i < 6) lp.addRule(RelativeLayout.BELOW, pp.getId()); else { lp.addRule(RelativeLayout.END_OF, pp.getId()); } } rl.addView(v, lp); pp = v; } } }