I have created dynamic layout that picks out data from xml and then create layout based on that.
I want to retain what I have written in in edittext and hence get the filled value.
But I am not getting success.
public void generateFields()
{
ll.removeAllViewsInLayout();
for(;i<=GenericXMLParser.nooffields;)
{
for(int j=i;j<i+3 ;j++)
{
if((j+1)>GenericXMLParser.nooffields)
{
break;
}
if(isonphone.get(j).equalsIgnoreCase("false"))
{
Log.i("isonphone", isonphone.get(j));
TextView textview = new TextView(this);
textview.setText(field.get(j));
textview.setTextSize(30);
textview.setTextColor(Color.parseColor("#000000"));
ll.addView(textview);
if(typefield.get(j).equalsIgnoreCase("text"))
{
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 5, 0, 15); // llp.setMargins(left, top, right, bottom);
EditText et = new EditText(this);
et.setHint(field.get(j));
et.setTextSize(30);
et.setLayoutParams(llp);
et.setTextColor(Color.parseColor("#000000"));
et.setBackgroundResource(R.drawable.genericedittext);
ll.addView(et);
savePreferences("field"+j,et.getText().toString().trim());
texts.add(et.getText().toString().trim());
Toast.makeText(getApplicationContext(), "field"+i+texts, 1).show();
}
else if(typefield.get(j).equalsIgnoreCase("boolean"))
{
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 5, 0, 15); // llp.setMargins(left, top, right, bottom);
RadioGroup rg = new RadioGroup(this);
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
rg.setLayoutParams(llp);
rb1.setText("Yes");
rb2.setText("No");
rb1.setTextColor(Color.parseColor("#000000"));
rb2.setTextColor(Color.parseColor("#000000"));
rb1.setTextSize(30);
rb2.setTextSize(30);
rg.addView(rb1);
rg.addView(rb2);
ll.addView(rg);
}
else if(typefield.get(j).equalsIgnoreCase("numeric"))
{
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 5, 0, 15); // llp.setMargins(left, top, right, bottom);
EditText et = new EditText(this);
et.setHint(field.get(j));
et.setTextSize(30);
et.setLayoutParams(llp);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.setTextColor(Color.parseColor("#000000"));
et.setBackgroundResource(R.drawable.genericedittext);
ll.addView(et);
}
}
this.setContentView(rl);
}
break;
}
//Toast.makeText(getApplicationContext(), "i="+i, 1).show();
if(i>GenericXMLParser.nooffields)
{
//Take me to the summary page.
Intent gmsIntent = new Intent(getApplicationContext(),
//GateManagementActivity.class);
GateManagementActivity.class);
gmsIntent.putExtra("userId", userId);
gmsIntent.putExtra("clientId", clientId);
startActivity(gmsIntent);
}
i+=3;
}
Related
I'm creating of form with inputs received by an API request, I'm using switch to create each form with it's 'type' , for example if type=='text' var = editText , type=='title' var = 'textview' and if type='submit' var = button.
I'm using volley with the APIs, I need to submit the form after user submits it, but I didn't know how to get the list of variables and it's data (because they are dynamically created I don't know).
switch (type) {
case "text":
LinearLayout lyt = new LinearLayout(context);
lyt.setOrientation(LinearLayout.HORIZONTAL);
ViewGroup.LayoutParams prms = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
lyt.setLayoutParams(prms);
TextView textViewLabel = new TextView(context);
textViewLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textViewLabel.setTypeface(null, Typeface.BOLD);
textViewLabel.setText(rowObj.getString("label"));
textViewLabel.setMaxWidth(150);
lyt.addView(textViewLabel);
EditText editText = new EditText(context);
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsL.setMargins(15, 0,0,0);
if(rowObj.has("maxLength")) {
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(Integer.parseInt(rowObj.getString("maxLength")));
editText.setFilters(fArray);
}
editText.setLayoutParams(paramsL);
editText.setSingleLine(true);
editText.setWidth(500);
editText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
lyt.addView(editText);
parent.addView(lyt);
break;
case "textarea":
LinearLayout lyt2 = new LinearLayout(context);
lyt2.setOrientation(LinearLayout.HORIZONTAL);
ViewGroup.LayoutParams prms2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
lyt2.setLayoutParams(prms2);
TextView textViewLabel2 = new TextView(context);
textViewLabel2.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textViewLabel2.setTypeface(null, Typeface.BOLD);
textViewLabel2.setMaxWidth(150);
textViewLabel2.setText(rowObj.getString("label"));
lyt2.addView(textViewLabel2);
EditText editText2 = new EditText(context);
LinearLayout.LayoutParams paramsL1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsL1.setMargins(15, 0,0,0);
if(rowObj.has("maxLength")) {
InputFilter[] fArray1 = new InputFilter[1];
fArray1[0] = new InputFilter.LengthFilter(Integer.parseInt(rowObj.getString("maxLength")));
editText2.setFilters(fArray1);
}
editText2.setLayoutParams(paramsL1);
editText2.setSingleLine(true);
editText2.setWidth(500);
editText2.setHeight(300);
editText2.setEms(10);
editText2.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5.0f, getResources().getDisplayMetrics()), 1.0f);
editText2.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE|InputType.TYPE_CLASS_TEXT);
editText2.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
lyt2.addView(editText2);
parent.addView(lyt2);
break;
case "image":
LinearLayout lLayout1 = new LinearLayout(context);
ViewGroup.LayoutParams params1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
lLayout1.setLayoutParams(params1);
ImageView imageView = new ImageView(context);
imageView.setPadding(5,5,5,5);
Picasso.with(context).load(rowObj.getString("src")).into(imageView);
lLayout1.addView(imageView);
parent.addView(lLayout1);
break;
case "title":
TextView textView = new TextView(context);
ViewGroup.LayoutParams params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, Float.parseFloat(rowObj.getString("colSpan")));
textView.setLayoutParams(params2);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setTypeface(null, Typeface.BOLD);
textView.setText(rowObj.getString("label"));
parent.addView(textView);
break;
case "subtitle":
TextView textView1 = new TextView(context);
ViewGroup.LayoutParams params3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
textView1.setLayoutParams(params3);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView1.setText(rowObj.getString("label"));
parent.addView(textView1);
break;
case "file":
break;
case "radio":
LinearLayout lyt3 = new LinearLayout(context);
lyt3.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams prms3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
lyt3.setLayoutParams(prms3);
TextView textViewLabel3 = new TextView(context);
textViewLabel3.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textViewLabel3.setTypeface(null, Typeface.BOLD);
textViewLabel3.setText(rowObj.getString("label"));
lyt3.addView(textViewLabel3);
// Initialize a new RadioGroup
RadioGroup rg = new RadioGroup(context);
rg.setOrientation(RadioGroup.VERTICAL);
if(rowObj.has("options")){
JSONArray options = rowObj.getJSONArray("options");
if(options.length()>0){
for (int i=0; i<options.length(); i++){
JSONObject radioObj = options.getJSONObject(i);
// Create another Radio Button for RadioGroup
RadioButton rb = new RadioButton(context);
rb.setText(radioObj.getString("label"));
rb.setTextColor(Color.BLACK);
rg.addView(rb);
}
}
}
lyt3.addView(rg);
parent.addView(lyt3);
break;
case "submit":
Button button = new Button(context);
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, Float.parseFloat(rowObj.getString("colSpan")));
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
button.setSingleLine(true);
button.setText(rowObj.getString("label"));
button.setTextColor(Color.WHITE);
parent.addView(button);
break;
case "hidden":
break;
}
}else {
TextView view = new TextView(context);
ViewGroup.LayoutParams params3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
view.setLayoutParams(params3);
view.setBackgroundColor(Color.WHITE);
view.setText("");
parent.addView(view);
}
}
I want to put the variables in a JSON array to send a POST request :
[
{
"variable1": "value1",
"variable2": "value2",
...
}
]
public class Test extends Activity{
private String variableOne;
private String variableTwo;
switch (whatever) {
case "text":
varbiableOne = "firstvaluetext";
variableTwo="secondvaluetext"
break;
case "textarea":
varbiableOne = "firstvaluetextarea";
variableTwo="secondvaluetextarea"
break;
default:
//whatever
break;
}
There you have your variables and you can build your json out of it.
Really hard to get what you want.
It's been a few month since I am working heavely on this one app. Everything needs to be just perfect before the launch.
Now more and more functunallty is implemented and exactly that is the problem: The activities now start to be up to 1000 lines! Why is that, you may ask?
It is because of the layout that needs to be set programmatically.
In almost every activity in my app we have a scrollview that downloads content off a server and then puts it into a foreach loop. This renders it impossible to set most of the layout in the xmls. Just to scare you a little bit:
private void DrawLayoutFriends()
{
// Start with the header
frmHeaderFriends.SetBackgroundColor(Color.ParseColor("#38a0c2"));
frmHeaderFriends.LayoutParameters = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MatchParent, intHeightOfDisplay / 7);
ImageButton btnHeader = new ImageButton(this);
btnHeader.SetBackgroundResource(Resource.Drawable.mainmenu_btn_news_friends);
btnHeader.Background.Mutate().SetColorFilter(new Color(Color.White), PorterDuff.Mode.SrcIn);
FrameLayout.LayoutParams paramsHeaderBtn = new FrameLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsHeaderBtn.Gravity = GravityFlags.Center;
TextView txtHeader = new TextView(this);
txtHeader.Text = "17 news!";
txtHeader.SetTypeface(font, TypefaceStyle.Normal);
txtHeader.SetTextColor(Color.ParseColor("#ffffff"));
FrameLayout.LayoutParams paramsForTextHeader = new FrameLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsForTextHeader.Gravity = GravityFlags.Bottom | GravityFlags.Right;
txtHeader.LayoutParameters = paramsForTextHeader;
btnHeader.LayoutParameters = paramsHeaderBtn;
frmHeaderFriends.AddView(btnHeader);
frmHeaderFriends.AddView(txtHeader);
// Now the loop
foreach (var friend in newfriends)
{
LinearLayout linlayForLoop = new LinearLayout(this);
LinearLayout.LayoutParams paramsForLinLayLoop = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, intHeightOfDisplay / 2);
paramsForLinLayLoop.Gravity = GravityFlags.Center;
linlayForLoop.Orientation = Orientation.Vertical;
ImageButton btnAvatarOfFriend = new ImageButton(this);
btnAvatarOfFriend.SetBackgroundResource(Resource.Drawable.general_dummy_avatar_big);
LinearLayout.LayoutParams paramsForCenter = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsForCenter.Gravity = GravityFlags.Center;
paramsForCenter.SetMargins(0, intHeightOfDisplay / 80, 0, 0);
btnAvatarOfFriend.LayoutParameters = paramsForCenter;
TextView txtNameOfFriend = new TextView(this);
txtNameOfFriend.Text = friend.fkUsername.ToUpper()+ " " +
Resources.GetString(Resource.String.AddedU);
txtNameOfFriend.TextSize = 18;
txtNameOfFriend.Gravity = GravityFlags.Center;
txtNameOfFriend.SetTypeface(font, TypefaceStyle.Normal);
txtNameOfFriend.SetTextColor(Color.ParseColor("#007762"));
LinearLayout.LayoutParams paramsForTextFriend = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsForTextFriend.Gravity = GravityFlags.Center;
paramsForTextFriend.SetMargins(0, intHeightOfDisplay / 80, 0, 0);
txtNameOfFriend.LayoutParameters = paramsForTextFriend;
TextView txtSubLineFriends = new TextView(this);
txtSubLineFriends.Text = "traveler | lvl 99 | 999,999ap";
txtSubLineFriends.TextSize = 10;
txtSubLineFriends.SetTypeface(font, TypefaceStyle.Normal);
txtSubLineFriends.SetTextColor(Color.ParseColor("#000000"));
LinearLayout.LayoutParams paramsForTextSubLineFriend = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsForTextSubLineFriend.Gravity = GravityFlags.Center;
txtSubLineFriends.LayoutParameters = paramsForTextSubLineFriend;
linlayForLoop.AddView(btnAvatarOfFriend);
linlayForLoop.AddView(txtNameOfFriend);
linlayForLoop.AddView(txtSubLineFriends);
btnAvatarOfFriend.Click += delegate
{
Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);
alert.SetMessage("Hey there! I added you as my friend to follow your profile! Would you like to add me back? :-)");
alert.SetPositiveButton("Befriend Me!", (senderAlert, args) =>
{
Toast.MakeText(this, "Not yet possible", ToastLength.Short).Show();
});
alert.SetNegativeButton("Visit My Profile!", (senderAlert, args) =>
{
Toast.MakeText(this, "Not yet possible", ToastLength.Short).Show();
});
alert.SetNeutralButton("Dismiss Me!", (senderAlert, args) =>
{
TranslateAnimation animation = new TranslateAnimation(0, -2000, 0, 0);
animation.FillAfter = true;
animation.SetInterpolator(this, Android.Resource.Animation.AccelerateDecelerateInterpolator);
animation.Duration = intDurationOfAnimation;
btnAvatarOfFriend.StartAnimation(animation);
txtNameOfFriend.StartAnimation(animation);
txtSubLineFriends.StartAnimation(animation);
animation.AnimationEnd += delegate
{
linlayForLoop.RemoveAllViews();
};
});
Dialog dialog = alert.Create();
dialog.Show();
};
linlayForAddedFriends.AddView(linlayForLoop);
}
btnDismissAllLeft.Click += delegate
{
// TranslateAnimation animation = new TranslateAnimation(0, -2000, 0, 0);
// animation.FillAfter = true;
// animation.SetInterpolator(this, Android.Resource.Animation.AccelerateDecelerateInterpolator);
// animation.Duration = intDurationOfAnimation;
// btnAvatarOfFriend.StartAnimation(animation);
// txtNameOfFriend.StartAnimation(animation);
// txtSubLineFriends.StartAnimation(animation);
//
// animation.AnimationEnd += delegate
// {
// linlayForLoop.RemoveAllViews();
// };
};
//Put into side drawer
}
private void DrawLayoutBolNews()
{
// Start with the header
frmHeaderNews.SetBackgroundColor(Color.ParseColor("#38a0c2"));
frmHeaderNews.LayoutParameters = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MatchParent, intHeightOfDisplay / 7);
ImageButton btnHeader = new ImageButton(this);
btnHeader.SetBackgroundResource(Resource.Drawable.mainmenu_btn_news_bol);
btnHeader.Background.Mutate().SetColorFilter(new Color(Color.White), PorterDuff.Mode.SrcIn);
FrameLayout.LayoutParams paramsHeaderBtn = new FrameLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsHeaderBtn.Gravity = GravityFlags.Center;
TextView txtHeader = new TextView(this);
txtHeader.Text = "17 news!";
txtHeader.SetTypeface(font, TypefaceStyle.Normal);
txtHeader.SetTextColor(Color.ParseColor("#ffffff"));
FrameLayout.LayoutParams paramsForTextHeader = new FrameLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsForTextHeader.Gravity = GravityFlags.Bottom | GravityFlags.Left;
txtHeader.LayoutParameters = paramsForTextHeader;
btnHeader.LayoutParameters = paramsHeaderBtn;
frmHeaderNews.AddView(btnHeader);
frmHeaderNews.AddView(txtHeader);
// Now the loop
LinearLayout linlayForLoop = new LinearLayout(this);
LinearLayout.LayoutParams paramsForLinLayLoop = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, intHeightOfDisplay / 2);
paramsForLinLayLoop.Gravity = GravityFlags.Center;
linlayForLoop.Orientation = Orientation.Vertical;
ImageButton btnNews = new ImageButton(this);
btnNews.SetBackgroundResource(Resource.Drawable.general_imgv_news);
LinearLayout.LayoutParams paramsForCenter = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsForCenter.Gravity = GravityFlags.Center;
paramsForCenter.SetMargins(0, intHeightOfDisplay / 80, 0, 0);
btnNews.LayoutParameters = paramsForCenter;
TextView txtInfo = new TextView(this);
txtInfo.Text = "123 new challenges unlocked!";
txtInfo.TextSize = 15;
txtInfo.SetTypeface(font, TypefaceStyle.Normal);
txtInfo.SetTextColor(Color.ParseColor("#007762"));
txtInfo.Gravity = GravityFlags.Center;
LinearLayout.LayoutParams paramsForTextFriend = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
paramsForTextFriend.Gravity = GravityFlags.Center;
paramsForTextFriend.SetMargins(0, intHeightOfDisplay / 80, 0, 0);
txtInfo.LayoutParameters = paramsForTextFriend;
linlayForLoop.AddView(btnNews);
linlayForLoop.AddView(txtInfo);
linlayForLoop.Click += delegate
{
Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);
alert.SetPositiveButton("Dismiss!", (senderAlert, args) =>
{
TranslateAnimation animation = new TranslateAnimation(0, 2000, 0, 0);
animation.FillAfter = true;
animation.SetInterpolator(this, Android.Resource.Animation.AccelerateDecelerateInterpolator);
animation.Duration = intDurationOfAnimation;
txtInfo.StartAnimation(animation);
btnNews.StartAnimation(animation);
animation.AnimationEnd += delegate
{
linlayForLoop.RemoveAllViews();
};
});
alert.SetNeutralButton("Show!", (senderAlert, args) =>
{
Toast.MakeText(this, "Not yet possible", ToastLength.Short).Show();
});
Dialog dialog = alert.Create();
dialog.Show();
};
btnDismissAllRight.Click += delegate
{
TranslateAnimation animation = new TranslateAnimation(0, 2000, 0, 0);
animation.FillAfter = true;
animation.SetInterpolator(this, Android.Resource.Animation.AccelerateDecelerateInterpolator);
animation.Duration = intDurationOfAnimation;
txtInfo.StartAnimation(animation);
btnNews.StartAnimation(animation);
animation.AnimationEnd += delegate
{
linlayForLoop.RemoveAllViews();
};
};
//Put into side drawer
linlayForBolNews.AddView(linlayForLoop);
}
This is JUST the layout from two methods in one class. Not even everything.
This is veryinconvinient. Not only is the class extemely long, it is also very confusing to read and therefore fix in case something breaks.
Now my question:
What is the best way to handle programmatic layout inside my many classes / activities?
Have one class ONLY with layout functions and then send data back and forth when needed? This would get the layout out off the activities, yet it still remains impossible to read. Also, that one class then would easily reach 10k lines which is just rediculous...
I hope you guys can hint me towards the right way, before I start moving all layout files and then realize it was wrong.
Hi In this code finding the total marks and setting the value into textView.But it showing last value and output showing 65.Based on marks I want to find the total marks of the student
Can any one please help me.
java
total_marks=new int[numOfStudents];
addHeader();
int j = 0;
for (Iterator i = users.iterator(); i.hasNext();) {
subject[j] = (EditText) findViewById(R.id.subject);
subject[j].setVisibility(View.GONE);
Maxmarks[j] = (EditText) findViewById(R.id.max_mark);
Maxmarks[j].setVisibility(View.GONE);
Minmarks[j] = (EditText) findViewById(R.id.min_mark);
Minmarks[j].setVisibility(View.GONE);
ObtainedMarks[j] = (EditText) findViewById(R.id.obtained_mark);
ObtainedMarks[j].setVisibility(View.GONE);
Grades[j] = (EditText) findViewById(R.id.grade);
Grades[j].setVisibility(View.GONE);
StudentgradeDetails p = (StudentgradeDetails) i.next();
tr = new TableRow(this);
subject[j] = new EditText(this);
subject[j].setKeyListener(null);
subject[j].setText(p.getSubject());
subject[j].setId(p.getId());
subject[j].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
subject[j].setPadding(3, 3, 3, 3);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
Ll.addView(subject[j], params);
tr.addView((View) Ll);
Maxmarks[j] = new EditText(this);
Maxmarks[j].setKeyListener(null);
Maxmarks[j].setText(p.getMaxMark());
Maxmarks[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Maxmarks[j].setPadding(2,2, 2, 2);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
Ll.addView(Maxmarks[j], params);
tr.addView((View) Ll);
Minmarks[j] = new EditText(this);
Minmarks[j].setKeyListener(null);
Minmarks[j].setText(p.getMinMark());
Minmarks[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Minmarks[j].setPadding(3, 3, 3, 3);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 3, 3);
Ll.addView(Minmarks[j], params);
tr.addView((View) Ll);
ObtainedMarks[j] = new EditText(this);
ObtainedMarks[j].setKeyListener(null);
ObtainedMarks[j].setText(p.getObtainedMark());
ObtainedMarks[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ObtainedMarks[j].setPadding(3, 3, 3, 3);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 3, 3);
Ll.addView(ObtainedMarks[j], params);
tr.addView((View) Ll);
Grades[j] = new EditText(this);
Grades[j].setKeyListener(null);
int marks1 =Integer.parseInt(ObtainedMarks[j].getText().toString());
#SuppressWarnings("unused")
String grade=Integer.toString(marks1);
if(marks1 >= 80){
grade="A+";
} else if(marks1>=70){
grade="A";
}
else if(marks1>=60){
grade="B+";
}
else if(marks1>=50){
grade="B";
}
else if(marks1>=40){
grade="C";
}
else{
return;
}
Grades[j].setText(grade);
try {
total_marks[j]+=marks1;
TotalMarks=(TextView)findViewById(R.id.total);
TotalMarks.setText("Total Marks:"+total_marks[j]);
}
catch (Exception e) {
e.printStackTrace();
}
Grades[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Grades[j].setPadding(5, 5, 5, 5);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 3, 3);
Ll.addView(Grades[j], params);
tr.addView((View) Ll);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
j++;
}
use this
int total_marks=0;
int marks1 =Integer.parseInt(ObtainedMarks[j].getText().toString());
total_marks+=marks1;
TotalMarks=(TextView)findViewById(R.id.total);
TotalMarks.setText("Total Marks:"+total_marks);
how to align this
private TableRow nappyRow(int id) {
System.out.println("***nappies_list **get id*****"+id);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, 0, 1f);
TableRow.LayoutParams itemParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT, 1);
TableRow.LayoutParams rg_params = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 2f);
TableRow table_row = new TableRow(this);
table_row.setLayoutParams(rowParams);
final TextView when = new TextView(this);
RadioGroup radio_group=new RadioGroup(this);
radio_group.setOrientation(RadioGroup.HORIZONTAL);
radio_group.setLayoutParams(rg_params);
radio_group.setBackgroundColor(Color.BLUE);
RadioButton dry=new RadioButton(this);
RadioButton wet=new RadioButton(this);
RadioButton soiled=new RadioButton(this);
RadioGroup.LayoutParams button_params = new RadioGroup.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,0.4f);
//button_params.setMargins(40, 0, 0, 0);
dry.setLayoutParams(button_params);
wet.setLayoutParams(button_params);
soiled.setLayoutParams(button_params);
dry.setButtonDrawable(R.drawable.custom_radio_button);
wet.setButtonDrawable(R.drawable.custom_radio_button);
soiled.setButtonDrawable(R.drawable.custom_radio_button);
radio_group.addView(dry);
radio_group.addView(wet);
radio_group.addView(soiled);
// creating the add icon
ImageView img_nappy = new ImageView(this);
img_nappy.setBackgroundResource(R.drawable.icon_tick2);
ImageView img_cream = new ImageView(this);
img_cream.setBackgroundResource(R.drawable.icon_tick2_disabled);
// creating the add icon
ImageView icon = new ImageView(this);
icon.setBackgroundResource(R.drawable.icon_add);
// setting the layout parames
when.setLayoutParams(itemParams);
// Setting alignment
when.setGravity(Gravity.CENTER);
// setting text
when.setText("00:00");
LinearLayout icon_layout = new LinearLayout(this);
TableRow.LayoutParams params = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.5f);
params.gravity = Gravity.RIGHT;
icon_layout.setLayoutParams(params);
icon_layout.addView(icon);
Integer max_list = Collections.max(nappies_list);
if (id == max_list) {
System.out.println("add::" + id);
icon.setBackgroundResource(R.drawable.icon_add);
} else {
System.out.println("delete::" + id);
icon.setBackgroundResource(R.drawable.icon_delete);
}
// adding the widget into table row
table_row.addView(when);
table_row.addView(radio_group);
table_row.addView(icon_layout);
table_row.addView(img_nappy);
table_row.addView(img_cream);
icon.setId(id);
icon_layout.setId(id);
//click on the plus button
icon_layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Integer max_list = Collections.max(nappies_list);
if(v.getId() < max_list){
Logger.e("**********", "remove");
nappies_list.remove(v.getId());
nappies();
System.out.println("nappies_list::::::::::::" + nappies_list);
}else{
Logger.e("**********", "add");
nappies_list.add(bottle_list.size() + 1);
nappies();
System.out.println("nappies_list::::::::::::" + nappies_list);
}
System.out.println("click id::::::::::::" + v.getId());
System.out.println("size nappies_list::::::::::::"
+ nappies_list.size());
System.out.println("max_list::::::::::::" + max_list);
System.out.println("nappies_list::::::::::::" + nappies_list);
}
});
table_row.setPadding(0, 12, 0, 12);
return table_row;
}
i have created list linearlayout with my code
String[] name = {"Chicken", "Beef", "Milk" ,"Juice"};
final Integer[] price = {10, 50, 5, 3};
final Integer[] qty = {1, 2, 5, 5};
listOrder.removeAllViews();
LinearLayout.LayoutParams image = new LinearLayout.LayoutParams(Utility.getDip(175, CartActivity.this), Utility.getDip(70, CartActivity.this));
LinearLayout.LayoutParams text = new LinearLayout.LayoutParams(Utility.getDip(140, CartActivity.this), Utility.getDip(80, CartActivity.this));
LinearLayout.LayoutParams textQty = new LinearLayout.LayoutParams(Utility.getDip(100, CartActivity.this), Utility.getDip(80, CartActivity.this));
LinearLayout.LayoutParams textPrice = new LinearLayout.LayoutParams(Utility.getDip(125, CartActivity.this), Utility.getDip(80, CartActivity.this));
LinearLayout.LayoutParams textTotal = new LinearLayout.LayoutParams(Utility.getDip(130, CartActivity.this), Utility.getDip(80, CartActivity.this));
LinearLayout.LayoutParams editImage = new LinearLayout.LayoutParams(Utility.getDip(40, CartActivity.this), Utility.getDip(40, CartActivity.this));
for(int i = 0; i < 4; i++){
list = new LinearLayout(ctx);
list.setOrientation(LinearLayout.HORIZONTAL);
//if((i % 2) == 0) list.setBackgroundColor(R.color.bg_grey);
listImage = new ImageView(ctx);
//listImage.setImageDrawable(getResources().getDrawable(R.drawable.sample_logo_publisher));
listImage.setBackgroundColor(getResources().getColor(R.color.bg_grey));
listImage.setAdjustViewBounds(Boolean.TRUE);
listImage.setScaleType(ScaleType.FIT_XY);
image.gravity = Gravity.CENTER;
image.setMargins(Utility.getDip(10, CartActivity.this), 0, Utility.getDip(10, CartActivity.this), 0);
list.addView(listImage, image);
listText = new TextView(ctx);
listText.setText(name[i]);
listText.setGravity(Gravity.CENTER);
text.gravity = Gravity.CENTER;
text.setMargins(Utility.getDip(10, CartActivity.this), 0, 0, 0);
list.addView(listText, text);
listQty = new TextView(ctx);
listQty.setText(qty[i].toString());
listQty.setGravity(Gravity.CENTER);
textQty.gravity = Gravity.CENTER;
textQty.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(5, CartActivity.this), 0);
list.addView(listQty, textQty);
listPrice = new TextView(ctx);
listPrice.setText(price[i].toString());
listPrice.setGravity(Gravity.CENTER_VERTICAL);
textPrice.gravity = Gravity.CENTER;
list.addView(listPrice, textPrice);
Integer total = qty[i]*price[i];
listTotal = new TextView(ctx);
listTotal.setText(total.toString());
listTotal.setGravity(Gravity.CENTER_VERTICAL);
textTotal.gravity = Gravity.CENTER;
textTotal.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(2, CartActivity.this), 0);
list.addView(listTotal, textTotal);
plus = new ImageView(ctx);
plus.setImageDrawable(getResources().getDrawable(R.drawable.plus));
plus.setAdjustViewBounds(Boolean.TRUE);
plus.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(plus, editImage);
final int pos = i;
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
qty[pos] = qty[pos] + 1;
listQty.setText(qty[pos].toString());
listTotal.setText(""+ (qty[pos]*price[pos]));
}
});
minus = new ImageView(ctx);
minus.setImageDrawable(getResources().getDrawable(R.drawable.min));
minus.setAdjustViewBounds(Boolean.TRUE);
minus.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(minus, editImage);
remove = new ImageView(ctx);
remove.setImageDrawable(getResources().getDrawable(R.drawable.trash));
remove.setAdjustViewBounds(Boolean.TRUE);
remove.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(remove, editImage);
listOrder.addView(list);
}
I want to add quantity when onclick on plus image. But when i clicked plus image in the list, textQty changed in end of the list, not in the same list that I have clicked the image.
how to fix my code?
You need to link the TextViews and button together. They don't know they are in the same iteration of the for loop.
You could put each set of buttons and textviews into an array when you go through your loop.
Then in your onClickListener you would for do something like:
for(int i = 0; i < buttonArray.length; i++){
if(v == buttonArray[i]){
// make whatever changes to TextViews at index i
}
}
I have resolved my problem. Here is my code ...
I set id in listQty and listTotal
listQty = new TextView(ctx);
listQty.setId(99000+i);
listQty.setText(qty[i].toString());
listQty.setGravity(Gravity.CENTER);
textQty.gravity = Gravity.CENTER;
textQty.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(5, CartActivity.this), 0);
list.addView(listQty, textQty);
listTotal = new TextView(ctx);
listTotal.setId(98000+i);
listTotal.setText(total.toString());
listTotal.setGravity(Gravity.CENTER_VERTICAL);
textTotal.gravity = Gravity.CENTER;
textTotal.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(2, CartActivity.this), 0);
list.addView(listTotal, textTotal);
and in clicklistener set variable of listQty and listTotal
plus = new ImageView(ctx);
plus.setImageDrawable(getResources().getDrawable(R.drawable.plus));
plus.setAdjustViewBounds(Boolean.TRUE);
plus.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(plus, editImage);
final int pos = i;
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
listQty = (TextView)findViewById(99000+pos);
listTotal = (TextView)findViewById(98000+pos);
qty[pos] = qty[pos] + 1;
listQty.setText(qty[pos].toString());
listTotal.setText(""+ (qty[pos]*price[pos]));
}
});