I just started learning Android and got a problem implementing a special layout. I read a lot about layouts and tried everything that crossed my mind, but i couldnt solve it. Probably its very easy and I would be very grateful for your help!
I need a simple layout, having a bigger view at the top and a smaller one at the bottom. The lower view should have a constant size of 150dp, the upper view should be variable in height and fill the rest . It is no problem giving the upper one a constant size and the lower one the rest - but thats not the way i need it...
Thats a simplified screenshot how it should look like:
Screenshot
Thanks a lot!
Here you go:
<?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:id="#+id/rl_layout_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
android:layout_above="#+id/rl_layout_two">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_layout_two"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#android:color/darker_gray"
android:padding="15dp"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>
This matches your expectation. Howerev, i would like to point out that generally, layout height or width are not specified.It is bad practice. I would suggest another option, using LinearLayouts with weights, find the code below.
<?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"
android:weightSum="1">
<RelativeLayout
android:id="#+id/rl_layout_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
android:layout_weight="0.4">
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_layout_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:padding="15dp"
android:layout_weight="0.6">
</RelativeLayout>
</LinearLayout>
Hope these help.
EDIT 1:
Check the 1st solution, it was missing the layout_above attribute, that is why it took the whole screen space and centered the image according to the whole screen space. Now, it will not, since layout one is above layout two, it will center your image according to layout one.
Related
I'm trying to achieve something like this:
as you can see the bottom sheet is expandable when there is more than one line of the recycleview.
I want to implement the same thing in the profile page so that if posts are more than three lines then I can expand the bottom sheet, then I can scroll the recycleview (when the sheet is expanded).
my current xml:
<LinearLayout
android:id="#+id/allPosts"
android:layout_below="#+id/userinfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:background="#drawable/rounded_behind_recycleview"
android:orientation="vertical"
android:elevation="1dp">
<com.taimoorsikander.myapplication.Widgets.B_RecycleView
android:id="#+id/recycle_view"
android:overScrollMode="never"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
^
it looks like the image I attached but it is not expandable yet.
is this possible in Android Studio? if yes, please help.
In this tutorial they teach you how to set the Behaviour in the BottomSheetFragment,
take a look it's really helpful, there you will find how to set the Behaviour that you want with BottomSheetBehaviour.from(...)...
Kotlin language
Basically you have to set the height(Ex: peekHeight = 50) you want than and set "false" to hide the bottomSheet and "true" to (isDraggable).
There is 2 ways of doing this declarative in the XML or programmatically, I'll show the first:
1º Your first layout needs to be CoordinatorLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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">
<include layout="#layout/bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
2º The bottomSheetLayout:
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="100dp"
app:behavior_skipCollapsed="true"
app:layout_behavior="#string/bottom_sheet_behavior">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardBackgroundColor="#81D4FA" />
</LinearLayout>
Pay attention to this TAGS, which states the behavior and state of the BottomSheet:
app:behavior_hideable="false"
app:behavior_peekHeight="100dp"
app:behavior_skipCollapsed="true"
app:layout_behavior="#string/bottom_sheet_behavior"
I'm new in android. I want to show activity by moving up and down. (We can see it in many music players). But I don't know how to do it. Please give me some solutions, examples or suggestions.
Как дела?
Suggestion: You may find all crazy animation, including 2D and 3D animation in the following link. I did not have enough time find the exact one but I am sure they exist in this following collection.
https://android-arsenal.com/tag/6?sort=created
Advice: You can easily achieve it with Animation APIs in Android SDK. Or you may find some relevant answers here
Make activity animate from top to bottom
Good luck.
Finally, I decided to use Android Bottom Sheet. Here is code example:
<?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"
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="340dp"
android:background="#android:color/darker_gray"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="80dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/colorAccent"
android:gravity="center"
android:text="#string/bottom_sheet_peek"
android:textColor="#android:color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/bottom_sheet_content"
android:textColor="#android:color/white" />
</LinearLayout>
I'm facing a problem right now that i tried to imply the color function background in the app:itemBackground="#color/purpleBoo"
but the color seems like not to fill in the entire bottom navigation? why is this happening to me?
my bottom navigation code
<?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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/purpleBoo"
app:menu="#menu/bottom_nav_menu"
app:itemIconTint="#color/bottom_nav_color"
app:itemTextColor="#color/bottom_nav_color"/>
</RelativeLayout>
For the current layout that you are using the bottom navigation panel can be maximized to width "match_parent" so the color will also be available inside the width section.
Well, in case the white space around the panel is disturbing the look of your app then you can change the background of the layout as a whole then that would seem better.
Just add the android android:background="#color/purpleBoo" in BottomNavigationView
<?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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/purpleBoo"
app:menu="#menu/bottom_nav_menu"
android:background="#color/purpleBoo"
app:itemIconTint="#color/bottom_nav_color"
app:itemTextColor="#color/bottom_nav_color"/>
</RelativeLayout>
But the same activity blows up loading on a zebra m6000. Let me state that I am a novice using Android Studio and Java. I have been a VB programmer for many years.
The XML has 2 includes. One foe each vertical linear layout. I can add the XML's for them if needed.
What could cause the rendering to work on one android device (which is running an earlier version of OS ) but fail on newer device.
Here is XML
<?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"
android:background="#color/colorBackgroundGray"
tools:context="com.procatdt.stockright.activities.PutAwayAreaActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="#+id/include2"
layout="#layout/frm1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="left" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:orientation="vertical">
<include
android:id="#+id/include"
layout="#layout/layout_numpad5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="220dp"
android:layout_weight="1"
android:layout_gravity="right"
/>
</LinearLayout>
</RelativeLayout>
Here is how it is rendering on MC67
Found Issue. One of the Buttons in my include was the culprit. KitKat didn't care but lollipop didn't like the backgroundtint value.
Looking through logcat I saw culprit was a button and from there it was process of elimination.
Below is the xml layout for my Android App. I am trying to positions a webview below the gridview on the screen.
Can anyone help explain what I need to do to get that to work?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/HomePage"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="#+id/Grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:scrollbars="vertical" />
<WebView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/webView1" />
</LinearLayout>
</LinearLayout>
Thanks
I think two minor changes will help make this work for you. First, when creating a LinearLayout, the android:orientation attribute is required to indicate whether the children should be added horizontally or vertically. Since you want one view on top of the other, add android:orientation=vertical to the HomePage LinearLayout. Second, your GridView currently has a android:layout_height of fill_parent. This will cause it to expand vertically and fill up the entirety of the LinearLayout, which will keep your WebView from being displayed. Try changing this attribute to wrap_content.
Making the height of both children wrap_content could possibly cause you to have some empty space in your app. If you need either the GridView or WebView to take up all of the empty space in the view, you can use the android:layout_weight attribute. Assign the view you want to expand to fill all remaining space after the rest are drawn the attribute like this: android:layout_weight=1. (Here, the 1 represents 100% of the remaining space) If you want more granular control over what percentage of the parent your children occupy, look into using decimal values with the android:layout_weight attribute or android:weightSum on the parent in conjunction with a android:layout_weight on each of the children. Hope this helps!
Use android:layout_height="wrap_content" in Gridview
Set orientation attribute in inner linear layout.
android:orientation="vertical"
Give fixed height of WebView and set android:layout_weight="" in both child.
Try below code and change according to your webview height and grid view height.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/HomePage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="2"
android:orientation="vertical">
<GridView
android:id="#+id/Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:scrollbars="vertical" />
<WebView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_weight="1"
android:id="#+id/webView1"
/>
</LinearLayout>
</LinearLayout>
In above code I give Height of WebView 200dp change according to your requirement.