How can I stretch background horizontally in xml style? - java

I'm trying to stretch vertical line in calendar_header.png horizontally as background in xml style:
<style name="CaldroidCalendarViewLayout">
<item name="android:background">#drawable/calendar_header</item>
</style>
But it changes height of the control by height of the background. How can I solve that? I just want to fill the actual size of the control with stretched background. (It acts like background image is a part of the content - not like it's just a background.)

The following answer helped me: https://stackoverflow.com/a/19405297/865475
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="#drawable/activation_popup"
android:scaleType="fitXY"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignTop="#+id/activation_layout"
android:layout_alignBottom="#id/activation_layout"
android:layout_alignLeft="#id/activation_layout"
android:layout_alignRight="#id/activation_layout"
android:contentDescription="#string/act_code_label" />
<LinearLayout
android:id="#id/activation_layout"
android:clipToPadding="true"
android:padding="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- LinearLayout wraps a bunch of smallish views here -->
</LinearLayout>
</RelativeLayout>

Related

Set background with gravity without resizing the container View in Android

How can I add a background to a view, without resizing its size, only showing the part of the image that fits the previous space?
Example:
I have the following initial layout:
<?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="match_parent"
android:layout_height="match_parent"
android:background="#8900"
android:orientation="vertical"
tools:context="com.tappx.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#0f0"
android:gravity="center"
android:text="HELLO" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#f0f"
android:gravity="center"
android:text="HELLO" />
</LinearLayout>
</LinearLayout>
And I want to apply the following image as gradient to the white background:
To achieve this (please note the gradient is filling the horizontal and has bottom gravity):
TARGET:
My problem is that setting the gradient as android:background expands the container view size to this (losing the red bottom part)
Also tried making a xml bitmap with no results:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:gravity="bottom"
android:src="#drawable/gradient" />
</item>
</layer-list>
How can I achieve it?
The image you are using is very long, and your container is set to wrap content and so it is expanding to accommodate the image.
Since you are already using fixed heights on the inner elements, you can simply set a fixed height for the container (200dp) and it'll work

Android Rounded Corner Layout with Filled Tiled Image

I'm hoping there is an xml solution to this... I am trying to create a layout that has rounded corners and then filling that layout with an image (tiled, repeat). So basically I have a LinearLayout that has a drawble layered-list assigned to the background. Here is the test layout (xml):
<?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:id="#+id/rate_buttons"
android:layout_width="100dp"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="0dp"
android:background="#drawable/test_rounded">
</LinearLayout>
</LinearLayout>
here is the test_rounded drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item ><bitmap android:src="#drawable/my_tiled_image" android:tileMode="repeat"/></item>
<item >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="3dip" android:color="#B1BCBE" />
<corners android:radius="20dip"/>
</shape>
</item>
</layer-list>
The image is overflowing at the corners of the shape/layout border. I get the following
As you can see the corners are overflowed. I heard about modifying the image part (blue pattern) in code but I have issues with that since I do modify it and animate it along the way. SO, if its at all possible to just fill in the layout with that image, please let me know. Thank you all for your help.
You can do this only programatically. But if you want to do this using xml, you have to add the background image in a layout above the rounded corner layout with the help of Relative layout and set margin. This will not give a perfect solution but will solve your problem.
The first is the XML way
Create an xml file in your respective drawable folder for the round border.
I have namedd it as popupborder.xml.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke android:width="2dp" />
<corners android:radius="15dip" />
</shape>
The main 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="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#drawable/popupborder" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" >
<LinearLayout
android:id="#+id/property_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inner view with Background and margin"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>
i recommend you to use an image loader library such as universal image loader, picasso etc. Here is an example code for it:
String url = "file://" + your_file_path
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, ivPicture, options);
You have to add
.displayer(new RoundedBitmapDisplayer(px))
to your Universal Image Loader display options.
Also there is another library (i think this is what you are looking for) https://github.com/vinc3m1/RoundedImageView
You can check this. But in second way you can get "Out of Memory Exception" cause of loading big sized image.
I hope this'll help you.

Cannot see View after changing background of TextView

