I have created a Layout file for an activity. In this layout I have created a LinearLayout with a textview and an edittext.
Now I want to create additional LinearLayouts that will look and contain the exact same views that my original LinearLayout, but with different texts. I also want to do it programmatically during run because the amount of these LinearLayout will vary between runs.
I've read some about inflaters but I don't understand them enough to use them.
I'm thinking something like this, obviously the code is wrong, but hopefully you understand what I want to do:
LinearLayout llMain = (LinearLayout)findViewById(R.id.mainLayout);
LinearLayout llToCopy = (LinearLayout)findViewById(R.id.linearLayoutToCopy);
for(int player = 0; player < size; player++)
{
LinearLayout llCopy = llToCopy.clone();
TextView tv = (TextView)llCopy.getChildAt(0);
tv.setText(players.get(player).getName());
llMain.addView(llCopy);
}
There are several ways to accomplish this.
A quick and easy approach is to inflate a new layout in every iteration of the loop:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main, null);
for (int i = 0; i < 10; i++) {
View child = inflater.inflate(R.layout.child, null);
TextView tv = (TextView) child.findViewById(R.id.text);
tv.setText("Child No. " + i);
parent.addView(child);
}
setContentView(parent);
Another (more elegant) solution is to create a separate class extending LinearLayout:
public class ChildView extends LinearLayout {
private TextView tv;
public ChildView(Context context) {
super(context);
View.inflate(context, R.layout.child, this);
tv = (TextView) findViewById(R.id.text);
}
public void setText(String text) {
tv.setText(text);
}
}
Now you can create a ChildView in every iteration of your loop and set the text via the setText(String text) method:
for (int i = 0; i < 10; i++) {
ChildView child = new ChildView( this );
child.setText("Child No. " + i);
parent.addView(child);
}
You can achieve that by using layout inflater
get the layout inflatter by using this
LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout newlayout = inflater.inflate(R.layout.yourlayout, null);
// newlayout is the copy of your layout and you can use it and to get
// the textview and edittext do it like this
TextView text = (TextView) newlayout.findView(R.id.yourtextviewid);
text.setText("new text");
EditText et = (EditText) newlayout.findView(R.id.yourtextviewid);
Related
in the bleow posted code, i am trying to add a horizontal divider after each added view to the linearlayout as shown in the code.
the problem i have is, at run time the divider is not showing
would please let me know why the divider is not showing and how to make it appears?
code:
private void inflateView(String bez, String ges) {
LinearLayout linLay = (LinearLayout) findViewById(R.id.versicherungsListeActivity2mod_linLay_meineDocList_container);
//divider
View viewDivider = new View(this);
viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
viewDivider.setBackgroundColor(Color.parseColor("#000000"));
LayoutInflater inflator = this.getLayoutInflater();
View view = inflator.inflate(R.layout.versicherung_docs_row_model, null);//<<<<<< this is the view i want to add to the map as a key
ImageView imgViewLogo = (ImageView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_imgVie_logo);
TextView texVieBez = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docBezeichnung);
TextView texVieExtraItem = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_addMoreDocs);
TextView texVieGes = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docGesellschaft);
Button btnMore = (Button) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_btn_more);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc, this.getTheme()));
} else {
imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc));
}
texVieBez.setText(bez);
texVieGes.setText(bez);
btnMore.setVisibility(View.VISIBLE);
linLay.addView(view);
linLay.addView(viewDivider);
}
viewDivider has height WRAP_CONTENT and as the view is empty, its height is calculated to 0.
You have to set desired height of the divider.
int dividerHeight = getResources().getDisplayMetrics().density * 1; // 1dp to pixels
viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dividerHeight));
I have to add multiple view programatically to a HorizontalScrollView but i can not add the same inflated view twice. So i have to re-inflate my XML layout N times. An adapter solution is not an option.
There is a way to recycle this layout without re-inflating?
Thanks to all.
UPDATE: My code is something like this.
View view = getLayoutInflater().inflate(R.layout.my_view, null);
HorizontalScrollView h = (HorizontalScrollView)findViewById(R.id.scroll);
for(int i = 0; i < 25; i++)
{
// Process textview and images inside the view
h.addView(view);
}
I am not sure this what you are looking for we can inflate layout like this
LayoutInflater.from(context).inflate(R.layout.abc_action_bar_decor, parent, true);
HorizontalScrollView h = (HorizontalScrollView)findViewById(R.id.scroll);
LinearLayout parent= new LinearLayout(context);
parent.setOrientation(LinearLayout.HORIZONTAL);
h.addView(parent);
for(int i = 0; i < 25; i++)
{
View view = getLayoutInflater().inflate(R.layout.my_view, parent, true);
// Process textview and images inside the view
}
I am calling the function setLayoutData() for many places i want to clear the view whenever it is called.What is happening now the layout get appends .But i don't want to append how can we clear the view .Please help me this is my function
void setLayoutData() {
resultTrips = (LinearLayout) findViewById(R.id.trip_list);
LinearLayout.LayoutParams mainTripDetailsLayout = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0;i<4;i++) {
final LinearLayout tripdetailsdata = (LinearLayout) inflater
.inflate(R.layout.mytripinner, null);
tripdetailsdata.setId(i);
resultTrips.addView(tripdetailsdata);
i++;
}
TextView dummy = new TextView(this);
dummy.setLayoutParams(mainTripDetailsLayout);
resultTrips.addView(dummy);
}
If I understand you right, you want to clear your LinearLayout?
so put in your second line after findView... resultTrips.removeAllViews();
The better aproach would be only adding one TextView to your Linearlayout and edit this one.
You can use resultTips.removeAllViews(); to remove the resultTips layout or resultTips.removeView(dummy); to remove the TextView. Hope it helps.
I checked the solution here:
Adding multiple views of the same type
Its given that, create a new View everytime you add it instead of changing only 1 view.
But i am doing this:
for (int i = 0; i < 10; i++) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(
CommentsActivity.LAYOUT_INFLATER_SERVICE);
View cv = vi.inflate(R.layout.item, null);
TextView textView1 = (TextView) cv.findViewById(R.id.tv1);
textView1.setText("-" + i);
TextView textView2 = (TextView) cv.findViewById(R.id.tv2);
textView2.setText("--" + i);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.layout);
insertPoint.addView(cv, 0, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
so its like creating a new inflater and view for every i. But i am only getting the last item.
ie.., only 1 inflatedView with tv1 as -9 and tv2 as --9
seems like everytime i go into the for loop, the old view is being replaced by the new view. How to add all the 10 views??
ThankYou
usually I use this
private void renewDetail(){
llDetail.removeAllViews();
for (int i = 0; i < 10; i++) {
llDetail.addView(new ChildDetailNotePieDiagram(context, "Name", 1000, 10));
}
}
the logic is first I clear all view from the parent layout and then add view to the parent layout.
where llDetail is a linear layout and I create a linear layout class ChildDetailNotePieDiagram and add it to the linear layout so basically it's a different solution from what you use now
but I think you can try my solution if you want :)
feel free to ask any question about this in the comment
I have lists of LinearLayouts with horizontal orientation each one containing two textviews added dynamically.
This LinearLayout is finally wrapped into master LinearLayout.
I want the second textview of each linear layout to be right aligned progrmatically. How can I do this dynamically.
Here's sample code:
LinearLayout placeHolderLinearLayout = (LinearLayout)findViewById(R.id.listhosts);
//Several such layouts with 2 text views will be added to placeholder
LinearLayout l = new LinearLayout(this);
l.setClickable(true);
TextView h = new TextView(this);
h.setText("left");
h.setSingleLine(true);
TextView t = new TextView(this);
t.setText("right");
t.setSingleLine(true);
l.addview(h);
l.addview(t);
placeHolderLinearLayout.addView(l);
There is android:layout_alignParentRight attribute. But how to set this dynamically in this case. Any clue?
The android:layout_alignParentRight can only be applied to a view if its parent is a RelativeLayout. Change your container to that, and the 2 sub-views can use any of the layout_alignParent* attributes.
If you can't do this programatically (which I can't see how to do quickly), then you could always define your inner layout in xml (where you can easily get the layout correct) and inflate manually via:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View l = vi.inflate(R.layout.inner_relative_layout, null);
TextView leftTextView = (TextView) l.findViewById(R.id.left_text);
leftTextView.setText("left");
// ... fill in right text too
placeHolderLinearLayout.addView(l);
Edit: added layout definition
Use a layout like this, and inflate it in the code as above:
<RelativeLayout android:id="#+id/inner_relative_layout" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/left_text" android_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="#+id/right_text" android_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
You will be creating multiple of these layouts for each item you're adding to your list.
this one is woking and simple answer::
public class MainActivity extends Activity {
TextView text[];
TextView texto[];
CheckBox check[];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.layout);
text = new TextView[5];
texto = new TextView[5];
check = new CheckBox[5];
for (int i = 0; i < 5; i++) {
text[i] = new TextView(this);
text[i].setText("First one is::" + i);
texto[i] = new TextView(this);
texto[i].setText("sennd one ibs::" + i);
check[i] = new CheckBox(this);
((ViewGroup) view).addView(check[i]);
((ViewGroup) view).addView(text[i]);
((ViewGroup) view).addView(texto[i]);
}
}
}
Please try below
LinearLayout placeHolderLinearLayout = (LinearLayout)findViewById(R.id.listhosts);
//Several such layouts with 2 text views will be added to placeholder
LinearLayout l = new LinearLayout(this);
l.setClickable(true);
TextView h = new TextView(this);
txt1.setGravity(Gravity.LEFT);
h.setText("left");
h.setSingleLine(true);
TextView t = new TextView(this);
txt1.setGravity(Gravity.RIGHT);
t.setText("right");
t.setSingleLine(true);
l.addview(h);
l.addview(t);
placeHolderLinearLayout.addView(l);