0

I wanted to create to circle button. So I got the hint from here How to get round shape in Android . As mention in the link it is necessary to have both height and weight of button to be of same size to get shape as circle, otherwise it will be oval shape. We cannot use wrap_content than it will be oval shape.

Buy the problem is now I creating button dynamically and I try to set height and width of button same but still I am getting oval shape button instead of circle.

And I try through xml file keeping button weight and height same it's work, but through dynamic it is not. Below is the code.

for (int count = 1; count <= rowb; count++) 
{
tblRow[count] = new TableRow(getApplicationContext());
tbl.addView(tblRow[count]);

    for (int j = 1; j <= rowb; j++) {   
    String nameB=""+i;
    btn[i] = new Button(getApplicationContext());
    btn[i].setId(i);
    btn[i].setText(nameB);
    btn[i].setWidth(1);
    btn[i].setHeight(1);
    tblRow[count].addView(btn[i]);
    btn[i].setOnClickListener(getOnClickDoSomething(btn[i],i));
    i++;
    }
}
notifyAllObservers();
move--;
}

I also try but it also did,t work

TableLayout.LayoutParams lp = new TableLayout.LayoutParams(5,5);
btn[i].setLayoutParams(lp);

Can anybody let me know what the problem is ?How i get tge circle shale button instead of oval ?

Community
  • 1
  • 1
eswaat
  • 733
  • 1
  • 13
  • 31
  • First of all Welcome to the Stack Overflow. Now first thing Before asking question here . Have you tried to search your question on the internet??? You can get everything there – The Hungry Dictator Jan 07 '14 at 05:52
  • @RonakBhatt I tried and also as shown in the code I tried two thing which I get from stack overflow itself but it didn't work. – eswaat Jan 07 '14 at 06:14
  • are you getting any kind of error??? Or its just dosent work?/ – The Hungry Dictator Jan 07 '14 at 06:15
  • no I am not getting any error. It just I want button to be round shape and I am getting oval shape tha's it. – eswaat Jan 07 '14 at 06:25
  • You can make it possible but adjusting height and width.... Have you tried to adjust the height and width??? Like you have taken here 1 1 for both try with 1 2 or 2 1 and check results... Nad remove */ from the code. – The Hungry Dictator Jan 07 '14 at 06:28
  • And if you want round button then you can use thins link as well http://stackoverflow.com/questions/9884202/custom-circle-button – The Hungry Dictator Jan 07 '14 at 06:29
  • @RonakBhatt there also height and width are same if you change then it will be oval. – eswaat Jan 07 '14 at 06:32
  • I know but if you are making it same then also you are getting oval type thats why i suggested you... – The Hungry Dictator Jan 07 '14 at 06:45

2 Answers2

5

You can set width and height by following code:

Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tblRow.addView(btnTag);

OR you can also set dp instend of wrap_content in it. like:

Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(30, 30));
tblRow.addView(btnTag);
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
0

I have this and it worked for me.

final Button bt = new Button(ClassListActivity.this);
bt.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 450));

The first one with MATCH_PARENT is the width. the 450 is the height. Hope it helps!

ashlrem
  • 117
  • 1
  • 3
  • 17