0

I have a fragment class Frag2 and a view class Rectangle which sends onDraw to another fragment class Frag1, and i need to send some data to Rectangle from Frag 2. what do i do ? :D

(sorry its abit messy ive been trying everything for last 24 hours.)

Frag 2

public class FragmentTwo extends Fragment {

TextView changingText;
Button changeTextButton;

Button XButton;
Button YButton;

private int SecondX;

NumberPicker Pick100 = null;
private int entered;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    // TODO Auto-generated method stub
    View myInflatedView = inflater.inflate(R.layout.fragment_two_layout, container, false);


    changingText = (TextView) myInflatedView.findViewById(R.id.textView);
    changeTextButton = (Button) myInflatedView.findViewById(R.id.button);


    XButton = (Button) myInflatedView.findViewById(R.id.buttonX);
    YButton = (Button) myInflatedView.findViewById(R.id.buttonY);

    Pick100 = (NumberPicker) myInflatedView.findViewById(R.id.secondD);



    changeTextViewValueRandomlyOnButtonClick();
    return myInflatedView;
}


private void changeTextViewValueRandomlyOnButtonClick() {
    final String[] text = {"A", "B", "C", "D"};


    changeTextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random rand = new Random();
            int random = rand.nextInt(100) + 1;

            if (random == 100) changingText.setText(text[3]);
            if (random > 90 && random <= 99) changingText.setText(text[2]);
            if (random < 60 && random >= 1) changingText.setText(text[0]);
            if (random >= 60 && random <= 90) changingText.setText(text[1]);
        }

    });
}

You can ignore most of it really. Im trying to send Pick100 to this class

public class Rectangle extends View {
//FragmentTwo F2 = new FragmentTwo();


public Rectangle(Context context) {
    super(context);
}

public Rectangle(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public Rectangle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Log.d("X","RED DOT EUQALS TO "+x);


    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    canvas.drawRect(x,100,x+60,100+60,paint);
}

}

The Rectangle works fine.It sends a rect to Frag1 i just want to be able to send the number here and sub it in x ( int rectangle)

Frag 1 class :

public class FragmentOne extends Fragment {
RelativeLayout relativeLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View myInflatedView= inflater.inflate(R.layout.fragment_one_layout,container,false);


    return myInflatedView;
}

}

Frag 1 xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/Frag1"
android:layout_height="match_parent"
android:background="#000">


<com.redot.puzzle1.Rectangle
    android:id="@+id/Rect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

2 Answers2

0

I am fairly sure you can simply define a void setX(int x) in the Rectangle class.

In whatever Fragment you've put the Rectangle View, you would need something like

// Make a field for this
rect = (Rectangle) myInflatedView.findViewById(R.id.Rect);

// Call your method whenever
rect.setX(x);

Note: Looks like you are trying to randomly change the size of a view and expecting the onDraw method to do that change.

I think you should be using a Canvas object instead if you want to be working with Shapes and such

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Ive posted the Frag 1 class and xml. the rectangle is in the xml so i cant call it from frag2 or i think i cant. – amirsoltani Sep 20 '17 at 01:16
  • Okay, then you need some instance of Frag1 and use `setArguments()` on it – OneCricketeer Sep 20 '17 at 01:18
  • just to be clear I should send rectangle from fragment 1 to 2 then setX in fragmet 2 then use it in rectangle? – amirsoltani Sep 20 '17 at 01:21
  • It sounds complicated, but yes. You can try a simple string to get started. https://stackoverflow.com/questions/13700798/basic-communication-between-two-fragments – OneCricketeer Sep 20 '17 at 01:23
  • Thank you very much, btw i'll be trying to learn https://developer.android.com/training/custom-views/custom-drawing.html this instead – amirsoltani Sep 20 '17 at 01:23
0

Create a static field in Rectangle class public static int pick100Value = 0 and subtract pick100Value from x in onDraw()

public class Rectangle extends View {
    public static int pick100Value = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // ...
        canvas.drawRect(x - pick100Value , 100, (x - pick100Value)  + 60, 100 + 60, paint);
    }
}   

In FragmentTwo listen to the onValueChanged and set Rectangle.pick100Value = newVal

public class FragmentTwo extends Fragment implements OnValueChangeListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        // TODO Auto-generated method stub
        View myInflatedView = inflater.inflate(R.layout.fragment_two_layout, container, false);


        changingText = (TextView) myInflatedView.findViewById(R.id.textView);
        changeTextButton = (Button) myInflatedView.findViewById(R.id.button);


        XButton = (Button) myInflatedView.findViewById(R.id.buttonX);
        YButton = (Button) myInflatedView.findViewById(R.id.buttonY);

        Pick100 = (NumberPicker) myInflatedView.findViewById(R.id.secondD);
        Pick100.setOnValueChangedListener(this);

        changeTextViewValueRandomlyOnButtonClick();
        return myInflatedView;
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        Rectangle.pick100Value = newVal;
    }
}
MD TAREQ HASSAN
  • 1,188
  • 20
  • 46
  • In fragment 2 it i get an error it says it cant resolve `pick100Value ` – amirsoltani Sep 20 '17 at 01:36
  • Did you create static field ? `public static int pick100Value` in Rectangle. – MD TAREQ HASSAN Sep 20 '17 at 01:39
  • I updated my answer: instead of anonymous implement OnValueChangeListener in Frag2 and `Pick100.setOnValueChangedListener(this);` – MD TAREQ HASSAN Sep 20 '17 at 01:44
  • it said the frag2 class must be abstract or implement onValueChange() but when i implemented it ran but its buggy the rectangle doesnt show up as well as fragment 2 – amirsoltani Sep 20 '17 at 01:51
  • I dot think the way im trying is a not the way to go so im gonna try a new way, it got too complicated anyways THank You so much – amirsoltani Sep 20 '17 at 01:53
  • `Pick100 = (NumberPicker) myInflatedView.findViewById(R.id.secondD);` Then how did you get Pick100 value before? if you were not listening to `OnValueChangeListener` in Frag2? – MD TAREQ HASSAN Sep 20 '17 at 01:55
  • well i didnt get there first i was looking for a way to send the data – amirsoltani Sep 20 '17 at 02:03
  • `OnValueChangeListener` is an interface and therefore implementing it in Frag2 should not hurt. Also, creating a static field in Rectangle class would not cause problem either – MD TAREQ HASSAN Sep 20 '17 at 02:05
  • idk what the problem is I just copied your code. the rectangle and frag 2 layout dont show up im pretty sure there is a better way.I'll be looking for it tomorrow and if you want i can informe you when i've found it thank you – amirsoltani Sep 20 '17 at 02:09