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

  • Home
  • SEARCH
  • 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 8958441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:12:51+00:00 2026-06-15T15:12:51+00:00

So, I have created an android activity that draws a triangle on the canvas.

  • 0

So, I have created an android activity that draws a triangle on the canvas. I also added 4 menus(Color, Enlarge, Shrink, and Reset) to the VM. The color works fine but I’m not quite sure how to resize a triangle in android once that menu button is pressed.The assignment says to just fix the top point of the triangle, and then change the coordinates of the bottom two points of the triangle. Can anyone point me in the right direction on how to do that in Android?

Here’s my code, although the implementation of enlarge, shrink, and reset are set up to work with a circle(project I did before), not a triangle. Please note that the “Color” menu works so no need to do that.

public class MainActivity extends Activity
{
    final Context context = this;
    private Graphics graphic;
    private Dialog radiusDialog; //Creates dialog box declaration
    private SeekBar red;
    private SeekBar green;
    private SeekBar blue;
    private Button radiusButton;

    private TextView progress1;
    private TextView progress2;
    private TextView progress3;
    private TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);       
        graphic = new Graphics(this); //Create new instance of graphics view
        setContentView(graphic); //Associates customized view with current screen     
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) //This acts as a menu listener to override
    {
        switch(item.getItemId()) //returns menu item
        {
        case R.id.Color:
            showDialog();
            break;
        case R.id.Shrink:
            graphic.setRadius(graphic.getRadius() -1);
            graphic.invalidate();
            break;
        case R.id.Enlarge:
            graphic.setRadius(graphic.getRadius() +1);
            graphic.invalidate();
            break;
        case R.id.Reset:
            graphic.setColor(Color.CYAN);
            graphic.setRadius(75);
            graphic.invalidate();
            break;
        }
        return super.onOptionsItemSelected(item);
    }





    void showDialog() //creates memory for dialog
    {
        radiusDialog = new Dialog(context);
        radiusDialog.setContentView(R.layout.draw_layout);  //binds layout file (radius) with current dialog
        radiusDialog.setTitle("Select Color:");


        red = (SeekBar)radiusDialog.findViewById(R.id.seekBar1);
        green = (SeekBar)radiusDialog.findViewById(R.id.seekBar2);
        blue = (SeekBar)radiusDialog.findViewById(R.id.seekBar3);

        progress1 = (TextView)radiusDialog.findViewById(R.id.textView2);
        progress2 = (TextView)radiusDialog.findViewById(R.id.textView4);
        progress3 = (TextView)radiusDialog.findViewById(R.id.textView6);    

        mychange redC = new mychange();
        red.setOnSeekBarChangeListener(redC);

        mychange greenC = new mychange();
        green.setOnSeekBarChangeListener(greenC);

        tv = (TextView)radiusDialog.findViewById(R.id.textView7);
        mychange c = new mychange();
        blue.setOnSeekBarChangeListener(c);     
        radiusButton = (Button) radiusDialog.findViewById(R.id.button1);
        radiusButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int color = Color.rgb(red.getProgress(), green.getProgress(), blue.getProgress());  
                radiusDialog.dismiss();
                setContentView(R.layout.activity_main);
                setContentView(graphic);
                graphic.setColor(color);//Create new instance of graphics view
                graphic.invalidate();
            }
        });
        radiusDialog.show(); //shows dialog on screen
    } 

    public class mychange implements OnSeekBarChangeListener{

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            int color = Color.rgb(red.getProgress(), green.getProgress(), blue.getProgress());  
            tv.setBackgroundColor(color);
            progress1.setText(String.valueOf(red.getProgress()));
            progress2.setText(String.valueOf(green.getProgress()));
            progress3.setText(String.valueOf(blue.getProgress()));

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }
    }

}

Graphics Class to draw triangle

public class Graphics extends View 
{

    private Paint paint;
    private int radius;
    private int color;

    public void setColor(int color)
    {
        this.color = color;
    }

    public Graphics(Context context) //creates custom view (constructor)
    {
        super(context);
        paint = new Paint(); //create instance of paint
        color = Color.CYAN;
        paint.setStyle(Paint.Style.FILL); //draw filled shape
        radius = 75;
    }

    @Override
    protected void onDraw(Canvas canvas) //override onDraw method
    {
        super.onDraw(canvas);
        paint.setColor(color);  
        paint.setStyle(Paint.Style.STROKE);
        Path path = new Path();
        path.moveTo(230, 200);
        path.lineTo(330, 300);
        path.lineTo(130, 300);
        path.close();
        canvas.drawPath(path, paint);
    }

    void setRadius(int radius)
    {
        this.radius = radius;
        invalidate(); //just like repaint method
    }
    public int getRadius()
    {
        return radius;
    }   
}
  • 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-15T15:12:52+00:00Added an answer on June 15, 2026 at 3:12 pm

    If the top coordinate remains fixed, you can change the height of the triangle to shrink/enlarge it.

    Lets say the triangle is equilateral – all 3 sides have the same length. In this case:

    enter image description here

    So if the top vertex coordinates are (x, y), the bottom coordinates will be:

    (x - side / 2, y + h)
    

    And:

    (x + side / 2, y + h)
    

    So your path code should be written as:

    float side = Math.sqrt(3) / 2 * height;
    Path path = new Path();
    path.moveTo(x, y);
    path.lineTo(x - side / 2, y + height);
    path.lineTo(x + side / 2, y + height);
    path.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an application for Android that has an activity that has a
I have created an android application that calls (using kSOAP library) a SOAP based
I have an Android activity that displays a list of log entries (using a
I have created an Activity that shows a listview and on swipe action another
I have created an app that suppose to toggle the button background color on
I have a View that was created at runtime then I draw some canvas
I have created my android code wherein I fetch all the data from facebook
I have created a MazeSolver android app for a school project. I am using
I have created following thing in android using android compatibility support package Basically i
I have created a listview with 3 rows in Android. If I would like

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.