Canvas not drawing text? - java

I want to display some text on Screen when the user clicks a button with canvas in android studio. I have been looking at many posts and videos of how to do that. The problem, when I tried their code, when I clicked my button, nothing showed up. Anyone know what I am doing wrong?
Here is my Java class:
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button drawCanvas = findViewById(R.id.draw_canvas_button);
drawCanvas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Paint paint = new Paint();
Canvas canvas = new Canvas();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(20f);
canvas.drawText("My Text", 10f, 15f, paint);
}
});
}
}

It seems that you have to use a method called onDraw by importing View.
import android.view.View;
public class myClass extends View{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//Where you put the code
}

Related

How to set a RelativeLayout background color?

I am trying to set a RelativeLayout backgroundColor and I get cannot resolve symbol
here is my code
package com.example.butka.clickme;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
//set layout
super.onCreate(savedInstanceState);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setBackgroundColor(Color.BLACK);
//LayoutParameters
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.CENTER_VERTICAL);
//button
Button btn = new Button(this);
btn.setText("Click me");
btn.setBackgroundColor(Color.WHITE);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
colors();
}
});
//add stuff
layout1.addView(btn, params);
setContentView(layout1);
}
//void on button click
private void colors()
{
Random random = new Random();
short num1 = (short)random.nextInt(9);
if(num1 == 0)
{
layout1.setBackgroundColor(Color.BLACK);
}
}
}
everything runs good, until the color void. the error is cannot resolve symbol But the interesting thing is that I can set the color using layout.setBackgroundColor() before the void.
So the question is, how do you set a layout backgroudColor?
Use this:
layout1.setBackgroundColor(ContextCompat.getColor(this, R.color.black));
or
layout1.setBackgroundColor(Color.parseColor("#000000"));
Your RelativeLayout is in the onCreate() method scope, you must move it to class scope. Like this:
public class MainActivity extends AppCompatActivity {
RelativeLayout layout1; // Make it class scope.
#Override
protected void onCreate(Bundle savedInstanceState) {
//set layout
super.onCreate(savedInstanceState);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setBackgroundColor(Color.BLACK);
...
}
// Then you can access it from other method:
private void colors() {
Random random = new Random();
short num1 = (short)random.nextInt(9);
if(num1 == 0) {
layout1.setBackgroundColor(Color.BLACK); // You can access it now.
}
}

Class or interface expected at the end of the first set of closing brackets

Keep getting a class or interface expected error at the end of the first set of closing brackets, I have a feeling it has to do with the onCreate method, but I'm not sure how to go about fixing it.
package shake.shake;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainPage extends AppCompatActivity {
private static Button ShakeButton1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
OnClickButtonListener();
}
}
public void OnClickButtonListener()
{
ShakeButton1 = (Button)findViewById(R.id.ShakeButton);
ShakeButton1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentSecondActivity = new
Intent(MainPage.this, ShakePage.class);
startActivity(intentSecondActivity);
}
}
);
}}
you have extra braces here
public class MainPage extends AppCompatActivity {
private static Button ShakeButton1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
OnClickButtonListener();
}
} <-------- extra brace.
It's better having a look at codes before posting.
Your method public void OnClickButtonListener() is outside the class MainPage. Move that method inside your class.

How to set image resource in Android?

I am writing code for a poker game on eclipse and I am stuck on how to make the card clicked in the cardHand area show up in the playedCards area and have it removed from the deck I am drawing it from. Here's the code:
package com.viktorengineering.poker254;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
public class Poker extends Activity {
private ImageView mViewDeck;
private ImageView mViewHand;
private ImageView mViewPlayedCards;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().setBackgroundDrawableResource(R.drawable.green_back);
mViewDeck = (ImageView) findViewById(R.id.deckImage);
mViewDeck.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int[] imagesArray = {R.drawable.clubs_ace, R.drawable.hearts_seven, R.drawable.diamonds_five,
R.drawable.clubs_three, R.drawable.hearts_eight, R.drawable.spades_six};
Random random = new Random();
mViewHand.setImageResource(imagesArray[new Random().nextInt(6)]);
}
});
mViewHand = (ImageView) findViewById(R.id.cardHand);
mViewHand.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPlayedCards.setImageResource(0);
mViewHand.setImageResource(0);
}
});
mViewPlayedCards = (ImageView) findViewById(R.id.playedCards);
}
}
Your Image in Drawable Folder means use bellow code
mViewPlayedCards.setImageResource(R.drawable.icon);
mViewHand.setImageResource(R.drawable.icon_home);

How to draw to canvas on onResume

I have an android app that draws to a canvas. Up to now I've been drawing each time I create the app usine onCreate as such:
package com.example.drawdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class DrawDemo extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
However I'd like to migrate the drawing to onResume. Ideally I do not draw the first time someone starts the program, only when they resume the program. Why do the following two code snippets not work:?
package com.example.drawdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class DrawDemo extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
#Override
public void onResume(){
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
and
package com.example.drawdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class DrawDemo extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onResume(){
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
Make sure you call super.onResume() - your activity will not function properly without it.

Android: Constantly updating TextView with a new value

I need some suggestions as to how I can update a Textview located in the XML with a value generated through my code.
The program draws a straight line through the canvas, I want the textView to reflect the line's tip x-value.
My code as follows:
package com.example.threadexperiment1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
lineDrawing InfiniteLine;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InfiniteLine = new lineDrawing (this);
setContentView(R.layout.activity_main);
Thread thread = new Thread(InfiniteLine);
thread.start();
RelativeLayout RL1 = (RelativeLayout)findViewById(R.id.RelativeLayout1);
RL1.addView(InfiniteLine);
}
public class lineDrawing extends View implements Runnable {
float x=100, y=100;
Paint lineColour = new Paint ();
TextView TV1 = (TextView)findViewById(R.id.textView1);
//Constructor
public lineDrawing(Context context) {
super(context);
lineColour.setColor(Color.BLACK);
}
//ondraw codes
#Override
public void onDraw (Canvas canvas){
canvas.drawLine(0,0,x,y,lineColour);
if (x==500){
x=0;
y=0;
}
invalidate();
}
#Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(250);
}
catch (Exception e) {}
x+=10;
y+=10;
}
}
}
}
Try textView.setText(string) within the thread.

Categories