0

I have an content_main.xml:

<LinearLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <com.example.CustomView
        android:id="@+id/customView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout >

In MainActivity.java, in onCreate I call the CustomView:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    customView = new CustomView(this, attributeSet);
    customView.init();
}

And in CustomView, I have an onDraw:

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLUE);
    drawValidMoves(canvas, squareSize);
}

in MainActivity, the onTouch

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        int x = (int)event.getX();
        int y = (int)event.getY();

        String text = customView.isTouched(x, y, customView.getSquareSize());
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }
    return super.onTouchEvent(event);
}

is calling an method from CustomView.java - isTouched that change some things in a matrix of drawed elements and after that, I'm making some:

  setWillNotDraw(false);
  invalidate();

but my game doesn't reach onDraw again... If I'm calling in onCreate directly the CustomView, the invalidate works:

customView = new CustomView(this, attributeSet);
customView.init();
setContentView(customView);

but I don't have the toolbar anymore, and I need that

LATER EDIT:

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/linearLayoutId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <com.example.clotz.CustomView
        android:id="@+id/customView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout >

MainActivity.java - onCreate - I moved the data from init method into constructor and get rid of init.

public class MainActivity extends AppCompatActivity {

    CustomView customView;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        customView = findViewById(R.id.customView);
    }

But customView is null now in onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        int x = (int)event.getX();
        int y = (int)event.getY();

        String text = customView.isTouched(x, y, customView.getSquareSize());
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }
    return super.onTouchEvent(event);
}
WDrgn
  • 521
  • 10
  • 29
  • 1
    Put your custom `View` in your layout, give it an ID, and use `findViewById()` to get it like any other regular `View` – https://stackoverflow.com/q/10410616. – Mike M. Aug 07 '19 at 07:05
  • Thanks Mike, I added a LATER EDIT, still not working... – WDrgn Aug 07 '19 at 07:51
  • 1
    Replace everything after the `setSupportActionBar()` call with `customView = findViewById(R.id.customView);`. Oh, except the `customView.init();` call, I mean. You probably still want that. – Mike M. Aug 07 '19 at 07:53
  • I get rid of init() also, moving the data from there in constructor, but it seems customview is Null after it exits from onCreate... – WDrgn Aug 07 '19 at 08:11
  • yes, it seems, after customView = findViewById(R.id.customView); the customView variable is still null. If I create it with customView = new CustomView(this, attributeSet); it is not null... – WDrgn Aug 07 '19 at 08:24
  • 1
    If `findViewById()` is returning null, then the layout you've shown above possibly isn't being loaded. Are you sure you still have an `` in `activity_main` for `content_main`? Do you possibly have multiple `res/layout*/` versions – e.g., one each for portrait and landscape – and one of those versions doesn't have your `` element? Also, in your `CustomView` class, in the `public CustomView(Context context, AttributeSet attrs)` constructor, are you sure you're passing `attrs` in the `super` call? – Mike M. Aug 07 '19 at 08:31
  • 1
    yes, the problem was in public CustomView(Context context, AttributeSet attributeSet) { i had super(context); Now it works, many many thanks!!! – WDrgn Aug 07 '19 at 08:39
  • if you put this as an answer I will approve it, thanks a lot! – WDrgn Aug 07 '19 at 08:39
  • 1
    Oh, I'm good. :-) Just a few common issues. Nothing major. Please feel free to post an answer yourself, outlining what you did to get things going. And don't forget to accept it in a couple of days, when the system lets you, so this question will show as answered. Thanks, though. I appreciate the offer. Glad you got it working. Cheers! – Mike M. Aug 07 '19 at 08:43

2 Answers2

1

@Mike M helped me with this and I put in content_main.xml the id of the linearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/linearLayoutId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <com.example.clotz.CustomView
        android:id="@+id/customView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout >

in MainActivity.java - onCreate (I moved the data from init method into constructor and get rid of init) and I created the customview with findViewById(R.id.customView);

public class MainActivity extends AppCompatActivity {

    CustomView customView;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        customView = findViewById(R.id.customView);
    }

And the problem was in CustomView.java, I forgot to pass attributeSet to the superclass:

public CustomView(Context context, AttributeSet attributeSet) {
    super(context);

    initializeMovedMatrix();
    setInitialPositionForPieces();
    // create the Paint to set its color
    paint = new Paint();
    piecesMoves = new PiecesMoves(this);
}

and putting it, combined with customView = findViewById(R.id.customView);

super(context, attributeSet);

Solved the problem. Thanks again MikeM!

WDrgn
  • 521
  • 10
  • 29
0

you cannot delete super.onDraw(canvas) in onDraw method.

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.BLUE);
    drawValidMoves(canvas, squareSize);
}
igarasi
  • 137
  • 7