Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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);

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);

        }
    }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top