How do I set margins for my Android app table? - java

I am trying a add rows to my table by pressing a button, I can add the rows but I don't know how to add layout to the added row.
Here is my code:
public void testRow(View view) {
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.accountTable);
// create a new TableRow
TableRow row = new TableRow(this);
TextView x = new TextView(this);
x.setText("text ");
row.addView(x);
TextView y = new TextView(this);
y.setText("text ");
row.addView(y);
TextView z = new TextView(this);
z.setText("text ");
row.addView(z);
// add the TableRow to the TableLayout
table.addView(row);
}

You should add TableRow to TableLayout with LayoutParams, then you can also set the margins with it:
LayoutParams params = table.generateDefaultLayoutParams();
params.leftMargin = ...
params.rightMargin = ...
table.addView(row, params);

Related

Why, Both radio buttons selected? i want single selection

I'm creating a radio question here I want select only one button to be selected. But here both buttons are got selected. kindly give me the solutions.
private void radioButtonQuestion(String radioquest, JsonArray optionsArray) {
RadioButton radio;
RadioGroup radioGroup;
JsonArray dependenciesArray = (JsonArray) indQuestions.get("dependencies");
ArrayList<String> list = new ArrayList<>();
for(int a=0; a<optionsArray.size();a++) {
JsonObject optionsObject = (JsonObject) optionsArray.get(a);
JsonObject dependenciesObject = (JsonObject) dependenciesArray.get(a);
String option = optionsObject.get("value").getAsString();
list.add(option);
}
LinearLayout ll=new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
cardview = new CardView(context);
layoutparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutparams.setMargins(10,20,10,20);
cardview.setLayoutParams(layoutparams);
cardview.setRadius(30);
cardview.setPadding(10, 10, 10, 10);
cardview.setCardBackgroundColor(Color.WHITE);
cardview.setMaxCardElevation(20);
cardview.setMaxCardElevation(6);
textview = new TextView(context);
// textview.setLayoutParams(layoutparams);
textview.setText(radioquest);
textview.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
textview.setTextColor(Color.BLACK);
textview.setPadding(15, 25, 25, 15);
textview.setGravity(Gravity.NO_GRAVITY);
ll.addView(textview);
for(int i = 0; i< list.size();i++) {
radio = new RadioButton(context);
radioGroup = new RadioGroup(context);
radio.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
radio.setPadding(15, 15, 15, 15);
radio.setId(i);
radio.setText(list.get(i));
radioGroup.addView(radio);
ll.addView(radioGroup);
}
ll.setId(id);
cardview.addView(ll);
cardview.setId(id);
cardview.setTag("Radio "+id);
id++;
relativeLayout.addView(cardview);
}
Your code created a different group for each individual radio button, that's why each can be selected at the same time. Each group can have one selected radio button. Instead, you should create just one group, and add all the buttons to that group.
Just place radioGroup before options like below
radioGroup = new RadioGroup(context);
for(int i = 0; i< list.size();i++) {
radio = new RadioButton(context);
radio.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
radio.setPadding(15, 15, 15, 15);
radio.setId(i);
radio.setText(list.get(i));
radioGroup.addView(radio);
ll.addView(radioGroup);
}
From the reference from your code, I am able to find the cause which produces this issue, You need to put this line(ll.addView(radioGroup)) out of for loop as below :
//...
for(int i = 0; i< list.size();i++) {
radio = new RadioButton(context);
radioGroup = new RadioGroup(context);
radio.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
radio.setPadding(15, 15, 15, 15);
radio.setId(i);
radio.setText(list.get(i));
radioGroup.addView(radio);
}
ll.addView(radioGroup);
//...
If this solution helps then please accept this answer.

Populate Tablelayout programatically through String Array

