How to show one layout on top of the other programmatically in my case?
Use a FrameLayout with two children. The two children will be overlapped. This is recommended in one of the tutorials from Android actually, it's not a hack...
Here is an example where a TextView is displayed on top of an ImageView:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/golden_gate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</FrameLayout>
Is it possible to place one view over another in android?
FrameLayouts
let you pile each view on top of the one below. This can also be achieved with a RelativeLayout
.
how to put 2 layouts on top of each others
Use Relative layout to place views on top of each other , like this
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:background="#f00"
android:orientation="vertical"
android:weightSum="1" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="170dp"
android:layout_centerInParent="true"
android:layout_gravity="bottom|center"
android:background="#fff"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="10dp"
android:layout_height="100sp"
android:layout_centerInParent="true"
android:layout_marginLeft="10sp"
android:background="#37c100" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6sp"
android:background="@drawable/arrow_shape" >
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:layout_margin="25sp"
android:layout_weight="0.5"
android:background="#0000ff"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="40 000 "
android:textColor="#ffffff"
android:textSize="25sp" />
</LinearLayout>
</RelativeLayout>
How would I display one view as an overlay of another?
Use a RelativeLayout
for starters.
You could use the ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params)
or a variant along with ViewGroup.removeView(View view)
or ViewGroup.removeViewAt(int index)
.
This would obviously require you to inflate the views manually using LayoutInflater
but you could keep a global reference to each after inflating and just flip between the two.
Android ConstraintLayout - Put one view on top of another view
Set an elevation on the ProgressBar
; 2dp
seems to work.
android:elevation="2dp"
You could also try setting translationZ
as suggested in the accepted answer to a similar question.
I also came across this answer as an alternative.
Programmatically add view one below other in relative layout
Important: Remember to set the ID for each view.
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("textView1");
TextView tv2 = new TextView(this);
params2.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
tv2.setId(2);
tv2.setText("textView2");
TextView tv3 = new TextView(this);
params3.addRule(RelativeLayout.BELOW, tv1.getId());
tv3.setId(3);
tv3.setText("textView3");
TextView tv4 = new TextView(this);
params4.addRule(RelativeLayout.RIGHT_OF, tv3.getId());
params4.addRule(RelativeLayout.ALIGN_BOTTOM, tv3.getId());
tv4.setId(4);
tv4.setText("textView4");
layout.addView(tv1, params1);
layout.addView(tv2, params2);
layout.addView(tv3, params3);
layout.addView(tv4, params4);
How to define a layout with two parts in my case
I am thinking to define two separate layout files, e.g. yellow_area.xml & green_area.xml, and includes them in one main.xml.
If you are wanting to re-use one or both of them then doing this or using fragments
is a fine idea...whichever works best in your case.
But how to keep yellow area always visible & have green area scrollable? (That's to make yellow area looks like it is above green area)
You should be able to accomplish this by using a RelativeLayout and using the property
android:layout_below="@id/topLayout"
in the <include>
tag for your "greeen layout
assuming that topLayout
is the id
of your "yellow" layout
.
Edit
I think my above answer didn't work due to the number of View
s inside of the included layout
. Using a LinearLayout
as the parent layout
and using layout_weight
inside of each child layout
( and ListView
fixes the problem. Something like
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
>
<!--included a title_layout-->
<include
android:id="@+id/upper_area"
android:layout_width="fill_parent"
android:layout_height="0dp"
layout="@layout/title_layout"
android:layout_weight=".25"/> // add weight here
<!--Listview below title_layout-->
<ListView
android:id="@+id/data_list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".75" /> // and here
</LinearLayout>
Using layout_weight
with a vertical
orientation the height
must be set to 0dp
(layout_width="0dp"
for horizontal orientation)
How to create a RelativeLayout programmatically with two buttons one on top of the other?
Found the answer in How to lay out Views in RelativeLayout programmatically?
We should explicitly set id's using setId(). Only then, RIGHT_OF rules make sense.
Another mistake I did is, reusing the layoutparams object between the controls. We should create new object for each control
View over another View use RelativeLayout or?
You can use FrameLayout
or RelativeLayout
. But I recommend RelativeLayout
as it is more flexible and easier to use.
Related Topics
Gmail 5.0 App Fails with "Permission Denied for the Attachment" When It Receives Action_Send Intent
How to Highlight Row in Listview in Android
Android: Detect Orientation Changed
Urlconnection or Httpclient: Which Offers Better Functionality and More Efficiency
Unzip a Zipped File on Sd Card in Android Application
Android Mediarecorder - "Start Failed: -19"
How to Get Contacts from Native Phonebook in Android
Place Imageview Over Button Android
How to Use the Gradle Build System for Android with Eclipse
Android Volley Self Signed Https Trust Anchor for Certification Path Not Found
What's the Maximum Size for an Android Shared Preference Value
Google Maps API V2 'Failed to Load Map. Could Not Contact Google Servers'
Android: Programmatically Adding Buttons to a Layout
Animate Progressbar Update in Android
Livedata Prevent Receive the Last Value When Start Observing