I have a linear layout which contains a set of child views (mixture of text, images and other layouts).
I want to grey out the linear layout and all the child views so they look disabled (similar to how a button works).
Is this possible to achieve? I know i can set a colour filter when painting on the canvas, is there something similar for layouts?
Another way is to call setEnabled() on each child (for example if you want to do some extra check on child before disabling), check out this answer:
https://stackoverflow.com/a/7069377/4840812
You can use Frame-layout
Using TEST ONE you can achieve above text view like disabled. You can set root layout clickable false for actually disable views inside layout.
TEST TWO is for understanding purpose. Like you need to manage order of view to show effect.
<?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">
<!--THIS IS TEST ONE-->
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="150dp"
android:background="#000000"
android:gravity="center"
android:text="TEST DATA"
android:textColor="#android:color/white"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#DAD6D6D6"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/colorAccent"
android:textSize="30sp" />
</LinearLayout>
<!--THIS IS TEST TWO-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="300dp"
android:background="#DAD6D6D6"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/colorAccent"
android:textSize="30sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="400dp"
android:background="#000000"
android:gravity="center"
android:text="TEST DATA"
android:textColor="#android:color/white"
android:textSize="30dp" />
</FrameLayout>
To achieve your layout you can use the RelativeLayout. In RelativeLayout you can use the 2 different views or LinearLayout. In first layout you can add views according to your need, and In second layout you can set the alpha colorcode for disable the layout.
Please try below code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/demo_img_1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/demo_img_1"
android:layout_marginLeft="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/trans"/>
</RelativeLayout>
Here is output for code:
Related
I want to display a custom text (mainly person info) inside an image like it's shown above. How can I do it in android? For example, I have a known size of this image (it differs from device to device) and want to display it with the different text size inside it in the special place.
You can use, for example, RelativeLayout.
Make your ImageView be match_parent and use some container for your text lines in bottom of parent RelativeLayout.
Example:
<?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">
<ImageView
android:scaleType="fitXY"
app:srcCompat="#drawable/ic_launcher_background"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:padding="20dp"
android:background="#92fffaaa"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:textSize="20sp"
android:text="Some test text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Result:
Try this (adjust dimensions as your requirement)
<RelativeLayout
android:layout_width="200dp"
android:layout_height="150dp">
<ImageView
android:scaleType="centerCrop"
android:src="#drawable/your_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:background="#64000000"
android:padding="8dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Title"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="Sub Title"
android:textColor="#ffffff"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I've done something similar previously.
Use Framelayout.
Add ImageView in frame layout with match_parent constraints.
Add LinearLayout having background="#80000000" and layout_gravity="center" in FrameLayout which contains your two TextViews.
Let me know in case this does not work.
I need to implement the following UI:
For this, I am using the following layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<ImageView
android:id="#+id/news_image0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="#dimen/news_cell0_imageview_min_height"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<RelativeLayout
android:id="#+id/news0_cell_image_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/news_image0"
android:layout_alignTop="#+id/news_image0"
android:layout_alignLeft="#+id/news_image0"
android:layout_alignStart="#+id/news_image0"
android:layout_alignEnd="#+id/news_image0"
android:layout_alignRight="#+id/news_image0"
android:background="#drawable/news_cell0_overlay_gradient">
<TextView
android:id="#+id/news_title0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_vertical|start"
android:layout_alignParentBottom="true"
android:maxLines="3"
android:ellipsize="end"
android:textColor="#color/colorNewsCellType0TitleText"
android:layout_margin="#dimen/news_cell0_textview_margin"
android:textSize="#dimen/news_cell0_title_text_size"
android:typeface="monospace" />
</RelativeLayout>
</RelativeLayout>
Into the ImageView I am loading image using Picasso from a URL. I am getting output correctly in most of the devices, but in some of the devices running android less than Lollipop, this shows up like the screenshot below. The text shows up at the top, instead of bottom. But I have set android:layout_alignParentBottom="true" for the TextView. The RelativeLayout is rendered correctly, and it fits the image(it has a background gradient, which can clearly be seen at the bottom of the image).
What is causing this problem and how can I solve this?
You can try with FRAMELAYOUT
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="#drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<TextView
android:text="Hi.."
android:textSize="17sp"
android:textStyle="bold"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center|bottom"/>
</FrameLayout>
You can try this layout,
<?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="wrap_content"
android:background="#android:color/white">
<ImageView
android:id="#+id/news_image0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="100dp"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/news_title0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/news_image0"
android:layout_centerHorizontal="true"
android:ellipsize="end"
android:maxLines="3"
android:padding="12dp"
android:text="fsdsdfdsdsfsdfsdf"
android:textColor="#f00"
android:typeface="monospace"/>
</RelativeLayout>
hello i am trying to wrap a textview that has a long text. but when i test it, it just goes beyond the screen. What im trying to do is it will go to the next line if the text is long and it automatically adjust the layout. I am using this in a listview.
i am referring to this old question Android TextView Text not getting wrapped, it works on him so i tried to implement it but it wont effect on mine. thanks for answering.
here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="#e6e6e6"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="4dip"
android:layout_weight="1">
<TextView
android:id="#+id/tv_reviewer_name"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceLarge"
android:text="Reviewer Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="5dp"
android:id="#+id/tv_review_details"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceSmall"
android:text="Review"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:width="0dip"/>
</LinearLayout>
</LinearLayout>
UPDATE
here's my listview:
<ListView
android:divider="#android:color/transparent"
android:dividerHeight="2.0sp"
android:id="#+id/lv_reviews"
android:background="#f2f2f2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
You can use the inputType to auto manage a multiline text.
<TextView
android:layout_marginTop="5dp"
android:id="#+id/tv_review_details"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceSmall"
android:text="Review"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:width="0dip"
android:inputType="textMultiLine"
/>
if i use listView or recyclerView, i well put the layout width match parent as mentioned below:
<LinearLayout
<!--change to match_parent-->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:padding="4dip"
android:layout_weight="1">
try it once, hope this will help you.
problem solved. it turns out android studio didnt update my changes when i ran my app. i uninstalled the app and then run it again and it works perfectly.
When you are using Gravity in vertical orientation, you have to use layout_height=0dp & when you are using Gravity in Horizontal orientation then you have to use layout_width=0dp for every child of Linear Layout so that it can resize the layout by itself
<LinearLayout
android:background="#e6e6e6"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="4dip"
android:layout_weight="1">
<TextView
android:id="#+id/tv_reviewer_name"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceLarge"
android:text="Reviewer Name"
android:layout_width="wrap_content"
android:layout_height="0dp" />
<TextView
android:layout_marginTop="5dp"
android:id="#+id/tv_review_details"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceSmall"
android:text="Review"
android:layout_height="0dp"
android:layout_width="match_parent"/>
</LinearLayout>
</LinearLayout>
I have a layout with 1 layout inside of it which is all inside a ScrollView. I am trying to get the whole screen to scroll with the content inside of the listview then the textviews underneath that listview. I get the whole listview to populate and show correctly but the bottom layout with all the textviews do not show up.
<?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">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_below="#id/app_bar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="#+id/bannerReceiptLogo"
android:layout_width="170dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/img_logo_receipt_cub" />
<TextView
android:id="#+id/bannerAddressHeader"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#id/bannerReceiptLogo"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="#string/storeHeader"
android:textAlignment="center"
android:textSize="18sp" />
<ListView
android:id="#+id/fullEReceiptListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bannerAddressHeader"
android:layout_marginTop="10dp"
android:scrollbars="none"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/fullEReceiptListView"
android:layout_weight="1">
<View
android:id="#+id/totalDivider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#color/colorGrey"
/>
<TextView
android:id="#+id/txtSubTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBTOTAL"
android:textSize="20sp"
android:layout_below="#+id/totalDivider"/>
<TextView
android:id="#+id/txtSubTotalFinal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$13.58"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/totalDivider"/>
<TextView
android:id="#+id/txtTaxText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAX"
android:textSize="20sp"
android:layout_below="#+id/txtSubTotal"/>
<TextView
android:id="#+id/txtTaxTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$0.80"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/txtSubTotalFinal"/>
<TextView
android:id="#+id/txtCompleteTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:textSize="20sp"
android:layout_below="#+id/txtTaxText"/>
<TextView
android:id="#+id/txtCompleteTotalNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$14.38"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/txtTaxText"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Do not put ListView inside ScrollView. ListView is a widget that is already scrollable. See NestedScrollView from Support library.
If you set the ListView height to wrap_content the height will have the height to comport all items. That way, the below content will be pushed away.
However Android don't have a great support for nested scrolls, it's considered a bad practice. This way, your ListView will not have the native scroll feature, and will need some hack to work as expected.
A better workaround is use the NestedScrollView, so you scroll may work (maybe you will need put your ListView inside another parent to control the height)
change ListView's layout_height to wrap_content
I am in the process of designing my first Android application and I am trying to get the hang of creating an XML layout. (I have a great deal of experience with Java)
Essentially what I wanna do:
Where the outlines describes:
Blue: A basic View (where i can set a text)
Red: A ButtonView (Used so the user can synchronize data)
Green: A nested RelativeLayout (Where i am going to add a lot of other stuff)
More specifically what i wanna do:
So my question is this: How can i set up the different layouts? I am having the must trouble with setting up the blue and red views because i want the red view to fill up around 25% of the screen width(and the blue to fill up the last 75%).
Thank you in advance.
Use layout_weight property to specify the percentage of screen space a view takes.
Like here, for e.g:
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent"
android:orientation="vertical" .........>
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent"
android:orientation="horizontal">
<EditText android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="3"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1"/>
</LinearLayout>
//Other view
</LinearLayout>
Use following xml, this is specially designed for you.
<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="0.75">
<TextView
android:id="#+id/headingTextView"
style="#android:style/TextAppearance.WindowTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cafe Vigeos"
android:textColor="#android:color/white"
android:textSize="30sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="0.25">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/nested_relative_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
</LinearLayout>