I need 2 relative layouts to overlap in a parent layout. I'm not sure what the parent layout should be, but I think it has to be FrameLayout.
<FrameLayout>
<RelativeLayout id=layout_one>
<Button></Button>
<ImageView></ImageView>
...
</RelativeLayout>
<RelativeLayout id=layout_two>
<Button id = a></Button>
<Button id = b></Button>
...
</RelativeLayout>
</FrameLayout>
Only one of the RelativeLayouts will be seen at a time. At first it is "layout_two". Then when Button "a" is clicked "layout_two" needs to be invisible and "layout_one" must appear. However, clicking Button a doesn't call onClick method, although I can see the button. Is there better way of doing what I'm trying to do?
Make Relative Layout as parent and then put your layouts on it.what I suggest is use Relative.
Related
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.
I'm new to programming. I was using Graphical Layout then when I was reading xml file, I saw FrameLayout. Then I searched, but I couldn't find something useful. What is FrameLayout and what does it do?
You use a FrameLayout to stack child views on top of each other, with the most recent child on top of the stack. In the example below, the TextView is the most recent, so it is automatically placed on top of the ImageView.
For example:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/backgroundImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="#drawable/bitmapie" />
<TextView
android:id="#+id/descTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="70dp"
android:background="#android:color/holo_blue_light"
android:padding="10dp"
android:text="TextView placed at the top of the Imageview"
android:textColor="#android:color/white"
android:textSize="22sp" />
</FrameLayout>
Output:
FrameLayout is the simplest implementation of ViewGroup. Child views are drawn are in a stack, where the latest added view is drawn at the top. Usually you can use one of the next approaches or combine them:
Add a single view hierarchy into FrameLayout
Add multiple children and use android:layout_gravity to navigate them
Another popular approaches of using FrameLayout:
as a Fragment container
as an ancestor of your custom ViewGroup
You can consider the word frame as regular photo frame. What you do with that frame? you can place photos in that frame by one top to another. Same as in FrameLayout we can place views ( Any layout, or widget like button, text, image so on) top of other as #ojonugwa shows you the textview top of the Image.
Are you sure that you googled it?
http://www.tutorialspoint.com/android/android_frame_layout.htm
Frame Layout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
You can, however, add multiple children to a FrameLayout and control
their position within the FrameLayout by assigning gravity to each
child, using the android:layout_gravity attribute.
http://blog.neteril.org/blog/2013/10/10/framelayout-your-best-ui-friend/
The secret of FrameLayout is how it layouts its children. Although normally designed to contain one
item, it will happily stack up other element on top of each other.
Thus FrameLayout is essentially a way to manipulate the Z-order of
views on the screen.
This is super useful for a couple of UI tricks from HUD-like elements
to sliding panels to more complex animated transitions. In this post
we will see an example for each of those.
http://www.learn-android-easily.com/2013/05/frame-layout-in-androis.html
FrameLayout is designed to display a single item at a time. You can
have multiple elements within a FrameLayout but each element will be
positioned based on the top left of the screen. Elements that overlap
will be displayed overlapping. I have created a simple XML layout
using FrameLayout that shows how this works.
Basically it puts one view on top of another for example :
Inflating text on Image
<FrameLayout>
<ImageView>
<Textview>
</FrameLayout>
Simple question, i have this code
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
...
<LinearLayout
android:id="#+id/insideLinearLayout1"
...
<TextField>
android:text='beforeClick'
...
<Button
android:id="#+id/button1"
android:onClick="updateExpression"
...
</Linear Layout
<LinearLayout
android:id="#+id/insideLinearLayout2"
...
<TextField>
android:text='beforeClick'
...
<Button
android:id="#+id/button1"
android:onClick="updateExpression"
...
</Linear Layout
</LinearLayout>
</HorizontalScrollView>
The idea, is to change the textField text property, when clicking the button on the same layout!
Ok for 2 is easy, just need the reference of each button and each textfield and change use the get and set method!
But i want to add more layouts dynamically, and grab each one's reference would be a hard task.
So two questions :
How can i grab the id of the Linear layout, that have the clicked button?
I want to handle the 'insideLinearLayout(1 or 2 depending on the clicked button)'id in the 'updateExpression'!
How can i add more layouts with the same widgets as the ones created manually?
Thank you in advance.
Best of codings!
1)Your onClick function is passed the view that was clicked. Every view has a getParent() function. You can use it to get the LinearLayout, then get the id.
2)Create them with the new keyword then add them to the parent layout. For something like this I would probably make a custom compund view holding everything you want to instantiate at once, so you can treat the entire set of widgets as one.
My current layout displays activity that is not full screen (that's OK).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dip"
android:layout_height="200dip" >
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="fill_parent" android:text="#string/hello"></TextView>
I also added android:theme="#android:style/Theme.Translucent" to manifest for my activity.
My 200x100dip activity now shows in the upper left corner. How can i specify position of my linear layout (or my activity)?
You can use either FrameLayout or RelativeLayout as outer most layout for this. Ant then use absolute position in dp or android:layout_centerInParent or similar.
I believe your Activity´s outmost layout element (LinearLayout) will be placed in a FrameLayout that is the parent given from Android. I suggest you let your outmost layout match_parent/fill_parent in layout_height and _width and then center the content inside it with gravity="center" on your outmost layout. By letting the outmost layout being transparent and not catch click element it will appear as layout in the middle where elements behind is visible. If Im correct guessing that's what you want to achieve here.
put that layout in another absolute layout in which you use android:layout_width="fill_parent" and android:layout_height="fill_parent" the other thing you can do is to use this: http://www.droiddraw.org/
you can move your elements around manually with that and it will give you the XML code that is used to do that. I found it very useful in laying out XML in android.
I need to implement the layout as in the picture. Parent and Sibling are in a vertical LinearLayout. So I need to make a child view to overlap it's parent. Can I do that in android?
If:
sibling is a sibling of parent
parent is a ViewGroup
and you really want child to be a child of parent
then maybe you could consider using android:clipChildren set to false on parent.
I was actually just looking at an example of a FrameLayout that had a TextView overlaid on top of an ImageView. So, there are obviously multiple ways to get it done. Your next question might be which one is best ... to that I have no idea, but here's a guy that might:
http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/
Just contain them all within a RelativeLayout, and remember the draw order is top to bottom, so put the top most view on the bottom of the XML definition.
If you use a RelativeLayout you should have no problem achieving this effect. By default it will stack all of its children on top of each other in the top left corner if you don't supply them with android:layout parameters. So it will definitely support overlapping children. You'd just have to figure out what the best way to tell it where the child should go on the screen relative to something else.
There are at least two layouts that can do that. AbsoluteLayout and RelativeLayout. I suggest that you put your views in a RelativeLayout and add them with LayoutParams that specify their offset form the top and left of the parent:
RelativeLayout.LayoutParams rlp;
label = new TextView(ctx);
label.setBackgroundColor(0x00000000);
label.setTextColor(0xFF7ea6cf);
label.setTextSize(13);
label.setGravity(Gravity.LEFT);
label.setText("Examples:\n- Fentanyl\n- Dilaudid 2 mg PO q 4 hours prn moderate pain");
rlp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,100);
rlp.topMargin=189;
rlp.leftMargin=30;
rlp.rightMargin=30;
rlParent.addView(label,rlp);
In my case, I have to set android:clipCildren to be false on the parent of parent.
i.e.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:id="#+id/parent1">
<FrameLayout
android:id="#+id/parent2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="64dp"
android:background="#android:color/holo_blue_bright">
<View
android:id="#+id/This_is_the_view_I_want_to_overlap_parent2"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_gravity="top|start"
android:layout_marginTop="-40dp"
android:background="#000000" />
</FrameLayout>
</FrameLayout>