drawable right for toggle button - android

I have setup a circle (shape) in an xml for a drawable right on a toggle button. The circle.xml has an initial color of green but I cannot get it to show on button. When I used image I am able to see the image so I am sure the issue is with the circle or shape.
This it he toggle button with the drawable right.
<ToggleButton
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#drawable/custom_fixture_buttons"
android:textColor="#drawable/white"
android:textOff="F1"
android:textOn="F1"
android:drawableRight="#drawable/circle"
android:textSize="30sp" />
This is the code for the circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/circle"
android:shape="oval"
android:color="#ff00FFFF" >
</shape>
I have a total of 10 toggle buttons with the drawable right and need to change the colors of each of them separatly, here is a method to set each of the color dots. The first generation used a separate image over each button but I need to change code to apply this to the toggle button drawableright. I tried the setCompoundDrawablesWithIntrinsicBounds but get errors.
switch (index) {
case 0: {
Resources res = getResources();
final Drawable drawable = res.getDrawable(R.drawable.circle);
drawable.setColorFilter(Color.rgb(i, j, k), Mode.SRC_ATOP);
// ImageView img = (ImageView) findViewById(R.id.colordot1);
// img.setBackgroundDrawable(drawable);
// Fixture1.setCompoundDrawablesWithIntrinsicBounds(0, 0,img, 0);
break;
}

You've created the drawable incorrectly.
Here's an example of a drawable that would create a coloured circle:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid
android:color="#00FFFF" />
</shape>

Related

Android: How to change the color of the drawable background of TextView dynamically in the java code

I need to make TextView with rounded background with dynamic color.
I know how to make drawable background but I don't know how to change the color of it in the code?
the drawable bg xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/colorPrimary"></solid>
<!-- I want to change this color dynamically in the java code -->
<corners android:radius="7dp"></corners>
</shape>
</item>
</selector>
the textview in the layout xml:
<TextView
android:id="#+id/txt_taskTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg_rounded_solid"
android:paddingEnd="10dp"
android:paddingStart="10dp"
android:text="work"
android:textColor="#fff"
android:layout_marginEnd="10dp"
android:textSize="12sp" />
in the Java file code:
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.txt_taskCategory.setText(holder.mTask._catName);
holder.txt_taskCategory.setBackgroundColor( Color.parseColor( holder.mTask._catColor));
//when i do that it remove the drawable background and just color it.
}
What i need is to change the color of the drawable background (not the Textview) whith "holder.mTask._catColor"
You can do this like this:
Drawable drawable = getResources().getDrawable(R.drawable.bg_rounded_solid);
drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
yourTextView.setBackground(drawable);
This should work (not tested, just from my mind)!
Just reset the drawable once you change the color of it.
You can also create 2 seperate drawables and switch them accordingly.

android: TextView with rounded background with dynamic background color

I need to make TextView with rounded background with dynamic color.
I know how to make drawable background but I don't know how to change the color of it in the code?
the drawable bg xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/colorPrimary"></solid>
<corners android:radius="7dp"></corners>
</shape>
</item>
</selector>
the textview in the layout xml:
<TextView
android:id="#+id/txt_taskTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg_rounded_solid"
android:paddingEnd="10dp"
android:paddingStart="10dp"
android:text="عمل"
android:textColor="#fff"
android:layout_marginEnd="10dp"
android:textSize="12sp" />
in the Java file code:
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.txt_taskCategory.setText(holder.mTask._catName);
holder.txt_taskCategory.setBackgroundColor( Color.parseColor( holder.mTask._catColor));
//when i do that it remove the drawable background and just color it.
}
What i need is to change the color of the drawable background (not the Textview) whith "holder.mTask._catColor"
This code worked for me
Drawable drawable = getResources().getDrawable(R.drawable.yourViewBackground);
drawable.mutate().setColorFilter(your_resolved_color, PorterDuff.Mode.SRC_IN);
yourView.setBackground(drawable);
v.setBackgroundColor(0xFF00FF00);
try this (where 0xFF00FF00 is color code) to change color in java code with the condition in which you want to change it
if you want to change after some time you can set timer or use gif

How to change background color of button with image in Android

