Android: How to create a customized edit text box like this? - android

I'm trying to create to make this form in Android
You see the EditText has some shadows at the front. How do I create that?
I created AutoComplete Text Views for State and Place. But how to I add that line and arrow on it?

You can set background drawable to achieve.either create custom drawable with shape or use background images and put it in the layout xml
android:background="#drawable/text_bg"
create an "testing_gradient.xml" file in /res/drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/grey">
<shape
android:shape="rectangle"
android:thickness="10dp"></shape>
</item>
<item
android:drawable="#color/white"
android:left="50dp">
<shape android:shape="rectangle"></shape>
</item>
</layer-list>
apply the code in layout xml file
<EditText
android:background="#drawable/testing_gradient"
android:layout_width="match_parent"
android:textColor="#color/black"
android:text="User input"
android:paddingLeft="60dp"
android:layout_height="60dp"/>
final result
Again, you can add more layer to input more images

Related

Unable to apply background style on button in Android

I am trying to apply background style on button for that I created a drawable resource file but it's unable to take its effect. It's only showing white button only.
Below is my code:
log_out.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/accent"/>
<corners
android:radius="10dp"/>
</shape>
activity_main.xml
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="LOGOUT"
android:background="#drawable/log_out"/>
How can I achieve the desired layout?
It is due to the application theme. This structure will not work if you are using a Material Design theme.
If you change the main theme with parent = "Theme.AppCompat.DayNight" as an example, it will work.

How can I modify how a button looks while pressed in Android Studio

I have been experimenting with button attributes and what I can do with buttons in general. I wanted to modify the way a button looks when clicked.
I created three drawable resource files, two for each button states(pressed or default) and one for state list to pass to the button in the main activity xml.
However this does not work as the button stays the same when I click it.
custom_button_pressed.xml file code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/button_color_pressed"/>
<corners android:radius="30dp"/>
</shape>
custom_button.xml file (default state file) code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="30dp"/>
<solid android:color="#color/button_color"/>
</shape>
custom_button_full.xml file code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/custom_button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/custom_button"/>
</selector>
code fragment for the button in the main xml file:
<Button
android:id="#+id/buttonAboutApp"
android:layout_width="160dp"
android:layout_height="105dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="44dp"
android:background="#drawable/custom_button_full"
android:drawableLeft="#mipmap/about_app_icon"
android:text="#string/About_app"
android:textAllCaps="false"
android:textColor="#color/app_background"
app:backgroundTint="#color/button_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
what am I doing wrong ? Thank you in advance
If you are using a version of the Material Components library less than 1.2.0, the android:background attribute is not supported by MaterialButton. If your app/activity uses a MaterialComponents theme, any <Button> tag will be automatically inflated as a MaterialButton instance, so your custom background won't work.
Since you're trying to override the button's appearance anyway, you probably want just a basic android.widget.Button instead of a MaterialButton. You can achieve this by specifying the fully-qualified name in the view tag of your layout file:
<android.widget.Button
android:background="#drawable/custom_button_full"
.../>

Android: how to create splash screen with text

How can I add some text to splash screen? My splash screen isn't a separate activity and I don't want to make it as standard activity.
I created it by following this article:
https://android.jlelse.eu/launch-screen-in-android-the-right-way-aca7e8c31f52
Is it possible to add some text?
You can create a new layout for your splash screen. But if you still don't want to create it then there is a nasty hack to achieve it.
Open Paint write your text and save the file as png image
Import png file in drawables and add it in <layer-list> as an item below or above your logo/icon
<item android:bottom="50dp"> set position as you want
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="#color/colorPrimary"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/icon"/>
</item>
<item android:bottom="50dp">
<bitmap
android:gravity="center"
android:src="#drawable/myTextImage"/>
</item>
</layer-list>

Defined custom shape for button in xml. Now I want to change the color dynamically. How?

