I tried to make a layout look like it
It looks like kinda button type.How can both text and image implement in button?
Create Your Custom Button as:
STEP 1: Create a Button drawable shape xml in res/drawable/button_background.xml:
<?xml version=”1.0″ encoding=”UTF-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>
<solid android:color=”#F000″/>
<stroke android:width=”1px” android:color=”#BB000000″ />
<padding
android:left=”10dp”
android:top=”7dp”
android:right=”10dp”
android:bottom=”7dp”
/>
<corners
android:bottomRightRadius=”5dp”
android:bottomLeftRadius=”5dp”
android:topLeftRadius=”5dp”
android:topRightRadius=”5dp”
/>
<gradient
android:angle=”90″
android:startColor=”#777″
android:centerColor=”#777″
android:endColor=”#BBB”
android:type=”linear”
/>
</shape>
STEP 2: add in your main Button xml layout as:
<Button android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Facebook"
android:drawableLeft="#drawable/leftimg"
android:drawableRight="#drawable/rightimg"
android:padding="15dip"
android:background="#drawable/button_background"/>
you can do this in your button android:drawableLeft="#drawable/your_icon"
Related
I have created a drawable button_oval.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="rectangle">
<corners android:radius="120dp" />
<solid android:color="#eceef5" />
<stroke
android:width="3dp"
android:color="#29395e" />
<size
android:width="300dp"
android:height="120dp" />
</shape>
and then i use it on my layout like this:
// I want to add a icon inside this button
<Button
android:id="#+id/goToPersonalPage"
android:layout_width="110dp"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:background="#drawable/button_oval"
android:text="#string/memberButton"
android:textColor="#29395e"
android:textSize="18dp" />
I want to add an icon inside the Button, is it any possible to complete it by changing my button_oval.xml?
The final output should look Just like the image below :
Any help would be greatly appreciated.
You have to add these lines in your Button element-
android:drawableLeft="#mipmap/ic_launcher_round" // to set an icon
android:drawablePadding="10dp" // to set the padding of icon from text
android:paddingLeft="20dp" // to set the padding of the icon and text
adjust the values according to your need.
It will look like-
Add this code in your Button element
android:drawableLeft="#android:drawable/yourIcon"
in XML
android:drawableLeft="#drawable/button_icon"
android:drawablePadding="2dip"
or in Acitvity class
yourButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
yourButton.setCompoundDrawablePadding(padding_value);
I'm trying to create a button which is both transparent and has corner radius.Near the corners transparency is more than the rest of the button.How do I prevent it? I'm posting an image of it.
https://drive.google.com/file/d/0B8Yekz-VsuhmQzdQY0lfZHFONEU/view?usp=sharing`
<Button
android:layout_width="60pt"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/main_button_shapes"
android:text="#string/get_now"
android:textColor="#color/white"
android:layout_margin="10dp"
android:textStyle="bold" />
<resources>
<color name="mediumblue">#990000CD</color>
<color name="white">#ffffff</color></resources>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp" />
<solid android:color="#color/mediumblue"/>
</shape>`
The easiest way of your problem is creating ImageButtons. Do your imagebutton's background image. Then add your image into drawable folder.
Then use it like:
<ImageButton
android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/mybuttonbackimage" />
Don't forget use android:background="#null" for transparency of button.
You can create a XML with:
<corners android:radius="15dp"/>
<solid android:color="#color/your_color"/>
Then use this XML like this:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/xml_name"
/>
I have few buttons.
I'd like to change color of a button when it is pressed.
And to change color of other buttons to normal state.
The problem is that after simple changing the background of a button,
it's shape is also changed. After changing the color the default android
button with shadows
is just replaced with painted rectangle with more button height.
Yes. It is possible to define button in normal and pressed positions as drawable XMLs.
But I'd like to use default android buttons instead of fancy mine.
My code is below:
buttonAny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonAny.setBackgroundColor(COLOR_BUTTON_SELECTED);
buttonMale.setBackgroundColor(COLOR_BUTTON_GRAY);
buttonFemale.setBackgroundColor(COLOR_BUTTON_GRAY);
}
});
buttonMale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonAny.setBackgroundColor(COLOR_BUTTON_GRAY);
buttonMale.setBackgroundColor(COLOR_BUTTON_SELECTED);
buttonFemale.setBackgroundColor(COLOR_BUTTON_GRAY);
}
});
XML:
<Button
android:id="#+id/buttonGenderAny"
android:layout_marginLeft="#dimen/dim1"
android:layout_marginRight="#dimen/dim1"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text='Any'
android:textColor="#color/text1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/buttonGenderMale"
android:layout_marginLeft="#dimen/dim1"
android:layout_marginRight="#dimen/dim1"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text='Male'
android:textColor="#color/text1"
android:textAppearance="?android:attr/textAppearanceSmall" />
My app after start looks as follows
pic1
(Sorry I have not permission to post images)
After I press a button it is as follows:
pic2
Very strange, that the size of the buttons was changed.
There are many similar questions in google.
But it seems they do not suit me.
For example this link.
But there is not written how to define normal or pressed button in drawable.
So. What is the best way to change (switch) colors of a few buttons.
Thanks!
create a new xml file under your drawable resourses with the properties you want to apply to your button... for example...
<?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="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#c53e2b" />
<gradient android:angle="-90" android:startColor="#a11005" android:endColor="#d62608" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#c53e2b" />
<solid android:color="#e0341e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#c53e2b" />
<gradient android:angle="-90" android:startColor="#ff6c52" android:endColor="#e0341e" />
</shape>
</item>
</selector>
then in the on click listener aplyy it like this...
buttonMale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonAny.setBackgroundResource(R.drawable.YOUR_XML_NAME);
buttonMale.setBackgroundResource(R.drawable.YOUR_XML_NAME);
buttonFemale.setBackgroundResource(R.drawable.YOUR_XML_NAME);
}
});
Yeah, don't try to change the button color in your onClick event.
What you are looking for is a StateListDrawable. As an example, check out how the Android platform defines the drawable btn_default_holo_light. You will want to follow exactly this pattern, providing your own drawables (e.g. PNG images) for the different states.
Once you've got these drawables created, you simply reference it in your layout file like this
<Button
...
android:background="#drawable/my_fancy_button_background" />
You can do it like this,create a xml file int project's res/drawable,eg:"buttonGenderAny_bg.xml":
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="COLOR_BUTTON_SELECTED" android:state_pressed="true"/>
<item android:drawable="COLOR_BUTTON_SELECTED" android:state_focused="true"/>
<item android:drawable="COLOR_BUTTON_SELECTED" android:state_selected="true"/>
<item android:drawable="COLOR_BUTTON_GRAY"/>
then set button's background in layout xml file,eg:
<Button
android:id="#+id/buttonGenderMale"
android:layout_marginLeft="#dimen/dim1"
android:layout_marginRight="#dimen/dim1"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text='Male'
android:textColor="#color/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="#drawable/buttonGenderAny_bg"/>
Actually I also did some thing like this a few months back with buttons, but I don't recommend it. I think for your situation it is much simpler to use radio buttons.
This is what it looked like!
The advantage of using the RadioGroup method is that you do not have to manage the color state of your "buttons"
To setup the following 3 or 4 files are needed (This is my EXACT usage, but I have also provided a few hints on how to get it like how you want it):
Layout:
<RadioGroup
android:id="#+id/sortOptionsBar"
style="?android:attr/buttonBarStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="#+id/rbSortOption1"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/rbSortOption1"
style="#style/menu_buttons"
android:text="#string/sort_option1" />
<RadioButton
android:id="#+id/rbSortOption2"
style="#style/menu_buttons"
android:text="#string/sort_option2" />
<RadioButton
android:id="#+id/rbSortOption3"
style="#style/menu_buttons"
android:text="#string/sort_option3" />
</RadioGroup>
HINT: Above I wanted mine to look like a bar, for yours switch the android:orientation="horizontal" to vertical.
values\style.xml (To bring together all the styling for the radio buttons)
<style name="menu_buttons" parent="#android:style/Widget.Holo.Button.Borderless">
<item name="android:textColor">#color/menu_button_text</item>
<item name="android:background">#color/menu_button_background</item>
<item name="android:button">#android:color/transparent</item>
<item name="android:layout_weight">1.0</item>
</style>
What is important to note is the android:button above, this hides the checkbox of the radiobutton
You can also remove the android:layout_weight I used this in my bar so that all the radio buttons would get equal width.
The parent="#android:style/Widget.Holo.Button.Borderless" will allow you to keep the buttons the same size.
color\menu_button_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="#drawable/menu_button_unselected" />
<item android:state_pressed="true" android:drawable="#drawable/menu_button_selected" />
<item android:state_checked="true" android:drawable="#drawable/menu_button_selected" />
<item android:drawable="#drawable/menu_button_unselected" />
</selector>
The same can be done for the #color/menu_button_text. (For example you could invert the color of the text when it is selected)
Then to know which one is selected in the onCheckedChanged event I did this.
int id = sortBar.getCheckedRadioButtonId();
switch (id) {
case R.id.rbSortOption1:
break;
case R.id.rbSortOption2:
break;
case R.id.rbSortOption3:
break;
}
Instead of trying to change colors in the code just use a selector for all the buttons as the background property of each button.
Selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"><color android:color="#color/button_background_pressed" />
</item>
<item android:state_pressed="true"><color android:color="#color/button_background_pressed" />
</item>
<item><color android:color="#color/button_background" />
</item>
</selector>
EDIT:
You can create a selector with the above XML in your drawable folder and just use it as a background with "#drawable/selector_button"
EDIT 2:
Instead of using it like this:
<Button
android:id="#+id/buttonGenderMale"
android:layout_marginLeft="#dimen/dim1"
android:layout_marginRight="#dimen/dim1"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text='Male'
android:textColor="#color/text1"
android:textAppearance="?android:attr/textAppearanceSmall" />
Use it like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="1dip" />
<Button
android:id="#+id/buttonGenderMale"
android:layout_marginLeft="#dimen/dim1"
android:layout_marginRight="#dimen/dim1"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text='Male'
android:textColor="#color/text1"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Adjust the View for the distance you want between buttons!
All you have to do is create an xml file in the drawable folder which will set the back ground colour of your buttons according to the states i.e pressed or not.
For example create a file and call it button_background.xml and place it in drawable folder.The in the file put the following
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<solid android:color="#449def" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#2f6699" />
</item>
Then in your button background attribute
<Button
android:background="#drawable/button_background"/>
You can edit the colors in the button_background file to your taste.
When creating a PopupWindow it shows a border like in the following image:
How do I remove it?
Try to add this line :
mPopup.setBackgroundDrawable(new BitmapDrawable());
You can create one custom style and put that border the same color on background, try something like:
New | Android XML File.
myborder.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dip"
android:color="#android:color/darker_gray" />
<solid
android:color="#android:color/background_dark" />
<padding
android:left="7dip"
android:top="7dip"
android:right="7dip"
android:bottom="7dip" />
<corners
android:radius="6dip" />
</shape>
Using the drawable Android XML file in a layout
Layout.xml
<LinearLayout
android:orientation="vertical"
android:background="#drawable/myborder"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Text"
/>
<!-- ..................... -->
You need to create a custom layout and set border of parent layout,
i'll giving you a Logical idea for doing this.
Your layout must be like below.
dialog_layout.xml
<RelativeLayout>
<LinerLayout> <!-- You can **Set/Remove** all background properties of this LinearLayout-->
<!-- Here are all child element like EditText/ Or TedxView-->
</LinerLayout>
</RelativeLayout>
Here are link for border :
Border
This is my Dialog code
AlertDialog.Builder builderofme = new AlertDialog.Builder(this);
final FrameLayout frameView = new FrameLayout(this);
builderofme.setView(frameView);
builderofme.setCancelable(true);
builderofme.setPositiveButton(" Yes ", new OkOnClickListener());
builderofme.setNegativeButton(" No ", new CancelOnClickListener());
final AlertDialog alertDialog = builderofme.create();
LayoutInflater inflater = alertDialog.getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.myediteddialog, frameView);
alertDialog.show();
The Code is working Perfectly.But i want to use the White background for Yes and No button.Presently its coming like this..
This is my myediteddialog.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="#drawable/delete"
>
<TextView
android:id="#+id/editeddialogboxtextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/background_dark"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Do You Want To Delete?"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Currently you are using the default theme that's because you are not getting the background color as Dark holo theme I would suggest if you want a consistent theme on all the devices then you should declare buttons in the xml file itself and then use it accordingly. This would be the best and right approach in my experience.
you can see the next example.
Creating a Custom Layout
If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.
By default, the custom layout fills the dialog window, but you can still use AlertDialog.Builder methods to add buttons and a title.
Here You can declare the two buttons in the layout files of myediteddialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="#drawable/delete"
>
<TextView
android:id="#+id/editeddialogboxtextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/background_dark"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Do You Want To Delete?"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="ffffff"
android:text="Yes" />
<Button
android:id="#+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="ffffff"
android:text="No" />
</RelativeLayout>
I have done this in my way using few xml and not changing anything else.Thought to share with you guys who are going for the solution.
First i have created a custom selector drawable xml file for my button
here it is
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#83776b" />
<gradient android:angle="-90" android:startColor="#5f4427" android:endColor="#cbb59d" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#83776b" />
<solid android:color="#92806c"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#83776b" />
<gradient android:angle="-90" android:startColor="#cbb59d" android:endColor="#92806c" />
</shape>
</item>
</selector>
Then in my activity i have included this two lines
Button b = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.dialogbutton));
Button c = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if(c != null)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.dialogbutton));
Easy solution for me that has worked.
Use custom dialog , here is the best example
custom dialog
As per your requirement u have to design custom dialog layout this layout contains one textview and 2 buttons which look as default alert dialog. and write listeners for two buttons and design buttons according to your requirement .