Add Views to LinearLayout programmatically - android

I have an Activity that displays comments. The comments themselves have a layout, so I can't just use a ListView.
I'm adding the comments with a loop, and the program goes through the whole loop (checked via LogCat), but only adds the first View (comment) to the linearlayout.
My code (in reality the fillComments parameter will be something else than String[]):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_layout);
String[] comments = {"kommentaar 1", "kommentaar 2", "kommentaar 3"};
mTitle = (TextView) findViewById(R.id.comments_title);
mTextArea = (EditText) findViewById(R.id.comment_editor);
mAddButton = (Button) findViewById(R.id.add_comment);
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
mTitle.setText(getIntent().getStringExtra("name"));
fillComments(comments);
}
private void fillComments(String[] comments) {
View comment;
TextView commentator;
TextView commentDate;
TextView commentText;
LayoutInflater inflater = getLayoutInflater();
for (String s : comments) {
Log.d("Comment adder", "Adding comment " + s);
comment = inflater.inflate(R.layout.comment_row_layout, null);
commentator = (TextView) comment.findViewById(R.id.commentator);
commentDate = (TextView) comment.findViewById(R.id.comment_date);
commentText = (TextView) comment.findViewById(R.id.comment_text);
commentator.setText("Test commentator");
commentDate.setText("12-12-2012");
commentText.setText(s);
mCommentArea.addView(comment);
}
}

i think
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
this layout orientation is Horizontal so this problems occurs. please if horizontal orientations then please change it to vertical and enjoy

How have you defined the LinearLayout? It could be just a display issue.
Check the size and orientation of LinearLayout.

Related

Optimize code - create custom text view from array and assign value

This code generate some textview and assign value to it using an array.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_list);
LinearLayout myLayout = (LinearLayout) findViewById(R.id.llContainer2);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
int textViewCount = 24;
TextView[] pairs=new TextView[textViewCount];
for(int i=0; i<textViewCount; i++)
{
pairs[i] = new TextView(this);
pairs[i].setLayoutParams(lp);
pairs[i].setId(i);
pairs[i].setText(getString(R.string.t1));
//--------------------
pairs[i].setClickable(true);
pairs[i].setOnClickListener(this);
pairs[i].setTextColor(Color.parseColor("#4682B4"));
pairs[i].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 21);
pairs[i].setPadding(8,8,8,8);
//------------------
myLayout.addView(pairs[i]);
}}
I found these codes inside //-- and //-- is repeatative.
I am finding way to optimise it.
My thinking
1. inflate textview layout inside the loop and using a customlayout.xml
2. set the layout once on the start, so it inherent the layout from parent
Tried
LayoutInflater layoutInflater =
(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View child = getLayoutInflater().inflate(R.layout.style_textview, null);
myLayout.addView(child);
//------------------------------
LinearLayout container = (LinearLayout)
findViewById(R.id.llContainer);
View inflatedLayout= getLayoutInflater().inflate(R.layout.style_textview, null);
container.addView(inflatedLayout);
Anyone can show me how to use the xml inside loop or inflate the parent.
Secondly, how to use
public void onClick(View v) {
switch (v.getId()){
case R.id.i:
//start activity
}}
I am open to better suggestion.
Inflate the view like this:
pairs[i] = (TextView) LayoutInflater.from(this).inflate(R.layout. style_textview, myLayout, false);
// Don't set Id by a code-defined value like this, use setTag for additional data or use View.generateViewId()
pairs[i].setId(i);
pairs[i].setText(getString(R.string.t1));
pairs[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Do stuff
}
}
myLayout.addView(pairs[i]);
LayoutParams and other properties are defined in xml

Differ between elements when XML reused by including?

I got a LinearLayout where I add the Layout of a seperate XML.
In that seperate XML I added a OnClickListener on that Layout.
Now when the Layout is clicked, a new View is added where the User has to type some information.
When ready the additional View is closed and the data has to be put in the right element.
But it does always put it in the first of them.
How can I tell the Listener in which one it has to put it?
Thanks in advance.
editElement is called if one of those multiple included elements is clicked.
The saveEdit when the finish button of the editor is clicked.
public void editElement(View view) {
FrameLayout base = (FrameLayout) findViewById(R.id.base);
LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
LinearLayout editor = (LinearLayout) inflater.inflate(R.layout.element_editor, null);
editorID = editor.getId();
base.addView(editor);
currEdit = view.getId();
TextView day = (TextView) view.findViewById(R.id.day);
TextView day_editor = (TextView) editor.findViewById(R.id.day_editor);
day_editor.setText(day.getText());
}
public void saveEdit(View view) {
FrameLayout base = (FrameLayout) findViewById(R.id.base);
GridLayout parent = (GridLayout) view.getParent();
LinearLayout element = (LinearLayout) findViewById(currEdit);
TextView dur_act = (TextView) element.findViewById(R.id.duration_activity);
EditText act = (EditText) parent.findViewById(R.id.de_activity);
EditText dur = (EditText) parent.findViewById(R.id.de_duration);
dur_act.setText(dur.getText()+(!dur.getText().equals("")?"":" min ")+act.getText());
TextView comment = (TextView) diaryelement.findViewById(R.id.comment);
EditText comm = (EditText) parent.findViewById(R.id.e_comment);
comment.setText(comm.getText());
base.removeView(findViewById(editorID));
}

