Setting % of a background color programmatically

963 views Asked by At

Say I want to color only 64% of the background of a linearlayout. I want to color it from left to right and have it be a solid color.

At first I tried using Gradient Drawables because it allowed me to specify the orientation of the color, and the color...but it doesn't allow me to specify a %. If I could do this in an xml drawable it would be ideal, but the percents are going to be changing and I need to re-adjust the background based on the %.

Here is my code for coloring the whole view. Is there a way to only color say 35% of the view instead of the whole view?

GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                                        new int[] {getContext().getResources().getColor(R.color.icitizen_poll_opaque_gold),
                                        getContext().getResources().getColor(R.color.icitizen_poll_opaque_gold)});
                                gradient.setCornerRadius(1f);
                                v.setBackground(gradient);
3

There are 3 answers

1
3mpty On BEST ANSWER

It's totally possible, few ways to do it, but I would recommend using custom Drawable, for example:

public class PercentDrawable extends Drawable {

private final int percent;
private final Paint paint;

public PercentDrawable(int percent) {
    super();

    this.percent = percent;
    this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.paint.setColor(0xffff0000);
}

@Override
public void draw(Canvas canvas) {
    canvas.drawRect(0, 0, canvas.getWidth(), percent * canvas.getHeight() / 100, paint);
}

@Override
public void setAlpha(int i) {

}

@Override
public void setColorFilter(ColorFilter colorFilter) {

}

@Override
public int getOpacity() {
    return 0;
}

}

...and to use it:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.background).setBackground(new PercentDrawable(30));
} //... rest of Activity

R.id.background is simple LinearLayout with match_parent w/h, It also can be simple View. You should keep xml layout as simple as possible.

2
Anton Kovalyov On

You can create another layout under yours using FrameLayout and color it's background.

<FrameLayout
   android:layout_height="match_parent"
   android:layout_width="match_parent">
   <LinearLayout
     android:layout_height="match_parent"
     android:layout_width="0dp"/> // set 35% of screen programmatically
   <LinearLayout
     android:layout_height="match_parent"
     android:layout_width="match_parent"/>
</FrameLayout>

How to get screen size:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
0
Satish Bonagiri On

I think it is not possible, instead you can try with nested linearlayouts and use a weight attribute (to specify the %)

Sample Example :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.7"
        android:background="@android:color/darker_gray">

     </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:background="@android:color/black">

    </LinearLayout>




</LinearLayout>