Android : Combining xml file and custom view - android

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?

Yes, you can use layout resource from XML and custom views created programmatically together.
For this you need to:
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">
setContentView(R.layout.xml_resource);
Find your custom view container:
LinearLayout custonViewContainer = (LinearLayout)findViewById(R.id.customViewContainer);
Add custom view to container:
custonViewContainer.addView(view);
That's pretty much it!

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>

Related

ViewGroup - Add XML content into different container within inflated ViewGroup

Is it possible to inflate a LinearLayout from XML that contains some static objects and another LinearLayout and later when "XML" code is used inside the LinearLayout it's content is being added inside the inner LinearLayout.
Explanation with some code removed:
<LinearLayout id="main">
<LinearLayout id="top">
<TextView text="This is always here" />
<ImageView src="#drawable/image_alwayshere" />
</LinearLayout>
<LinearLayout id="bottom">
</LinearLayout>
</LinearLayout>
This is then inflated by my View "CustomLinearLayout" or whatever we call it
and when using the View in another Layout:
<com.my.views.CustomLinearLayout>
<ImageButton id="button1" src="#drawable/button1" />
</com.my.views.CustomLinearLayout>
In this case, the ImageButton should not be added below "bottom" it should be added into it. SO whatever I have in top, stays static and whatever I want to change is added to the Bottom LinearLayout.
Is this possible and if it is how could it be done?
Not sure if it's good or bad practice, if it would work. But if I have a constant layout (top container, middle container and bottom container) and I have 10 different activities and the only one changing content is the middle one, I can easily make one change to the top and bottom container at one place instead of 10 places and have whatever "View" I want to show in my activity be added inside.
Maybe I need to create a whole new ViewGroup for this? But currently working on LinearLayout since it's functionality is pretty much what I need.
If not, then what I'm looking for is where and when a LinearLayout reads the content of the XML and then override that to be added to my inner LinearLayout instead.
It is possible, you can either use include tag to add the other xml layout into the "bottom" layout and make its visibility as "gone" and change it to "visible" when you need it.
Or you can do that dynamically and inflate the xml whenever you want then add it to "bottom" using ViewGroup.addView(View) method.

showing a grid like View on top of a custom View

I want to show up a 3*3 grid like matrix view on top of a custom View.(That custom View is used as a view for displaying a ball acting upon the accelerometer). What I wanted to achieve is when the ball on that custom View is acted upon by accelerometer, it would move along the custom View(which I named "mSimulationView"), passing through any one of the 9 grid like boxes. Passing through any of the boxes(grid) triggers a directional control as that of joypad. I want to make the boxes(grid) visible to the users and also change the color of the box(grid) as the ball passes through it.
The problem is I have no idea about how to add the grid view on top of the custom View. If I add "table Rows" or "Grid Views", it would block the custom View completely. I was thinking of using "GLSurfaceView" for this purpose,but I am not sure if that will work or not either. I am trying to make a gravity Simulated Joypad for android.v2.1.
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>
I need a 3*3 grid like structure on top of the linearLayout.

Add layout elements to Layout with a View

I'm trying out the Finger Paint api demo and I'm trying to add buttons to it. I have included
<view class="com.triopsys.imosandroid.ViewControllers.SignatureViewController$MyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
In my layout xml, but I can't add buttons on the top or bottom of this view.
You can't add buttons to a view... You need a view group ... Linear Layout/Relative Layout.... Rather than trying to do the markup yourself use the visual editor and add two buttons and then take a look at the xml it should point you in the right direction

how to control the transparency of a view in a RelativeLayout in android?

I have a RelativeLayout that holds a set of views and i would like that one of the views will
appear in front of all the other views? how can i do that ?
Thanks
EDIT :
OK, i will be more specific.
here is the code of my relativelayout, this is where i put all of my view.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cfff"
android:id="#+id/myMap">
i'm adding view to this relativelayout this way :
creating different views......
relativelayout.addView(view);
now, some of the view intersect with each other and when this happens i want to control which view will be on top of the other one. so how can i do that ?
Thanks.
The view will be ordered on the Z-axis in the order that they are added. So the first view will be the further in your layout. The next view added will be on top of the first view and so on.

creating a camera view with button for android

i have set up an activity that loads up the camera and allows me to preview it but i need to add a button to the screen but the only way i can get the screen to display is by using the following layout:
<android.view.SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.view.SurfaceView>
is there a way for me to add a button to the view?
Put your SurfaceView and button into a RelativeLayout. Both of those types of layouts allow views to overlap so your button will be on top of your SurfaceView. The set up would be something like this
<RelativeLayout>
<SurfaceView></SurfaceView>
<Button></Button>
</RelativeLayout>
DeeV's answer is correct for the RelativeLayout part. The FrameLayout isn't intented to contain multiple children. So the RelativeLayout is the way to go.

Categories

Resources