I am trying to include Caldroid calendar in my activity. I want that the calendar ocuppy 3/4 of the screen like this:
But it always shows at the top of the screen.
This is my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/opciones"
android:layout_width="299dp"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/calendar1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
And this is how i attach the caldroid:
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.calendar1, caldroidFragment);
t.commit();
I search it in StackOverFlow but i dont find the solution.
Finally i solved it implementing my own custom_cell and modify the padding_bottom to increase the cell size.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_cell"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="68dp"/>
</LinearLayout>
It is one solution but maybe not the most elegant.
there are two way for this :
first way :
use android:layout_weight="" instead static size for LinearLayout
for example :
<LinearLayout
android:id="#+id/parentLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/options"
android:layout_width="0dp"
android:layout_weight="0.3"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/calendar1"
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Second Way :
using PercentLayout (work with percentage and maybe Clearer)
add Percent library to your app by adding this line to gradle dependency:
dependencies {
...
compile 'com.android.support:percent:24.1.2' //or any other versions
...
}
in XML layout :
<LinearLayout
android:id="#+id/options"
android:layout_width="0dp"
app:layout_widthPercent="30%"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_toRightOf="#id/options"
android:id="#+id/calendar1"
android:layout_width="0dp"
app:layout_widthPercent="70%"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
notice that :
add http://schemas.android.com/apk/res-auto" to first parent of layout
android:layout_toRightOf specify the second view(layout_below is possible too)
Related
I am doing adaptation of my app for Android TV.
I have RecyclerView with items. Each item includes three LinearLayouts.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/LLChannel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/item_touch_selector"
android:paddingLeft="#dimen/list_item_extra_padding"
android:paddingRight="#dimen/list_item_extra_padding"
android:weightSum="1"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/item_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0.8"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="#dimen/abc_list_item_padding_horizontal_material"
android:paddingRight="#dimen/abc_list_item_padding_horizontal_material"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/LLFav"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.1"
>
</LinearLayout>
<LinearLayout
android:id="#+id/LLURL"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.1"
>
</LinearLayout>
</LinearLayout>
My question is: can I focus these three LinearLayouts separately instead of full item (LLChannel) focus? How?
UPD 17:12 10.07.2020:
Adding this stuff for each LinearLayout didn't help me:
android:background="#drawable/item_touch_selector"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusRight="#id/LLFav"
UPD 12.07.2020: Any ideas how it would be reached?
UPD 13.07.2020: This was easy. I will answer on my own question later.
All of LinearLayouts must be wraped in one more LinearLayout.
I have some difficulties with designing my app. My screen should contain toolbar and some edit fields. these fields should be places in the upper part of the screen under the toolbar. In the end it should look like this (sorry for quality of the image):
For this I decided to break the screen height into four equals parts and to pose my edit fields in the second quarter. I tried to use the following code to acchieve this, but it doesn't help: all the smaller relative layouts take all the screen.
I also tried:
to substitute the parent Relative layout with Linear layout
to replace "fill_parent" with "wrap_content".
After these actions the smaller layouts simply disappear.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false" >
<include android:id = "#+id/toolbarLogo" layout="#layout/toolbar_logo"></include>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.25"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.25"
>
<EditText
.../>
<EditText
...
/>
<Button
.../>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5"
/>
</RelativeLayout>
Could you help me please with this problem?
Change your root layout to be a LinearLayout and ensure that the orientation is set to vertical. Change the layout_width of the children to be 1. See code below for an example:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f00"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0f0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00f"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f0f"/>
</LinearLayout>
Try It.. Set
android:layout_weight="1"
for each LinearLayout and
set
android:weightSum="4"
for Parent LinearLayout and
Like This Example..
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0ff"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#01a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#101"/>
</LinearLayout>
</RelativeLayout>
You can use google library PercentRelativeLayout with this library you can set width, height and margin of your views by procentege which is great because in all screen they look the same and of course it is not hard to code it. Here example:
<android.support.percent.PercentRelativeLayout
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">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentRelativeLayout>
you must add this line in your build.gradle
dependencies {
compile 'com.android.support:percent:23.2.0'
}
Official documentation by Google https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html
And for your case should look maybe like this:
<RelativeLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_heightPercent="10%"
/>
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/firstLayout"
android:orientation="vertical"
app:layout_heightPercent="40%">
<EditText
android:layout_width="200dp"
android:layout_height="60dp"/>
<EditText
android:layout_width="200dp"
android:layout_height="60dp"/>
<Button
android:layout_width="200dp"
android:layout_height="60dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/secondLayout"
app:layout_heightPercent="30%"
/>
</android.support.percent.PercentRelativeLayout>
Hope this helps.
I am novice in Android trying to make this layout:
Simply what I want to achieve:
The red container should take as much space as it needs but not as much that green container have to be shrinked. If there are too many items, red container will be scrollable. Green container is also always centered in orange one if there is space for it (if not it is actually still centered).
I don't know how to do it at all :( . Here is my try:
The problem is that I want to always maintain height of green container (minHeight does not work I can't understand why) and make the green container centered in orange one. I have problem with Scenario 2 (as you can see in the picture), this code works good in first scenario.
<LinearLayout 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">
<ScrollView
android:id="#+id/red_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_dark"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Items are here -->
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/orange_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_dark"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="#+id/green_container"
android:layout_width="100dp"
android:background="#android:color/holo_green_dark"
android:layout_height="50dp"
android:orientation="horizontal">
<!-- My content -->
</LinearLayout>
</LinearLayout>
</LinearLayout>
Edit: minHeight does not help:
Edit: image for user Illegal Argument:
Try setting property android:minHeight = "(whatever)dp" to orangeContainer. This worked for me in others "similiar" cases.
:)
Is this what you are looking for:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/list_items"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/background_dark" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/list_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#android:color/darker_gray"
android:gravity="center"
android:minHeight="200dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
Snapshot in ecllipse:
My app have an activity with layuot like this
<RelativeLayout
android:id="#+id/layoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundchatfixed">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layoutKhungChat"
android:id="#+id/svChat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/layoutChat">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_khungchat"
android:layout_alignParentBottom="true"
android:id="#+id/layoutKhungChat">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/khungchat_chat"
android:layout_centerVertical="true"
android:id="#+id/etChatbox"/>
</RelativeLayout>
</RelativeLayout>
When i touch my Edittext, only layout "layoutKungChat" push up.
Any solution to push up also Scrollview above it?
Thanks.
Try adding this to the activity tag in the manifest:
android:windowSoftInputMode="adjustPan"
use this code
<RelativeLayout
android:id="#+id/layoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundchatfixed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_khungchat"
android:layout_alignParentBottom="true"
android:id="#+id/layoutKhungChat">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/khungchat_chat"
android:layout_centerVertical="true"
android:id="#+id/etChatbox"/>
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layoutKhungChat"
android:id="#+id/svChat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/layoutChat">
</LinearLayout>
</ScrollView>
the one in the last always be written over the other layouts so if you want like buttons to be above an image layout as an example write it inside of layout below the image
I suddenly have the problem that Scrollview extends beyond the bottom of the screen so that even if you scroll all the way down it does not show all its content. The XML is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#FFBBBBBB"
android:orientation="vertical" >
<View
android:layout_width="100dp"
android:layout_height="300dp"
android:layout_margin="15dp"
android:layout_gravity="center_horizontal"
android:background="#FFDDDDFF"/>
<View
android:layout_width="100dp"
android:layout_height="300dp"
android:layout_margin="15dp"
android:layout_gravity="center_horizontal"
android:background="#FFDDDDFF"/>
</LinearLayout>
</ScrollView>
It doesn't get any simpler than that. Once you have scrolled all the way down (as indicated by the shape of the scroller) you should see the bottom white margin but instead this is what it looks like:
Compare with the top:
The bottom should look like the top, only reversed. This happens in the emulator, on real devices and in pretty much every Android version I have tried. I am at a loss as to what I am doing wrong (if anything...).
Please no guesses and no shooting from the hip! Only tested answers. I wasted enough time on this already as it is. Thanks.
After wasting much time in dead alleys I was finally put on the right track by this other SO thread: the issue was the layout margin on the LinearLayout. Apparently ScrollView doesn't like that, just as it doesn't like its child to be centered (an issue flagged by many other people but not my problem here) and who knows what else. Very choosy widget. It's issues like this that make me reconsider my commitment to Android: it's just too time-consuming relative to alternative platforms and, even if you enjoy the challenge, time is money.
Anyway, for the benefit of those who will flop here later, here is a side-by-side demonstration of a broken layout (a simpler version of the one above) on the left and a working one on the right. The trick is to emulate the verboten margin with padding on an extra container.
<?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="wrap_content"
android:background="#00FFFF"
android:orientation="horizontal"
android:baselineAligned="false">
<ScrollView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#FFBBBB22"
android:orientation="vertical">
<View
android:layout_width="100dp"
android:layout_height="1000dp"
android:layout_margin="15dp"
android:layout_gravity="center_horizontal"
android:background="#FFDDDDFF"/>
</LinearLayout>
</ScrollView>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#FF000000"/>
<ScrolllView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="#FFFFFF"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFBBBB22"
android:orientation="vertical">
<View
android:layout_width="100dp"
android:layout_height="1000dp"
android:layout_margin="15dp"
android:layout_gravity="center_horizontal"
android:background="#FFDDDDFF"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
try putting a padding on the bottom of the scroll view, so that you see a white line at the bottom - your view indeed does scroll all the way down , i tried it with this code and the result is this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#FFBBBBBB"
android:orientation="vertical" >
<View
android:layout_width="100dp"
android:layout_height="1500dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="#FFDDDDFF" />
<View
android:layout_width="100dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="#FFDDDDFF" />
<View
android:layout_width="100dp"
android:layout_height="10dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="#FF00FF00" />
</LinearLayout>
Just try putting the LinearLayout inside another one
Like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#FFBBBBBB"
android:orientation="vertical">
<View
android:layout_width="100dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="#FFDDDDFF" />
<View
android:layout_width="100dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="#FFDDDDFF" />
</LinearLayout>
</LinearLayout>
</ScrollView>