How to create dynamic horizontal table row in android? - java

My current code is:
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String b = json_data.getString("examnames");
JSONObject getexamnamesobject = new JSONObject(b);
getexamnames=getexamnamesobject.getString("types");
TableRow tr = new TableRow(this);
TextView tv1 = new TextView(this);
createView(tr,tv1,getexamnames);
t1.addView(tr);
}
public void createView(TableRow tr, TextView t, String viewdata) {
t.setText(viewdata);
//adjust the porperties of the textView
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//t.setTextColor(Color.DKGRAY);
//t.setBackgroundColor(Color.CYAN);
t.setPadding(20, 0, 0, 0);
tr.setPadding(0, 1, 0, 1);
//tr.setBackgroundColor(Color.BLACK);
tr.addView(t); // add TextView to row.
}
My output for above code is, I'm getting textviews vertically like this:
English
Maths
Science
What must be the change in the above coding to get the horizontal output like this:
English Maths Science
Please help me. This very important for me
Thanks in advance

Set setStretchAllColumns() and setShrinkAllColumns() to the table layout. and make it orientation "horizontal" for the table layout.

You are creating a TableRow and adding it to your TableLayout once for each item in jArray. Your createView method adds one string to that row: the value for the "types" key on the json object from the corresponding item in jArray (getexamnamesobject.getString("types")).
You might have to just loop through and collect all the "types" strings with a StringBuilder and put them in a single TableRow.

Your new LayoutParams should be new TableRow.LayoutParams. Views with "naked" LayoutParams could not be stacked one after another.

Related

Looping through specific widgets