In my Android app I have button with arrow image on the right side. On my SettingsActivity you can change the color of app (change the color of buttons and TextViews).
I changed the color of TextViews fine, but when I changed the color of buttons, the image (arrow on the right side) is gone.
Thats my code now. It change color and delete the imge.
loginButton = (Button) findViewById(R.id.btnLogin);
loginButton.setTextColor(color);
I find this on some post. But this only works for ImgageView, not for button.
loginButton = (Button) findViewById(R.id.btnLogin);
GradientDrawable bgShape = (GradientDrawable)loginButton.getBackground();
bgShape.setColor(getResources().getColor(Color.BLACK));
text.setTextColor(color);
I also tryed to change the color like this. But this does not change the color at all.
Drawable button = context.getResources().getDrawable(R.drawable.button);
button.setColorFilter(new
PorterDuffColorFilter(0xffff00,Mode.MULTIPLY));
The last thing which I hope should work is to define new drawable for every color. But this is really awful...
I also think, that the problem is not only with the image. I think this solution overrides all the drawable file...
Is there anyone who knows how to change the color of drawable and keep the image on the button?
EDIT: Just trying... I can move all colors to colors.xml file. And change the path of color to all drawable files. Like
<resource>
Color 1
Color 2
Color 3
...
</resource>
But than how can drawable files decide which color it should use?
EDIT2: Button layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
style="#style/bgGrey">
<RelativeLayout
android:layout_height="0dip"
android:layout_weight="30"
android:layout_width="fill_parent"
>
<TextView
...
/>
</RelativeLayout>
<LinearLayout
...>
<TextView
.../>
<EditText
.../>
<TextView
.../>
<EditText
.../>
<Button
android:id="#+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/password"
android:gravity="center|left"
android:paddingLeft="15dp"
android:layout_marginTop="10dp"
android:background="#drawable/login_button_background"
android:text="Login" />
</LinearLayout>
</LinearLayout>
Button drawable, background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/layer"></item>
</selector>
layer.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:left="0dp" android:top="5dp" android:bottom="5dp" android:right="0dp" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffb500"/>
</shape>
</item>
<item android:left="350dp" android:top="5dp" android:bottom="5dp" android:right="5dp" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffb500"/>
</shape>
</item>
<item android:right="15dp" >
<bitmap android:src="#drawable/btn_right" android:gravity="right" android:tileMode="disabled" />
</item>
</layer-list>
An alternative approach can be: Remove the arrow from the background and set it as Compound Drawable.
As Button extends TextView, you can set drawables to the top, bottom, left or right.
In the XML, just set the property drawableRight to an arrow, or via JAVA code:
button.setCompoundDrawablesWithIntrinsicBounds (idLeft, idTop, idRight, idBottom)
So to set a right arrow:
button.setCompoundDrawablesWithIntrinsicBounds (0, 0, R.drawable.your_arrow, 0);
This way, the background is independent from the arrow, and it will be easier to modify it without creating complex versions or selectors.
There are methods and properties to adjust the drawable padding so you can place it more or less where you need it.

Changing button background color removes white space

I am changing some button background colors before the activity is shown and when the buttons are displayed, they are missing the white space around them.
How do I get the buttons to be red, where the grey is, and keep the white spacing?
You probably have missed setting margin for layout. Actually there is a white space around your button so all the buttons are touched, so when you set background that white space also turns red. This should be okay I guess.!
Layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="About"
android:layout_margin="5dp" />
<Button
android:id="#+id/two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_margin="5dp"/>
<Button
android:id="#+id/third"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Media"
android:layout_margin="5dp"/>
</LinearLayout>
In strings.xml
<color name="red">#FF0000</color>
In Java
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.third);
one.setBackgroundResource(R.color.red);
two.setBackgroundResource(R.color.red);
three.setBackgroundResource(R.color.red);
Output
Create a red_button.xml in drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp"
android:color="#80FFFFFF" />
<corners android:radius="25dp" />
<gradient android:angle="270"
android:centerColor="#ff0000"
android:endColor="#ff0000"
android:startColor="#ff0000" />
</shape>
To get same shape as the default Button
You can play around with radius and stroke width and stroke color to get your Button as you want.
EDIT: You can add the color to original default Button like this:
Drawable d = yourButton.getBackground();
PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); // or whatever color you like
d.setColorFilter(filter);

How can I change the colour of my button linked to a drawable?

I have this Button in my Relative Layout:
<Button
android:id="#+id/gpsButton"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/tableLayout1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="GPS"
android:textColor="#ffffff" />
It is linked to this shade:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FF0000"/>
<stroke android:width="2sp" android:color="#fff" />
</shape>
My question is how can I change the colour from the button in my activity?
I've tried it but the app crashed -.-
Here is a snippet of my code:
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.reli);
GradientDrawable bgShape = (GradientDrawable) relLayout.getBackground();
bgShape.setColor(Color.parseColor("#FFFF00"));
What do I wrong? I don't understand how to do this with the view.
Thank you in advance :)
You can easily change the background color of your Button programatically by adding a color filter. For that, set a white background in the XML file. Then, to make the button red, you can do this:
gpsButton.getBackground().setColorFilter(Color.RED, Mode.MULTIPLY);
The red and white will be multiplied which will result as red. Of course, you can replace the Color.RED by the color you want.

Categories

Resources