I am trying to create buttons at run time, which I did. The problem is they all overlap each other. I want to change the position and nothing is working!
Here is my code:
for (int i = 1; i < 5; i++) {
RelativeLayout.LayoutParams p= new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button b = new Button(this);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
b.setText(""+ i);
b.setId(100+i);
b.setWidth(50);
b.setHeight(40);
p1.this.addContentView(b, p);
}
Use LinearLayout and LayoutParams.Set params's values and add it like linearLayout.addView(button, params) that.
Related
can somebody solve my problem (there is no margin/space for vertical menu) ? see my code link http://pastebin.com/zzdY3QQR
Try this..
LinearLayout menuvertical = (LinearLayout) findViewById(R.id.menuvertical);
for (int i = 0; i < arrseparate.size(); i++) {
TextView menucategory = new TextView(getBaseContext());
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0);
menucategory.setText(arrseparate.get(i).toString());
menucategory.setTextSize(25);
menucategory.setTextColor(Color.WHITE);
menucategory.setBackgroundResource(R.layout.bordermenu);
final int j = i;
menucategory.setGravity(Gravity.CENTER_HORIZONTAL);
if (j != 0) {
menucategory.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
OtherClass.setCategory(arrseparate.get(j).toString());
Global.MainActivity.finish();
Intent MyIntent = new Intent(getBaseContext(),
ViewProduct.class);
startActivity(MyIntent);
}
});
}
menuvertical.addView(menucategory, llp);
}
Does anybody know how to create buttons for the alphabet (for a hangman-application) in a for loop?I'm not sure what needs to be done in the java class and what needs to be done in the xml file.
You do not need to do anything in the XML file. This can all be done in a class.
for(int i=0; i < x; i++) // where x is the size of the list containing your alphabet.
{
Button button = new Button(this);
button.setId(i);
yourView.add(button);
}
here you go. but you must also note that your layout must be linear and the orientation must be set depending on how you want your button arranged.
If you use relative view the buttons will stack over each other and it is your last looped button that will be shown.
LinearLayout layout = (LinearLayout) findViewById(R.id.rl_table_of_contents);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
Button[] btn = new Button[your_array];
for (int i = 0; i < your_array.length(); i++) {
btn[i] = new Button(getApplicationContext());
btn[i].setText("Button "+ i);
//btn[i].setBackground();
btn[i].setTextSize(20);
//btn[i].setHeight(100);
btn[i].setLayoutParams(param);
btn[i].setPadding(15, 20, 15, 20);
layout.addView(btn[i]);
//btn[i].setOnClickListener(handleOnClick(btn[i]));
}
View.OnClickListener handleOnClick(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
}
};
}
int count=26;
Button[] btnArray = new Button[26];
LinearLayout layout=new LinearLayout(this);
LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
for(int i=0;i<count;i++){
btnArray[i]=new Button(this);
layout.addView(btnArray[i],params);
}
for(int i=0; i<n; i++)
{
Button b = new Button(this);
b.setId(i);
}
I want to create several ImageButtons programmatically. I am able to create them but the click event listener keeps receiving the same view (Button 2), whether I click on button 0 ,button1 or button 2.
RelativeLayout gameBoard = (RelativeLayout) findViewById(R.id.RelGameboard);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
for(int i = 0 ; i < 3 ; i++)
{
ImageButton btnGreen = new ImageButton(this);
btnGreen.setImageResource(R.drawable.bola_verde);
btnGreen.setLayoutParams(lp);
btnGreen.setOnClickListener(mGreenBallOnClickListener);
btnGreen.setBackgroundColor(Color.TRANSPARENT);
btnGreen.setTag(i);
btnGreen.setId(i);
gameBoard.addView(btnGreen);
}'
Click event listener:
private View.OnClickListener mGreenBallOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//check which green ball was clicked
ImageButton imgBtn = (ImageButton) v;
Log.i("greeny","Clicked on green ball->"+imgBtn.getTag()+" v.ID->"+v.getId());
}
};
For any imagebutton drawn, when I click I get : Clicked on green ball->2 v.ID->2
The buttons are in different positions ( i set a different padding for each one of them but in order to simplify the code I didn't put it here )
Try adding a onClickListener -
private View.OnClickListener ClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
int selected_item = (Integer) v.getTag();
}
};
EDIT
Try creating a array of ImageButtons -
ImageButton[] btnGreen = new ImageButton[3];
for (int i = 0; i < 3; i++) {
btnGreen[i] = new ImageButton(this);
btnGreen[i].setImageResource(R.drawable.bola_verde);
btnGreen[i].setLayoutParams(lp);
btnGreen[i].setOnClickListener(ClickListener);
btnGreen[i].setBackgroundColor(Color.TRANSPARENT);
btnGreen[i].setTag(i);
btnGreen[i].setId(i);
gameBoard.addView(btnGreen[i]);
}
This happen because you do not set layout of button, All button in same place.
Use Aboveof,Belowof,left,right,center
Example
params.addRule(RelativeLayout.ALIGN_PARENT_ABOVE);
relativeLayout.addView(button, params);
Try this code:
RelativeLayout gameBoard = (RelativeLayout)findViewById(R.id.RelGameboard);
ImageButton[] imageButtons;
private final int NUMBER_OF_IMAGE_BUTTONS = 5; //your number of image buttons
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
imageButtons = new ImageButton[NUMBER_OF_IMAGE_BUTTONS];
for(int i = 0 ; i < NUMBER_OF_IMAGE_BUTTONS; i++){
imageButtons[i] = new ImageButton(this);
imageButtons[i].setImageResource(R.drawable.bola_verde);
imageButtons[i].setLayoutParams(lp);
imageButtons[i].setOnClickListener(mGreenBallOnClickListener);
imageButtons[i].setBackgroundColor(Color.TRANSPARENT);
imageButtons[i].setTag(i);
imageButtons[i].setId(i);
gameBoard.addView(imageButtons[i]);
}
I want to show 4 options of numbers as like quiz options, out of four three should be wrong and one should be the right answer. All options are numeric numbers, and I want to change right answer's position every time when page opens. Please help me
and my result is in a String result, where i will add result.....
I'm trying this , but not getting the solution
LinearLayout rowoptions = (LinearLayout) findViewById(R.id.linearlayout);
ArrayList<Integer> numbers = new ArrayList<Integer>();
String[] s = new String[4];
while (numbers.size() < 4) {
int random = r.nextInt(10)+1;
int chk = r.nextInt(4)+1;
if (!numbers.contains(random)) {
numbers.add(random);
s[chk] =String.valueOf(random);
}
}
for (int i = 0; i < 4; i++) {
Button optionbutton = new Button(this);
optionbutton.setText(s[i]);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70);
layoutParams.setMargins(5, 5, 0, 0); // left, top, right, bottom
optionbutton.setLayoutParams(layoutParams);
//ivBowl.setBackgroundDrawable(null);
rowoptions.addView(optionbutton);
}
Hi do you need something like this
LinearLayout layout=(LinearLayout)findViewById(R.id.mainlayout);
ArrayList list=new ArrayList();
list.add(answer);
Random r=new Random();
for(int i=0;i<3;i++)
{
while(true)
{
int next=r.nextInt(10)+1;
if(!list.contains(next))
{
list.add(next);
break;
}
}
}
Collections.shuffle(list);
for(int i=0;i<list.size();i++)
{
final Button b=new Button(this);
b.setText(list.get(i).toString());
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedAnser=Integer.parseInt(b.getText().toString());
if(!(selectedAnser==answer))
{
Toast.makeText(context, "Wrong Answer", Toast.LENGTH_SHORT).show();
}
}
});
layout.addView(b);
}
How can I set the gravity and margin for auto-generated(within the code) TextViews and Buttons? They are inside a LinearLayout.
Here is my generation code:
for(int i=0; i<num_enter; i++){
final int cuco = i;
LinearLayout linlay = new LinearLayout(this);
linlay.setOrientation(0);
TextView text = new TextView(this);
text.setText(name[cuco] + " ");
linlay.addView(text);
TextView num = new TextView(this);
num.setId(cuco);
num.setText("" + current[cuco]);
linlay.addView(num);
Button minus = new Button(this);
minus.setText("-");
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] - 1;
current[cuco] = current[cuco] -1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
minus.setLayoutParams(p);
linlay.addView(minus);
Button plus = new Button(this);
plus.setText("+");
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] + 1;
current[cuco] = current[cuco] + 1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
plus.setHeight(LayoutParams.WRAP_CONTENT);
plus.setWidth(50);
plus.setLayoutParams(w);
linlay.addView(plus);
main.addView(linlay);
}
Hope you find a way to set the button gravity without changing it size.
1) Get Reference of TextView say tv.
2) tv.setGravity(Gravity.CENTER);
It is not sure work with LinearLayout.. Try Relative Or Absolute
Use View.setLayoutParams(ViewGroup.LayoutParams params). Look at http://developer.android.com/reference/android/R.styleable.html#ViewGroup_Layout for the LayoutParams.
Since nobody has addressed margins
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
p.setMargins(10, 10,10,10);
p.gravity = Gravity.CENTER;
textView.setLayoutParams(p);
When using setLayoutParams make sure the type of layout params matches the parent view which, in this case, is LinearLayout
setGravity(Gravity.CENTER); Will set the gravity of the text within the view