I want to add button to grid-layout dynamically one by one as they are created but in my case the buttons are created one by one through each iteration but they are added to layout at once when the loop iteration complete.
// here is the code...
public void Add_Button(View view){
final MediaPlayer mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.button_sound);
gridlayout= (GridLayout) findViewById(R.id.layout);
animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.scale_button);
for(int i=0;i<10;i++) {
Button button = new Button(MainActivity.this);
button.setText(i+1 + "");
button.setPadding(10,10,10,10);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.height=70;
params.width=70;
params.topMargin = 10;
params.leftMargin = 10;
*//after one 1 second i want to add button*
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
button.setLayoutParams(params);
button.setAnimation(animation);
mediaPlayer.start();
gridlayout.addView(button);
}
}
You can add like this
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++ {
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
Related
I want to make that TextView fill parent but my code doesn't work.
Here is my code:
TableLayout tableLayout = new TableLayout(this);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
TableRow.LayoutParams param = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
param.span = 2;
param.setMargins(1, 1, 1, 1);
int trainIndex = 0;
int stationIndex = 1;
for(int i = 0; i < 6; i++){
TableRow row = new TableRow(this);
if(i == 0){
TextView text = new TextView(this);
text.setText("Way");
text.setBackgroundColor(Color.GRAY);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(text, tableRowParams);
for(int j = 0; j < 4; j++) {
TextView text1 = new TextView(this);
text1.setText(getInfo(0, stationIndex));
text1.setBackgroundColor(Color.GRAY);
row.addView(text1, param);
stationIndex++;
}
}else {
for (int j = 0; j <= 8; j++) {
TextView text = new TextView(this);
if(j == 0){
text.setText(getInfo(1,trainIndex));
text.setBackgroundColor(Color.GRAY);
row.addView(text, tableRowParams);
trainIndex++;
}else {
text.setText(" train " + j);
text.setBackgroundColor(Color.GRAY);
row.addView(text, tableRowParams);
}
}
}
tableLayout.addView(row);
}
setContentView(tableLayout);
When I apply layout params to textview it doesn't work and when text isn't very long it causes white space in cells, I want that all cells have grey background.
Here is the sample how it looks:
What am I doing wrong ?
Be sure that your container has the FILL_PARENT property
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
You forgot to set LayoutParams for the TextView. By default is uses WRAP_CONTENT for both width and height.
Also, use MATCH_PARENT instead of FILL_PARENT
EDIT
Try this:
TableRow row = new TableRow(this);
TextView text = new TextView(this);
text.setText("Way");
text.setBackgroundColor(Color.GRAY);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(text);
I want to set user input to the textviews in a tablelayout for a word game application.i am new to android please help me through this.
tl = (TableLayout)findViewById(R.id.table);
TableLayout.LayoutParams tablelparam = new TableLayout.LayoutParams(30,30);
for (int r = 1; r <=9; r++) {
r1= new TableRow(this);
r1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int c = 1; c<=5; c++) {
TextView txtview=new TextView(MainActivity.this);
txtview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
txtview.setId(counter);
counter++;
txtview.setText( +counter + inputWord);
r1.addView(txtview);
}
tl.addView(r1,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
}
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<String>words = new ArrayList<String>();
for (int j = 0; j < words.size(); j++) {
TextView txtview = new TextView(MainActivity.this);
txtview.setId(j);
txtview.setText("" + words.get(j));
txtview.getLayoutParams();
t3.setText(t3.getText() + " " + txtview.getText());
}
};
Set the onClickListener() for each TextView you instantiate inside your for loop.
How to get an array items like this
["String1", "string2", "string3",.....,"Stringn"]
but if we use Arrays.toString(array) it will display like [string1, string2, string3] but i want like above.
Thnaks in advance
This is how I did it:
private void btn3() {
LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
layout.removeAllViewsInLayout();
layout.setPadding(15, 15, 15, 15);
tv2 = new TextView[list.size()];
for (int i = 0; i < list.size(); i++) {
tv2[i] = (TextView) new TextView(Random.this);
}
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
for (int i = 0; i < list.size(); i++) {
tv2[i].setLayoutParams(lparams);
tv2[i].setText((i + 1) + " " + list.get(i));
layout.addView(tv2[i]);
}
}
I want this table to extend as possible to fill the remaining (empty) area of the linear layout that contains it
I generate the table using the following method
public void drawTable(LinearLayout tableLayout,String tableTitle, String[][] data,String[] headerTitles, int type){
//clearing previous views
tableLayout.setVisibility(View.VISIBLE);
tableLayout.setGravity(Gravity.CENTER);
int chids = tableLayout.getChildCount();
if (chids > 0){
tableLayout.removeAllViews();
}
if((headerTitles!= null) && (headerTitles.length != data[0].length) ){
return;
}
TableLayout table = new TableLayout(context);
// table.setStretchAllColumns(true);
// table.setShrinkAllColumns(true);
//Rendering the table title
TextView tableTitleView = new TextView(context);
tableTitleView.setText(tableTitle);
tableTitleView.setTextColor(Color.WHITE);
tableTitleView.setGravity(Gravity.CENTER);
tableTitleView.setTextSize(18);
tableTitleView.setTextAppearance(context, R.style.boldText);
tableTitleView.setBackgroundColor(Color.parseColor("#008888"));
tableTitleView.setPadding(2, 2, 2, 10);
tableLayout.addView(tableTitleView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
ScrollView scroll1 = new ScrollView(context);
HorizontalScrollView scroll2 = new HorizontalScrollView(context);
int rows = data.length;
int columns = data[0].length;
if(headerTitles!=null){
//Rendering the header
TableRow headerRow = new TableRow(context);
for (int i = 0; i < columns; i++) {
TextView t = new TextView(context);
t.setTextSize(17);
t.setTextAppearance(context, R.style.boldText);
t.setPadding(5, 5, 5, 5);
t.setText(headerTitles[i]);
t.setGravity(Gravity.CENTER);
t.setTextColor(Color.WHITE);
t.setBackgroundResource(R.drawable.text_border_header_1);
headerRow.addView(t, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(headerRow, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
//Rendering the Table Data
TableRow row;
for (int current = 0; current < rows ; current++) {
row = new TableRow(context);
TextView t;
for(int i =0 ; i < columns ; i++){
t = new TextView(context);
t.setTextSize(15);
t.setPadding(5,5,5,5);
t.setText(data[current][i]);
t.setGravity(Gravity.CENTER);
if (type == 1) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd);
} else {
t.setBackgroundResource(R.drawable.text_border_even);
}
}else if (type == 2) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_2);
} else {
t.setBackgroundResource(R.drawable.text_border_even_2);
}
}else if (type == 3) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_3);
} else {
t.setBackgroundResource(R.drawable.text_border_even_3);
}
}
t.setTextColor(Color.BLACK);
row.addView(t,TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
scroll2.addView(table,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.addView(scroll2,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.setPadding(2, 20, 2, 20);
tableLayout.addView(scroll1,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
}
I couldn't figure out exactly the problem with my code.
EDIT :
when I added the table directly to the linear layout instead of the scroll views the table fits with the linear layout width. It seems that my problem is with the scroll view
I found the answer Here (thanks to Felix)
Just a single line of code to be added to the scroll views
scrollView.setFillViewPort(true);
try this on your TableLayout object.
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.width = LayoutParams.FILL_PARENT;
table.setLAyoutParams(params);
Edit:
TextView tView = new TextView(this);
LinearLayout.LayoutParams params = tView.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.weight = 1;
tView.setLayoutParams(params);
regards,
Aqif
I have a scroolView containing a table with buttons, I'm trying to request focus to one of the buttons but with no success
here is the code (some lines were removed to avoid floating)
private void reDraw()
{
ll = new LinearLayout(this);
ll.setOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ll.setGravity(Gravity.FILL);
ll.setBackgroundResource(R.drawable.modern_background);
ll.setPadding(20, 20, 20, 20);
sv = new ScrollView(this);
sv.setPadding(0, 20, 0, 0);
tl = new TableLayout(this);
tl.setGravity(Gravity.FILL);
tl.setStretchAllColumns(true);
get_start_and_end_of_month(Start.globalDate);
day = 1;
int dayInWeek = startOfTheMonth.get(java.util.Calendar.DAY_OF_WEEK);
String temp;
tr = new TableRow(this);
tr.setGravity(Gravity.FILL_HORIZONTAL);
for(int k = 0; k < 7; k++)
{
b = new Button(this);
b.setGravity(Gravity.CENTER);
b.setBackgroundResource(R.drawable.widget_cell_day);
b.setText(Start.hebLayout ? daysHebrew[k] : daysEnglish[k]);
tr.addView(b);
}
tl.addView(tr);
for(int i = 0; i < 5; i++)
{
tr = new TableRow(this);
tr.setGravity(i == 0 ? Gravity.RIGHT : Gravity.LEFT);
for(int j = 1; j < 8; j++)
{
if(!(i == 0 && j < dayInWeek))
{
db = new DateButton(this, startOfTheMonth.getTime());
db.setGravity(Gravity.CENTER);
if(new HdateNew(Start.globalDate).get_hd_day() == day)
db.setFocusableInTouchMode(true);
db.setId(day);
tr.addView(db);
if(day == new HdateNew(endOfTheMonth.getTime()).get_hd_day())
break;
day++;
startOfTheMonth.add(java.util.Calendar.DATE, 1);
}
}
tl.addView(tr);
}
sv.addView(tl);
ll.addView(sv);
setContentView(ll);
db = (DateButton)findViewById(new HdateNew(Start.globalDate).get_hd_day());
db.setFocusable(true);
db.requestFocus();
}
Now as you can see I did set the button to be focusable but it ain't working for me
//try set content view # last
db = (DateButton)findViewById(new HdateNew(Start.globalDate).get_hd_day());
db.setFocusable(true);
db.requestFocus();
setContentView(ll);