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
Related
Here the code sample:
TableLayout ll = (TableLayout)findViewById(R.id.dyn_lyr);
also same with LinearLayout
//LinearLayout ll = (LinearLayout)findViewById(R.id.dyn_lyr);
TextView tv1 = (TextView) findViewById(R.id.testEditText);
tv1.setText("SomeTextGoesHere");
for(int i=1 ; i<= 5 ; i++){
ll.addView(tv1);
}
the LinearLayout/Table is inside ScrollView!
You should create TextView Dynamically and add in ll...
for(int i=1 ; i<= 5 ; i++){
TextView tv1 = new TextView(getApplicationContext());
tv1.setText("SomeTextGoesHere");
ll.addView(tv1);
}
There is not enought info but I guess that Tv1 already has a parent . I guess that you should add new TextView every time . Try this one. Ok then create brand new TextView
for(int i=1 ; i<= 5 ; i++){
TextView tv1 = new TextView(this);
tv1.setText("SomeTextGoesHere");
ll.addView(tv1);
}
If you are using findViewById, the resulting view is already a part of your layout and has a parent. Each view can only be added to a ViewGroup once. You need to be making new TextViews and adding those to your LinearLayout. You can do this either via the TextView constructor or a LayoutInflater with a separate xml layout.
I'm trying to programmatically create UI elements in Android. It is working, but when I press back button and after that return to the activity that creates elements, I want to delete previous created ones. I tried the following code in my onCreate method:
((LinearLayout) findViewById(R.id.mainContainer)).removeAllViews();
((LinearLayout) findViewById(R.id.mainContainer)).addView(generateElementsDynamically());
My generateElementsDynamically() method:
public static LinearLayout generateElementsDynamically()
{
LinearLayout main = new LinearLayout(context);
main.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
main.setLayoutParams(layoutParams1);
for (int i = 0; i < ServicesStorage.listServiceDetails.size(); i++)
{
LinearLayout lin = new LinearLayout(context);
for (int j = 0; j < ServicesStorage.listServiceDetails.get(i).size(); j++)
{
lin.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lin.setLayoutParams(layoutParams);
TextView txt = new TextView(context);
txt.setText(ServicesStorage.listServiceDetails.get(i).get(j).toString());
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lin.addView(txt);
}
main.addView(lin);
}
return main;
}
Any idea what is wrong?
Thanks
I've found solution, though I'm not sure if it is the best.
For some reason i had to clear ServicesStorage.listServiceDetails list and then populate it again...and it's working
I think you are trying to refresh UI with new datas. I would suggest you to change your design. You can use ListView and fill it your new datas. Anytime they changed you can call
listview.notifySetDataChanged();
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
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);
}
I'm writing a task manager app that downloads a list of tasks and subtasks from a server and creates a new checkbox for each item and adds it to a linear layout (called ll below). The problem I'm having is that I cannot set the "layout margin left" using java like I can with XML (this is for the subtasks to indent them a bit on the screen). I can set most other xml properties, but cb.setMargins() doesn't work (says undefined for type checkbox). Setting the padding of course does not achieve the desired result.
for(int i=0;i<tasks.size();i++) {
CheckBox cb = new CheckBox(this);
cb.setText(tasks.get(i).subtask_desc);
cb.setButtonDrawable(R.drawable.checkbox_xml);
ll.addView(cb);
}
Any ideas or how I would work through this?
I think you should add the checkbox to the LinearLayout using the correct LayoutParams:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = 123;
li.addView(cb, params);
Hope that helps!
Hi Try this...
private LinearLayyout ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = 123;;
CheckBox cb;
for(int i = 0; i < 10; i++) {
cb = new CheckBox(this);
cb.setText(categoryListArray[i]);
ll.addView(cb, ll);
}