ANDROID: How can I set up buttons in a for loop? - android

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

Related

TextView on the right of dynamically created Buttons

As seen in the picture, everything works fine except that myTextView, instead of appearing just on the right of the last Button, it does on top of 16, 17 and 18. I can’t manage these 3 Buttons to appear bellow the rest. Here is my essential code, where I create dynamically the Buttons and myTextView:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout1); // id del XML
layout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 4; i++) {
LinearLayout fila = new LinearLayout(this);
fila.setLayoutParams(new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 5; j++) {
if (i==3 && j==3){
TextView myTextView = new TextView(this);
LinearLayout.LayoutParams layoutParams=newLinearLayout.LayoutParams(490, 40);
layoutParams.setMargins(870, 30, 0, 0);
myTextView.setTextSize(26);
myTextView.setLayoutParams(layoutParams);
layout.addView(myTextView);
break;
}
Button btnTag = new Button(this); //
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(255, 166));
fila.addView(btnTag);
btnTag.setId(j + 1 + (i * 5));
btnTag.setOnClickListener(prueba);
}
layout.addView(fila);
}
}
At last I have solved the problem. Instead of a LinearLayout I have to use a RelativeLayout. Like this, I can set the TextView wherever I want on the screen throughout the xml. Here is the code, where with rel_btn I set the Buttons wherever I want, too:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1); // id del XML
for (int i = 1; i < 19; i++) {
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
switch(i) {
case 1: rel_btn.leftMargin = 0; rel_btn.topMargin = 0; break;
case 2: rel_btn.leftMargin = 255; rel_btn.topMargin = 0; break;
case 3: rel_btn.leftMargin = 510; rel_btn.topMargin = 0; break;
…… as many as Buttons requiered
}
rel_btn.width = 255; rel_btn.height = 165;
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setId(0+i); // les pone el ID
btnTag.setOnClickListener(prueba);
layout.addView(btnTag);
}
}
1. Set layout weight sum
fila.setWeightSum(5);
2. Set width = 0dp and weight = 1 for all buttons
btnTag.setLayoutParams(new LinearLayout.LayoutParams(0, 166, weight));
3. Set width=0dp and weight = 2 for textview
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 40);
layoutParams.weight = 2;
//layoutParams.setMargins(870, 30, 0, 0);
myTextView.setTextSize(26);
myTextView.setLayoutParams(layoutParams);
Updated onCreate method
public void onCreate(Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) findViewById(R.id.layout1); // id del XML
layout.setOrientation(LinearLayout.VERTICAL);
int width = 0; //0dp, we will use weight to set width
int weight = 1;
for (int i = 0; i < 4; i++) {
LinearLayout fila = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, weight);
fila.setLayoutParams(params);
fila.setWeightSum(5);
for (int j = 0; j < 5; j++) {
if (i==3 && j==3){
TextView myTextView = new TextView(this);
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 40);
layoutParams.weight = 2;
//layoutParams.setMargins(870, 30, 0, 0);
myTextView.setTextSize(26);
myTextView.setLayoutParams(layoutParams);
layout.addView(myTextView);
break;
}
Button btnTag = new Button(this); //
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 166, weight));
fila.addView(btnTag);
btnTag.setId(j + 1 + (i * 5));
btnTag.setOnClickListener(prueba);
}
layout.addView(fila);
}
}

Adding a button to last row of TableLayout

