I have a SeekBar with a progressDrawable.
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:progress="50"
android:progressDrawable="#drawable/image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
In this seekbar I used the image.png
My phone's screen is bigger, than this image. So I want to use this image like "fitXY" .
Actually it's multiply the image.
How can I set the image to match_parent?
Thank you !
Try this one
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:progress="50"
android:progressDrawable="#drawable/seekbar_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Drawble folder
seekbar_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:left="300dip">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#0000FF" />
</shape>
</item>
<item android:right="400dip">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff0000" />
</shape>
</item>
</layer-list>
You can use 9-patch.
I just made it in paint, so probably there is some error, but hope you get the idea. Also you can make image smaller, to reduce its size.
Related
I have a shape drawable resource file.
it's the drawable.xml code below.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="-2dp"
android:bottom="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke android:width="#dimen/px02"
android:color="#color/white" />
</shape>
</item>
</layer-list>
and this is layout xml code.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray333"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!!!"
android:textColor="#color/black"
android:background="#drawable/bg_category_border"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and the result is this.
you can see the white bar on the left side.
and i want to make the bar size shorter top and bottom as high as the TextView's text.(the red line)
i tried to give a padding and size in the drawable.xml.
and i set android:includeFontPadding on the TextView.
but it didn't work.
how can i make it??
i was able to do it like this
It was little bit tricky to remove padding from bottom and top, but you can try the following xml and do some adjustments as u like.
bg_category_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/white" />
</shape>
</item>
</layer-list>
layout xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3C3C3C"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#drawable/bg_category_border"
>
<TextView
android:textSize="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!!!"
android:includeFontPadding="false"
android:layout_marginBottom="-8dp"
android:layout_marginTop="-10dp"
android:textColor="#color/black"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I am using this PNG in my project (within my resources):
Programmically, I want to achieve this:
Change color to white.
Add a solid-colored circle around the icon.
The result should look to something like this:
The red color has to be changeable dynamically at runtime. How do I achieve this?
Add this to your XML file:
<ImageView
android:id="#+id/image_view"
android:layout_width="56dp"
android:layout_height="56dp"
android:background="#drawable/circle"
android:padding="8dp"
android:src="#drawable/bull_image"
app:tint="#android:color/white" />
This is circle.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#android:color/white"/>
</shape>
And to change the background color dynamically:
imageView.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.your_color))
Something like this shoud do the trick (adjust the cardCornerRadius to your liking):
<androidx.cardview.widget.CardView
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/red"
app:cardCornerRadius="10dp"
android:layout_width="wrap_content"
app:cardElevation="0dp">
<ImageView
android:src="your_image"
android:tint="#color/white" />
</androidx.cardview.widget.CardView>
Define a drawable that you can use as your background, for example:
<!-- res/drawable/the_drawable_above.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" android:padding="10dp">
<stroke android:width="1dp"
android:color="#color/colorLightGrey"/>
<solid
android:color="#color/colorAccent"/>
</shape>
For your image view:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/your_image"
scale_type="fit_center"
android:tint="#android:color/white"
android:background="#drawable/the_drawable_above"/>
I want to develop a text view background as shown below image:
How should I achieve this? and the yellow dot will change based on data.
I have just spend sometime to achieve same. This is not perfect, but you can refer this at-least . :)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="#dimen/d10dp"
android:paddingTop="#dimen/d10dp"
android:paddingRight="#dimen/d10dp"
android:paddingBottom="#dimen/d10dp">
<item
android:id="#+id/item1">
<shape android:shape="rectangle">
<solid android:color="#3CF8C2"/>
<corners android:radius="12dp"/>
<size
android:width="40dp"
android:height="40dp"/>
</shape>
</item>
<item
android:id="#+id/item2"
android:width="16dp"
android:height="16dp"
android:end="-2dp"
android:gravity="end"
android:top="-2dp">
<shape
android:gravity="right"
android:shape="oval">
<stroke
android:width="2dp"
android:color="#fff"/>
<solid android:color="#FFC730"/>
<size
android:width="16dp"
android:height="16dp"/>
</shape>
</item>
</layer-list>
To dynamically change color of drawable, refer below code.
Find drawable by id and change its color:
LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(YourActivity.this,R.drawable.here_drawable_name)
GradientDrawable gradientDrawable = (GradientDrawable) shape.findDrawableByLayerId(R.id.item2);
gradientDrawable.setColor(Color.Green); // changing color to Green
I have tried to do like this before. Maybe it helps.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_margin="4dp"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_launcher_background"
android:layout_width="20dp"
android:layout_height="20dp" />
<TextView
android:layout_margin="4dp"
android:gravity="center"
android:layout_centerInParent="true"
android:textSize="25sp"
android:background="#color/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I'm trying to achieve a text box styling similar to the image below.
I tried creating a text view dynamically and I achieved something like this. Can someone help me achieve the styling similar to above image?
Below is the code block I used to achieve the above style.
chat_bubble.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#D8000000" />
<corners android:bottomLeftRadius="1dp" android:radius="4dp" />
</shape>
Activity.kt
fun createTextView(activity: PlacesActivity){
var textView = TextView(this)
textView.setText("Text ABCDD")
textView.textSize = 24F
textView.alpha = 0F
textView.setTextColor(Color.MAGENTA)
textView.setBackgroundResource(R.drawable.chat_bubble)
textView.setPadding(10,10,10,10)
var params =
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(50,50,50,50)
textView.setLayoutParams(params)
linearLayout.addView(textView, params);
fadeMarker(textView)
}
Thanks in advance.
Create a shape for your chat bubble background:
shape_pill_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="40dp"
android:bottomRightRadius="40dp"
android:radius="40dp"
android:topLeftRadius="40dp"
android:topRightRadius="40dp" />
<solid android:color="#555555" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size android:height="40dp" />
</shape>
create your arrow drawable:
shape_arrow_down.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="230%"
android:pivotY="30%"
android:toDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#555555" />
</shape>
</rotate>
</item>
</layer-list>
find an asset that best represents your "gold" arrow, i took the in built android studio vector asset which will be referred to as ic_arrow_forward_wattle_24dp.xml. Create your layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayoutTextBubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/shape_pill_background"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textViewInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#919191"
android:padding="4dp"
android:text="This is a text message"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageViewArrow"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageViewArrow"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="#+id/textViewInput"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/textViewInput"
app:layout_constraintTop_toTopOf="#+id/textViewInput"
app:srcCompat="#drawable/ic_arrow_forward_wattle_24dp" />
</android.support.constraint.ConstraintLayout>
<View
android:id="#+id/viewDownArrow"
android:layout_width="22dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#drawable/shape_arrow_down"
app:layout_constraintEnd_toEndOf="#+id/constraintLayoutTextBubble"
app:layout_constraintStart_toStartOf="#+id/constraintLayoutTextBubble"
app:layout_constraintTop_toBottomOf="#+id/constraintLayoutTextBubble" />
</android.support.constraint.ConstraintLayout>
The result will look like this:
There is a few problems with this however. When you have a fairly large message it will push the arrow ImageView out of the bubble because of how wrap_content works in chains and constraint layout. I suggest you try giving your message TextView a fixed width or set its width to match constraints. This will give your text the grey background where there is no text. Play around with it and see what works best for you.
I want to add a gradient on the bottom of my image . Something like this :
I tried something like this but I only get the gradient no image..
<ImageView
android:id="#+id/trendingImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/trend_donald_sterling"
android:src="#drawable/trending_gradient_shape"
/>
trending_gradient_shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#android:color/darker_gray"
android:startColor="#android:color/darker_gray" />
<corners android:radius="0dp" />
</shape>
You need two layers: An ImageView, and a View on top of that with your gradient as android:background. Put these two Views in a FrameLayout:
<FrameLayout
... >
<ImageView
...
android:src="#drawable/trend_donald_sterling" />
<View
...
android:background="#drawable/trending_gradient_shape"/>
</FrameLayout>
Simply set the alpha value in your gardient.xml:
Your imageView:
android:background="#drawable/trend_donald_sterling"
android:src="#drawable/trending_gradient_shape"
Your gradient xml file:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>
In the color value, the first two places after # correspond to the alpha value, while the rest are the actual color value in R G B format, two for each.
try using the "foreground" attribute in your imageview
<ImageView
...
android:src="#drawable/trend_donald_sterling"
android:foreground="#drawable/trending_gradient_shape" />
it worked for me.
Use android:foreground="..." instead of android:background="..."
Now you won't need to put ImageView and View inside a FrameLayout!
So your final code will be:
ImageView
<ImageView
...
android:foreground="#drawable/trend_donald_sterling"/>
Drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>
this is how im gonna do,
i used relative layout as my parent layout, use the following code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/img_sample"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradiant"/>
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="Events"
android:gravity="bottom"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="Some description about the events goes here"
android:textSize="14sp"
android:textColor="#ffffff"/>
</LinearLayout>
</RelativeLayout>
hope you can figure out, here i attach my gradiant code below.use it inside the drawable folder....
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>
This is an easy way that creates a similar effect yet doesn't actually have the image disappear. Sometimes using the foreground attribute is not the best for the gradient, especially if using a motionlayout or you have nested scrollviews. Create an entirely new imageview and set the background to the gradient.
XML With Both Imageviews
<ImageView
android:id="#+id/main_imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="#drawable/peakpx__1_"
ads:layout_constraintHeight_percent=".55"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/main_imageView_gradient"
android:layout_width="0dp"
android:layout_height="0dp"
ads:layout_constraintHeight_percent=".55"
android:background="#drawable/gradient_theme_background"
app:layout_constraintEnd_toEndOf="#id/main_imageView"
app:layout_constraintBottom_toBottomOf="#id/main_imageView"
app:layout_constraintStart_toStartOf="#id/main_imageView" />
Then for the gradient, I use black #000000 for darker themes, and white #ffffff for lighter ones. A lot of answers I see on this are not adding the center color. This is important if you want to have the gradient start closer to the edge of the image.
gradient_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:type="linear"
android:endColor="#ff000000"
android:centerColor="#00000000"
android:startColor="#00000000"/>
</shape>
**1> Create file black_shadow.xml**
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="270"
android:startColor="#00000000"
android:centerColor="#9c000000"
android:endColor="#000000"
android:type="linear" />
</shape>
</item>bla
</selector>
**2> Just add below line in Imageview.**
android:foreground="#drawable/black_shadow"