Shift layout programmatically - android - android

I am implementing a approach where there is fragment and it contains the initial layout with some data. Now I want to shift that initial layout down if there is notification pops up and up if notification is gone. I am wondering how can I implement that ? How can I shift up and down the layout based on notification is available or not ?
Here is a design.
layout
Image-A is the initial layout with no notification and Image-B is the layout after additional layout(notification) at top.
Thanks

Just have the notification always in the layout, but set its visibility to GONE when it shouldn't show. That tells the layout to not show it and not take up any space for it. Then when you want to show it, set it to VISIBLE.

What you want to implement is called Banners and you can implement it using Motion layout, Here's more detail about them on material components
Follow this tutorial, you can implement it very easily.

You can use animateLayoutChanges as true on your root layout (considering that notification and body layout are in the same file)
<?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:animateLayoutChanges="true"
android:orientation="vertical">
<LinearLayout
android:id="#+id/notificationLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="12dp"
android:background="#color/orange_300"
android:orientation="vertical">
<!-- body here -->
</LinearLayout>
<LinearLayout
android:id="#+id/mainBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="12dp"
android:background="#color/blue_300"
android:orientation="vertical" />
</LinearLayout>
When you change notificationLayout visibility (VISIBLE OR GONE), you will get the animation you want.

Related

Why android studio designer is not working for relative layout

is not auto generating XML codes for relative layout, it only works for constraint layout.
When I try to position a view inside a relative layout it only gets positioned at the top corner of the screen.
<?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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout
What should I do?
I faced the same issue and the i tried the following step;
Click on the magnet icon (next to the eye icon, top left, next to the Palette menu) so that it's not crossed out: this turns on Autoconnect.
this will now help you to move the button where ever you want to place it.
Try it and let me know.
Yes if you pull the component on ConstraintLayout, it will guess what you want and set the attributes for you.
For RelativeLayout, you need to make the attribute view visible and edit from there. You need to edit parameters like these:
android:layout_below="#id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/times"

RecyclerView (wrap_content) inside of a BottomSheetDialogFragment

I'm facing a tricky situation here and I don't know how to solve this problem.
In my project I have a custom BottomSheetDialogFragment and in the layout a FrameLayout to add or replace Fragments.
Now I have a Fragment and inside I have a RecyclerView with the height:="wrap_content" because I want the BottomSheetDialogFragment only use the necessary space. Everything looks great, the problem appear when I put another view inside of the same layout and set the RecyclerView bellow or above of that view.
The RecyclerView ignores the size of the other view (or views) and always grows to the max screen size, and then it's no possible to see a few elements and even scroll.
I saw a solution, some developers are suggesting to add paddingBottom equals to the height of the view. But in my case doesn't works because I want to have a dynamic solution.
Above I'll share a few images of the problem and GitHub Repository with a sample.
Thanks for your attention!
I've manage to do what you need just need to use this as your fragment_sample.xml:
<LinearLayout 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="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rclItems"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<Button
android:id="#+id/btnAddMoreItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rclItems"
android:text="#string/add_1_item"/>
</LinearLayout>
Explanation
Using a LinearLayout gives you the ability to work with weight, and the vertical orientation allows you to place an item below the other. The weight on the recyclerview will increase the height of it as needed until filling the screen. The next item you add would be added to the recyclerview but you'll need to scroll the list to see it
The android developers blog says that :-
The scrolling containers in your bottom sheet must support nested scrolling .
Try changing your fragment_sample.xml as below to make the recyclerview scroll working and to make the add button persistent.
<?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="wrap_content">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:id="#+id/next"
android:layout_above="#id/btnAddMoreItems"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/rclItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.NestedScrollView>
<Button
android:id="#+id/btnAddMoreItems"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:text="#string/add_1_item"/>
</RelativeLayout>
Note: making bottomsheet layout a child view of CoordinatorLayout will allow you to get the implement BottomSheetBehavior and recieve its transitions callbacks .

Android half screen activity

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.

How to display a view on top of another view in same activity

I am have an activity in android application . It displays a view (lets say CurrentView)with a button in the centre of the screen . I am setting the content of the activity using :
setContentView(R.layout.layout_xml_file);
The layout xml file defines the button.
My requirement is that , when user clicks on the button , i need to show another view (lets say NewView)containing a listView on Current View.
The NewView transition should happen like this :
It should start from bottom of the screen and should take some proportion of the screen , say 3/4 or 1/2 . I mean the view's height should be configurable .
This NewView should be in the same activity . I do not want to create another activity for displaying it.
I don't understand which android widget is should use for holding the NewView
Thanks. Your help is appreciated ...
Take a Relative Layout align to bottom and set its height what you want and inside Relative Layout Take Listview after that setVisibility gone. And when click button in activity setVisibility Visible.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="128dp"
android:id="#+id/button" />
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#f0f0f0"
android:layout_below="#+id/button"
android:visibility="gone">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:divider="#000"
android:dividerHeight="1dp"/>
</RelativeLayout>
</RelativeLayout>
use LinearLayout - layout_weight attribute in layout.xml
View.setVisibility(View.VISIBLE or View.GONE)
perhaps use FrameLayout for both views parent layout. Initially the list should be inivisble and button visible.
I think you are looking something like this library ->
https://github.com/umano/AndroidSlidingUpPanel

Android UI: align a fragment above another fragment that is aligned bottom

I haven't developed for Android in more than a year and I'm a bit rusty with it. I'm trying to setup a kinda simple UI: a bottom bar at the bottom of the screen and a fragment above it (but without filling the whole height). Something like this:
I thought this would be quite simple, but after a while struggling with it I can't manage to make it work without some "hacks".
The bottom bar is also implemented as a Fragment. This is my XML:
<RelativeLayout 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:orientation="vertical" >
<fragment
android:id="#+id/bottomBar"
android:name="com.mytestpackage.BottomBarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/bottomBar"
android:layout_alignParentBottom="true" />
</RelativeLayout>
The Fragment in fragmentContainer is dynamically loaded from code. With the code paste above, fragmentContainer is aligned bottom but it's not above bottom bar, they're overlapped. If I remove the alignParentBottom from fragmentContainer, then it's placed in top of the screen.
Like I said, I found two "hacks" to solve it but I don't like them much:
1- Set a padding/margin bottom to fragmentContainer.
2- Use a filler empty layout on top of the screen and set fragmentContainer to be below that one.
Is there any way to achieve the layout I want without having to use some tricks like the ones I said?
Thanks!
Add to the relative layout:
android:gravity="bottom"
Ah, and android:orientation="vertical" is meaningless for RelativeLayout
A simpler solution would be to use a LinearLayout with vertical orientation and gravity bottom instead of the RelativeLayout.

Categories

Resources