My problem is creating activity to enter an array manually in Android. So I tried to create multiple EditText by these code lines:
public void EnterArray(int n)
{
for(int i=0; i<n; i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
EditText txt = new EditText(this);
txt.setId(i);
layout.addView(txt, params);
}
}
The remain problem is how can I get the text from them by using the Save Button I create in the layout. Is there any resolve? I hope there's someone could help with it.
Thank you in advance ^^
You can try something like this.
int count =layout.getChildCount();
for(int i=0;i<count;i++)
{
EditText text=(EditText)layout.getChildAt(i);
String value= text.getText().toString()
}
Related
I want to create two TextView dynamically and show them in a linearLayout called activity_ranking. I try this and the LogCat says that the problem is in the line: "ranking.addView(fecha);" Do you know what it happens? Thanks!
public void listarPuntuaciones(){
LinearLayout ranking = (LinearLayout)findViewById(R.layout.activity_ranking);
for(int i=0; i<puntuaciones.size(); i++){
String aux[]=this.puntuaciones.elementAt(i).split(";");
TextView fecha = new TextView(this);
Log.i(aux[0],"aux0");
fecha.setText(aux[0]);
ranking.addView(fecha);
TextView puntos = new TextView(this);
Log.i(aux[1],"aux1");
puntos.setText(aux[1]);
ranking.addView(puntos);
}
}
I guess ranking is null, because when you tried to find it byId, you should have given R.id.layout_id as a parameter.
Look here
My android app requires me to create some LinearLayouts according to data i got through the run time, so i don't know it's number and I have to put it into a for loop to create it, and as a result the name assigned to the layouts or the elements inside it will be overridden with each iterate through the for loop, and that's my code:
List<LinearLayout> inner_ver = new ArrayList<LinearLayout>();
for(int i = 0 ; i < size_from_run_time ; i++){
LinearLayout temp_inner_ver = new LinearLayout(this);
temp_inner_ver.setLayoutParams(temp_lay);
temp_inner_ver.setOrientation(LinearLayout.VERTICAL);
temp_inner_ver.setWeightSum(2);
temp_inner_ver.setPadding(7, 7, 7, 7);
inner_ver.add(temp_inner_ver);
}
for(int j = 0 ; j < inner_ver.size() ; j++){
LinearLayout icon1 = new LinearLayout(this);
inner_ver.get(j).addView(icon1);
icon1.setLayoutParams(lp_icon);
icon1.setBackgroundResource(R.drawable.ac_overlay);
icon1.setOrientation(LinearLayout.HORIZONTAL);
icon1.setTag(NORMAL);
// icon1
TextView text1 = new TextView(this);
icon1.addView(text1);
text1.setLayoutParams(text_name);
text1.setText("something");
text1.setTextSize(12);
text1.setTextColor(Color.WHITE);
ImageButton rgp1 = new ImageButton(this);
icon1.addView(rgp1);
rgp1.setLayoutParams(lp_ineer_ver);
rgp1.setImageResource(R.drawable.grp);
rgp1.setBackgroundColor(Color.TRANSPARENT);
Button rgp_value1 = new Button(this);
icon1.addView(rgp_value1);
rgp_value1.setLayoutParams(lp_ineer_ver);
rgp_value1.setText("something");
rgp_value1.setTextSize(12);
rgp_value1.setBackgroundColor(Color.TRANSPARENT);
rgp_value1.setTextColor(Color.WHITE);
ImageButton cool1 = new ImageButton(this);
icon1.addView(cool1);
cool1.setLayoutParams(lp_ineer_ver);
cool1.setImageResource(Color.TRANSPARENT);
cool1.setBackgroundColor(Color.TRANSPARENT);
TextView cool_value1 = new TextView(this);
icon1.addView(cool_value1);
cool_value1.setLayoutParams(text_cool);
cool_value1.setText("something");
cool_value1.setTextSize(12);
ver_rooms.addView(inner_ver.get(j)); // ver_rooms is a LinearLayout defined through the xml
}
So, what if i want to add an OnClickListener for the created items (e.g rgb1, rgb_value1, cool1), as I may have like 10 of inner_ver and all of them for sure contains all of these elements with the same name.
You would probably create an array or ArrayList of 10 inner_ver.Here I have assumed, innerVerList is an Array that holds List of LinearLayout.
for (LinearLayout l: list){
for(int i=0; i<l.getChildCount(); ++i){
l.getChildAt(i).addOnClickListner(/*name of onClick listener here*/ );
}
}
But I still don't get when will you need so many LinearLayouts. Though I hope it helps!
Happy coding!
for (int i = 0; i < sub.length; i++) {
tr[i] = new TableRow(this);
EditText et = new EditText(this);
et.setText(message2);
tr[i].addView(et);
EditText et1 = new EditText(this);
et1.setText(sub[i]);
tr[i].addView(et1);
EditText et2 = new EditText(this);
et2.setText(cde[i]);
tr[i].addView(et2);
EditText et3 = new EditText(this);
et3.setText(crd[i]);
tr[i].addView(et3);
ll.addView(tr[i]);
}
This is my code to create four edit text components in a row.
I need to give same text parameters to all the edittext components. But using methods to all four edittext components individually makes the code too lengthy.
Is there any solution so that I can use only one method to set text parameters for all the edittext components?
Using a method while adding your EditTexts to your TableRow can make your code easier to read.
private void addEditTextToTableRow(TableRow tableRow, String text) {
EditText editText = new EditText(this);
editText.setText(text);
tableRow.addView(editText);
}
Your for loop then becomes:
for (int i = 0; i < sub.length; i++) {
TableRow tr[i] = new TableRow(this);
addEditTextToTableRow(tr[i], message2);
addEditTextToTableRow(tr[i], sub[i]);
addEditTextToTableRow(tr[i], cde[i]);
addEditTextToTableRow(tr[i], crd[i]);
ll.addView(tr[i]);
}
try creating your components in the XML file, which will serve as a vision, and activity or in the XML Set The only text you need to set ... if you need utilzar one table, use the listView in XML and put all your EditText inside the listView
Edit: as Blumer pointed out, I was not adding the items to the table, so that this question appeared just because I was careless and I didn't see my mistake.
I am trying to create a dynamic TableLayout, as I have to receive results from the server and add rows based on the results, but the table is not updating. (Also, the TableLayout already has 1 initial row, the header row).
This is my code:
Room[] rooms = State.rooms;
TableLayout tblBookDetails = (TableLayout) findViewById(R.id.tblbookdetails);
for(int i = 0; i < rooms.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LayoutParams layout_wrapwrap = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_wrapwrap.rightMargin = 10; //TODO: Convert into DP
Resources res = getResources();
TextView txt1 = new TextView(this);
txt1.setLayoutParams(layout_wrapwrap);
txt1.setTextColor(res.getColor(android.R.color.black));
txt1.setText(rooms[i].name);
TextView txt2 = new TextView(this);
txt2.setLayoutParams(layout_wrapwrap);
txt2.setTextColor(getResources().getColor(android.R.color.black));
txt2.setText(rooms[i].price + " " + rooms[i].currency);
EditText edit1 = new EditText(this);
edit1.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit1.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit1.setEms(3);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
EditText edit2 = new EditText(this);
edit2.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit2.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit2.setEms(3);
edit2.setInputType(InputType.TYPE_CLASS_NUMBER);
Spinner spinner = new Spinner(this);
layout_wrapwrap.rightMargin = 0;
spinner.setLayoutParams(layout_wrapwrap);
Integer[] numbers = new Integer[rooms[i].count];
for(int j = 0; j < numbers.length; j++) {
numbers[j] = i + 1;
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
BookActivity.this, R.layout.spinner_textview, numbers);
spinner.setAdapter(adapter);
tblBookDetails.addView(tr);
}
//Another exquisite beauty of Java.
Log.d("USR", Integer.valueOf(tblBookDetails.getChildCount()).toString());
tblBookDetails.invalidate();
tblBookDetails.refreshDrawableState();
To prevent any confusion, the Room[] array is just a simple property-holder class.
This code looks enormously convoluted, and the table is not updating. I've searched quite a bit on the Internet, and I could not find any solution for this problem.
Thank you in advance.
I see where you add tr to tblBookDetails, but I don't see anywhere where you put txt1, txt2, edit1, etc. in tr. Try adding those views to the row, and I think that should get you there, because right now you appear to be adding the TableRow, but there's nothing in it.
Well i made an activity where i am creating some TextViews based on the size of a string array! But despite the fact that my string array has 4 items on it, which i tested it with debugging, the textviews that are created is only 1. If anyone has an idea about it please tell me :)
setContentView(R.layout.program);
String[] daily_lessons = getResources().getStringArray(R.array.firstGradeLessons);
final TextView[] tv = new TextView[daily_lessons.length];
final LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
fasa = (TextView) findViewById(R.id.textView1);
fasa.setText(String.valueOf(daily_lessons.length));
for (int i=0; i<daily_lessons.length; i++){
tv[i] = new TextView(this);
tv[i].setText(daily_lessons[i]);
tv[i].setTextSize(20);
tv[i].setLayoutParams(new LinearLayout.LayoutParams((int)LayoutParams.FILL_PARENT,(int) LayoutParams.WRAP_CONTENT));
tv[i].setGravity(Gravity.CENTER);
layout.addView(tv[i]);
}
If you still need an answer to this question here is what I would do.
setContentView(R.layout.program);
String[] daily_lessons = getResources().getStringArray(R.array.firstGradeLessons);
LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
fasa = (TextView) findViewById(R.id.textView1);
fasa.setText(String.valueOf(daily_lessons.length));
TextView tmpView = null;
for (int i=0; i<daily_lessons.length; i++){
tmpView = new TextView(this);
tmpView.setText(daily_lessons[i]);
tmpView.setTextSize(20);
layout.addView(tmpView , new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
I use this type of code alot for my dynamically genenerated content (obtain content from prepopulated database).
The TextView seems to be created but might now be visible on the GUI may be stacked over each other etc.
1.Use the layout.setOrientation(ORIENTAIION.VERTICAL) on the parent linear layout's.
2.Use the childCount() on layout to make it sure on the fact that the all 4 text views have been added to the snippet.
3.Also make sure your are not using removeALLView() etc methods for your case study to the problem case.