Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9104447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:52:16+00:00 2026-06-17T01:52:16+00:00

I would like each time I press one of the buttons the rectangles on

  • 0

I would like each time I press one of the buttons the rectangles on the screen to change position. Or the entire SurfaceView to refresh.

The app seems to run but the rectangles do not change position with the counter.

Thank you very much.

Hare is the xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
    android:id="@+id/surface"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />
<Button
    android:id="@+id/l"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:text="L" />
<Button
    android:id="@+id/r"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="19dp"
    android:text="R" />
</RelativeLayout>

Here is the Java:

package com.example.rectangle;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
DrawView drawView;
Paint paint = new Paint();
int w, h, edge, axis_w;
int counter;
Button add, sub;
TextView display;

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    counter = 0;
    add = (Button) findViewById(R.id.r);
    sub = (Button) findViewById(R.id.l);
    display = (TextView) findViewById(R.id.textView1);

    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter += 10;
            display.setText("Total is" + counter);

        }
    });

    sub.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter -= 10;
            display.setText("Total is" + counter);
        }
    });

    SurfaceView surface = (SurfaceView) findViewById(R.id.surface);

    surface.getHolder().addCallback(new Callback() {

        public void surfaceCreated(SurfaceHolder holder) {

            Canvas canvas = holder.lockCanvas();

            w = canvas.getWidth();
            h = canvas.getHeight();
            edge = 5;
            axis_w = 3;
            // canvas.drawRect(left, top, right, bottom, paint)
            // Background
            paint.setColor(Color.rgb(255, 255, 255));
            canvas.drawRect(0, 0, w, h, paint);
            // Canvas Background Color
            paint.setColor(Color.rgb(0, 206, 209));
            // Borders
            canvas.drawRect(0, 0, w, edge, paint);
            canvas.drawRect(0, h - edge, w, h, paint);
            canvas.drawRect(0, 0, edge, h, paint);
            canvas.drawRect(w - edge, 0, w, h, paint);
            // Axis
            canvas.drawRect(((w - axis_w) / 2) + counter, 0,
                    ((w + axis_w) / 2) + counter, h, paint);
            canvas.drawRect(0, (h - axis_w) / 2, w, (h + axis_w) / 2, paint);

            holder.unlockCanvasAndPost(canvas);
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
        }
    });
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:52:18+00:00Added an answer on June 17, 2026 at 1:52 am

    Ok. Try Another solution. Try to make your own View class where you will draw your rectangle. I have prepared some code for you. Customize it to your needs.

    Simlpe XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainL"
        >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, MainActivity"
        android:id="@+id/button"
        />
    </LinearLayout>
    

    and java code

    public class MainActivity extends Activity
    {
        MyRectg myRectg;
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            myRectg = new MyRectg(this);
            ((LinearLayout)findViewById(R.id.mainL)).addView(myRectg);
            ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View arg0)
            {
                myRectg.invalidate();
            }
        });
    }
    
    public class MyRectg extends View
    {
        public MyRectg(Context c)
        {
            super(c);
        }
    
        @Override
        public void onDraw(Canvas c)
        {
            //this simulate new positions calculating
            Random rnd = new Random();
            Paint p = new Paint();
            p.setColor(Color.GREEN);
            c.drawRect(rnd.nextFloat()*100, rnd.nextFloat()*500, rnd.nextFloat()*300, rnd.nextFloat()*500, p);
        }
    }
    

    Now canvas randomly moves when you click the button. Is this what you wanted?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a div that I would like to reposition each time the page
I would like to make a ListBox function like a Grid . Each time
I've got a pickerview. I would like to call a specific function each time
I would like to put a time & date stamp on each row added
I've a collection of a class' properties and would like to update each one's
I would like to receive a notification each time Siri appears or disappears from
I would like to iterate on a dictionary, amending the dictionary each time rather
I would like to each time the div expands, to add the class 'selecionado'
I would like to run multiple websites, and I would like each website (differentiated
I would like to replace each two spaces from the beginning of each line,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.