Hello fellow stackoverflowers,
i just wrote this code to populate a tablelayout inside my activity_customer layout. For some reason in runs through without throwing the slightest error, but at the end of "populateView" the application just stops/crashes.
I tried some many things I found via google/stackoverflow, but none seems to work. I hope someone can help me find why the app stops.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
populateView(DataClass.getReturnData());
}
private void populateView(String[] Array){
int len = Array.length;
TableLayout tab = (TableLayout) findViewById(R.id.table);
if (len != 0){
for (int i = 0; i <= len - 1; i++) {
TableRow row = new TableRow(this);
TextView tvName = new TextView(this);
tvName.setText("" + Array[i]);
System.out.println(Array[i]);
tab.addView(row);
}
}
}
Thanks in advance,
Soulrox
Here is a complete example:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6",
"Row 7"
};
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4",
"COLUMN5", "COLUMN6"
};
int rl=row.length;
int cl=column.length;
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount)
{
// 1) Create a tableLayout and its params
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for (int i = 0; i < rowCount; i++)
{
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
for (int j= 0; j < columnCount; j++)
{
// 4) create textView
TextView textView = new TextView(this);
// textView.setText(String.valueOf(j));
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
Log.d("TAG", "-___>"+id);
if (i ==0 && j==0)
{
textView.setText("0==0");
}
else if(i==0)
{
Log.d("TAAG", "set Column Headers");
textView.setText(cv[j-1]);
}
else if( j==0)
{
Log.d("TAAG", "Set Row Headers");
textView.setText(rv[i-1]);
}
else
{
textView.setText(""+id);
// check id=23
if(id==23)
{
textView.setText("ID=23");
}
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
}
Output:

android Table layout wiht table read rows

I have table row inside table layout, I need to read all row and move data to sql. How i read table by row next row need to read row one and go to row 2 and read
code for insert into table row :
TableLayout l1 = (TableLayout) findViewById(R.id.table1);
l1.setStretchAllColumns(true);
l1.bringToFront();
TableRow tr0 = new TableRow(getBaseContext());
TextView tv1 = new TextView(getBaseContext());
TextView tv2 = new TextView(getBaseContext());
TextView tv3 = new TextView(getBaseContext());
TextView tv4 = new TextView(getBaseContext());
TextView tv5 = new TextView(getBaseContext());
tv1.setTextColor(Color.BLACK);
tv2.setTextColor(Color.BLACK);
tv3.setTextColor(Color.BLACK);
tv4.setTextColor(Color.BLACK);
tv5.setTextColor(Color.BLACK);
tv1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv2.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv3.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv4.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv5.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv1.setText(Integer.toString(id_row=id_row+1));
tv2.setText(e_entrbond_calcname.getText().toString());
tv3.setText(e_entrybonds_mdin1.getText().toString());
tv4.setText(e_entrybonds_dain1.getText().toString());
tv5.setText(e_entrybonds_details2.getText().toString());
tr0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr0.addView(tv1);
tr0.addView(tv2);
tr0.addView(tv3);
tr0.addView(tv4);
tr0.addView(tv5);
l1.addView(tr0);
try this code for Reading values from TableRow;
TableLayout l1 = (TableLayout) findViewById(R.id.table1);
for (int i = 0; i < l1.getChildCount(); i++) {
View child = l1.getChildAt(i);
if (child instanceof TableRow) {
TableRow row = (TableRow) child;
for (int x = 0; x < row.getChildCount(); x++) {
//View view = row.getChildAt(x);
TextView text = (TextView)row.getChildAt(x); // get child index on particular row
String title = text.getText().toString();
Log.i("Value", title);
}
}
}

radio group not working and vertical alignment android

JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
LinearLayout lll = new LinearLayout(Questionnaire.this);
lll.setOrientation(LinearLayout.VERTICAL);
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
lll.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
lll.addView(group);
}
}
}
ll.addView(lll);
}
This is my code for dynamically adding radio button.
Problem is when I select one option then select anther it does not deselect the previous one. Why doesn't it deselect the the first one selecting another when they are both added to same radio group. Also it is not placing the radio buttons next to each other but on top of each other when the orientation is set to horizontal
UPDATE
I moved my declaration of
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
out side parent for loop now i am getting
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
How should i deal with it?
you are added only one RadioButton in RadioGroup that's why you got many selected radio button. RadioGroup need minimum 2 RadioButton for check or Uncheck effect.
try this,I hope it may work.
JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
LinearLayout lll = new LinearLayout(Questionnaire.this);
lll.setOrientation(LinearLayout.VERTICAL);
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
lll.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
}
}
}
lll.addView(group);
ll.addView(lll);
}
try like this i have make code as you want.
ScrollView scrollView = (ScrollView) findViewById(R.id.templayout);
LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(mLinearLayout);
JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
mLinearLayout.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
}
}
}
mLinearLayout.addView(group);
}
}

Dynamically added views do not reset?

I have a LinearLayout in which I am adding some CheckBoxes dynamically, they are adding perfectly well on first call, when I am calling the same method with different values, then they are not appearing in that LinearLayout, old values are not getting replaced with new values.
I checked values in LogCat and they seem fine, whatever I'm passing is being displayed in LogCat but not appearing in LinearLayout.
Here's my method that adds the view:
private void setOptions(int questionID2) {
ArrayList<String> options = pustakDB.getOptions(questionID2);
Log.e("optionsInAct", options.toString() + " size " + options.size());
OptionView = new LinearLayout(this);
OptionView.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, img.getId());
p.addRule(RelativeLayout.RIGHT_OF, questionNumber.getId());
rel.addView(OptionView, p);
int i = 0;
while (i < options.size()) {
CheckBox c = new CheckBox(this);
c.setText(options.get(i));
c.setId(i);
OptionView.addView(c);
i++;
}
}
Edit: "rel" here is RelativeLayout.
// put this somewhere else
OptionView = new LinearLayout(this);
OptionView.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, img.getId());
p.addRule(RelativeLayout.RIGHT_OF, questionNumber.getId());
rel.addView(OptionView, p);
private void setOptions(int questionID2) {
ArrayList<String> options = pustakDB.getOptions(questionID2);
Log.e("optionsInAct", options.toString() + " size " + options.size());
// this will remove all views from OptionView
OptionView.removeAllViews();
int i = 0;
while (i < options.size()) {
CheckBox c = new CheckBox(this);
c.setText(options.get(i));
c.setId(i);
OptionView.addView(c);
i++;
}
}
Try this and see if it works.

Categories