0

here i want to add a black color border dynamically b/w each button .

As you can see the gray button , they are getting mixed because of same color. I want a black line or any thing b/w each button. So I can differentiate it.

enter image description here

Hope you have understood my problem.?

Following is the code

                int i = 1;

        int nLeftStartPnt        = 18;
        int nTopStartPnt         = 0;
        int nMarkerHeight        = 28;
        int nMarkerVwActualWidth = 780;
        int nMarkerWidth         = 50;
        int nlen                 = 10;
        int nsetPaddingTop       = 123;
        int nsetPaddingLeft      = 0;

   FrameLayout layout = (FrameLayout) findViewById(R.id.MarkerLinearlayout); 
     for(i = 1; i < nlen ; i++)
     {
         Button bMarkerBtn = new Button(this);
         layout.addView(bMarkerBtn);

         bMarkerBtn.setId(i);
          bMarkerBtn.setX(nsetPaddingLeft + i);
         //bMarkerBtn.setTop(nsetPaddingTop);
         bMarkerBtn.setWidth(nMarkerWidth);
         bMarkerBtn.setHeight(nMarkerHeight);

        // bMarkerBtn.getTotalPaddingLeft()

         // bMarkerBtn.setPadding(2,0, 0, 0);

         if(i == 0)
         {
            bMarkerBtn.setBackgroundColor(Color.DKGRAY);
         }
         if(i == 1)
         {
             bMarkerBtn.setBackgroundColor(Color.BLUE);
         }
         if(i == 2)
         {
            bMarkerBtn.setBackgroundColor(Color.RED); 
         }
         if(i == 3)
         {
            bMarkerBtn.setBackgroundColor(Color.GREEN); 
         }
         if(i == 4)
         {  
         bMarkerBtn.setBackgroundColor(Color.BLUE); 
         }
         if(i == 5)
         {
            bMarkerBtn.setBackgroundColor(Color.YELLOW); 
         }
         if(i == 6)
         {
            bMarkerBtn.setBackgroundColor(Color.GRAY); 
         }
         if(i == 7)
         {
            bMarkerBtn.setBackgroundColor(Color.GRAY); 
         }
         if(i == 8)
         {
            bMarkerBtn.setBackgroundColor(Color.GRAY); 
         }
         if(i == 9)
         {
            bMarkerBtn.setBackgroundColor(Color.GRAY); 
         }
         if(i == 10)
         {
         bMarkerBtn.setBackgroundColor(Color.GRAY); 
         }

        nsetPaddingLeft = nsetPaddingLeft + nMarkerWidth +1;

        // Log.d("Button Click","Clicked ON "+ Integer.toString(i));

         bMarkerBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {

                    Log.d("Button Click","Clicked ON "+ Integer.toString(view.getId()));
                }
            });
     }
Tejas
  • 358
  • 5
  • 23

3 Answers3

2

You should use layout_marginleft so that there is a gap of "0.1dp" between the gray buttons set the left margins of the gray buttons

 params.setMargins();

 layout.addView(button, params);

you need params to set your margin which is same as

      android:layout_marginLeft=""  
          android:layout_marginRight="" 

         android:layout_marginTop=""   
         and android:layout_marginBottom=""  

     params.setMargins(left, top, right, bottom);


       layout.addView(button, params);


Add  params.setMargins(1,0,0,0); for gray buttons
Metalhead1247
  • 1,978
  • 1
  • 17
  • 28
0

Consider to use LinearLayout with margins, it will do the thing and keep your code clean

... in fact Button is just a TextView with custom background, and you just replaced fancy one to custom.

Andrew Matiuk
  • 924
  • 1
  • 7
  • 21
  • can you provide some more help or can you edit my Answer please? – Tejas Sep 11 '13 at 10:51
  • change FrameLayout to LinearLayout, then LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height) lp.rightMargin = margin; layout.addView(bMarkerBtn); => layout.addView(bMarkerBtn, lp); – Andrew Matiuk Sep 11 '13 at 10:55
  • what exactly is not working? Can you populate LinearLayout with buttons in xml? you can also inflate buttons from xml as well using LayoutInflater.inflate – Andrew Matiuk Sep 11 '13 at 11:18
0

I got the Solution !!!!

Updated Code

    FrameLayout layout = (FrameLayout) findViewById(R.id.MarkerLinearlayout); 
     for(i = 1; i < nlen ; i++)
     {
         Button bMarkerBtn = new Button(this);
         GradientDrawable drawable = new GradientDrawable();
         drawable.setShape(GradientDrawable.RECTANGLE);
         drawable.setStroke(1, Color.BLACK);
         layout.addView(bMarkerBtn);

         bMarkerBtn.setId(i);
         bMarkerBtn.setX(nsetPaddingLeft + i);
         //bMarkerBtn.setTop(nsetPaddingTop);
         bMarkerBtn.setWidth(nMarkerWidth);
         bMarkerBtn.setHeight(nMarkerHeight);
        // bMarkerBtn.setPadding(2,0, 0, 0);

         if(i == 0)
         {
             drawable.setColor(Color.DKGRAY);
         }
         if(i == 1)
         {
             drawable.setColor(Color.BLUE);
         }
         if(i == 2)
         {
             drawable.setColor(Color.RED); 
         }
         if(i == 3)
         {
             drawable.setColor(Color.GREEN); 
         }
         if(i == 4)
         {  
             drawable.setColor(Color.BLUE); 
         }
         if(i == 5)
         {
             drawable.setColor(Color.YELLOW); 
         }
         if(i == 6)
         {
             drawable.setColor(Color.GRAY); 
         }
         if(i == 7)
         {
             drawable.setColor(Color.GRAY); 
         }
         if(i == 8)
         {
             drawable.setColor(Color.GRAY); 
         }
         if(i == 9)
         {
             drawable.setColor(Color.GRAY); 
         }
         if(i == 10)
         {
             drawable.setColor(Color.GRAY); 
         }

         bMarkerBtn.setBackgroundDrawable(drawable);

        nsetPaddingLeft = nsetPaddingLeft + nMarkerWidth +1;

        // Log.d("Button Click","Clicked ON "+ Integer.toString(i));

         bMarkerBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {

                    Log.d("Button Click","Clicked ON "+ Integer.toString(view.getId()));
                }
            });
     }
Tejas
  • 358
  • 5
  • 23
  • but setBackgroundDrawable is deprecated in API16 so to be absolutely correct do something like this [set_border](http://stackoverflow.com/questions/11947603/setbackground-vs-setbackgrounddrawable-android) – Manishika Sep 11 '13 at 11:24
  • Do you have any sample code, manishiKa? I searched there is nothing called set_border. – Tejas Sep 11 '13 at 11:53
  • Its just a link, click on it. – Manishika Sep 11 '13 at 12:00