Dynamically creates TextView boxes - android

I want to create multiple TextView boxes dynamically, If i enter 2 in the Edittext it wants to create 2 TextView, If i enter 5 in the EditText it wants to create 5 TextView and so on... can anyone help me...
Thanks in advance.,

first you need to get the parent layout.if it is ,say LinearLayout means,you can add the textview by using addView(view).
letterTextView = new TextView[length];
for (int Index = 0; Index < length; Index++)
{
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 25, 0);
letterTextView[Index] = new TextView(FriendsPanel.this);
letterTextView[Index].setTextSize(20);
letterTextView[Index].setTextColor(Color.WHITE);
letterTextView[Index].setText("TextView"+letterIndex);
linearLayout.addView(letterTextView[Index], layoutParams);
}

Related

Not able to add margins on buttons through code

Here is my code, I am trying to add few buttons in a row. Its working fine, But these buttons appear too close to each other, I want to apply horizontal margins to buttons. But not able to add. To achieve this I tried to keep button inside a Linearlayout and applied margins to it. But it somehow don't show any buttons, And when I comment out this line- ll.setLayoutParams(lp); Buttons can be seen again, But without any margin. Please let me know how can I make buttons at some distance from each other.
maintable = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
for (int z = 0; z < 3; z++) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(5, 5, 5, 5);
LinearLayout ll = new LinearLayout(this);
//ll.setLayoutParams(lp);
Button b = new Button(savedLists.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
params.setMargins(0, 20, 0, 20);
b.setBackgroundResource(R.drawable.circular);
b.setText(Integer.toString(c));
b.setPadding(10,10,10,10);
ll.addView(b,params);
tr.addView(ll);
c++;
}
maintable.addView(tr);
R.drawable.circular is just creating a simple circular button. Please let me know, if I should post that too.
You haven't set any margin for the button here. You are setting it's padding! Here is how you can set the margin for a button!
Button b = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
params.setMargins(top, left, bottom, right);
yourLinearLayout.addView(b,params);

get TextView and EditText from Programatically created

i create 1 LinearLayout inside LinearLayout, that contains 1 EditText and 1 TextView programmatically and i need to get text from EditText and TextView. And this is my code:
main.xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<LinearLayout
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
MainActivity.java:
linearLayout = (LinearLayout) view.findViewById(R.id.result);
.......
public void addItem(){
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
EditText quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
Help me, please.
I've edit my question
Actually, my case above happened when i clicked addItem(), my app will display new TextView and EditText, so if this process is finished, and move to the next Fragment to display all the text of TextViews and EditTexts like this using StringBuilder:
Items Qtt
Cola......1
Chicken...1
Meat......2
Help me.
Do you want to get like this?
String str = item.getText().toString();
String str2 = quantity.getText().toString();
As stated above, using item.getText().toString(); is all you need, nonetheless I do presume your problem lies in the fact that you are declaring your Views inside the loop, by doing it locally later anywhere on the code you won't be getting any text at all.
Try declaring your Views as global variables or doing it outside of the loop such as;
//....
TextView item;
EditText quantity;
for(int i = 0; i < cartCount; i++) {
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
On the other hand, by looking at your code, are you trying to create a list of EditText and TextViews? If so, it would be wise to consider using a ListView with custom items; Here you can find a nice tutorial doing something simmilar.
This feels like you should be looking at an adapter view such as a recyclerview. You might start to see some performance issues using a linearlayout.
Anyway. You could add the views to an list as you add them to the linearlayout.
List<TextView> items = new LinkedList<>();
List<EditText> quantities = new LinkedList<>();
for(int i = 0; i < cartCount; i++) {
...
childLayout.addView(item);
childLayout.addView(quantity);
items.add(item);
quantities.add(quantity);
linearLayout.addView(childLayout);
}
then loop through your views
for (TextView textView : items) {
}
for (EditText editText : quantities) {
}
item.getText().toString()
quantity.getText().toString()

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

I want to add buttons to relative layout dynamically as per button and screen width as shown in image. Number of buttons is not fixed and width of button depends on the text which is set to the button.
I tried to achieve this using below code but that to not working fine as per my requirement.
Can anyone please help me ? Please help me through this.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.genre_layout);
for(int i=0; i < genreList.size(); i++){
Button btn = new Button(MovieDetailsActivity.this);
btn.setText(genreList.get(i).getGenreName());
btn.setPadding(15, 5, 15, 5);
btn.setBackgroundColor(Color.parseColor("#FFFFFF"));
btn.setBackgroundColor(Color.parseColor("#80333333"));
LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
param.setMargins(5, 5, 5, 5);
if (i != 0){
int prevId = genreList.get(i).getGenreId();
param.addRule(RelativeLayout.RIGHT_OF, prevId);
}
btn.setLayoutParams(param);
btn.setId(genreList.get(i).getGenreId());
layout.addView(btn);
}
Try this
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
btn.setWidth(width);
or if you have two buttons, do
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
btn1.setWidth(width/2);
btn2.seTwidth(width/2);
Update
LinearLayout.LayoutParams paramz = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
0dp, 1.0f);
then btn.setLayoutParams(paramz);
I have the code to add buttons dynamically to linear layout. Just check out whether it helps you https://docs.google.com/document/d/1QA1tAIe601fZT2Dlp1A0qDuLD0U4IqKL3f_Crx1rtkE/edit?usp=sharing
Try this for adding dynamic buttons in relativelayout
RelativeLayout layout = (RelativeLayout)findViewById(R.id.slidingDrawerContent);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
for (int i=0; i<4; i++) {
Button btn = new Button(this);
btn.setId(i);
btn.setText("some_text");
// lp.addRule(RelativeLayout.RIGHT_OF, <Id>);
layout.addView(tv2, lp);
}
I would like to suggest use LinearLayout as you asked in your question and give a Weight.
LinearLayout
I Solved my problem by using FlowLayout.
Sample code and references are available here
Its simple to use. Thank you all for help.