Suppose I have a Table layout and i have a button called 'add people' which dynamically adds a row with two EdiTexts in it each time I click it. now there is another button which is supposed to save the first editText values in database and Add(sum) all the values of second editText .in HTML is very straight forward I can simply add class attribute to both inputs and loop through each class name. but I have no idea how i am supposed to do it in android.
What about saving the references in two lists? Then you can simply iterate over the lists to either store the values in a db or to sum them.
original answer check
public void init(){
TableLayout tablelayout = (TableLayout) findViewById(R.id.displayLinear);
for (int i = 0; i <2; i++) {
//Dynamic Button
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}

aligning dynamic buttons in gridlayout

I am trying to align dynamic buttons in Grid layout.
Problem:
However, if text is more than one line the formation of buttons in row
is not as needed.
String item[] = new String[]{"item1", "item2", "item3 yes",
"item4","item5"};
Button[] myButton = new Button[item.length];
GridLayout scrViewButLay = (GridLayout) findViewById(R.id.grid);
scrViewButLay.setColumnCount(4);
scrViewButLay.setRowCount(10);
// GridLayout.LayoutParams leftMarginParams = new GridLayout.LayoutParams(
// );
for (int index = 0; index < item.length ; index++) {
myButton[index] = new Button(this); //initialize the button here
myButton[index].setText(item[index]);
myButton[index].setHeight(100);
myButton[index].setWidth(100);
myButton[index].setTag(index);
scrViewButLay.addView(myButton[index]);
}
}
`
I recommend to use an Adapter and then customize however you want
check out this example
http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76
I think using a recyclerView with GridLayoutManager should help you out.
This post https://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/ should help you out.
You only need to apply a layout_gravity as the default seems to be to align the text to be the same height (this is why it only happens with the item 3 with two lines).
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = GridLayout.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.WRAP_CONTENT;
param.setGravity(Gravity.BOTTOM);

Is it possible to convert a table row into string in Android?

I am developing my first Android app. I have a table layout in my app, which will add rows dynamically. But, I want to convert the table rows into string. I tried "row.toString()" but I am not getting the expected result. Please find the following code snippet:
View view = null;
for(i = 0, j = tblayout.getChildCount(); i < j; i++)
{
view = tblayout.getChildAt(i);
TableRow rw = (TableRow)tblayout.getChildAt(i);
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
int colorId = viewColor.getColor();
if(colorId == Color.parseColor("#E143ED23")) {
num ++;
//temCnt = view.toString();
temCnt = rw.toString();
}
}
Toast.makeText(getApplicationContext(),temCnt, Toast.LENGTH_LONG).show();
But it is not showing the actual table row data. It is showing some ids' or garbage value.
My requirement is:
I have 2 text boxes and 2 buttons. When the first button "Ok" is pressed the value inside the text boxes will be populated into the Table layout along with a checkbox. By default the row will be checked. But user can uncheck each items. After that, when the second button "Save All" is pressed, then it will search for all the table rows which are checked and it should insert the table rows into the my MS SQL table.
Is there any way for converting the table row into a string in Java? I really trapped on this. It would be very helpful if someone can help me on this.
Please help...
toString() is a very basic method in Java, C# and other programming languages. Java's Object class has a basic implementation for it and you can override this method in any class that inherits from Object (that is, every class whatsoever :)). I don't know if those that developed the TableRow class of Android have overridden this method. It could be that what you're getting is some unreadable, meaningless representation of a TableRow object.
Let's say you have a TextView called txtName within the TableRow, followed by a second TextView called txtNumber. You can get what you want by doing the following:
...
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
int colorId = viewColor.getColor();
if(colorId == Color.parseColor("#E143ED23")) {
num ++;
//temCnt = view.toString();
TextView nameField = (TextView) rw.findViewById(R.id.txtName);
TextView numberField = (TextView) rw.findViewById(R.id.txtNumber);
temCnt = nameField.getText().toString() + ": " + numberField.getText().toString();
}
...

How To Solve Editext in More Numbers in a Layout And Get Values From That?

See this picture:
I am designing a sudoku in android, have done the layout like this, so 81 Editext is there. I want to get all the values in the Editext so its difficult to declare and mange 81 views in activity. Can you suggest me a method which is effective for processing my problem?
Create edittexts dynamically.
In every iteration you are rewriting the ed variable, so when loop is finished ed only points to the last EditText instance you created.
You should store all references to all EditTexts:
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < count; i++) {
ed = new EditText(Activity2.this);
allEds.add(ed);
ed.setBackgroundResource(R.color.blackOpacity);
ed.setId(id);
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linear.addView(ed);
}
Now allEds list hold references to all EditTexts, so you can iterate it and get all the data.
getting data
String[] strings = new String[](allEds.size());
for(int i=0; i < allEds.size(); i++){
string[i] = allEds.get(i).getText().toString();
}
NOTE:
This is not results your design there may be some changes.but follow these procedure to get exact results. I think you must you use two for loops to get your edittexts in a gridview.
I would suggest you to use array of Edittext's like this
EditText text1,text2,text3,text4,text5,text6,text7,text8,text9, text21,text22,text23,text24....text 99;
EditText[] fields={text1,text2,text3,text4,text5,text6,text7,text8,text9, text21,text22,text23,text24....text 99};
//Give your edittext ids in ids array like this
int ids[]={R.id.text1,R.id.text2,R.id.text3.....R.id,text99};
int values[9][9]={};
for(int i=0;i<fiends.length;i++)
{
fields[i]=(EditText)findViewById(ids[i]);
}
To get values use the same way take an array and save those values in array like this.
use for loop and get the values from the textfields and save it in the 2d array as i have declared in values array. I suppose you know how to code the rest of the thing you really want to know please comment, I'll post the rest.thank u.. I hope this will help you.
You can try out this way:
public static ArrayList<EditText> m_arr_edit= = new ArrayList<EditText>();
RelativeLayout p_rl_layout=new RelativeLayout(m_context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout m_ll_timelayout = new LinearLayout(m_context);
m_ll_timelayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int i = 0; i < p_intervals; i++)
{
EditText m_edit = new EditText(m_context);
m_edit.setLayoutParams(new LinearLayout.LayoutParams(90, LayoutParams.WRAP_CONTENT));
m_edit.setText("Set Text");
m_edit.setId(i);
m_arr_edit.add(m_btn);
m_ll_timelayout.addView(m_btn);
}
layoutParams.addRule(RelativeLayout.BELOW);
p_rl_layout.addView(m_ll_timelayout, layoutParams);
I hope it will help you.

How to fix my TableLayout and TableRow into specific positions dynamically?

I'm having problems setting up my TableRow with some TextViews in TableLayout dynamically. I have two pictures, one is my current situation shown, and the other is my mockup, expected situation (the goal which I need to achieve). I do not have any XML layouts; All of these are created programmatically, which is also something I need to achieve.
The snapshots shown are for a High Score screen, where I get a list of players with high scores, and display them altogether in a TableLayout. I'm just having trouble with the row/column positions.
Here's my code:
public void onResume() {
super.onResume();
//=============================================
TextView number = new TextView(this);
number.setText("1");
TextView place = new TextView(this);
place.setText("4th");
TextView testScore = new TextView(this);
testScore.setText("113489");
table = new TableLayout(this);
//rows = new Stack<TableRow>();
TableRow row = new TableRow(this);
row.addView(number);
row.addView(place);
row.addView(testScore);
table.addView(row);
this.setContentView(table);
}
If anyone knows how I should change my code from the Current Situation to the mockup Expected Situation, I gladly appreciate it. If I'm doing something wrong, please post a comment. Thanks in advance.
Set the layout weight of each textview to 1 and the layout width to 0dp. That will cause them to split the available space equally. You could use layout gravity left, center and right as appropriate to further ensure maximum separation.
I just found a good link about TableLayout and TableRow, creating specific positions dynamically. Link is here. Of course, the code here is just the barebones of it, used to demostrate a simple position layout.
Here's my code and the given picture of the result:
public void onResume() {
super.onResume();
//=============================================
dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 1, this.getResources().getDisplayMetrics());
if (dip <= 0)
dip = 1;
TextView number = new TextView(this);
TextView place = new TextView(this);
TextView testScore = new TextView(this);
number.setText("1");
place.setText("4th");
testScore.setText("113489");
number.setWidth(50 * dip);
place.setWidth(75 * dip);
testScore.setWidth(150 * dip);
number.setPadding(20*dip, 0, 0, 0);
table = new TableLayout(this);
TableRow row = new TableRow(this);
row.addView(number);
row.addView(place);
row.addView(testScore);
table.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
this.setContentView(table);
}
I'm actually astonished that there's a method for obtaining the DIP unit. Should've searched for that in the first place. :P

Categories