Okay I have a layout for my ListView's items:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/draweritemtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:fontFamily="sans-serif-thin"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#111"
android:textSize="20sp" />
<View
android:id="#+id/separator"
android:layout_width="fill_parent"
android:layout_height="0.2dp"
android:background="#android:color/darker_gray"
android:visibility="visible" />
</LinearLayout>
So all works fine with this setup. It shows up like this:
ITEM
-------
ITEM
-------
ITEM
-------
But I don't want the blue select color from android, so I would like to change it by changing the textviews background to #drawable/drawer_active_selector.
Here is my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/notquiteverylightgray" />
<item android:drawable="#drawable/white" />
</selector>
And my drawables
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="semitransparent_white">#77ffffff</drawable>
<drawable name="verylightgray">#eee</drawable>
<drawable name="notquiteverylightgray">#ddd</drawable>
<drawable name="white">#fff</drawable>
</resources>
Now it will look like this:
ITEM
ITEM
ITEM
------
The padding is right, but the things I miss are the seperators (The <View />)
How can I get them back?
There are a couple of mistakes in your solution. By defaultListView's item clicks are handled by the ListView itself, not by the child view that was clicked. So instead of changing the TextView's background to #drawable/drawer_active_selector you should set the item selector drawable of the ListView using android:listSelector xml property or one of List.setSelector() methods. The other mistake concerns the dividers. Your solution is unnecessarily verbose. There is a much easier way to add dividers. Just use ListView's android:divider property or ListView.setDivider() method.

aligning 2 imageViews in a RelativeLayout

I have a problem with aligning 2 imageviews in a RelativeLayout:
these 2 images need to be close to eatch other without space. Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_portrait"
android:orientation="vertical" >
<!-- header -->
<ImageView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/header" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/dia1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/dia1" />
<ImageView
android:id="#+id/dia2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dia1"
android:contentDescription="#string/desc"
android:src="#drawable/dia2" />
</RelativeLayout>
What am I doing wrong?
UPDATE
dia1 is:
dia2 is:
From the 1st picture you posted, it seems the bottom image has a great deal of transparent "padding" (not actual padding, just transparent image pixels between the visible part and the upper limit).
In my opinion, you should combine RelativeLayout with cropped versions of your upper and lower images.
Otherwise, consider checking this SO thread about overlapping images.
Your question doesn't explain much about the problem. So you can add a single image. I mean merge the 2 images before and use it as 1.

How do I reduce the inner padding around the text within an Android button object?

So, at the moment I have a button which looks like the first image above. How do I reduce the padding around the text inside the button itself (To look more like the second image)?
Layout width and height is set as:
android:layout_width="match_parent"
android:layout_height="wrap_content"
The custom style shape has parameters"
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
With the rest just being color attributes and radii values.
Just to make it clear, I want the frame of the button to hug the "Login" text closer.
All help and feedback is greatly appreciated. Thanks.
It took me forever to find this but the "padding" around the text in a button isn't really padding at all. The default Widget.Button style includes a minHeight property. Changing minHeight on the button will allow you to adjust padding as expected.
<Button
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/test"
android:textColor="#color/black"
android:minHeight="40dip"/>
<style name="Widget.Holo.Button" parent="Widget.Button">
<item name="android:background">#android:drawable/btn_default_holo_dark</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">#android:color/primary_text_holo_dark</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
</style>
In the Material Components Library, the MaterialButton has a default style with insetBottom and insetTop with a value of 6dp.
You can change them in the layout:
<com.google.android.material.button.MaterialButton
android:insetTop="0dp"
android:insetBottom="0dp"
../>
or in a style:
<style name="Button_no_insets" parent="Widget.MaterialComponents.Button"..>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
</style>
try this in your custom shape.xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="1dp"
android:padding="1dp">
also you can make change in your button's xml
android:layout_width="wrap_content"
android:layout_height="wrap_content"
If you want a smaller top and bottom padding, use this:
android:paddingTop="2dp"
android:paddingBottom="2dp"
For me, need to set all below
minWidth
minHeight
paddingHorizontal
paddingVertical
layout wrap_content
android:paddingVertical="0dp"
android:paddingHorizontal="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:layout_width="wrap_content"
this will remove all spaces between text and border of button and border(if used) will be around the text without any space.
This should be the selected answer.
Done
Update: If you want to use Material Button: >
You can use style="#style/Widget.MaterialComponents.Button.TextButton" attribute, set insetTop & insetBottom to 0dp. Also set app:cornerRadius="0dp" to get same result of my given picture.
<com.google.android.material.button.MaterialButton
android:id="#+id/btnAddMorePassenger"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/primary_variant"
android:insetTop="0dp"
android:insetBottom="0dp"
android:text="#string/add_passenger"
android:textColor="#color/light"
android:visibility="visible"
app:cornerRadius="0dp"
app:icon="#drawable/ic_profile_24dp"
app:iconGravity="textStart"
app:iconTint="#color/light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar"
tools:visibility="visible" />
You can use Borderless style in AppCompatButton like below. Also use background color your preferred.
Widget.AppCompat.Button.Borderless.Colored
Button code
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_visa_next"
android:background="#color/colorPrimary"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_normal"
android:text="#string/next"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Output:

Categories

Resources