I have added buttons to the TableLayout programmatically usin TableRow. Each row should include three buttons. If number of buttons is multiple of three there is no problem. All buttons are placed with same size. However the number of buttons for last row is less than three, its size is not matched with other buttons.
Here is my code:
tableView.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams
.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams
.MATCH_PARENT, ScrollView.LayoutParams.WRAP_CONTENT,1));
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT,1);
layoutParams.setMargins(0,20,0,20);
int numberOfButtonsForLastRow = masalar.size() - numberOfRows*3;
for (int i = 0; i < numberOfRows; i++)
{
tr = new TableRow(fragmentView.getContext());
tr.setLayoutParams(layoutParams);
for (int j= 0;j<3;j++)
{
btn = new Button(getActivity());
btn.setBackgroundResource(R.drawable.buttonstyle);
btn.setText(masalar.get(masaCounter));
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams
.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1);
params.setMargins(20, 0, 20, 0);
btn.setLayoutParams(params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
tr.addView(btn);
masaCounter++;
}
tableView.addView(tr);
}
if(numberOfButtonsForLastRow>0)
{
tr = new TableRow(fragmentView.getContext());
tr.setLayoutParams(layoutParams);
for (int j= 0;j<numberOfButtonsForLastRow;j++)
{
btn = new Button(getActivity());
btn.setBackgroundResource(R.drawable.buttonstyle);
btn.setText(masalar.get(masaCounter));
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams
.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 20, 0);
btn.setLayoutParams(params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
tr.addView(btn);
masaCounter++;
}
tableView.addView(tr);
}
This is how the last button looks like. I want it to be same size with others. How can I do this?
Add a Layout weight in your TableRow.LayoutParams same as what you did for the first 2 rows so buttons are divided into your TableView.
TableRow.LayoutParams params = new TableRow.LayoutParams(btn.getWidth(), btn.getHeight());
You need weigths, you devide the space of one row between (0 .. 1)
TableLayout.LayoutParams rows = new
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 0,
1.0f / (float) NUMBUTTONS);
row.setLayoutParams(rows););

Creating Imagebutton programmatically

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

How to place text of an button in center

In my array of buttons i want to place the text of each button to be placed in center.I have use Gravity but it doesnt work. The code i have use is.Please anyone help me to solve this out.
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
//Create Button
for (int i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout, param);
for(int j=0; j<7; j++)
{
m_pBtnDay[i][j] = new Button(this);
m_pBtnDay[i][j].setTextSize(12);
m_pBtnDay[i][j].setGravity(Gravity.CENTER);
rowLayout.addView(m_pBtnDay[i][j], param);
m_pBtnDay[i][j].setOnLongClickListener(this);
m_pBtnDay[i][j].setOnClickListener(this);
m_pBtnDay[i][j].setOnTouchListener(this);
//save button position
m_pBtnDay[i][j].setTag(new CalendarForm(i , j));
}
}
try this: m_pBtnDay[i][j].getPaint().setTextAlign(Paint.Align.CENTER);

Dynamic TextView in Relative layout

I am triying to use dynamic layout for comment part of my project but when i settext of textview dynamicly the output only appears in top of the screen. And it puts the output over the other outputs
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++) {
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
ll.addView(cb);
}
So how can i put the output on the bottom of the screen linearly.
You should use LinearLayout to automatically add one TextView after another.
Assuming you can't live without RelativeLayout, you'll need to dynamically generate ids for all TextView you create in order to put one view under another. Here is example:
public class HelloWorld extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
Random rnd = new Random();
int prevTextViewId = 0;
for(int i = 0; i < 10; i++)
{
final TextView textView = new TextView(this);
textView.setText("Text "+i);
textView.setTextColor(rnd.nextInt() | 0xff000000);
int curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
layout.addView(textView, params);
}
}
}
You've to provide the location of your newly added view. As #Adinia said, with no position, it will be aligned to the top by default. So you can use the following code to do it with RelativeLayout;
RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
// Set the location of your textView.
dynaText.setPadding(0, (i * 30), 0, 0);
containerLayout.addView(dynaText);
}
If you want to show multiple textviews one after the other, then you should go with LinearLayout.
You may also add Dynamic textview to relative layout. Here with i have attached some code this may help you.
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++)
{
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
cb.setId(2000+i);
RelativeLayout.LayoutParams TextViewLayoutParams = new RelativeLayout.LayoutParams(100,100);
if (i != 0 )DispViewLayoutParams.addRule(RelativeLayout.BELOW, 2000 - (i-1));
ll.addView(cb,TextViewLayoutParams);
}

Categories

Resources