I hope someone can help me here...
I want to add rows to a table dynamically with a for Loop. This is just for example. But when I test it, the Screen where the table should be showed is empty. I don't know where the Problem is, it just Shows nothing.
Here is my Code:
private void setElementsList() {
TableLayout tableLayout = findViewById(R.id.tablelayout);
for (int i=0; i<5; i++) {
// Creation row
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 70));
tableRow.setPadding(0,0,0,8);
LinearLayout Newrowlayout =new LinearLayout(this);
Newrowlayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,5));
Newrowlayout.setOrientation(LinearLayout.HORIZONTAL);
Newrowlayout.setBackgroundColor(16746559);
// Creation textView
TextView text2 = new TextView(this);
text2.setText("Order ID: ");
text2.setTextSize(21);
text2.setTextColor(16508374);
text2.setGravity(Gravity.CENTER);
text2.setPadding(0,0,2,0);
text2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
// Creation textView
TextView text = new TextView(this);
text.setText(Integer.toString(i));
text.setTextSize(21);
text.setTextColor(16508374);
text.setGravity(Gravity.CENTER);
text.setPadding(2,0,10,0);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
// Creation textView
TextView text3 = new TextView(this);
text3.setGravity(Gravity.CENTER);
text3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT, 3));
// Creation button
Button button = new Button(this);
button.setText(">");
button.setTextSize(30);
button.setTextColor(16508374);
button.setBackgroundColor(16746559);
button.setLayoutParams(new TableRow.LayoutParams(70,70));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
// Creation textView
TextView text4 = new TextView(this);
text4.setGravity(Gravity.CENTER);
text4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT, 3));
// Creation textView
TextView text5 = new TextView(this);
text5.setGravity(Gravity.CENTER);
text5.setBackgroundColor(16741149);
text5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT, 1));
Newrowlayout.addView(text2);
Newrowlayout.addView(text);
tableRow.addView(Newrowlayout);
tableRow.addView(text3);
tableRow.addView(button);
tableRow.addView(text4);
tableRow.addView(text5);
tableLayout.addView(tableRow);
}
}
If you Need further Details plz ask.
Best regards
domidoof
Related
I want to create two tables which have different headers in first row. both tables have 4 columns in header row. How can i create two table with different header values without writing this same code twice?
public void addHeaders(TableLayout t) {
/* Create a TableRow dynamically */
TableRow tr = new TableRow(this);
tr.setBackgroundColor(GRAY);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
/* Creating a TextView to add to the row */
TextView addressTV = new TextView(this);
addressTV.setText("Address");
addressTV.setTextColor(Color.BLACK);
addressTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
addressTV.setGravity(Gravity.CENTER_HORIZONTAL);
addressTV.setPadding(0, 7, 0, 7);
addressTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.7f));
tr.addView(addressTV); // Adding textView to tablerow.
/* Creating a TextView to add to the row */
TextView typeTV = new TextView(this);
typeTV.setText("Type");
typeTV.setTextColor(Color.BLACK);
typeTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
typeTV.setGravity(Gravity.CENTER_HORIZONTAL);
typeTV.setPadding(0, 7, 0, 7);
typeTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
tr.addView(typeTV); // Adding textView to tablerow.
/* Creating another textview */
TextView amountTV = new TextView(this);
amountTV.setText("Amount");
amountTV.setTextColor(Color.BLACK);
amountTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
amountTV.setGravity(Gravity.CENTER_HORIZONTAL);
amountTV.setPadding(0, 7, 0, 7);
amountTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.2f));
tr.addView(amountTV); // Adding textView to tablerow.
/* Creating another textview */
TextView dateTV = new TextView(this);
dateTV.setText("Date");
dateTV.setTextColor(Color.BLACK);
dateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
dateTV.setGravity(Gravity.CENTER_HORIZONTAL);
dateTV.setPadding(0, 7, 0, 7);
dateTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.5f));
tr.addView(dateTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
Try passing the header names as an input. Then you can reuse the code to create your table objects while dynamically adjusting the header text fields.
e.g. Here is simple example, assuming always 4 columns...
public void callingFunction() {
TableLayout tl1 = // something;
TableLayout tl2 = // something else;
addHeaders(tl1, "Address", "Type", "Amount", "Date");
addHeaders(tl2, "new", "strings", "for", "table");
}
public TableRow addHeaders(TableLayout tl, String ... names) {
if (names.length != 4) throw new Exception("Unexpected number of table header names");
/* Create a TableRow dynamically */
TableRow tr = new TableRow(this);
tr.setBackgroundColor(GRAY);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
/* Creating a TextView to add to the row */
TextView addressTV = new TextView(this);
addressTV.setText(names[0]);
addressTV.setTextColor(Color.BLACK);
addressTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
addressTV.setGravity(Gravity.CENTER_HORIZONTAL);
addressTV.setPadding(0, 7, 0, 7);
addressTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.7f));
tr.addView(addressTV); // Adding textView to tablerow.
/* Creating a TextView to add to the row */
TextView typeTV = new TextView(this);
typeTV.setText(names[1]);
typeTV.setTextColor(Color.BLACK);
typeTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
typeTV.setGravity(Gravity.CENTER_HORIZONTAL);
typeTV.setPadding(0, 7, 0, 7);
typeTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
tr.addView(typeTV); // Adding textView to tablerow.
/* Creating another textview */
TextView amountTV = new TextView(this);
amountTV.setText(names[2]);
amountTV.setTextColor(Color.BLACK);
amountTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
amountTV.setGravity(Gravity.CENTER_HORIZONTAL);
amountTV.setPadding(0, 7, 0, 7);
amountTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.2f));
tr.addView(amountTV); // Adding textView to tablerow.
/* Creating another textview */
TextView dateTV = new TextView(this);
dateTV.setText(names[3]);
dateTV.setTextColor(Color.BLACK);
dateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
dateTV.setGravity(Gravity.CENTER_HORIZONTAL);
dateTV.setPadding(0, 7, 0, 7);
dateTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.5f));
tr.addView(dateTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
In my android application a love compatibility test is doing.. where first the details of boys and then details of girls are entered in the same page..one after another. and separate save button for saving boy details and girl details and these data is saved in separate databases. and there is a recall button also there for both boys and girls. When this recall button is pressed the database will opened and from that user can select the name to be checked. When he press the corresponding data has to be entered into the corresponding edittexts. In my case I don't know how to close this tablerow. that mean the database opened.then only the data will go to the corresponding edittext box.
I am giving my code below
MainActivity
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c = dbb.rawQuery("SELECT * from Data3", null);
int count = c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView1, textView2,textView3, textView4, textView5, textView6, textView7, textView8 ;
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText("First Name");
textView1.setTextColor(Color.BLACK);
textView1.setTypeface(null, Typeface.BOLD);
textView1.setPadding(20, 20, 20, 20);
tableRow.addView(textView1);
textView2 = new TextView(getApplicationContext());
textView2.setText("Last Name");
textView2.setTextColor(Color.RED);
textView2.setTypeface(null, Typeface.BOLD);
textView2.setPadding(10, 10, 10, 10);
tableRow.addView(textView2);
tableLayout.addView(tableRow);
for(Integer j=0; j<count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView3= new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex("ffirstname")));
textView3.setClickable(true);
ett1=textView3.getText().toString();
textView4 = new TextView(getApplicationContext());
textView4.setText(c.getString(c.getColumnIndex("fmiddlename")));
textView4.setClickable(true);
ett2=textView4.getText().toString();
textView5 = new TextView(getApplicationContext());
textView5.setText(c.getString(c.getColumnIndex("flastname")));
textView5.setClickable(true);
ett3=textView5.getText().toString();
textView6 = new TextView(getApplicationContext());
textView6.setText(c.getString(c.getColumnIndex("fdayofbirth")));
textView6.setClickable(true);
ett4=textView6.getText().toString();
textView7 = new TextView(getApplicationContext());
textView7.setText(c.getString(c.getColumnIndex("fmonthofbirth")));
textView7.setClickable(true);
ett5=textView7.getText().toString();
textView8 = new TextView(getApplicationContext());
textView8.setText(c.getString(c.getColumnIndex("fyearofbirth")));
textView8.setClickable(true);
ett6=textView8.getText().toString();
textView3.setPadding(10, 10, 10,10);
textView5.setPadding(10, 10, 10, 10);
tableRow.addView(textView3);
tableRow.addView(textView5);
tableLayout.addView(tableRow);
c.moveToNext();
final List<String> list = new ArrayList<String>();
list.add(textView3.getText().toString());
list.add(textView4.getText().toString());
list.add(textView5.getText().toString());
list.add(textView6.getText().toString());
list.add(textView7.getText().toString());
list.add(textView8.getText().toString());
textView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
c.close();
setContentView(tableLayout);
dbb.close();
}
});
Declare the table layout out side button click and to make it GONE/INVISIBLE do Like this
tablelayout.setVisibility(View.INVISIBLE);//white space will be available
Or
tablelayout.setVisibility(View.GONE);//No white space
First of all, the app looks like this.
How to make - if I click on Add, on its column the textview is updated with value + 1?
I'm creating TextView and Button in a Loop;
for(int i = 0;i < playercountint; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,(100/playercountint)));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tt = new TextView(this);
tt.setText("0");
tt.setTextSize(14);
tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tt.setGravity(Gravity.CENTER_HORIZONTAL);
Button btadd = new Button(this);
btadd.setText("Add");
btadd.setId(2);
btadd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
????? what here?
}
});
}
What should be written in onClick function , so only TextView value which is in buttons column is changed.
There is one outer LinearLayout which is horizontally oriented, and those which are created within a loop is vertical Linear Layouts.
Maybe i should create a template for 1 loop iteration?
First make your textview final and them set it's value in onClick
for(int i = 0;i < playercountint; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,(100/playercountint)));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
final TextView tt = new TextView(this);
tt.setText("0");
tt.setTextSize(14);
tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tt.setGravity(Gravity.CENTER_HORIZONTAL);
Button btadd = new Button(this);
btadd.setText("Add");
btadd.setId(2);
btadd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tt.setText(String.valueOf(Integer.parseInt(tt.getText())+1));
}
});
}
You can write one function that returns linearlayout:
public View getPlayerView(){
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,(100/playercountint)));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
final TextView tt = new TextView(this);
tt.setText("0");
tt.setTextSize(14);
tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tt.setGravity(Gravity.CENTER_HORIZONTAL);
Button btadd = new Button(this);
btadd.setText("Add");
btadd.setId(2);
btadd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tt.setText(String.valueOf(Integer.parseInt(tt.getText())+1));
}
});
layout.add(tt);
layout.add(btadd);
return layout;
}
Call this function in a loop.
tt.setText("something");
tt.invalidate();
my bad I didn't even notice it was a for loop uh you need to hold a reference to the text view.. make your textview final
tt.setText(Integer.parse(tt.getText().toString()) + 1);
But, I strongly recommend you design UI (layout) in the *.xml file!
See more: http://developer.android.com/guide/topics/ui/declaring-layout.html
You should provide your TextView with an unique id (.setId(...)). Having this, in your onClick method simply use something like this:
final TextView tv = (TextView) findViewById(R.id.your_textview_id);
final String cur_text = tv.getText();
Integer plus_one = Integer.valueOf(cur_text);
plus_one++;
tv.setText(plus_one.toString());
Didn't try it but should work.
static int counter = 0;
btn_Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter ++;
tv_Display.setText("Your Total is : " + counter);
}
});
I have the following code:
HashMap<String, String> tip = venue.tips.get(j);
TableRow tr = new TableRow(this);
TableRow.LayoutParams tableRowParams =
new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(tableRowParams);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView tips = new TextView(this);
tips.setText(tip.get("text"));
Log.v("TIPS TEXT", tip.get("text"));
tips.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ImageView avatar = new ImageView(this);
avatar.setImageBitmap(getBitmapFromURL(tip.get("photo")));
Log.v("PHOTO URL", tip.get("photo"));
avatar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ll.addView(tips);
ll.addView(avatar);
tr.addView(ll);
View v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
tipsTable.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tipsTable.addView(v);
I think I did something wrong with setting up the layout, but the issue now is that I can't see the rows added to the tipsTable. Why is this?
You're not calling setContentView() anywhere. You create this layout but never assign it to the activity. I think what you want to do is after you've set up all your layout stuff:
setContentView(ll);
along the same line: tipsTable seems to be your top view so try setContentView(tipsTable)
String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
TextView tv=new TextView(getApplicationContext());
tv.setText(textArray[i]);
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv, relativeParams);
}
I need to do something like that.. so it would display as
one
two
asdfasdfsomething
on the screen..
If it's not important to use a RelativeLayout, you could use a LinearLayout, and do this:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Doing this allows you to avoid the addRule method you've tried. You can simply use addView() to add new TextViews.
Complete code:
String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i++ )
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
Try this code:
final String[] str = {"one","two","three","asdfgf"};
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TextView[] tv = new TextView[10];
for (int i=0; i<str.length; i++)
{
tv[i] = new TextView(this);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin = 50;
params.topMargin = i*50;
tv[i].setText(str[i]);
tv[i].setTextSize((float) 20);
tv[i].setPadding(20, 50, 20, 50);
tv[i].setLayoutParams(params);
rl.addView(tv[i]);
}
public View recentView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a relative layout and add a button
relativeLayout = new RelativeLayout(this);
btn = new Button(this);
btn.setId((int)System.currentTimeMillis());
recentView = btn;
btn.setText("Click me");
relativeLayout.addView(btn);
setContentView(relativeLayout);
btn.setOnClickListener(new View.OnClickListener() {
#Overr ide
public void onClick(View view) {
//Create a textView, set a random ID and position it below the most recently added view
textView = new TextView(ActivityName.this);
textView.setId((int)System.currentTimeMillis());
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, recentView.getId());
textView.setText("Time: "+System.currentTimeMillis());
relativeLayout.addView(textView, layoutParams);
recentView = textView;
}
});
}
This can be modified to display each element of a String array in different TextViews.
You're not assigning any id to the text view, but you're using tv.getId() to pass it to the addRule method as a parameter. Try to set a unique id via tv.setId(int).
You could also use the LinearLayout with vertical orientation, that might be easier actually. I prefer LinearLayout over RelativeLayouts if not necessary otherwise.