I am new at Android and I downloaded some code for a login screen. I cannot see all the border and I only see half of it (the bottom of the edittext). I think the reason is something about the configuration of the layout.
Can anyone help me out?
This is what it looks like:
Below I show you how I would like to have it:
The difference there is based on the different styles of Android. The one you don't like is actually the new type of style that android is running with called Holo. It's a good idea unless you have to change to stick with this theme.
The other EditText is from an older version of android and in fact the EditText will change it's look just based on which android version you run it off.
For example, try making an emulator with android version 2.2 and you'll see the EditText look that you like, while i'm guessing this one you're running is android 4.0 or higher, so therefore has the Holo Theme.
If you really want to make it look like the old style android then have a look at this answer on how to do so: Link
For creating custom dialog, you can try following.
Step 1. Have a dialog.xml in your res/layout/ folder, which can contain how you want your EditText box to look like. Something like following will draw a border around the EditText. You can also describe the colors of the border:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#ffffff"/>
<padding android:left="10dp" android:top="7dp"
android:right="10dp" android:bottom="7dp" />
<corners android:radius="2dp" />
</shape>
Step 2 Then in the XML of your edit Text you may define the android:background as that drawable XML file. Something like:
<EditText
android:id="#+id/ed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog"/>
This will give your EditText the look you want. Hope this helps.
<style name="myTheme" parent="android:Theme.Holo">
<item name="editTextStyle">#style/MyWidget.EditText</item>
</style>
<style name="MyWidget.EditText" parent="android:Widget.EditText">
<item name="android:background">#drawable/edit_text</item>
</style>
You can use above code in your Theme style
Related
I am working on a project where I will have many buttons of the same size.
I was wondering if there is a way to create a standard button formatting with a set
height
weight
padding
etc.
I have looked into styles but I am not sure if that is the road I should be taking. I also thought about extending the button class and programmatically setting these values to a standard.
Hello, how are you!
The best way is to create a style in the style.xml resource, all the necessary features.
just add the style with style = "#style/mystyle" to all the buttons.
example:
<style name="mystyle">
<item name="android:textSize">20sp</item>
<item name="android:gravity">center|center_horizontal</item>
<item name="android:textAlignment">center</item>
</style>
In the same way when you want to create a customized button (java/kotlin)
it will also be added in the same way.
Greetings.
You can create a reusable component in a separate .xml file and use it everywhere since you said the dimensions exactly the same all across the app. Here is a sample:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="4dp" />
<!--customize and animate stuff-->
<gradient
android:angle="270"
android:endColor="#88b823"
android:startColor="#b0dc54" />
</shape>
Short question:
Suppose I use this style on a button:
<Button
style="#style/Widget.AppCompat.Button.Borderless.Colored" ...
(or without the "Colored" part).
How do I set the background of it to be a selector that has a different color when being pressed? The default one is quite a bold color on pre-Lollipop...
I want to have all of the style working as the default (including padding), except for the color of the selector.
I think the easiest would be to create a typical multi-state drawable and replicate Android's default button drawable properties for padding, etc...
From the Android SDK AppCompat theme:
<!-- Colored bordered ink button -->
<style name="Base.Widget.AppCompat.Button.Colored">
<item name="android:background">#drawable/abc_btn_colored_material</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Widget.Button.Inverse</item>
</style>
They use the drawable abc_btn_colored_material, whose source is like this: https://github.com/dandar3/android-support-v7-appcompat/blob/master/res/drawable/abc_btn_borderless_material.xml
You can see it's just a layer list with the following multi-state drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/abc_btn_default_mtrl_shape"/>
<item android:state_pressed="true" android:drawable="#drawable/abc_btn_default_mtrl_shape"/>
<item android:drawable="#android:color/transparent"/>
</selector>
https://github.com/dandar3/android-support-v7-appcompat/blob/master/res/drawable/abc_btn_default_mtrl_shape.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="#dimen/abc_button_inset_horizontal_material"
android:insetTop="#dimen/abc_button_inset_vertical_material"
android:insetRight="#dimen/abc_button_inset_horizontal_material"
android:insetBottom="#dimen/abc_button_inset_vertical_material">
<shape android:shape="rectangle">
<corners android:radius="#dimen/abc_control_corner_material" />
<solid android:color="#android:color/white" />
<padding android:left="#dimen/abc_button_padding_horizontal_material"
android:top="#dimen/abc_button_padding_vertical_material"
android:right="#dimen/abc_button_padding_horizontal_material"
android:bottom="#dimen/abc_button_padding_vertical_material" />
</shape>
</inset>
So just make it yours :) Copy the drawable xml into your app and customize it.
I don't know if you will be able to access the #dimen/... from outside the SDK package, those values are defined in the SDK Values.xml, take a look at https://github.com/dandar3/android-support-v7-appcompat/blob/master/res/values/values.xml . If it's not possible, just create your own version of the dimens in your values.xml
If there's a cleaner solution any android guru can point out, I'll be glad to know!
Requested Update
Another trick is to apply a ColorFilter to the whole Button View. It's a very simple thing but not useful for all situations, as the color filter will blindly change the view as a whole (including Font color, borders, etc...). However, smartly selecting Alpha on the colors, could work for specific situations.
I'd suggest a LightningColorFilter. To apply a color filter to the Button, or to any view, you can do something like
myButton.getBackground().setColorFilter(new LightingColorFilter(Color.WHITE, Color.RED));
Check out what every color means here: http://developer.android.com/reference/android/graphics/LightingColorFilter.html
I am trying to add a border to an alert dialog. I am hoping to make it look like this:
The best solution I have found thus far is to use a nine patch drawable as the background for the dialog.
The problem with this is that I have not found a way to make a nine patch background that actually gives the dialog a consistent white line around it. This has been my best attempt thus far (sorry it is a little hard to see...) :
The problem is that this produces a dialog like this:
The problems here are twofold; the lines at the sides are way too thick, and the lines at the top are kind of faded by the shadow.
My only ideas are to either find a working nine patch that gives a consistently thick border, or to find a way to get the 'main layout' of the alert dialog, so I can add a padding to that directly.
What is the best way to go about setting up a border on an Alert Dialog like this?
The answer by questioner was very close to what I wanted, but it still left a big black line around the dialog that I was not interested in.
I deleted one of the layers from the layer list, and customised the padding on the white border. I also did not set the background to transparent as suggested would be a good idea in the comments.
The shape the is inside the item is to give the background the same curved edges on a dialog (if you look really closely on 3.0+, you can see a few pixels of round corners).
In a dialog_border.xml file in the drawable folder, I had this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="6dp"
android:left="13dp"
android:right="13dp"
android:bottom="6dp">
<shape android:shape="rectangle" >
<solid android:color="#color/offWhite" />
<corners
android:radius="3dp"/>
</shape>
</item>
</layer-list>
And in my style I had this:
<style name="DialogTheme" parent="android:Theme.Holo.Dialog">
<item name="android:windowBackground">#drawable/dialog_border</item>
</style>
Although it could probably be set programatically rather than in the style if necessary.
In your dialog's xml set following background for top level view:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white"/>
<item
android:bottom="1dp"
android:drawable="#color/black"
android:left="1dp"
android:right="2dp"
android:top="2dp">
</item>
</layer-list>
You can customise border width on each side.
I have just begun creating Android apps using Eclipse and the ADT.
I have got the basic functionality working in a demo app, and would like to create a button that looks like a search field (there is no search functionality built in, I just want the user to press it and start a new activity).
My question is: what is the best practice for creating a button like the above (ignoring the 1px dark grey stroke around the outside)?
I have got as far as
Create the button in activity_main.xml
<Button
android:id="#+id/inputSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="#string/hint_search"
android:background="#drawable/input_search"
style="#style/input_search"
android:onClick="findProducts" >
</Button>
Create a few styles
<style name="input_search" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/light_grey</item>
<item name="android:background">#color/white</item>
</style>
Create the input_search_background.xml which has a light grey stroke, rounded corners
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke
android:width="#dimen/stroke_width" android:color="#color/light_grey" />
<corners
android:radius="#dimen/corner_radius" />
<solid
android:color="#color/white" />
</shape>
Create input_search.xml for the states.
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/input_search_background"
android:state_pressed="true" />
<item android:drawable="#drawable/input_search_background"
android:state_focused="true" />
<item android:drawable="#drawable/input_search_background" />
</selector>
I'm not sure how to create the orange block on the right. Should I define a shape which is orange and use a transparent png for the magnifying glass or should the png be the orange block with the magnifying glass all in one?
What tools do you use for creating the assets?
How do I know how big to create them?
I'll appreciate any words of wisdom or links that could help.
Thanks,
Andrew
You can simply use a 9 patch.
You can find a similar one, in your sdk platform graphics:
In Windows (install path may change),
C:\Program Files\adt-bundle-windows-x86_64-20130522\sdk\platforms\android-8\data\res\drawable-mdpi
You can find this:
With some (very little) work, you can make the perfect image that matches your needs.
Just add it as the background of your Button (I'd use a TextView, but it's a matter of tastes) and enjoy.
You see, no need for a custom style nor an extra xml drawable.
I am developing apps in android. I did design custom edittext for my apps see image.1 for image.1 i used edittext style but I want edittext style like following image.2. does anybody have solution.
image.1
image.1 edittext style
android:background="#drawable/edittext_shape"
following is the edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:bottom="1dp"
android:left="-4dp"
android:right="-4dp"
android:top="-4dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="2dp"
android:color="#color/marooncolor" />
</shape>
</item>
Following is image.2 whcih is I want
create a 9 patch drawable that will only stretch horizontally and set it as the background of your EditText..
The other way of doing this is use Relative Layout having Edit text and ImageView.
The simplest way is to use android holo colors. Generate styles and 9patch files for your custom EditText, copy and apply to your project. It should work especially if you need the EditText like in Androids Holo Theme.