1

What I want to make is that there is two buttons in left side, and imageView on the right side. The buttons activate the camera or bring a photo from the gallery and display it on the imageView. Also, if user touch the image, then a rect shape is following the move of user.

I made a xml file that contain the buttons and imageView. Then, I made a custom view for the rectangle shape using canvas.

What I was thinking is set touchListener on the imageView and according to the coordinates, app generate the rectangle on the canvas that overlapped on the imageView.

Here is my question, Can I use both a xml file and custom view at the same time? That means those two things can be overlapped?

I tried

v = new DrawingTheBall(this); // v is my custom view
setContentView(v);
setContentView(R.layout.activity_main);

no errors but, only the first called one appears.

If the overlapping is impossible, then should I just put the buttons and imageView in the custom view class?

2 Answers 2

2

Yes, you can use layout resource from XML and custom views created programmatically together. For this you need to:

  1. Specify view holder for custom view in XML and give it an ID (it can be root layout). For example:

    <LinearLayout android:id="@+id/customViewContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

  2. setContentView(R.layout.xml_resource);

  3. Find your custom view container:

LinearLayout custonViewContainer = (LinearLayout)findViewById(R.id.customViewContainer);

  1. Add custom view to container:

custonViewContainer.addView(view);

That's pretty much it!

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

1 Comment

Thanks, I will try it right away! You are a life saver~
0

You can also put your custom view in an XML layout file:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.package.DrawingTheBall
        android:id="@+id/drawingTheBall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Comments

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.