12

I have LinearLayout in xml:

    <LinearLayout
        android:id="@+id/progress"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/progress_height"
        android:layout_alignParentBottom="true"
        android:baselineAligned="false"
        android:orientation="horizontal" />

and I would like to generate dynamically few another LinearLayouts and put them to "progress" equaly spaced, for example:

  • First LinearLayout added will occupy all the space.
  • Second LL will share 50% of the space with 1LL
  • Third LL will share 33% of the space with 1LL and 2LL
  • and so on...

Every LinearLayout will have random background color I wrote something like this:

mProgress = (LinearLayout) findViewById(R.id.progress);
.
.
.
LinearLayout prog = new LinearLayout(this);
            prog.setBackgroundColor(CommonUtils.getNextRandomColor());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);

            prog.setLayoutParams(params);
            mProgress.addView(prog);

When user press the buttons, another LL will generate, with different color.

My method doesn't work. There is no background color in layouts.

Maybe there is another much simpler method to achive some kind of progress bar with colors sharing some space equally?

Bresiu
  • 2,123
  • 2
  • 19
  • 31

3 Answers3

14

Double check that getNextRandomColor is returning something like.-

getResources().getColor(colorResId);

and not just a colorResId. If that's the case, you could try this.-

prog.setBackgroundColor(getResources().getColor(CommonUtils.getNextRandomColor()));

Anyway, if you're building a multicolor progress bar, you should consider changing the width of a single layout, and using a gradient color.

ssantos
  • 16,001
  • 7
  • 50
  • 70
  • My getNextRandomColor method: public static int getNextRandomColor() { int[] colors = new int[]{ R.color.color1, R.color.color2 }; return colors[getNextRandomInt(0, colors.length)]; I have added "getResources().getColor" and now I have some compilation errors. Previously, everythings work well with this method, with .xml LinearLayouts. – Bresiu Oct 10 '13 at 07:34
  • Thanks @Bresiu. My guesses are right then. `setBackgroundColor` doesn't expect a colorResId like R.color.turquoise, but the real value of that color, which you can get by `getResources().getColor(R.color.turquoise)`. Just try returning `getResources().getColor(colors[getNextRandomInt(0, colors.length)]);` – ssantos Oct 10 '13 at 07:37
  • I have edited my first comment (Accidentally hit enter too soon) – Bresiu Oct 10 '13 at 07:38
  • Gradle: error: cannot find symbol variable getResources with: return getResources.getColor(colors[getNextRandomInt(0, colors.length)]); – Bresiu Oct 10 '13 at 07:42
  • Ok, you're missing the parenthesis in getResources(). – ssantos Oct 10 '13 at 07:44
  • No, its something else. I think its related to that, CommonUtils its external class. I think, I should do something like: context.getResources()... – Bresiu Oct 10 '13 at 07:54
  • 1
    Right, my bad, it would be easier then to leave your original `getNextRandomColor`, and call `getResources().getColor()` from the `Activity`. I'm editing my answer. – ssantos Oct 10 '13 at 07:59
  • ok, its working now with this: return context.getResources().getColor(colors[getNextRandomInt(0, colors.length)]); Thank You, I will mark your answer as correct. And I think i can try your idea with gradient - its more elegant. – Bresiu Oct 10 '13 at 08:05
5
LinearLayout lp = new LinearLayout(context) ;
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height , .60f);
lp.setLayoutParams(layoutParams);


//for setting the background color  // input your color
LinearLayout.setBackgroundColor(Color.parseColor("#000000"));

OR

Directly invoke a color

lp.setBackgroundColor(Color.WHITE);
DroidDev
  • 1,527
  • 4
  • 20
  • 42
ankit
  • 154
  • 1
  • 11
2

you can try this.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Layout" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:id="@+id/main_lay">
    </LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.testlayout;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity implements OnClickListener{


    private Button add_btn;
    private  LinearLayout main_lay;
    private LinearLayout.LayoutParams param;


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

    private void init()
    {
        main_lay = (LinearLayout)findViewById(R.id.main_lay);
        add_btn = (Button)findViewById(R.id.button1);
        add_btn.setOnClickListener(this);
        param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT,1);

    }

    @Override
    public void onClick(View v) {

        if(v == add_btn)
        {
            LinearLayout lay = new LinearLayout(this);
            lay.setLayoutParams(param);
            Random rnd = new Random(); 
            int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
            lay.setBackgroundColor(color);
            main_lay.addView(lay);

        }
    }


}
Thirumoorthy
  • 589
  • 2
  • 11
  • Thank you for providing code, but mine is more or less the same. It was getResourcess().getColor() problem (I have method which shuffle colors from R.color) – Bresiu Oct 10 '13 at 08:09