I have this:
round_button.xml
<xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#dec60000"/>
<size android:width="150dp" android:height="150dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#860000"/>
<size android:width="150dp" android:height="150dp"/>
</shape>
</item>
My Button:
<Button
android:id="#+id/incrementBTN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/round_button"
android:onClick="onClick"
android:soundEffectsEnabled="true"
android:text="0"
android:textSize="50sp"
tools:ignore="HardcodedText" />
Dynamically, I want to change the background color (which is defined in the round_button xml) programmatically. Is there a way I can do this?
If you want to define certain states for your button, you could set them all in xml, without having to do it programmatically (if you do, you can indeed set a filter, but it can get messy if you have many states and conditions IMO).
I'll detail the steps here:
1) Creating a xml with the states you want
You can create a xml with a selector in your drawable folder with the defined states. As an example,
button_bkg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bkg_is_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/bkg_is_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/bkg_default"/>
</selector>
Let's call this file button_bkg.xml. In the example above, I have listed 3 states: pressed, disabled and default, which means that, when the button is pressed, it will assume the bkg_is_pressed background and, when I set the button to disabled (either in xml or programmatically through setEnabled(boolean), it will assume bkg_is_disabled background.
2) Creating the backgrounds
Now you will define what you want the background to be in the xml files you defined (bkg_is_pressed, bkg_is_default, bkg_is_pressed). In your case, in example, you would take each shape defined in your round_button.xml file and separate them into each one of the xml files you defined for the states. In my case, I defined a layer-list:
bkg_is_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="#dimen/button_corner_radius"/>
<solid android:color="#color/color_alert"/>
<stroke
android:width="#dimen/universal_1_pixel"
android:color="#color/color_gray_dark"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="#dimen/button_corner_radius"/>
<solid android:color="#color/color_mask_highlighted"/>
</shape>
</item>
</layer-list>
You will do that for each of the states.
It is important to note that, if you are going to build for API 21+, you can define a ripple effect by creating ANOTHER button_bkg.xml file in your drawables-v21 folder, which would be like this:
button_bkg.xml (in your drawable-v21 folder)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bkg_is_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/bkg_is_pressed" />
To use the ripple, you can define a color as explained below:
bkg_is_pressed.xml (in your drawable-v21 folder)
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/color_mask_highlighted">
<item android:drawable="#drawable/bkg_is_default" />
</ripple>
You only have to put the button_bkg.xml and the bkg_is_pressed.xml into your drawable-v21 folder file. In my case, bkg_is_default and bkg_is_disabled.xml were the same for both 21+ and 21- APIs, so I didn't add it to my drawable-v21 folder, I just created it in the drawable folder.
I want to emphasize that you STILL need the other files in your regular drawable folder so that devices with API 21- will work properly.
3) Assigning that background to your button
Lastly, you just have to define that background to your button:
<Button
...
android:background="#drawable/button_bkg
/>
So, there you have it. This way, you don't need to set the styles programmatically, you can just define all the backgrounds (according to your states) in the xml files.
But, if you also prefer to set them all programmatically, you can do the same, just use setBackground and use the xml files you defined and apply the state logic you want to it (if button is pressed, setBackground(bkg_is_pressed) and so on)
I hope that helps, let me know if that works for you.
I solved it by setting a ColorFilter:
Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons);
mDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
myButton.setResource(mDrawable);
You could construct the shapes from code, depending on the color you need to use, create a StateListDrawable from those and set it as your buttons background.

Creating a Custom Compound Button Bar

I have to create a custom compound button bar control in Android like the one here. To create a button bar like this, I am thinking of extending a LinearLayout and add my buttons to it. The buttons have a black background but with a gradient fill; I am not keen on using PNGs since the color of the buttons can change at runtime.
How can I get similar effect in Android?
Thanks.
u have to use imageview as button.
set two image view you can change color the button
use xml file to src of that imageview like
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/back_normal" />
<item android:state_pressed="true"
android:drawable="#drawable/back_pressed" />
</selector>
for gredient style.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#663300"
android:centerColor="#330000"
android:endColor="#330000"/>
</shape>
thats it.

Categories

Resources