Search code examples
androidlayoutgeometrysetcontentview

setContentView and buttons in Android


I have an Activity which has a button and by code I draw a Circle, so my Circle class is:

public class Circle extends View {

private float x = 100;

private float y = 100;

private float radius = 50;

public Circle(Context context) {
    super(context);
}

protected void onDraw(Canvas canvas){
    Paint paint = new Paint();
    canvas.drawCircle(x, y, radius, paint);

}

}

And the code of my activity is:

public class AnotherTest extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Circle c = new Circle(this);
    setContentView(c);
    } 

   }

But when I invoke setContentView, the button seems to be deleted. Any tips to show the circle and preserve the button?


Solution

  • The button you mentioned is in your activity's layout XML file? If so, could you provide the code? setContentView() will only show the circle. If you want to add the circle to your existing layout you have to add it to a ViewGroup in your Activity's XML.

    You could do something like this:

    public class AnotherTest extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.anothertest_activity);
        } 
    }
    

    And the (res/layout/)anothertest_activity.xml file could look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <your.package.Circle
        android:id="@+id/myCircle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    In Android Studio, right below the xml code are two tabs: "Design" and "Text". Switch to "Text" to paste code, switch back to "Design" to position your elements.

    If you drag your views, you have an layout XML file. In the Activity you need to set this file as your content view, otherwise you wouldn't see your views. But you can add views dynamically by doing something like this:

    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout android:id="@+id/content"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_viewgroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </RelativeLayout>
    

    And in your Activity:

    public class AnotherTest extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Still your xml with your button but without circle
            setContentView(R.layout.anothertest_activity);
    
            // First find your ViewGroup to add Views to
            RelativeLayout viewGroup = (RelativeLayout) findViewById(R.id.my_viewgroup);
            // Add a new circle to existing layout
            viewGroup.addView(new Circle());
        } 
    }
    

    You also could add everything dynamically:

    public class AnotherTest extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ViewGroup viewGroup = new LinearLayout(this);           
            setContentView(viewGroup);
    
            viewGroup.addView(new Button());
            viewGroup.addView(new Circle());
        } 
    }
    

    But I strongly recommend using xml layouts. Might want to take a look at Android Layouts