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.
Related
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
I am trying to add items to array dynamically and then add these items in array of textviews but the layout doesn't show me any item. I need to know what is wrong with my code. Thanks in advance.
TextView[] tt;
LinearLayout testy;
List<String> x = new ArrayList<String>(99);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
testy=(LinearLayout) findViewById(R.id.testy);
tt=new TextView[100];
LinearLayout.LayoutParams dim=new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
x.add("hii");
for(int i=1;i<x.size();i++)
{
tt[i]=new TextView(this);
tt[i].setLayoutParams(dim);
x.add(i,"hello");
String data=x.get(i);
tt[i].setText(data);
testy.addView(tt[i]);
}
setContentView(testy);
}
You have only 1 item in your array list (which has an index of 0) but your loop starts at 1 which skips the whole for loop because 1<1 is false
R.layout.activity_test is a layout file that controls are how to display how to display the display interface of setContentView is to set a Activity, this sentence is the sentence and then set the Activity using main layout file under R.layout layout
remove this
setContentView(testy);
Edit
Try this .
TextView[] tt;
LinearLayout testy;
List<String> x = new ArrayList<String>(100);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
testy = (LinearLayout) findViewById(R.id.testy);
tt = new TextView[100];
LinearLayout.LayoutParams dim = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < x.size(); i++) {
tt[i] = new TextView(this);
tt[i].setLayoutParams(dim);
x.add(i, "hello");
String data = x.get(i);
tt[i].setText(data);
testy.addView(tt[i]);
}
}
Replace
for(int i=1;i<x.size();i++)
{
tt[i]=new TextView(this);
tt[i].setLayoutParams(dim);
x.add(i,"hello");
String data=x.get(i);
tt[i].setText(data);
testy.addView(tt[i]);
}
By
for(int i=0;i<tt.length;i++)
{
tt[i]=new TextView(this);
tt[i].setLayoutParams(dim);
x.add(i,"hello");
String data=x.get(i);
tt[i].setText(data);
testy.addView(tt[i]);
}
And
List<String> x = new ArrayList<String>(99);
By
List<String> x = new ArrayList<String>(100);
So I am creating headers and input fields programmatically in a loop. As it stands with the following code, all the elements appear bunched together on top of one another:
RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.checkFieldsLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
Bundle extras = getIntent().getExtras();
List<String> fields = extras.getStringArrayList("checkList");
for (int i = 0; i < fields.size(); i++) {
TextView header = new TextView(this); // Pass it an Activity or Context
header.setText(fields.get(i).toString());
header.setLayoutParams(params);
EditText field = new EditText(this);
field.setLayoutParams(params);
mRlayout.addView(header);
mRlayout.addView(field);
}
How do I programmatically lay out these elements so they match the width of the screen and dock one beneath the other? Thank you!
Try This:
RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.checkFieldsLayout);
Bundle extras = getIntent().getExtras();
List<String> fields = extras.getStringArrayList("checkList");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
TextView header1 = new TextView(this); // Pass it an Activity or Context
header.setText(fields.get(0).toString());
header.setLayoutParams(params);
header1.setId(0);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW,0);
EditText field1 = new EditText(this);
field.setLayoutParams(params2);
field1.setId(fields.size());
mRlayout.addView(header);
mRlayout.addView(field);
for (int i = 1; i < fields.size(); i++) {
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW,fields.size()+i -1);
TextView header = new TextView(this); // Pass it an Activity or Context
header.setText(fields.get(i).toString());
header.setId(i);
header.setLayoutParams(params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW, i);
EditText field = new EditText(this);
field.setLayoutParams(params2);
field.setId(fields.size()+i);
mRlayout.addView(header);
mRlayout.addView(field);
}
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 am dynamically creating the buttons. But the problem is they are getting created from top. I want to create it from bottom .
public class MainActivity extends Activity implements View.OnClickListener {
int i, j, butNum, lay1num = 1, lay2num = 100, lay3num = 100, store;
Button[] Button;
EditText numBut;
LinearLayout mainLayout;
LinearLayout[] subLayout;
LinearLayout.LayoutParams params;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
butNum = 5;
Button = new Button[butNum];
subLayout = new LinearLayout[3];
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.HORIZONTAL);
mainLayout.setWeightSum(90);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layoutGen();
for (j = 0; j < butNum; j++) {
int value = j + 1;
Button[j] = new Button(this);
Button[j].setText("" + value);
Button[j].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Button[j].setTextSize(20);
Button[j].setTag(value);
Button[j].setId(j);
subLayout[0].addView(Button[j]);
Button[j].setOnClickListener(this);
}
mainLayout.addView(subLayout[0], params);
setContentView(mainLayout);
}
Is there anyway I can create buttons which are aligned to the bottom.
subLayout.setGravity(Gravity.BOTTOM);
You nay need to set size of layout to match_parent.
Eventually You may set a layout_gravity like here