Saving information in firebase database from dynamically created Edittext and RadioButtons - android

Iam creating the survey app with firebase Realtimedatabase.I have stored questions in database and getting them in application by dynamically creating RadioButtons,Textview and Edittext.
Now I have to save the filled information from users back to firebase database.I searched and get to know to use setTags but couldnt really able to understand after setting tags how to seperate edittext and radio buttons and get them back separately while pushing them to firebase database.What I want is to get the data from each edittext and radiobutton selected and save them back uniquely in firebase database
Here is the code where Iam creating views dynamically and getting data in them
if (getIntent().hasExtra(Constants.ref_no)) {
surveyRefNo = getIntent().getStringExtra(Constants.ref_no);
}
if (getIntent().hasExtra(Constants.refChild)) {
surveyRefChild = getIntent().getStringExtra(Constants.refChild);
}
databaseReference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.survey).child(surveyRefNo).child(surveyRefChild);
System.out.println(databaseReference);
System.out.println(surveyRefNo);
linearLayout = findViewById(R.id.survey_question_linearlayout);
databaseReference.child(Constants.questions).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
System.out.println(ds);
if (ds.hasChildren() && ds.hasChild("title") && ds.hasChild("type") && ds.hasChild("options")) {
for (DataSnapshot option : ds.child("options").getChildren()) {
values.add(String.valueOf(option.getValue()));
System.out.println(values);
System.out.println(values.size());
}
String user = String.valueOf(ds.child("type").getValue());
title = (String) ds.child("title").getValue();
switch (Integer.parseInt(user)) {
case (1):
if (user != null) {
addRadioButtons();
values.clear();
}
break;
case (2):
if (user != null) {
questionView();
}
break;
default:
Toast.makeText(GetSurveys.this, "Sorry!!! Something went wrong", Toast.LENGTH_SHORT).show();
}
} else if (ds.hasChildren() && ds.hasChild("title") && ds.hasChild("type")) {
String user = String.valueOf(ds.child("type").getValue());
title = (String) ds.child("title").getValue();
switch (Integer.parseInt(user)) {
case (1):
if (user != null) {
addRadioButtons();
}
break;
case (2):
if (user != null) {
questionView();
}
break;
default:
Toast.makeText(GetSurveys.this, "Sorry!!! Something went wrong", Toast.LENGTH_SHORT).show();
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
public void addRadioButtons() {
CardView cardView = new CardView(this);
LinearLayout.LayoutParams cardParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
cardParams.setMargins(10, 10, 10, 10);
cardView.setLayoutParams(cardParams);
cardView.setCardBackgroundColor(Color.parseColor("#f8f8ff"));
LinearLayout cardLayout = new LinearLayout(this);
cardLayout.setOrientation(LinearLayout.VERTICAL);
cardView.addView(cardLayout);
linearLayout.addView(cardView);
TextView radioText = new TextView(this);
radioText.setText(title);
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textParams.setMargins(10, 10, 10, 10);
radioText.setLayoutParams(textParams);
cardLayout.addView(radioText);
RadioGroup radioGroup = new RadioGroup(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 10, 10, 10);
radioGroup.setLayoutParams(params);
radioGroup.setOrientation(LinearLayout.VERTICAL);
for (int index = 0; index < values.size(); index++) {
RadioButton radioButton = new RadioButton(this);
radioButton.setId(View.generateViewId());
radioButton.setText(values.get(index));
radioGroup.addView(radioButton);
}
cardLayout.addView(radioGroup);
}
public void questionView() {
CardView cardView = new CardView(this);
LinearLayout.LayoutParams cardParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
cardParams.setMargins(10, 10, 10, 10);
cardView.setLayoutParams(cardParams);
cardView.setCardBackgroundColor(Color.parseColor("#f8f8ff"));
LinearLayout cardLayout = new LinearLayout(this);
cardLayout.setOrientation(LinearLayout.VERTICAL);
cardView.addView(cardLayout);
linearLayout.addView(cardView);
TextView questionText = new TextView(this);
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textParams.setMargins(20, 10, 20, 10);
questionText.setPadding(15, 10, 10, 10);
questionText.setText(title);
questionText.setGravity(16);
questionText.setTextSize(17.0f);
questionText.setLayoutParams(textParams);
cardLayout.addView(questionText);
EditText answerText = new EditText(this);
LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editParams.setMargins(20, 5, 20, 5);
answerText.setPadding(15, 10, 10, 10);
answerText.setHint("Your Answer");
answerText.setTextSize(17.0f);
answerText.setLayoutParams(editParams);
cardLayout.addView(answerText);
}
}

Related

Two radiobutton got selected in a groupbutton in Android Studio

I created an app using Android Studio and one of its activity contain questions and their choices using radiobuttons. I created those groupbuttons and radiobuttons programmatically, but the first question can choose two radio button when it shouldn't.
This is my code:
FeedbackFragment.java
try {
loading.setVisibility(View.GONE);
JSONObject jsonObject = new JSONObject(response);
if (!jsonObject.isNull("questions")) {
JSONArray questions = jsonObject.getJSONArray("questions");
LinearLayout.LayoutParams params;
for (int i = 0; i < questions.length(); i++) {
final JSONObject oneQuestion = questions.getJSONObject(i);
if (!oneQuestion.isNull("answers")) {
TextView textQuestion = new TextView(getActivity());
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
textQuestion.setLayoutParams(params);
textQuestion.setText(oneQuestion.getString("question_text"));
if(textQuestion.getParent() != null) {
((ViewGroup) textQuestion.getParent()).removeView(textQuestion);
}
layoutFeedback.addView(textQuestion);
groupChoice = new RadioGroup(getActivity());
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0,0,0,20);
groupChoice.setLayoutParams(params);
groupChoice.setId(Integer.valueOf(oneQuestion.getString("question_id")));
groupChoices.add(groupChoice);
if(groupChoice.getParent() != null) {
((ViewGroup) groupChoice.getParent()).removeView(groupChoice);
}
layoutFeedback.addView(groupChoice);
JSONArray choices = oneQuestion.getJSONArray("answers");
for(int j=0;j<choices.length();j++) {
JSONObject choice = choices.getJSONObject(j);
radioChoice = new RadioButton(getActivity());
radioChoice.setId(Integer.valueOf(choice.getString("answer_id")));
radioChoice.setText(choice.getString("answer_text"));
radioChoice.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
radioChoice.setButtonTintList(new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled}, //disabled
new int[]{android.R.attr.state_enabled} //enabled
},
new int[] {
Color.BLACK //disabled
,getResources().getColor(R.color.colorPrimary) //enabled
}
));
groupChoice.addView(radioChoice);
}
LinearLayout borderBottom = new LinearLayout(getActivity());
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
1);
params.setMargins(0, 0, 0, 20);
borderBottom.setLayoutParams(params);
borderBottom.setBackgroundColor(Color.parseColor("#999999"));
borderBottom.setVerticalGravity(Gravity.BOTTOM);
layoutFeedback.addView(borderBottom);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Error JSON Exception! " + e.toString(), Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
}
The weird thing is, it only happened at first question, the other question worked fine. What went wrong? Please help.

