LinearLayout.removeAllViews doesn't work with programmatically created elements - android

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();

Related

Android views one by one

I want to add ImageViews to a layout from an ArrayList, but when there are too many views,
it starts to show some delay because it first loads every image and then applies them to the view. I need to know if there is a way to add them one by one to avoid delay.
I'm using this code.
ArrayList<String> pictures;
ImageView iv;
LinearLayout container;
for (int i = 0; i < pictures.size(); i++) {
LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
iv = new ImageView(getActivity());
iv.setLayoutParams(params);
iv.setImageBitmap(pictures.get(i));
container.addView(iv);
}

Android: Dynamically add views to ScrollView

I have a ScrollView and I want to insert a user specified number of HorizontalScrollViews. So what user says he wants to have a matrix of 5x5 elements, I want to insert 5 HorizontalScrollViews with 5 EditText objects each. My program adds the first line just as it's supposed to, but the rest not.
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(par2);
HorizontalScrollView row = new HorizontalScrollView(this);
row.setLayoutParams(par1);
row.addView(ll);
for (int j = 0; j < number; j++) {
EditText txt = new EditText(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setHint(i+","+j);
ll.addView(txt);
}
latout_in_scrollview.addView(row);
}
Any ideas why? Thanks!
EDIT:
The 1:1 code im using
LinearLayout dijkstra_rows;
FrameLayout.LayoutParams par1 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams par2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_dijkstra);
dijkstra_rows = (LinearLayout) findViewById(R.id.dijkstra_rows);
Bundle extras = getIntent().getExtras();
number = extras.getInt("vertexes");
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(par2);
HorizontalScrollView row = new HorizontalScrollView(this);
row.setLayoutParams(par1);
row.addView(ll);
for (int j = 0; j < number; j++) {
EditText txt = new EditText(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setHint(i+","+j);
ll.addView(txt);
}
dijkstra_rows.addView(row);
}
}
ScrollView can contain only one childView. You can put any layout as per your requirement. I generally use Relative Layout...
Then add views dynamically to relative layout
viewLayout = (ViewGroup) mView.findViewById(R.id.YOUR_RELATIVE_LAYOUT_ID);
View lastCard = viewLayout.getChildAt(viewLayout.getChildCount() - 1);
// INFLATE YOUR NEW VIEW YOU WANT TO ADD
CardView cardView = (CardView)
LayoutInflater.from(getContext()).inflate(R.layout.card_nearest_stop, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Set id to view
int id = 125;
if (lastCard != null) {
params.addRule(RelativeLayout.BELOW, lastCard.getId());
id = lastCard.getId() + 125;
}
cardView.setLayoutParams(params);
cardView.setId(id);
viewLayout.addView(cardView);
ScrollView is a single element container.
A ScrollView is a FrameLayout, meaning you should place one child in
it containing the entire contents to scroll; this child may itself be
a layout manager with a complex hierarchy of objects. A child that is
often used is a LinearLayout in a vertical orientation, presenting a
vertical array of top-level items that the user can scroll through.
You are adding multiple LinearLayouts here
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
.
.
}
You should have only one out of this loop. Then add this one to your scrollView, in Loop you can add muliple HorizontolScrollViews to this LinearLayout

How to Create multiple buttons in android

I tried the below code to create multiple buttons through programatically but it creates a single button as per my application i need to create a button based on input. For example if the input is 3 means i need to create three buttons in a layout. For your reference i attached the sample image and my code.
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
if in your example, your global container (main_layer) is a relativelayout or a frame layout, you have placed them on top of eachother. So you can't see the one on the back order.
Try this pls,
LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer);
for (int i = 0; i < 10; i++)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
You are creating multiple new linear layouts within the for loop. Linear layout need to be taken out of the for loop and just try adding the buttons in it.
1st run of for loop creates a new linear layout and adds a button
2nd run of for loop creates a new linear layout and adds a button on the top of previous added layout
Here you are creating a new LinearLayout per button... Try something more like...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
}
main_layer.addView(layout);
Try this
LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);
llParent.removeAllViews();
for (int i = 0; i < array_of_btn_input.size(); i++) {
BSView b = new BSView(this, new SimpleThumbBean(i + 1, bsList
.get(i).getLabel(), bsList.get(i).getThumbUrl()));
LinearLayout.LayoutParams llDynamic = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
llDynamic.weight = 1f;
llParent.addView(b, llDynamic);
}
llParent is a defined Layout and make sure you call removeAllViews() before adding layouts dynamically into it.
BSView is my own custom View. You can set BSView into ordinary Button as you need it.

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

Set a layoutmargin on a dynamically created checkbox

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);
}

Categories

Resources