Rodriquez Rodriquez - 2 months ago 11
Android Question

How to add more views, buttons to activity

I have a problem. I make a class DrawingView with button etc. and on activity i call it

public class PaintActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawingView view=new DrawingView(this);
setContentView(view);
addContentView(view.btnEraseAll, view.params);
}


and here is my configuration

class DrawingView extends View {

private Paint brush = new Paint();
private Path path = new Path();
public Button btnEraseAll, btnAccept, btnBack;
public ViewGroup.LayoutParams params;

public DrawingView(Context context) {

super(context);
brush.setAntiAlias(true);
brush.setColor(Color.BLACK);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(22f);
btnEraseAll = new Button(context);
btnEraseAll.setText("Clear");
btnAccept = new Button(context);
btnAccept.setText("Accept");
btnBack = new Button(context);
btnBack.setText("Back");
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btnEraseAll.setLayoutParams(params);
btnAccept.setLayoutParams(params);
btnBack.setLayoutParams(params);
btnEraseAll.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {
path.reset();
postInvalidate();
}

});

}

@Override

protected void onDraw(Canvas canvas) {

canvas.drawPath(path, brush);
}

public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
default:
return false;
}
postInvalidate();

return false;

}


}

but how i can add more buttons(i have 3) to activity main?
here i add button

addContentView(view.btnEraseAll, view.params);


But i want add this buttons too

btnAccept, btnBack;


can anyone help me? I want to add 3 buttons in view.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@drawable/monitor"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/MyCustomActionBar">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".WiFiServiceDiscoveryActivity" />
<activity android:name=".PaintActivity" />
</application>

</manifest>

Answer

I refactored some of your code to make it easier to undertand. I also extracted the buttons in .xml instead of creating them programmatically because it is much easier. I also extracted the DrawingView to a separate class. I also tested it so it works for sure.

public class PaintActivity extends Activity {

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

        setContentView(R.layout.activity_paint);
        final DrawingView drawingView = (DrawingView) findViewById(R.id.drawing_canvas);
        View clearBtn = findViewById(R.id.btn_clear);
        clearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawingView.reset();
            }
        });
    }
}

class DrawingView extends View {

    private Paint brush = new Paint();
    private Path path = new Path();

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        brush.setAntiAlias(true);
        brush.setColor(Color.BLACK);
        brush.setStyle(Paint.Style.STROKE);
        brush.setStrokeJoin(Paint.Join.ROUND);
        brush.setStrokeWidth(22f);
    }

    public void reset() {
        path.reset();
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, brush);
    }

    public boolean onTouchEvent(MotionEvent event) {
        float pointX = event.getX();
        float pointY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(pointX, pointY);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(pointX, pointY);
                break;
            default:
                return false;
        }
        postInvalidate();
        return false;
    }
}

activity_paint.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.myapplication.DrawingView
        android:id="@+id/drawing_canvas"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btn_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Clear"/>

    <Button
        android:id="@+id/btn_accept"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Accept"/>

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Back"/>

</RelativeLayout>

ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="btn_clear" type="id"/>
    <item name="btn_accept" type="id"/>
    <item name="btn_back" type="id"/>
</resources>

You can also download the entire project from here: http://expirebox.com/download/130bc35765d0d4704c0e105627077281.html

Comments