0

I have a custom CircleButton class:

public class CircleButton extends ImageView {

private  int radius;
private  int x;
private  int y;

public CircleButton(Context context) {
    super(context);
    constructorTask();
}

public CircleButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    constructorTask();
}

public CircleButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    constructorTask();
}

public CircleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    constructorTask();
}


private void constructorTask() {
    x = 300;
    y = 300;
    radius = 100;
}

@Override
public void setPressed(boolean pressed) {
    super.setPressed(pressed);
    Log.i("Button Logger","Button Pressed");
}

@Override
protected  void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(x, y, radius, GameView.green);
    Log.i("Drawing status", "CircleButton Drawing...");
}

}

I have a single activity. This activity contains a relative layout with a single custom view.

Here is the custom view:

public class GameView extends View {

public static Paint green = new Paint();

public GameView(Context context) {
    super(context);
    green.setARGB(255,0,255,0);
}

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    green.setARGB(255, 0, 255, 0);

}

public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    green.setARGB(255, 0, 255, 0);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Log.i("GameView Draw Status","Drawing...");
    Main.testButton.invalidate();
    invalidate();
}

}

And here is the activity code:

public class Main extends AppCompatActivity {

public static CircleButton testButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    testButton = new CircleButton(getApplicationContext());
    makeFullScreen();

        RelativeLayout screenLayout = (RelativeLayout) findViewById(R.id.screenLayout);
    screenLayout.addView(testButton);
}

private void makeFullScreen() {...}

 }

For some reason my testButton is not being drawn. Why is it not being drawn?

EDIT ONE: Here is the XML I have.

    <?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="com.example.vroy.customcirclebuttontest.Main"
    android:id="@+id/screenLayout">

    <com.example.vroy.customcirclebuttontest.GameView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        android:id="@+id/gameScreen" />


</RelativeLayout>

EDIT TWO: I did some further debugging by adding a normal button to the relative layout and it worked fine.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testCircleButton = new CircleButton(getApplicationContext());
        makeFullScreen();

        testButton = new Button(getApplicationContext());
        testButton.setX(100);
        testButton.setY(100);
        testButton.setText("HELLO WORLD");

        RelativeLayout screenLayout = (RelativeLayout) findViewById(R.id.screenLayout);
        screenLayout.addView(testCircleButton);
        screenLayout.addView(testButton);

        Log.i("Button Status","Adding Button To Layout");
    }

For some reason by circleButton is not working but a normal button is.

11
  • You did not add the button to your layout, you just created an instance of it. Add it to the xml layout file or dynamically using the contentview Commented Feb 6, 2016 at 16:25
  • @user2395334 Oops, just added that in. However it still is not working? Maybe I did not add it in properly? Commented Feb 6, 2016 at 16:27
  • can you post the relevant part of the xml? Commented Feb 6, 2016 at 16:29
  • By the way, don't use the activity context to create the button, pass getapplicationcontext as an argument to prevent memory leaks Commented Feb 6, 2016 at 16:31
  • yes i saw it i deleted that comment but it's just strange that you have a static button Commented Feb 6, 2016 at 16:36

1 Answer 1

1

You are not specifying the size of the View (layout_width and layout_height), and thus your view is getting rendered inside a 0px by 0px space and thus invisible. You can set those programatically using LayoutParams before adding your views to the layout.

For example with absolute size:

testButton.setLayoutParams(new ViewGroup.LayoutParams(100,100));

Although keep in mind the difference between px and dip. You would probably want to set the values using your internal radius attribute instead of harcoding them.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you give some example code on how to use LayoutParams to set the width/height of my custom button?
Edited. Also check this answer stackoverflow.com/questions/9678785/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.