Layout like Cards in android - android-layout

I want to make an android layout like Google Cards,i know there is an Open Source Libraries, however i want only the Layout and the gray hex background style. I could not find information about this, how i can make this possible? im Attaching new Google maps v7 Layout so you can get the idea.

bg_card.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CCC" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#FFF" />
</shape>
</item>
</layer-list>
Or just set the background of the card to #FFF and add at the bottom:
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#CCC" />
NOTE:
For everyone who doesn't care about using a library, you should use
a CardView from the Android support library.
Sample code.

<item name="android:windowBackground">#ffe3e3e3</item>
In your style.xml should make you happy ;-)
And for the layout, copying the card background should be enough isn't it?

We can use the LayerList as above to achieve this (OR) by using the nine patch image get same effect, we need not create nine patch image separately for this puprpose android drawable have one.
if you are using eclipse: android:background="#drawable/abc_menu_dropdown_panel_holo_light"
copy paste the above line as your View backround.
or if you are using android studio copy paste the following line:
android:background="#android:drawable/dialog_holo_light_frame"

Related

Shape with an alternated bi-color border in android studio

I would like to create a shape to use as a background for an element, since the app has red and orange as main colors I would like to make a cool border, however I have only managed to find how to put a single color and don't really know how to do a sort of a design.
I would like to do something like this (I made this quickly in paint just a demonstration of my idea):
Shape with cool alternated border
Ok I worked on it a bit and came to a solution that isn't exactly what I was trying to do but it will be good enough I guess. This is the code to put in the xml file in the drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
<stroke
android:width="10dp"
android:color="#color/colorAccent"/>
</shape>
</item>
<item>
<shape
android:shape="rectangle"
android:padding="10dp">
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
<stroke
android:width="8dp"
android:color="#color/colorPrimary"
android:dashGap="10dp"
android:dashWidth="8dp" />
</shape>
</item>
</layer-list>
this code will produce this result:
shape with bicolor border
Hope this helps anybody

Android Studio 4.1 bordered Button loses border setting background color in kotlin code

There are numerous threads for creating a bordered button in this forum.
It works for example perfect with this xml-file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFEB3B" />`<!--Background Color-->`
<stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
</shape>
where i fail is the following:
i need 2 bordered buttons, one is yellow, the other one blue. Due to program logic I want to switch the colors, the yellow one gets blue, the blue one to yellow.
in the xml-file
<solid android:color="#FFEB3B" />`<!--Background Color-->`
is used.
In Kotlin I only find
button.setBackgroundColor(Color.YELLOW)
But I find nor button.setColor oder button.setdologColor or anything else.
Using setBackgroundColor, the color changes, but the border disappears. Which statement would keep the border?
I solved it with
punktelinks.setBackgroundResource(R.drawable.shapecolory)
in code and with resource
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFF00" />`<!--Background Color-->`
<stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
</shape>
This works (for me)

How can I programmatically create a shape in Android

I have following myRec.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<corners android:radius="1dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="0dp"/>
<stroke
android:color="#color/my_button_border"
android:width="1dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
</selector>
.... and would like to create the same shape programmatically so that I can freely change color in runtime. How can I achieve it? Much thanks in advance
Take a look at the android.graphics.drawable.ShapeDrawable class and the Shape class subclasses, suchs as:
PathShape
RectShape
ArcShape
OvalShape
RoundRectShape
ShapeDrawable receives a shape in the constructor, and then you have a lot of function to modify the shape. Also, you can retrieve the Paint asociated to the ShapeDrawable so you can alter things like the color and so.
Also, take a look at this small example:
http://www.edumobile.org/android/android-tutorial/shape-drawing-example-in-android/
where you can see some diferent shapes, and some efects suchs as the CornerPathEffect,
Use shapedrawable, gradientdrawable with
http://developer.android.com/reference/android/graphics/CornerPathEffect.html

Dynamically setting Styles for View : Android

I want to change the look (Style) of various views dynamically in an
Activity. But on going through various blogs, I came to know that
android does not support dynamically setting styles for Views
programmatic / dynamic., though I am not sure.
1) Is there any other ways of implementing apart from making use THEMES or making use layout xml files having styles pre set ?
2) Is it possible to set the different styles for various views (say green colour text for TextView1 and red color text for TextView2) by making use of setTheme() for entire Activity?
Thanks
You can change the text color in your java file like this.
textview1.setTextColor(Color.GREEN);
textview2.setTextColor(Color.RED);
Use this xml file in as resource and add it in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:centerColor="#f77b2d"
android:endColor="#f99f66"
android:startColor="#f66a11" />
<stroke
android:width="0.5dp"
android:color="#313437" />
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
</shape>

Android Border Less Button Pressed State Background Color

How can we change the background colour of a Border less button when the button is pressed.
I used this:
android:background="?android:attr/selectableItemBackground"
android:text="#string/btn_skip"
Where my button Displays a label "SKIP" as defined in my "strings.xml".
Now When I test this it is Good Except It shows default "Blue" Colour when in Pressed State.
I want it to match it with other elements of my My UI theme that are "Orange" "#FF8800"
Any Ideas?
Thankx in advance...!
instead of using ?android:attr/selectableItemBackground you can create a xml in drawable in folder with following content.
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_pressed="true"
android:drawable="#color/orange"/>
<item
android:state_enabled="false"
android:drawable="#color/default"/>
<item
android:drawable="#color/default"/>
</selector>
Updated:
so you save this files as : btn_drawable.xml in drawable folder.
Simply replace ?android:attr/selectableItemBackground
with *#drawable/btn_drawable.xml*
Thanks #Nimish-Choudhary ...! I followed your advice. and I have explained it further for clarity to all having same question as me as a beginner.
Create an xml with name say "btntransparent.xml"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_pressed="true"
android:drawable="#color/pressed"/>
<item
android:state_enabled="false"
android:drawable="#color/defaultColor"/>
<item
android:drawable="#color/defaultColor"/>
</selector>
Next add the colors "pressed" and "defaultColor" to another xml say "colors.xml"
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed">#CCFF8800</color>
<color name="defaultColor">#00000000</color>
</resources>
Here "#CCFF8800" is my pressed state Translucent Orange and "#00000000" is the 100% Transparent Black (;-) thats invisible). You can find more about color codes at Color| Android Developers
Now put the "colors.xml" in values Folder and "btntransparent.xml" in drawable Folder.
Now the last step is to voila use it in the button as
<Button
android:id="#+id/id_btn_skip"
android:background="#drawable/transparentbtn"
android:onClick="skipToMain"
android:text="#string/btn_skip" />
And thats should do the job...!
Take care.
regards,
raisedadead
If I understand you correctly, what you could do is add new resource in #drawable for example my_ripple.xml
<?xml version="0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#FF8800">
</ripple>
Then simply set this as your background.
In your theme add the following:
<item name="android:selectableItemBackground">#FF8800</item>
to override the default blue.

Resources