How to edit view created programmatically

So in my application i have a linear layout, to which i'm adding programmatically some CardViews (android L cardview) like this :
//This is my LinearLayout
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
//Here i create my CardView from a prepared xml layout and inflate it to the LinearLayout
View card = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
//Now i change the 'text' value of the Card's text views
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title1");
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description1");
//...
//Now i do the same thing for another card
View card2 = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
TextView cardTitle2 = (TextView) card2.findViewById(R.id.text_card_title);
cardTitle2.setText("Title2");
TextView cardDecription2 = (TextView) card2.findViewById(R.id.text_card_description);
cardDecription2.setText("Description2");
//...
The two cards are displayed properly, but what happens is than the first card displayed has "Title2" and "Description2" written in the textViews, while the second card has the default values defined in the xml.
It seems to me that by calling card.findViewById() or card2.findViewById() i get always the TextView of the first card.
So my question is : how do i manage to differentiate the cards i create programmatically and then correclty access the view within them?
Try this way,hope this will help you to solve your problem.
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
for (int i=1;i<=2;i++){
View card = View.inflate(getApplicationContext(), R.layout.account_card, null);
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title"+i);
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description"+i);
card.setTag(i);
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Toast.makeText(context,pos,Toast.LENGTH_SHORT).show();
}
});
myLayout.addView(card);
}

Layoutinflator inflates mulitple buttons, how to make all others buttons invisible on click

I have the following issue, I inflate a layout which includes 2 buttons. The onClick method works fine and everything runs smoothly. However I would like to make both buttons that are inflated each time invisible once one of those two is being clicked. I know how to make the button invisbile that was clicked however I find no way of making the corresponding button invisible. Any help is greatly appreciated.
(If it is any concern, this is all done in a fragment)
for(i = 0; i < al.size(); i = i+6) {
TableLayout tl = (TableLayout)fragmentView.findViewById(R.id.myTableLayout);
LayoutInflater inflater1 = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater1.inflate(R.layout.element_request, null);
TextView t1 = (TextView) itemView.findViewById(R.id.tvdescription1);
t1.setText(al.get(i+2));
TextView t2 = (TextView) itemView.findViewById(R.id.tvdescription2);
t2.setText(al.get(i+3));
String id = al.get(i+1);
accept = (Button) itemView.findViewById(R.id.baccept);
accept.setTag(R.id.tvdescription1, id);
String id1 ="a"+id;
accept.setTag(R.id.tvdescription2, id1);
decline = (Button) itemView.findViewById(R.id.bdecline);
decline.setTag(R.id.tvdescription1, id);
String id2 = "b"+id;
decline.setTag(R.id.tvdescription2,id2);
tl.addView(itemView, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
accept.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
crid = v.getTag(R.id.tvdescription1);
crid2 = crid.toString();
....Code...
accept = (Button)v;
accept.setVisibility(View.GONE);
//----->set corresponding "decline" button also Invisible
}
}
}
You could try this:
ViewGroup row = (ViewGroup) v.getParent();
Button dec = (Button) row.getChildAt(3); //If decline is the 4th member in the view
dec.setVisibility(View.GONE);

Linearlayout display only last data in android

i want to integrate horizonal scrollview with custom linear layout inflater so i set below code
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mLinearLayout.removeAllViews();
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
}
in above my array list include 9 data but in screen it is display only one data so any idea how can i solve this ?
You should write this function like this
mLinearLayout.removeAllViews();
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
}
try this one...
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
child.setId(i+1)
child.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//your code here
int position=child.getId()-1;
Toast.makeText(HorizontalAcivity.this, mArrayListPersonDataLists.get(position).getName(), Toast.LENGTH_SHORT).show();
}
});
}
Remove a line with mLinearLayout.removeAllViews(); :)

Categories

Resources