Add layout elements to Layout with a View - android

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

Related

How to align a layout to left while both left and right constraints are applied to it inside Constraint Layout?

This is the layout I want to design:
As you can see from the image, I want the linear layout to be constrained to both Item A and Item B and I want it aligned to left. I couldn't find any way to implement this inside a Constraint Layout. When I use this:
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="#dimen/ui_size_xs">
linear layout moves to the middle.
When I use this:
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="0dp"
android:layout_height="#dimen/ui_size_xs">
the linear layout occupies the whole area between Items A and B.
I want the linear layout to be wrap-content but should also align to the left like in the image. The only solution I could find is to create a new layout as parent of this linear layout and give 0dp to new parent linear layout and wrap-conent to this layout child layout. I don't want to do that. Is there any way to achieve this without creating any extra layout?
As mentioned in offical docs,
The default when encountering such opposite constraints is to center the widget; but you can tweak the positioning to favor one side over another using the bias attributes:
layout_constraintHorizontal_bias
layout_constraintVertical_bias
Try the following and see if it works
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="#dimen/ui_size_xs"
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintLeft_toRightOf="item1id"
app:layout_constraintRight_toLeft="item2id">
// try different horizontal bias values to meet your need

Android : Combining xml file and custom view

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>

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.

Dynamically add imageViews in a circular layout in Android

I want to add imageViews dynamically in a circle like a round plate.
Android dose not provide a circular layout so please help me to create a circular layout..
Thanks in advance!
Just check this out , this link shows how to make view in circular shape... just imlement your image view in it ...
http://developer.samsung.com/android/samples-4
or you can do this by xml :
<LinearLayout orientation=vertical for whole screen>
<LinearLayout weight=1 gravity=center> <!--- Top --->
</LinearLayout>
<LinearLayout weight=1 orientation=horizontal> <!-- middle -->
<LinearLayout weight=1 gravity=left/>
<LinearLayout weight=1 gravity=right/>
</LinearLayout>
<LinearLayout weight=1> <!-- botton -->
<LinearLayout weight=1 gravity=center/>
<LinearLayout weight=1 gravity=center/>
</LinearLayout>
</LinearLayout>
You don't need a special layout for this.You can use a Relative Layout or a Frame Layout.
Just add the views dynamically in the normal way but position them using margin, padding etc so that they look like a circle.
From your question I can see that you need a circular layout on Android to arrange your images. I think this library will help you doing this. this library gives you the ability to add item, rotate them with animation. Check it out!
You can use HorizontalScrollView.

Can't get layout to work correctly with dynamic textviews in a nested linearlayout

In an XML layout that is a RelativeLayout I have a nested LinearLayout. Within this LinearLayout I dynamically add a few TextViews and buttons. My problem is, I can't get the items to appear under each other like would naturally happen within a LinearLayout. Here is the basic setup:
LinearLayout mobLayout = (LinearLayout) findViewById(R.id.mobButtons);
mobLayout.removeAllViewsInLayout();
I remove all previous junk inside the layout because I reuse it.
mobLayout.addView(mobName);
mobLayout.addView(mobTextHP);
mobLayout.addView(fightButton);
mobLayout.addView(goBackButton);
These should appear one on top of the other, but instead appear all side by side. When I tried adding LinearLayout.LayoutParams to the first one, it wiped everything after it, or pushed it off screen, I couldn't tell.
Lastly, here is the LinearLayout XML area where these items are added:
<LinearLayout
android:id="#+id/mobButtons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/fightText">
</LinearLayout>
Thanks in advance!
Use this option for your LinearLayout
android:orientation="vertical"

Categories

Resources