what is wrong in this first item in gridlayout?

In this code i am able to create gridlayout. but the first item is in default view and the rest is like what i want. I try debug and I couldn't see what is wrong. Would you help me to solve this.
question="this is a sentence";
String sLetter;
String question = question.replace(" ", "");
//char[] charAr = question.toCharArray();
final ArrayList<String> letters = new ArrayList<>();
for (int k = 0; k < question.length(); k++) {
sLetter = Character.toString(question.charAt(k));
letters.add(sLetter);
}
Collections.sort(letters);
int i;
for (i = 0; i < letters.size();) {
int count = recursiveMethod(letters,i,1);
if (count>0) {
//region text add
View main_view = new View(context);
main_view.setLayoutParams(new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT));
gridLayout.setColumnCount(7);
if (question.length()<=7){
gridLayout.setRowCount(1);
}else if (question.length()<12){
gridLayout.setRowCount(2);
}else {
gridLayout.setRowCount(3);
}
GridLayout.Spec colSpan = GridLayout.spec(GridLayout.UNDEFINED,1);
GridLayout.Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
rowSpan, colSpan);
gridParam.setGravity(Gravity.FILL_HORIZONTAL);
gridParam.setMargins(5, 10, 5, 10);
final TextView tv = new TextView(context);
tv.setText(letters.get(i).toUpperCase());
tv.setId(i);
tv.setPadding(15, 15, 15, 15);
tv.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
RelativeLayout.LayoutParams relTv = new RelativeLayout.LayoutParams(80, ViewGroup.LayoutParams.WRAP_CONTENT);
relTv.addRule( RelativeLayout.CENTER_HORIZONTAL);
relTv.addRule( RelativeLayout.CENTER_VERTICAL);
tv.setLinksClickable(true);
tv.setTextSize(0, 60);
//tv.setLayoutParams(relTv);
CardView cardView = new CardView(context);
cardView.setCardBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
cardView.setRadius(10);
cardView.setCardElevation(5);
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
final TextView textCount = new TextView(context);
textCount.setText(Integer.toString(count));
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);
textCount.setTypeface(Typeface.DEFAULT_BOLD);
textCount.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
textCount.setTextSize(0, 30);
textCount.setPadding(0,5,5,0);
//textCount.setLayoutParams(relParam);
relativeLayout.addView(tv,relTv);
relativeLayout.addView(textCount,relParam);
cardView.addView(relativeLayout);
//llLetters.addView(tv, lp);
gridLayout.addView(cardView,gridParam);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, tv.getText()+ "" + textCount.getText(), Toast.LENGTH_SHORT).show();
}
});
//llLetters.addView(gridLayout);
//endregion
i = i + count;
}
else {
i++;
}
}
The first letter of image is at the top left. I want to show that at the bottom right side of the letter. Would you help me to solve problem? Thanks =)
The problem lies here:
relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);
If you pass a zero to addRule, it is interpreted as false. Therefore you say, that ALIGN_BOTTOM should be false in your first letter, when i is zero.
See the documentation:
parameter: subject, int: the ID of another view to use as an anchor, or a boolean value (represented as RelativeLayout.TRUE for true or 0 for false).

