setting a Drawable background programmatically for a programmatically created button - android

I'm trying to set a drawable background to a button which is created programmatically.
Below is the code where I create the button and set the background
Button increaseQuantity = new Button(this);
increaseQuantity.setText("+");
//increaseQuantity.setBackgroundResource(R.drawable.quantity_button);
increaseQuantity.setBackgroundDrawable(getResources().getDrawable(R.drawable.quantity_button));
Below is the xml which has the code for the drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<stroke android:width="1dp" android:color="#ee404040" />
<size android:width="2dp" android:height="2dp"/>
<gradient android:startColor="#11809100" android:centerColor="#11910000" android:endColor="#55FFB900" android:angle="45" />
</shape>
As per the xml my button should be in round shape (because width and height are the same), but I'm not able to get a round shaped button, instead I'm getting an oval shaped button. Could anyone correct this code to get a circular button?

just two things here:
Replace the deprecated methods: setBackgroundDrawable and getDrawable are deprecated.
Set the dimensions programmatically
This should do the work:
increaseQuantity.setBackground(ContextCompat.getDrawable(this, R.drawable.round));
increaseQuantity.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
//replace 100 with your desired diameter of the button.
Also, you will have to replace LinearLayout to the parent layout you're associating your button with.

Related

Android button border stay deafault after adding drawable file

I'm working on a project in android studio and I'm trying to change the border to my button.
My default button when I just add it to my grid look like this:
After adding new XML file (called box_solid_border.XML) in the drawable file that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="4dp"
android:color="#000000" />
</shape>
the result is that it's creating the next shape that I can see:
and that exactly what I want to add to my button.
when I come back to my XML at the main file and add to my button the next line:
as you can see in line 226 I'm adding the background that I just created for this button to the button. and near the line, there is the exact border that I want.
but in the application, the button still stays the same as the default one with no border added to him.
Like this:
Does someone know the reason that the drawable file doesn't affect the button?
If I'm doing the same thing for a textView or something else its works perfectly fine, just the button looks like not affected by the drawable extension to him.
Thank you!
You need to add this attribute to your button:
app:backgroundTint="#null"
If it has both attributes android:background and app:backgroundTint(if you are using the default theme for your application, it has a default value), the color specified by app:backgroundTint will be cover on the shape of drawable.
For border to button try to edit box_solid_border.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:color="#color/black"
android:width="5dp"/>
<corners
android:radius="8dp"/>
<solid
android:color="#color/colorAccent"/>
</shape>
In layout xml file
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/box_solid_border"
android:text="Button"
android:textColor="#fff"
android:textSize="30sp" />

Changing color of a portion in picture

I am doing such type of project, in my project, i want to change the color of only a portion of my picture, for example when I click on a button I want the blue become green, i want to know if there is any way to do that or it's impossible
To achieve the above feature, you could define a shape and place it above the button and then change the color onClick. A simple example is:
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp">
</corners>
<solid
android:color="#4848ea">
</solid>
</shape>
Now change the color of the shape using the following code.
GradientDrawable Shape = (GradientDrawable)shape.getBackground();
Shape.setColor(Color.GREEN);
Place the above code within the button onClick.

Remove the top and bottom padding in a SeekBar

I am trying make a simple music player. When I try to use the android's default seek bar in my music playback controls section, there is a default padding at the top and bottom of the seekbar. The effect I am trying to achieve is similar to the seekbar used in shuttle music player.
But this the effect I'm getting. I would be grateful if any would tell me how to achieve the desired result.I'm using linear layout as the root container.
Because the option of specifying
android:background="#color/your_colour"
will not work because you have two colors there one for the Top and one for the Bottom.
The working solution is by using a layerlist as follows:
Go to your drawable folder create a file call it lets say
back_ground_seek_bar.xml and add the following code inside it:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="10dp">
<shape android:shape="rectangle" >
<size android:height="10dp" />
<solid android:color="#colour/your_top_color" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle" >
<size android:height="10dp" />
<solid android:color="#color/your_bottom_color" />
</shape>
</item>
</layer-list>
Replace the colours with your own colors when you paste this.
Remember we have added top and height of as 10.But because we want our SeekBar to have colour then we have to set the height of seekbar to twice the value so It should be twice and thats 20dp. So we should add our new height of the seekbar to be 20dp as well set the background as the xml we have defined as follows. Go to your SeekBar and paste this:
android:layout_height="20dp"
android:background="#drawable/back_ground_seek_bar"
For customization you can also change the values of height and whatsoever in the drawable/back_ground_seek_bar.xml but it should always be twice. The given height set in SeekBaraim is to divide it equally.

I want to design login n sign up(register) page like shown in image pz give mi source code

Please give me code to design edit text box, button with round corners, etc. Which is shown in the image.
you can use xml drawable and use it as background
first go to drawable directory and create new Drawable Resource file and name it MyRoundButton then copy all this code into it
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="#ff66cc"
android:width="2dp"/>
<corners
android:radius="10dp"/>
</shape>
then add this to your Button background
android:background="#drawable/MyRoundButton"
now you can change the corner radius by changing
android:radius="someValue"
and border color by
android:color="#someColor"

EditText margin lost on changing background color

I've created 5 Edit-text in a Linear layout with horizontal orientation and they have no padding or margins set. When I change the background color of these Edit-text's the borders are lost. Kindly help how to make sure my edit text do not loose their borders.Following is the code snippet.
Saving the padding and reassigning it back hasn't helped!
private void setToNonEditMode(EditText textBox, boolean nonEditMode){
if(nonEditMode){
textBox.setClickable(false);
textBox.setBackgroundColor(getResources().getColor(R.color.transparent));
textBox.setFocusable(false);
textBox.setFocusableInTouchMode(false);
}else{
textBox.setClickable(true);
textBox.setBackgroundColor(getResources().getColor(R.color.white));
textBox.setFocusable(true);
textBox.setFocusableInTouchMode(true);
}
}
Have you tried defining an edit text as a shape and then assign it?
I see you create your EditText programatically, but here is something you could try:
Create a drawable as an xml in your res/drawable folder:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#000000"/>
</shape>
Use this:
textBox.setBackgroundResource(R.drawable.name_of_your_xml_shape_file);

Categories

Resources