0

I have a simple app that I'm trying to create. When a button is clicked it hides or shows a square on the the device screen. The only problem that I currently have is that "setContentView()" will over write the buttons that I have placed with the layout design view.

My question is how would I prevent a second call from setContetView() from over writing my button?

MainActivity.java

package com.example.hps.shapes;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

 public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    setContentView(new CustomView(this));

}
}

CustomView.java

package com.example.hps.shapes;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

/**
 * Created on 7/2/2016.
*/

public class CustomView extends View {

    private Rect rectangle;
    private Paint paint;

    public CustomView(Context context) {
        super(context);
        int x = 150;
        int y = 150;
        int sideLength = 200;

        // create a rectangle that we'll draw later
        rectangle = new Rect(x, y, sideLength, sideLength);

        // create the Paint and set its color
        paint = new Paint();
        paint.setColor(Color.GRAY);
    }


    @Override
    protected void onDraw(Canvas canvas) {
      //  canvas.drawColor(Color.BLUE);

        canvas.drawRect(rectangle, paint);
    }

}

button created on the design layout view

enter image description here

Bex
  • 51
  • 1
  • 2
  • 9
  • 3
    "how would I prevent a second call from setContetView() from over writing my button?" - You don't. Instead, add your `CustomView` to a `ViewGroup` in your `activity_main` layout, using `ViewGroup#addView()`. – Mike M. Jul 03 '16 at 07:27
  • Mike, would you have any links/sources for this? I don't quite understand what you mean. – Bex Jul 03 '16 at 08:03
  • Have a look at [this post](http://stackoverflow.com/questions/10418929/how-to-add-a-view-programmattically-to-relativelayout). USKMobility gives another option in their answer below, if you'd rather declare your custom `View` directly in your layout instead. – Mike M. Jul 03 '16 at 08:09

1 Answers1

0

You can use your custom view in xml and use setVisibility method to hide or show custom view.

Your custom view will be :

public class CustomView extends View {

    private Rect rectangle;
    private Paint paint;

    public CustomView(Context context) {
        this(context,null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        int x = 150;
        int y = 150;
        int sideLength = 200;

        // create a rectangle that we'll draw later
        rectangle = new Rect(x, y, sideLength, sideLength);

        // create the Paint and set its color
        paint = new Paint();
        paint.setColor(Color.GRAY);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawRect(rectangle, paint);
    }
}

layout Xml file will be :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <yourpackagename.CustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/customView"
        android:layout_centerInParent="true"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Rectangle show/hide"
        android:id="@+id/btnshowhide"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

And don't forgot to update yourpackagename with your package name

Now your main activity :

public class MainActivity extends AppCompatActivity {

    CustomView customView;
    Button btnShowHide;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        customView = (CustomView) findViewById(R.id.customView);
        btnShowHide = (Button) findViewById(R.id.btnshowhide);

        btnShowHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(customView.getVisibility() == View.GONE){
                    customView.setVisibility(View.VISIBLE);
                }else{
                    customView.setVisibility(View.GONE);
                }
            }
        });

    }
}
USKMobility
  • 5,721
  • 2
  • 27
  • 34
  • Hi thanks for you help, but your code seems to be giving me an error with "setContentView(R.layout.activity_main);" any idea why? – Bex Jul 03 '16 at 12:17
  • I don't want to post all of it because I think some of it is unnecessary – Bex Jul 03 '16 at 18:29
  • Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class locale.techie.umesh.myapplication11.CustomView at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757) at – Bex Jul 03 '16 at 18:30
  • Caused by: java.lang.ClassNotFoundException: Didn't find class "locale.techie.umesh.myapplication11.CustomView" on path: DexPathList[[zip file "/data/app/com.example.hps.shapes-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) – Bex Jul 03 '16 at 18:31
  • it my mistake, please replace locale.techie.umesh.myapplication11 with your package name where you put your CustomView class – USKMobility Jul 04 '16 at 04:14