how to solve android.os.NetworkOnMainThreadException?

public class DoLogin extends AsyncTask<String,String,String>
{
ResultSet rs2;
String z = "";
Boolean isSuccess = false;
TextView DateOfBooking,Product,CustomerName,
Quantity,Destination,DealerName,Remarks,DueDate;
ArrayList DateOfBooking1 = new ArrayList();
ArrayList Product1 = new ArrayList();
ArrayList CustomerName1 = new ArrayList();
ArrayList Quantity1 = new ArrayList();
ArrayList Destination1 = new ArrayList();
ArrayList DealerName1 = new ArrayList();
ArrayList Remarks1 = new ArrayList();
ArrayList DueDate1 = new ArrayList();
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(String r) {
Toast.makeText(OrderRequest.this, r, Toast.LENGTH_SHORT).show();
if(isSuccess) {
try {
addHeaders();
do{
s1 = rs2.getString(1);
DateOfBooking1.add(s1);
s2 = rs2.getString(2);
CustomerName1.add(s2);
s3 = rs2.getString(3);
Destination1.add(s3);
s4 = rs2.getString(4);
DealerName1.add(s4);
s5 = rs2.getString(5);
Product1.add(s5);
s6 = rs2.getString(6);
Quantity1.add(s6);
s7 =rs2.getString(7);
Remarks1.add(s7);
s8 =rs2.getString(8);
DueDate1.add(s8);
}while(rs2.next());
if (DateOfBooking1.size() != 0) {
for (int j = 0; j < DateOfBooking1.size(); j++) {
/** Create a TableRow dynamically **/
tr = new TableRow(OrderRequest.this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
DateOfBooking = new TextView(OrderRequest.this);
DateOfBooking.setText(DateOfBooking1.get(j).toString());
DateOfBooking.setTextColor(Color.BLACK);
DateOfBooking.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DateOfBooking.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DateOfBooking.setPadding(5, 5, 5, 5);
DateOfBooking.setId(j);
tr.addView(DateOfBooking); // Adding textView to
tablerow.
CustomerName = new TextView(OrderRequest.this);
CustomerName.setText(CustomerName1.get(j).toString());
CustomerName.setTextColor(Color.BLACK);
CustomerName.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
CustomerName.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
CustomerName.setPadding(5, 5, 5, 5);
CustomerName.setId(j);
tr.addView(CustomerName); // Adding textView to
tablerow.
Destination = new TextView(OrderRequest.this);
Destination.setText(Destination1.get(j).toString());
Destination.setTextColor(Color.BLACK);
Destination.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Destination.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Destination.setPadding(5, 5, 5, 5);
Destination.setId(j);
tr.addView(Destination); // Adding textView to
tablerow.
DealerName = new TextView(OrderRequest.this);
DealerName.setText(DealerName1.get(j).toString());
DealerName.setTextColor(Color.BLACK);
DealerName.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DealerName.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DealerName.setPadding(5, 5, 5, 5);
DealerName.setId(j);
tr.addView(DealerName); // Adding textView to
tablerow.
Product = new TextView(OrderRequest.this);
Product.setText(Product1.get(j).toString());
Product.setTextColor(Color.BLACK);
Product.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Product.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Product.setPadding(5, 5, 5, 5);
Product.setId(j);
tr.addView(Product); // Adding textView to
tablerow.
Quantity = new TextView(OrderRequest.this);
Quantity.setText(Quantity1.get(j).toString());
Quantity.setTextColor(Color.BLACK);
Quantity.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Quantity.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Quantity.setPadding(5, 5, 5, 5);
Quantity.setId(j);
tr.addView(Quantity); // Adding textView to
tablerow.
Remarks = new TextView(OrderRequest.this);
Remarks.setText(Remarks1.get(j).toString());
Remarks.setTextColor(Color.BLACK);
Remarks.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Remarks.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Remarks.setPadding(5, 5, 5, 5);
Remarks.setId(j);
tr.addView(Remarks); // Adding textView to
tablerow.
DueDate = new TextView(OrderRequest.this);
DueDate.setText(DueDate1.get(j).toString());
DueDate.setTextColor(Color.BLACK);
DueDate.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DueDate.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DueDate.setPadding(5, 5, 5, 5);
DueDate.setId(j);
tr.addView(DueDate); // Adding textView to
tablerow.
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
} else {
Toast.makeText(OrderRequest.this,
DateOfBooking1+""+Product1+""+DealerName1
+""+Destination1+""+DueDate1+""+CustomerName1
+""+Quantity1+""+Remarks1+"Sorry.....",
Toast.LENGTH_LONG).show();
}
}catch(Exception e)
{
Log.e("showing",e+"");
}
}
}
#Override
protected String doInBackground(String... params) {
try {
Connection con = (Connection) connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select
DocDate,CustomerName,Destination,DealerName,
ProductName,Quantity,Remarks,DueDate from [Dealer].[dbo].
[BookingOrder]";
Statement stmt = con.createStatement();
rs2 = stmt.executeQuery(query);
try {
if (rs2.next()) {
isSuccess = true;
z = "Successfully Viewed";
}
}catch (Exception n)
{
z = "selecting";
Log.e("selecting",n+"");
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions";
Log.e("Exc", ex + "");
return null;
}
return z;
}
}
I am getting exception as "android.os.NetworkOnMainThreadException",Resultset is giving me the values which are located in SQL server.but the code after getting values from resultset is not executing.How can I overcome this exception,Already I checked this exception,but the solutions are not working.Help me.Thank you in advance.
android.os.NetworkOnMainThreadException
is thrown when an application attempts to perform a network related operation on the main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher versions. Ensure that your application is not attempting to perform any network
related operation on its main thread.

Dynamic Layout issue adding padding in layout

I have a LinearLayout object and want to dynamically change the background image with a dynamically adding image object (it changes its color dynamically).
here is the code :
if (categoryResponse != null && categoryResponse.result != null
&& categoryResponse.result.length > 0) {
int i = 0;
category = new TextView[categoryResponse.result.length];
for (Category cat : categoryResponse.result) {
category[i] = new TextView(getActivity());
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
par.setMargins(0, 10, 0, 0);
category[i].setLayoutParams(par);
category[i].setGravity(Gravity.CENTER);
category[i].setText(cat.name);
category[i].setTag(cat.id);
category[i].setPadding(0, 10, 0, 10);
category[i].setTextSize(25);
category[i].setBackgroundResource(R.drawable.category);
category[i].setTypeface(Global.AppsFont);
category[i].setTextColor(getResources().getColor(
R.color.white));
main.addView(category[i]);
//System.out.println("header textview width = " +category[i].getWidth() + "");
category[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String id = (String) v.getTag();
for (Entry<String, LinearLayout> entry : channels
.entrySet()) {
if (entry.getKey().equals(id)) {
entry.getValue().setVisibility(
View.VISIBLE);
} else
entry.getValue().setVisibility(
View.GONE);
}
}
});
i++;
LinearLayout LLM = null;
LLM = new LinearLayout(getActivity());
channels.put(cat.id, LLM);
LLM.setOrientation(LinearLayout.VERTICAL);
LLM.setVisibility(View.GONE);
LinearLayout.LayoutParams LLParamsm = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LLParamsm.setMargins(0, 0, 0, 0);
LLM.setPadding(0, 0, 0, 0);
LLM.setLayoutParams(LLParamsm);
LLM.setBackgroundResource(R.drawable.category_bg);
main.addView(LLM);
LinearLayout LL = null;
int j = 0;
ChannelResponse channelResponse = JsonUtils
.getChannel(cat.id);
channelResponseData.put(cat.id, channelResponse);
if (channelResponse != null
&& channelResponse.result != null
&& channelResponse.result.length > 0) {
for (Channel chan : channelResponse.result) {
if (j % 2 == 0) {
LL = new LinearLayout(getActivity());
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setWeightSum(1);
LL.setLayoutParams(LLParams);
LLM.addView(LL);
}
Button chn = new Button(getActivity());
LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 0.5f);
ladderFLParams.setMargins(10, 10, 10, 10);
chn.setBackgroundResource(R.drawable.gray_selector);
chn.setGravity(Gravity.CENTER);
chn.setLayoutParams(ladderFLParams);
chn.setText(chan.name);
chn.setTag(chan.id);
chn.setTextSize(25);
chn.setTextColor(getResources().getColor(
R.color.black));
chn.setTypeface(Global.AppsFont);
chn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
channelIdid = (String) v.getTag();
createDialogBox(v);
}
});
LL.addView(chn);
channel.add(chn);
j++;
}
if (j % 2 != 0) {
Button chn = new Button(getActivity());
LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, 0.5f);
ladderFLParams.setMargins(10, 10, 10, 10);
chn.setBackgroundResource(R.drawable.gray_selector);
chn.setGravity(Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL);
chn.setLayoutParams(ladderFLParams);
chn.setVisibility(View.INVISIBLE);
chn.setTextSize(25);
chn.setTextColor(getResources().getColor(
R.color.black));
chn.setTypeface(Global.AppsFont);
LL.addView(chn);
channel.add(chn);
}
}
}
}'
9-patch images automatically add the padding that is built into the 9-patch, as padding to the view. Thus when you call LLM.setPadding(0, 0, 0, 0) that is being overridden when you call LLM.setBackgroundResource.
Here is the doc about 9-patches: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
If you don't want padding, your best option would be to design your 9-patch without padding, in other words with both the drawable and stretchable areas going to the edge of the image.
I suppose you could also try calling setPadding after setBackgroundResource, but I'm not sure if that would work. Really you should just design your background with your desired padding in mind.
Does that make sense?

saving value of edittext in dynamic layout android

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

Categories

Resources