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););
Related
I have created a dynamic layout for edittext with add and minus icons. When i click on add, layout should recreated again and in place of add image, minus should shown.Add icon is changed to minus on first click but minus is not changed to plus.When i clicked on minus, sub layout is removed but not able to change icon.
This is my dynamic layout:
public void dLayout(){
count++;
lay_frame = new LinearLayout(this);
lay_frame.setOrientation(LinearLayout.VERTICAL);
lay_frame.setId(count);
for (int i =0; i<numClass; i++){
lay_main = new LinearLayout(this);
lay_uncle = new LinearLayout(this);
lay_cousin = new LinearLayout(this);
lay_main.setOrientation(LinearLayout.VERTICAL);
lay_uncle.setOrientation(LinearLayout.HORIZONTAL);
lay_uncle.setGravity(Gravity.CENTER);
lay_cousin.setOrientation(LinearLayout.HORIZONTAL);
lay_cousin.setGravity(Gravity.CENTER);
txt_uncle = new TextView(this);
txt_uncle.setText("Uncle Name");
txt_uncle.setTextColor(Color.BLACK);
txt_uncle.setPadding(0, 20 , 0, 20);
txt_uncle.setTextSize(14);
txt_cousin = new TextView(this);
txt_cousin.setText("Cousin Name");
txt_cousin.setTextColor(Color.BLACK);
txt_cousin.setPadding(0, 20 , 0, 0);
txt_cousin.setTextSize(14);
img_add = new ImageView(this);
img_add.setImageResource(R.drawable.add01);
img_add.setPadding(8, 0, 0 ,0);
img_minus = new ImageView(this);
img_minus.setImageResource(R.drawable.minus);
img_minus.setPadding(8, 0, 0 ,0);
img_minus.setVisibility(View.GONE);
img_cousinadd = new ImageView(this);
img_cousinadd.setImageResource(R.drawable.add01);
img_cousinadd.setPadding(8, 0, 0 ,0);
img_cousinminus = new ImageView(this);
img_cousinminus.setImageResource(R.drawable.minus);
img_cousinminus.setPadding(8, 0, 0 ,0);
ed_uncle = new EditText(this);
ed_uncle.setInputType(InputType.TYPE_CLASS_TEXT);
ed_uncle.setPadding(12, 8 ,8 ,8);
ed_uncle.setTextColor(Color.BLACK);
ed_uncle.setTextSize(14);
ed_uncle.setBackgroundResource(R.drawable.border);
ed_cousin = new EditText(this);
ed_cousin.setInputType(InputType.TYPE_CLASS_TEXT);
ed_cousin.setPadding(12, 8 ,8 ,8);
ed_cousin.setTextColor(Color.BLACK);
ed_cousin.setTextSize(14);
ed_cousin.setBackgroundResource(R.drawable.border);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(80, 0, 0, 0);
ed_cousin.setLayoutParams(params);
txt_cousin.setLayoutParams(params);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(40, 0, 0, 0);
ed_uncle.setLayoutParams(params1);
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
dLayout();
}
});
img_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) mainLayout.findViewById(count);
mainLayout.removeView(temp);
count--;
}
img_minus.setVisibility(View.GONE);
img_add.setVisibility(View.VISIBLE);
}
});
lay_main.addView(txt_uncle);
lay_uncle.addView(ed_uncle);
lay_uncle.addView(img_add);
lay_uncle.addView(img_minus);
lay_main.addView(lay_uncle);
lay_main.addView(txt_cousin);
lay_cousin.addView(ed_cousin);
lay_cousin.addView(img_cousinadd);
lay_main.addView(lay_cousin);
lay_frame.addView(lay_main);
}
mainLayout.addView(lay_frame);
}
Change your btn add click logic to this
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dLayout();
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
}
});
Because after setting visibility again your setting the minus button visibility gone in the dLayout(); which means the property is overridden by next value
In onClick listener for minus button,lay_frame LinearLayout been removed from the main layout. Minus button is child of lay_frame layout, which is no more attached to view tree. If you are removing the view on click on minus button,changing the icon from minus to plus image doesn't make any sense.
I have made a TableLayout with two for loops, one for row and one for columns.
Inside the tablelayout is buttons with backgroundresources(images).
The problem is I cant figure out how to set an onClickListener for each button because
i have not given them an id, (they are in the loops).
Any help would be very kind.
Here is the Code:
public class CartoonActivity extends Activity implements OnClickListener {
private static final int NUM_ROWS = 3;
private static final int NUM_COL = 3;
int[] images = new int[] {R.drawable.cartoon1, R.drawable.cartoon2,
R.drawable.cartoon3, R.drawable.cartoon4,
R.drawable.cartoon5, R.drawable.cartoon6,
R.drawable.cartoon7, R.drawable.cartoon8,
R.drawable.cartoon9};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags
(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.cartoons_menu);
populateButton();
}
private void populateButton() {
TableLayout table = (TableLayout)findViewById(R.id.tableForCartoons);
for (int row = 0; row < NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col < NUM_COL; col++) {
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT,
1.0f));
tableRow.addView(button);
button.setBackgroundResource(images[0]++);
button.setOnClickListener(this);
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getBackground().equals(images[0]) == true) {
Intent cartoon1Intent = new Intent(getApplicationContext(),
Cartoon1Activity.class);
startActivity(cartoon1Intent);
}
}
}
int i=0;
private void populateButton() {
TableLayout table = (TableLayout)findViewById(R.id.tableForCartoons);
for (int row = 0; row < NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col < NUM_COL; col++) {
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT,
1.0f));
tableRow.addView(button);
button.setBackgroundResource(images[0]++);
button.setOnClickListener(this);
button.setId(++i);//setId for button so that you can use it in onClick method
}
}
public void onClick(View v) {
v.getId(); //will give give respective Id which is set.
}
}
Here I have to add text view programmatically based on array list size. Text views should be appear in row like continues pattern...
eg. tv1, tv2, tv3 and so on till the size of array list.
But here I am getting text views which are appearing on each other. I can't read the text on them. Here is my code:
ArrayList<String> languageNames = new ArrayList<String>();
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
if(languageNames.size()>0)
{
int size = languageNames.size();
TextView[] tv = new TextView[size];
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, tvLocation.getId());
for(int i=0; i<size; i++)
{
tv[i] = new TextView(getBaseContext());
tv[i].setText(languageNames.get(i).toString());
tv[i].setLayoutParams(p);
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
rl.addView(tv[i]);
}
}
else
{
}
what needs to be done so that I can get text views in appropriate manner?
Add buttons inside a LinearLayout and add this LinearLayout in the RelativeLayout.
RelativeLayout r1 = (RelativeLayout) findViewById(R.id.r1);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, tvLocation.getId());
LinearLayout LL = new LinearLayout(getBaseContext());
LL.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i< size;i++) {
tv[i] = new TextView(getBaseContext());
tv[i].setText(languageNames.get(i).toString());
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
LL.addView(tv);
}
r1.addview(LL, p);
Try this code:
LinearLayout rl = (LinearLayout)findViewById(R.id.mainLayout);
TextView[] tv = new TextView[10];
for(int i=0; i<10; i++)
{
tv[i] = new TextView(getBaseContext());
tv[i].setText("TextView "+ i);
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
rl.addView(tv[i]);
}
Hope this will help you
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 put my number button like that. This is my code:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
TextView titleView = new TextView(this);
titleView.setText("Hello World!");
layout.addView(titleView);
android.widget.LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1);
android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
btnConnect.setLayoutParams(param);
layout2.addView(btnConnect);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
layout2.addView(btnDisconnect);
btnDisconnect.setLayoutParams(param);
layout.addView(layout2);
TableLayout tblLayout = new TableLayout(this);
tblLayout.setOrientation(TableLayout.HORIZONTAL);
TableRow tblrow = null;
for (int i = 1; i <= 9; i++) {
if (i % 3 == 1) {
tblrow = new TableRow(this);
tblLayout.addView(tblrow);
}
Button b = new Button(this);
b.setText("" + i);
tblrow.addView(b);
}
TableRow tr = new TableRow(this);
Button btnZero = new Button(this);
btnZero.setText("0");
Button btnHash = new Button(this);
btnHash.setText("#");
Button btnStar = new Button(this);
btnStar.setText("*");
tr.addView(btnZero);
tr.addView(btnHash);
tr.addView(btnStar);
tblLayout.addView(tr);
layout.addView(tblLayout);
setContentView(layout);
this complies:
In order to make my columns like that. I used this code:
android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1);
tblLayout.setLayoutParams(param);
but there's no change. What do i need to do? Is that Layoutparams not enough to do that?
You need to set the widths of layout, layout2, tblLayout, each tblRow, and tr to MATCH_PARENT and each Button must have an equal layout_weight.