ImageButton is not visible when adding to a tablerow dynamically

I am creating a form where the user enters his/her address and when the user saves the form, the flow goes to another activity where the users' name is shown one below the other. I am using TableLayout and in each TableRow I have a TextView which shows the name and an ImageButton beside the textview which can be used to delete the entry. Now when I run the app I can only see the TextView in the layout, the ImageButton is not visible.Can someone please help me where I am going wrong?
Here is part of my code:
if (requestCode == REC_INFO && resultCode == RESULT_OK && data != null) {
RecipientArray = (ArrayList<Person>) data
.getSerializableExtra("RecArray");
TableLayout tbl = new TableLayout(this);
TextView[] tv = new TextView[RecipientArray.size()];
ImageButton delete_btns[] = new ImageButton[RecipientArray.size()];
TableRow tr[] = new TableRow[RecipientArray.size()];
for (int i = 0; i < RecipientArray.size(); i++) {
tv[i] = new TextView(this);
tv[i].setBackgroundResource(R.drawable.fill_rece);
Person p = RecipientArray.get(i);
tv[i].setText(p.getName());
tv[i].setTextColor(Color.WHITE);
delete_btns[i] = new ImageButton(this);
delete_btns[i]
.setImageResource(R.drawable.ipad_postcare_landscape_from);
delete_btns[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tr[i] = new TableRow(this);
tr[i].addView(tv[i]);
tr[i].addView(delete_btns[i]);
tbl.addView(tr[i]);
}
recs_layout.addView(tbl);//I add the TableLayout to a RelativeLayout
}
delete_btns[i] = new ImageButton(this);
delete_btns[i].setImageResource(R.drawable.ipad_postcare_landscape_from);
delete_btns[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Drawable d = delete_btns[i].getDrawable();
d.setBounds(0, 0, delete_btns[i].getWidth(), delete_btns[i].getHeight());
I think your textview is taking all the space in the table row try setting layout params for that as well
For ex:
tv[i].setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2f));
And similarly for image button
delete_btns[i].setLayoutParams(new TableRow.LayoutParams(
0, LayoutParams.WRAP_CONTENT,1f));
And to table row
tr[i].setWeightSum(3f)
Here the third argument of float type is weight of the view as it is set to 1f for both your image view and textview both will occupy equal space you can change it and set it what you need.
While using weight set width to 0dp for both textview and imageview

setting an array of text into an array of textview

i faced this problem when trying to assign a character into an array of text view
counter is a count which i got from reading the number of characters i have in a text file
TextView[] tv = new TextView[counter];
for (int i = 0; i < counter; i++)
{
tv[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv[i].setText(singleText[i]);
setContentView(tv[i]);
}
after this, when i try to run the application i the application just force closes.. i have no idea on how to debug it
my application would need to set 1 character into 1 text view
You haven't initialized the TextViews properly. That's why you are getting nullpointerexception. You have to initialize the TextView as follows:
tv[i] = new TextView(this);
Here this is the your Activity instance.
And there is a problem in your
setContentView(tv[i]);
If you use this code then in the screen you will see only the last textview.
To see all the TextView you have to add all the TextView in a container like LinearLayout. Then you have set the container as content View.
Here is the code you can use:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TextView[] tv = new TextView[counter];
for (int i = 0; i < counter; i++)
{
tv[i] = new TextView(this);
tv[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv[i].setText(singleText[i]);
linearLayout.addView(tv[i]);
}
setContentView(linearLayout);
Hope it will help you.
TextView[] tv = new TextView[counter];
Here you creating array filled with null references. Of course it will crash here tv[i].setLayoutParams() with null pointer exception

Categories

Resources