I have a top bar with a show/hide menu button. I would like to define the top bar, the button and the menu in the same layout.
I tried a layout with the height of the top bar, but the menu is clipped and is not visible. Is that possible to have a view larger than its parent. Otherwise, what's the standard way to do that?
For your question: Yes It's possible. Simple example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f"
android:layout_marginLeft="-50dp"
android:layout_marginRight="-50dp"
/>
</LinearLayout>
And ImageViewWidth == LinearLayoutWidth + 100dp.
Hope its help.
Related
I'm trying to make a layout with a main area and a bottom button bar. Unfortunately, the main area always overlaps the bottom bar. What am I doing wrong?
The simple code below just contains the main area and the bottom bar, but I can see in the designer that the main area goes to the bottom of the window, despite the constraint
app:layout_constraintBottom_toTopOf="#+id/layout__bottomButtons"
This is presumably because I also have to put in the following line:
android:layout_height="match_parent"
Android complains if you don't have this, so I am wondering how the constraints are supposed to work in this case?
Note: I have tried using "wrap_content" instead but in my real view the content is in a ScrollView, and the ScrollView overlaps the buttons unless I hack around with margins. I have to think I am missing aomething?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/layout__main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
app:layout_constraintTop_toTopOf="#+id/layout__main"
app:layout_constraintBottom_toTopOf="#+id/layout__bottomButtons"
app:layout_constrainedWidth="#+id/layout__main"
app:layout_constraintStart_toStartOf="#+id/layout__main"
app:layout_constraintEnd_toEndOf="#+id/layout__main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:backgroundTint="#color/black"
>
</LinearLayout>
<LinearLayout
app:layout_constraintBottom_toBottomOf="#+id/layout__main"
app:layout_constraintStart_toStartOf="#+id/layout__main"
android:id="#+id/layout__bottomButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:backgroundTint="#color/teal_200"
>
<Button
android:id="#+id/btn__reset_all_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="Reset"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
app:layout_constraintTop_toTopOf="#+id/layout__main"
app:layout_constraintBottom_toTopOf="#+id/layout__bottomButtons"
app:layout_constrainedWidth="#+id/layout__main"
app:layout_constraintStart_toStartOf="#+id/layout__main"
app:layout_constraintEnd_toEndOf="#+id/layout__main"
android:layout_width="match_parent"
android:layout_height="0dp"//match constraints is appropriate here
android:orientation="vertical"
android:backgroundTint="#color/black"
>
you used match parent on the layout__main. Now since the parent is the constraint layout it is going to cover the whole screen. Match constraints( denoted by 0dp) will solve your problem. Match constraints translates as cover as much screen that is available to me defined by the constraints i have
I have a list of items in a horizontal recycler view, and a fairly simple item that should only have a TextView set to wrap_content and an ImageView that should be aligned to the right of the layout.
Theoretically I should be able to just set a layout_weight of 1 on the TextView, but that causes a random space to randomly show up sometimes in the view depending on where it is in the recycler view.
Is there anyway to align the close icon to the right, while still keeping the layout/textview width as wrap_content? The amount of text can vary quite a bit, so I can't actually set a fixed width. I'm fairly certain that I can't just set a layout_gravity of end on the ImageView since the parent layout's set to wrap_content.
See layout attached.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corners"
android:minWidth="#dimen/min_width"
android:maxWidth="#dimen/max_width"
android:orientation="horizontal">
<WPTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|end"/>
</LinearLayout>
With out knowing what the items are supposed to look like and how it needs to behave it is hard to judge what the problem you are having is.
If the image is just supposed to be a small image to the right of the text you could just add a right drawable to the text view:
https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableRight
If you can replace the LinearLayout by RelativeLayout then you are good to with
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corners"
android:minWidth="#dimen/min_width"
android:maxWidth="#dimen/max_width">
<WPTextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv" />
</RelativeLayout>
Spacing between TextView and ImageView can be adjusted with margin on them. The properties used in the above code are self-explanatory.
I want to create activity with one button. When you click on the button, half screen layout appears from bottom.
And more when you click on background layout, new front background disappear.
Do you have an idea who to implement this?
Thanks in advance.
EDIT:
I have found solution for this feature.
Firstly, I make new hidden view below the screen( You can found how from the answers below).
And then, I animate on button click with the following code:
dialog.animate().translationY(-HEIGHT).setDuration(TIME);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- view with button -->
<LinearLayout
android:id="#+id/task_list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
<!-- example content hided in bottom -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/height_of_your_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/minus_height_of_your_content"
>
</LinearLayout>
</RelativeLayout>
To show hide content change it marginBottom. In start marginBotton should be - of this layout height to be out of screen. You can do it using animation on translateY and on end change marginBottom to finish value. This is one of possible solutions.
I want to set the logo of my app to the bottom left hand side of the screen and be in the background. the reason i want to do this is to allow the users to modify the background colors which would not be possible if i simply set the entire activity or layout background to an image to give same impression.
Another way of asking this is how do i set an image to bottom left hand corner and below other controls?
i don't know if i understand correct. but what you ask is pretty simple.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbbbb"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="bottom|left"
android:layout_margin="10dp"
android:src="#ffff00" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="vertical">
<!-- Put your views here -->
</LinearLayout>
</FrameLayout>
you can change the frame layout's background as you wish and keep logo on bottom left corner.
Also you can set your LinearLayout (which you can use any layout. it is up to you) background to transparent so you will see background. and any extra view inside the linear layout (your view container) will be on top always.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_desired_full_background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher_logo"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
what changes shall I make to bring the relative layout in bottom?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp"
android:layout_alignParentBottom="true"
android:layout_weight="0.08"
android:gravity="bottom" >
</RelativeLayout>
</LinearLayout>
You need to change your parent Layout to be a RelativeLayout, since the attribute android:layout_alignParentBottom does nothing for LinearLayout children:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp"
android:layout_alignParentBottom="true" >
</RelativeLayout>
</RelativeLayout>
You can achieve that by using a FrameLayout instead of a LinearLayout since it stacks childs from top to bottom by default.
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp"
android:layout_gravity="bottom" >
</RelativeLayout>
</FrameLayout>
use
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- replace this by any other layout or view. You might want something here, right? -->
<View android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp">
</RelativeLayout>
</LinearLayout>
There are two issues here. The first being that
android:gravity
is wrong. This sets the gravity for the contents of this Views children. So for example used in a TextView it would place the text at the bottom if the TextView. What you want instead is
android:layout_gravity
this sets the Views gravity within its parent.
The second problem is that android:gravity="bottom" doesn't work as you would expect in a LinearLayout who's orientation is vertical. I'm not sure I can explain why well so I will try to find a link in a bit that explains it. But if you change the orientation of the LinearLayout to horizontal you will see this.
So your best options are to change the orientation if that's a possibility and if not then change the root ViewGroup to a RelativeLayout and use the property android:alignParentBottom="true" as someone already suggested.
Edit for explanation
This blog post and this SO answer, among others explain it a little. But basically, if the orientation is horizontal then the Views are layed out from left to right as we expect so you can't control the left and right gravity. With a vertical orientation, the same is true for top and bottom gravity because where they are placed vertically is already determined by Android when you set that orientation. I hope this makes sense.