How to combine canvas drawing with android activity layout which includes buttons and layouts?

user3635013

I am trying to build an app with buttons on its activity layout, but I want the score to be drawn on the same activity using

$canvas.drawText()

We use

$setContentView(R.id.activity)

for setting the content view of a layout, and use

$SurfaceView sv = new SurfaceView(this);
$setContentView(sv)

for drawing but how can we combine both in a same activity?

ILovemyPoncho

Since the score only needs to be redrawn occasionally, it seems that what you need is a custom view extending View class that will run on the main(UI) Thread. If you were animating, let's say a clock or some other animation which needs to be redrawn at constant time intervals or if the rendering takes too much time, then it would be better to extend SurfaceView together with another Thread, which would handle the animation times properly, without interruption of any other operations.

Let's see a basic example of a custom view that change its color each time the public changeColor() method is called:

public class CustomView extends View {

    ...

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Random rand = new Random();
        canvas.drawRGB(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
    }

    public void changeColor() {
       invalidate(); // redraws the view calling onDraw()
    }

}

To handle the view size properly you also need to override onSizeChanged(), onMeasure() or other callback methods depending of what you want. Now you can use it on the xml layout:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/change_color"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Change Color" />
    <org.example.yourpackages.CustomView
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />    
</LinearLayout>

And you can use it from your activity the same way any other widget:

public class CustomViewActivity extends Activity implements OnClickListener {

    private CustomView customView;            
    private Button changeColorBtn;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.custom_view_activity);

            customView = (CustomView)findViewById(R.id.custom_view);
            changeColorBtn = (Button)findViewById(R.id.change_color);
            changeColorBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        customView.changeColor();
    }

}

For a better understanding of how this works:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set a linear layout of buttons at the top of an image (layouts in android)?

From Dev

How do you determine which layout was last used by an activity that switches between multiple layouts?

From Dev

How to smooth canvas drawing in Android?

From Dev

Why are buttons making my canvas drawing like hexagon shape in Android?

From Dev

When creating an activity in Android Studio, two layouts are created, activity and fragment. Should I ignore the activity layout?

From Dev

Android fullscreen layout with additional layouts underneath, which are scrollable

From Dev

Android fullscreen layout with additional layouts underneath, which are scrollable

From Dev

Android how to open same activity with different relative layout views when onclick buttons?

From Dev

Android, problems with TextView contents when drawing Layout into canvas

From Dev

how to save drawing on canvas to reuse later in android?

From Dev

how to save drawing on canvas to reuse later in android?

From Dev

How can switch between two Layouts in only one Activity android

From Dev

How to implement two buttons in the same activity in android?

From Dev

How to implement two buttons or more in an activity with one layout?

From Dev

Android Drawing on Canvas

From Dev

canvas not drawing in java android

From Dev

canvas drawing is not working in android

From Dev

Android not drawing anything on canvas

From Dev

activity and fragment layouts in android studio

From Dev

How to arrange two linear layouts inside a relative layout in android?

From Dev

android how to set buttons in layout for all resolution

From Dev

How to add functions to buttons in an Android layout?

From Dev

Android - how to access buttons inside include layout

From Dev

Android - How to inflate an activity layout to fragment?

From Dev

Android - How to inflate an activity layout to fragment?

From Dev

How to add multiple layouts to the activity

From Dev

Android Create an activity with no buttons

From Dev

Android Create an activity with no buttons

From Dev

How to use a custom class (which extends View class) in activity class to draw path without changing the other parts of the main layout in android

Related Related

  1. 1

    How to set a linear layout of buttons at the top of an image (layouts in android)?

  2. 2

    How do you determine which layout was last used by an activity that switches between multiple layouts?

  3. 3

    How to smooth canvas drawing in Android?

  4. 4

    Why are buttons making my canvas drawing like hexagon shape in Android?

  5. 5

    When creating an activity in Android Studio, two layouts are created, activity and fragment. Should I ignore the activity layout?

  6. 6

    Android fullscreen layout with additional layouts underneath, which are scrollable

  7. 7

    Android fullscreen layout with additional layouts underneath, which are scrollable

  8. 8

    Android how to open same activity with different relative layout views when onclick buttons?

  9. 9

    Android, problems with TextView contents when drawing Layout into canvas

  10. 10

    how to save drawing on canvas to reuse later in android?

  11. 11

    how to save drawing on canvas to reuse later in android?

  12. 12

    How can switch between two Layouts in only one Activity android

  13. 13

    How to implement two buttons in the same activity in android?

  14. 14

    How to implement two buttons or more in an activity with one layout?

  15. 15

    Android Drawing on Canvas

  16. 16

    canvas not drawing in java android

  17. 17

    canvas drawing is not working in android

  18. 18

    Android not drawing anything on canvas

  19. 19

    activity and fragment layouts in android studio

  20. 20

    How to arrange two linear layouts inside a relative layout in android?

  21. 21

    android how to set buttons in layout for all resolution

  22. 22

    How to add functions to buttons in an Android layout?

  23. 23

    Android - how to access buttons inside include layout

  24. 24

    Android - How to inflate an activity layout to fragment?

  25. 25

    Android - How to inflate an activity layout to fragment?

  26. 26

    How to add multiple layouts to the activity

  27. 27

    Android Create an activity with no buttons

  28. 28

    Android Create an activity with no buttons

  29. 29

    How to use a custom class (which extends View class) in activity class to draw path without changing the other parts of the main layout in android

HotTag

Archive