I want to add x rows when user press the add button and in one of the row user enters no of types i.e. in edittext then according to that i need to populate that many no.of rows in two columns below that .. these columns alignment is missing..
LAYOUT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add requirefields" />
<ScrollView
android:id="#+id/scrolllayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/button1"
android:layout_marginLeft="10dp" >
<TableLayout
android:id="#+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</RelativeLayout>
ACTIVITY:-
public class MainActivity extends Activity {
Button addBtn;
TableLayout mTable;
TableLayout timingsTable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBtn = (Button) findViewById(R.id.button1);
mTable = (TableLayout) findViewById(R.id.tablelayout);
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "btnonclicklistener",
Toast.LENGTH_LONG).show();
createTable();
}
});
}
public void createTable() {
// mTable.setColumnShrinkable(1, true);
mTable.setStretchAllColumns(true);
mTable.setShrinkAllColumns(true);
TableRow rowMName = new TableRow(MainActivity.this);
TableRow rowDescription = new TableRow(MainActivity.this);
TableRow rowType = new TableRow(MainActivity.this);
TableRow rowTimesADay = new TableRow(MainActivity.this);
TableRow rowTimings = new TableRow(MainActivity.this);
TableRow rowNoofTimings = new TableRow(MainActivity.this);
TextView mNametv = new TextView(MainActivity.this);
mNametv.setText("MNAME");
TextView descriptiontv = new TextView(MainActivity.this);
descriptiontv.setText("Description");
TextView typetv = new TextView(MainActivity.this);
typetv.setText("TYPE");
TextView timesAdaytv = new TextView(MainActivity.this);
timesAdaytv.setText("TimesADay");
TextView timingstv = new TextView(MainActivity.this);
timingstv.setText("Timings");
rowMName.addView(mNametv);
rowDescription.addView(descriptiontv);
rowType.addView(typetv);
rowTimesADay.addView(timesAdaytv);
rowTimings.addView(timingstv);
EditText etMName = new EditText(MainActivity.this);
EditText etDescription = new EditText(MainActivity.this);
EditText etType = new EditText(MainActivity.this);
final EditText etTimesAday = new EditText(MainActivity.this);
// EditText etTimings = new EditText(MainActivity.this);
timingsTable = new TableLayout(MainActivity.this);
TextView dosagetv = new TextView(MainActivity.this);
dosagetv.setText("Dosage");
rowMName.addView(etMName);
rowDescription.addView(etDescription);
rowType.addView(etType);
rowTimesADay.addView(etTimesAday);
rowNoofTimings.addView(timingsTable);
rowTimings.addView(dosagetv);
etTimesAday.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (!s.toString().equals("")) {
int length = Integer.parseInt(s.toString());
if (length >= 1) {
timingsTable.removeAllViews();
timingsTable.setVisibility(View.VISIBLE);
dynamicEditBox(etTimesAday.getText().toString());
Toast.makeText(getBaseContext(), "if condition",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getBaseContext(), "else condition",
Toast.LENGTH_SHORT).show();
timingsTable.setVisibility(View.GONE);
timingsTable.removeAllViews();
if (s.equals("")) {
etTimesAday.setText("");
}
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
mTable.addView(rowMName);
mTable.addView(rowDescription);
mTable.addView(rowType);
mTable.addView(rowTimesADay);
mTable.addView(rowTimings);
mTable.addView(rowNoofTimings);
}
public void dynamicEditBox(String id) {
timingsTable.setStretchAllColumns(true);
timingsTable.setShrinkAllColumns(true);
for (int i = 0; i < Integer.parseInt(id); i++) {
TableRow tr = new TableRow(MainActivity.this);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams parms = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TextView ed = new TextView(getApplication());
ed.setInputType(InputType.TYPE_CLASS_NUMBER);
ed.setLayoutParams(parms);
ed.setId(i);
ed.setHint("enter number");
ed.setBackgroundColor(Color.DKGRAY);
tr.addView(ed);
EditText b = new EditText(getApplication());
b.setLayoutParams(new TableRow.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT));
b.setId(i);
b.setBackgroundColor(Color.LTGRAY);
b.setText("dosage");
tr.addView(b);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 0);
timingsTable.setLayoutParams(layoutParams);
timingsTable.addView(tr);
}
}
}
Currently i'm getting as follows..
I need to adjust those timings and dosage columns accordingly.help me in setting this alignment..
After making below changes in the dynamicEditBox method i got the alignment
public void dynamicEditBox(String id) {
timingsTable.setStretchAllColumns(true);
timingsTable.setShrinkAllColumns(true);
for (int i = 0; i < Integer.parseInt(id); i++) {
TableRow tr = new TableRow(MainActivity.this);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams parms = new TableRow.LayoutParams(0,
TableRow.LayoutParams.WRAP_CONTENT, 1f);
parms.setMargins(5, 5, 5, 5);
TextView tv = new TextView(getApplication());
tv.setLayoutParams(parms);
tv.setWidth(50);
tv.setPadding(20, 5, 5, 5);
tv.setId(i);
tv.setHint("10:25am");
tv.setBackgroundColor(Color.LTGRAY);
tr.addView(tv);
EditText et = new EditText(getApplication());
et.setLayoutParams(parms);
et.setPadding(5, 5, 5, 5);
et.setGravity(Gravity.CENTER);
et.setRawInputType(Configuration.KEYBOARD_12KEY);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.setId(i);
et.setBackgroundColor(Color.LTGRAY);
et.setHint("dosage");
tr.addView(et);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(0,
TableRow.LayoutParams.WRAP_CONTENT, 1f);
layoutParams.setMargins(-15, 10, 0, 0);
timingsTable.setLayoutParams(layoutParams);
timingsTable.addView(tr);
}
}
i got in the below way.
Related
Hi In My Application Fetching the data from database displaying in table layout in android.
Now I have One submit button if I click submit button I want to display the data from database displaying into my layout.And I want Match with class and section based on I want to display the data.
Can Anyone please help me.
class file
public class Attend extends Activity {
String data = "";
TableLayout tl;
TableRow tr;
TextView label;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.maintable);
final DataseData getdb = new DataseData();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayList<Users> users = parseJSON(data);
addData(users);
}
});
}
}).start();
}
public ArrayList<Users> parseJSON(String result) {
ArrayList<Users> users = new ArrayList<Users>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setClassname(json_data.getString("classname"));
user.setSectionname(json_data.getString("sectionname"));
//user.setDate(json_data.getString("date"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader(){
tr = new TableRow(this);
label = new TextView(this);
label.setText("classname");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll);
TextView sectionname = new TextView(this);
sectionname.setText("sectionname");
sectionname.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
sectionname.setPadding(5, 5, 5, 5);
sectionname.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
Ll.addView(sectionname,params);
tr.addView((View)Ll);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/*TextView date = new TextView(this);
date.setText("date");
date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
date.setPadding(5, 5, 5, 5);
date.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
Ll.addView(date,params);
tr.addView((View)Ll);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));*/
}
#SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users> users) {
addHeader();
for (Iterator i = users.iterator(); i.hasNext();) {
Users p = (Users) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getClassname());
label.setText(p.getSectionname());
//label.setText(p.getDate());
label.setId(p.getId());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
Ll.addView(label,params);
tr.addView((View)Ll);
TextView classname = new TextView(this);
classname.setText(p.getClassname());
classname.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
classname.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(classname,params);
tr.addView((View)Ll);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView sectionname = new TextView(this);
sectionname.setText(p.getClassname());
sectionname.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
sectionname.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 3, 3);
Ll.addView(sectionname,params);
tr.addView((View)Ll);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView date = new TextView(this);
date.setText(p.getClassname());
date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
date.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(classname,params);
tr.addView((View)Ll);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
MainActivity.lass
public class AndroidSpinnerExampleActivity extends Activity implements OnClickListener{
private EditText fromDateEtxt;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Button submit=(Button)findViewById(R.id.button1);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),Attend.class);
startActivity(intent);
}
});
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
findViewsById();
setDateTimeField();
// Spinner click listener
//spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> classnames = new ArrayList<String>();
classnames.add("Select");
classnames.add("Play Group");
classnames.add("Nursery");
classnames.add("Lkg");
classnames.add("Ukg");
classnames.add("Fifth");
classnames.add("Sixth");
List<String> sectionnames = new ArrayList<String>();
sectionnames.add("Select");
sectionnames.add("Batch A");
sectionnames.add("Batch B");
sectionnames.add("Batch C");
sectionnames.add("Batch D");
sectionnames.add("Batch E");
sectionnames.add("Batch F");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, classnames);
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, sectionnames);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner1.setAdapter(dataAdapter1);
}
private void findViewsById() {
fromDateEtxt = (EditText) findViewById(R.id.etxt_fromdate);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();
//toDateEtxt = (EditText) findViewById(R.id.etxt_todate);
//toDateEtxt.setInputType(InputType.TYPE_NULL);
}
private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
//toDateEtxt.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClick(View view) {
if(view == fromDateEtxt) {
fromDatePickerDialog.show();
}
}
I want to add onClicklistener to the items from the dynamic table that is generated.
My Code is
for(int k=0;k<i;k++)
{
tr[k]=new TableRow(getApplicationContext());
tr[k].layout(0, 0, 0, 0);
ids[k] = new TextView(getApplicationContext());
ids[k].setText(loc_id[k]);
ids[k].setPadding(30, 15, 30, 15);
loc[k] = new TextView(getApplicationContext());
loc[k].setText(loc_name[k]);
loc[k].setPadding(30, 15, 30 ,15);
tr[k].setPadding(0, 1, 0, 0);
tr[k].addView(ids[k]);
tr[k].addView(loc[k]);
tl.addView(tr[k], new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
Please help.
You need to add OnClickListner Interface to your activity and then add all dynamic view to setOnClickListner and finally you can catch click event for all view inside onClick(View view) method.
Try this
public class MainScreen extends Activity implements OnClickListener {
int i = 10; // input no of row
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // set here your layout xml name
//TableLayout tl = new TableLayout(MainScreen.this);
TableLayout tl = (TableLayout) findViewById(R.id.table);
for (int k = 0; k < i; k++) {
TableRow tr = new TableRow(MainScreen.this);
tr.layout(0, 0, 0, 0);
TextView ids = new TextView(MainScreen.this);
ids.setText(loc_id[k]);
ids.setPadding(30, 15, 30, 15);
TextView loc = new TextView(MainScreen.this);
loc.setText(loc_name[k]);
loc.setPadding(30, 15, 30, 15);
tr.setPadding(0, 1, 0, 0);
tr.addView(ids);
tr.addView(loc);
tr.setId(k); // here you can set unique id to TableRow for
// identification
tr.setOnClickListener(MainScreen.this); // set TableRow onClickListner
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
//setContentView(tl);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int clicked_id = v.getId(); // here you get id for clicked TableRow
// now you can get value like this
String ids = loc_id[clicked_id];
String loc = loc_name[clicked_id];
}
}
i made a program and in my program, i have an editText. How can i get the value of my edittext...please help me i can't get excellent.getText().toString();.
Is there a remedy to get the value of my edittext? Help me please thank you in advance..
here are my codes..
TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
TableRow tableRow = new TableRow(getApplicationContext());
TextView txt = new TextView(getApplicationContext());
tableRow.addView(txt);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Excellent ");
table.addView(tableRow);
int j = 0;
for (j = 1; j <= count; j++) {
TableRow tableRow2 = new TableRow(getApplicationContext());
EditText excellent = new EditText(getApplicationContext());
tableRow2.addView(excellent);
}
TableRow tableRow1 = new TableRow(getApplicationContext());
Button showtable = new Button(getApplicationContext());
tableRow1.addView(showtable);
showtable.setText("Show Table");
showtable.setTextSize(8);
showtable.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
excellent.getText().toString();// i cant get the value of
// excellent here!
}
});
create the excellent reference in outside of the method.
TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
EditText excellent;
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Excellent ");
table.addView(tableRow);
int j=0;
for(j = 1; j<=count; j++){
TableRow tableRow2 = new TableRow (getApplicationContext());
excelent = new EditText (getApplicationContext());
tableRow2.addView(excellent);
}
TableRow tableRow1 = new TableRow (getApplicationContext());
Button showtable = new Button(getApplicationContext());
tableRow1.addView(showtable);
showtable.setText("Show Table");
showtable.setTextSize(8);
showtable.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
excellent.getText().toString();//i cant get the value of excellent.
}
});
TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
EditText excellent = new EditText (getApplicationContext());
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Excellent ");
table.addView(tableRow);
int j=0;
for(j = 1; j<=count; j++){
TableRow tableRow2 = new TableRow (getApplicationContext());
tableRow2.addView(excellent);
}
TableRow tableRow1 = new TableRow (getApplicationContext());
Button showtable = new Button(getApplicationContext());
tableRow1.addView(showtable);
showtable.setText("Show Table");
showtable.setTextSize(8);
showtable.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
excellent.getText().toString();//i cant get the value of excellent here!
}
});
I used the code below to create a TableRow with content dynamically. It works good but I wish to get the values in the TableRow. Here is the sample code (I got it from Google), it has two text values in the TableRow. When I click the TableRow at any position it gives the corresponding value in the TableRow (I wish something similar to a ListView).
All_CustomsActivity.java
public class All_CustomsActivity extends Activity {
String companies[] = { "Google", "Windows", "iPhone", "Nokia", "Samsung",
"Google", "Windows", "iPhone", "Nokia", "Samsung", "Google",
"Windows", "iPhone", "Nokia", "Samsung" };
String os[] = { "Android", "Mango", "iOS", "Symbian", "Bada", "Android",
"Mango", "iOS", "Symbian", "Bada", "Android", "Mango", "iOS",
"Symbian", "Bada" };
TableLayout tl;
TableRow tr;
TableRow mTable = null;
TextView companyTV, valueTV;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tl = (TableLayout) findViewById(R.id.maintable);
// addHeaders();
addData();
}
/** This function add the headers to the table **/
public void addHeaders() {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
TextView companyTV = new TextView(this);
companyTV.setText("Companies");
companyTV.setTextColor(Color.GRAY);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 0);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
TextView valueTV = new TextView(this);
valueTV.setText("Operating Systems");
valueTV.setTextColor(Color.GRAY);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 0);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// we are adding two textviews for the divider because we have two
// columns
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating another textview **/
TextView divider = new TextView(this);
divider.setText("-----------------");
divider.setTextColor(Color.GREEN);
divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
divider.setPadding(5, 0, 0, 0);
divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider); // Adding textView to tablerow.
TextView divider2 = new TextView(this);
divider2.setText("-------------------------");
divider2.setTextColor(Color.GREEN);
divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
divider2.setPadding(5, 0, 0, 0);
divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider2); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
/** This function add the data to the table **/
public void addData() {
for (int i = 0; i < companies.length; i++) {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// ImageView im = new ImageView(this);
// im.setBackgroundResource(R.drawable.sample_image);
// im.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
// LayoutParams.WRAP_CONTENT));
// tr.addView(im);
/** Creating a TextView to add to the row **/
companyTV = new TextView(this);
companyTV.setText(companies[i]);
companyTV.setTextColor(Color.RED);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 5);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
valueTV = new TextView(this);
valueTV.setText(os[i]);
valueTV.setTextColor(Color.GREEN);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 5);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// tr.setOnClickListener(new View.OnClickListener() {
// public void onClick(View view) {
// view.setBackgroundColor(Color.DKGRAY);
// }
// });
//
// tr.setOnLongClickListener(new View.OnLongClickListener() {
// public boolean onLongClick(View v) {
// mTable = (TableRow) v; // assign selected TableRow gobally
// openContextMenu(v);
// return true;
// }
// });
}
}
// #Override
// public void onCreateContextMenu(ContextMenu menu, View v,
// ContextMenu.ContextMenuInfo menuInfo) {
// super.onCreateContextMenu(menu, v, menuInfo);
// menu.add(0, v.getId(), 0, "Do YourStuff");
//
// }
//
// #Override
// public boolean onContextItemSelected(MenuItem item) {
// int ccount = (mTable).getChildCount();
// String[] str = new String[ccount];
// for (int i = 0; i < ccount; i++) {
// TextView tv = (TextView) (((TableRow) mTable)).getChildAt(i);
// str[i] = tv.getText().toString(); // set selected text data into the
// // String array
// }
// Toast.makeText(All_CustomsActivity.this, Arrays.toString(str), 2)
// .show();
// return true;
// }
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none" >
<TableLayout
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1" >
</TableLayout>
</ScrollView>
</LinearLayout>
In the code above some lines are commented, these lines are what I already tried to get the row values but it failed. Can anyone help me with this?
I guess you're talking about getting those values on a TableRow click. If this is the case you could add a listener to your TableRow and use getChildAt to get a hold of the two TextViews and get the data:
//...
tr.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow t = (TableRow) view;
TextView firstTextView = (TextView) t.getChildAt(0);
TextView secondTextView = (TextView) t.getChildAt(1);
String firstText = firstTextView.getText().toString();
String secondText = secondTextView.getText().toString();
}
});
//...
To get data from textview first u need to identify its parent from that u need to get your textView child position then u can get data from it... Here is a sample
String myText;
((TextView) findViewById(R.id.tv)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TableRow tablerow = (TableRow)v.getParent();
TextView items = (TextView) tablerow.getChildAt(2);
myText = items.getText().toString();
}
});
spinnerSelection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TableRow selectedRow = (TableRow)spinnerSelection.getParent();
int index = tableLayout.indexOfChild(selectedRow);
Here index is the selected row index value and you can call all the views under the parent row (selectedRow) like the below set of lines.
Button roomNo = (Button) selectedRow.getChildAt(0);
Spinner selectionA = (Spinner) selectedRow.getChildAt(1);
Spinner selectionB = (Spinner) selectedRow.getChildAt(2);
Happy Coding!!
I have list of product.That may be contain 5000 records.I put pagination.First It load 50 records.Then if we press next option(>) then load next 50 option likewise.I designed table-layout.
ArrayList<Product> productList = new ArrayList<Product>();
productList = getProducts();
TableLayout tl;
tl = (TableLayout) findViewById(R.id.tableLayout1);
if(productList.size() >0){
for (int i = 0; i < productList.size(); i++) {
TableRow tr = new TableRow(this);
tr.setTag(i);
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=10;
int topMargin=2;
int rightMargin=10;
int bottomMargin=2;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
TextView txtCode = new TextView(this);
TextView txtDes = new TextView(this);
EditText txtQty = new EditText(this);
TextView txtVal = new TextView(this);
TextView txtDisVal = new TextView(this);
TextView txtDisQty = new TextView(this);
txtQty.setId(i);
txtQty.setFocusable(true);
txtQty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txtQty.setHeight(5);
txtQty.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
createView(tr, txtCode, productList.get(i).getProductCode());
createView(tr, txtDes, productList.get(i).getDescription());
txtQty.setText(Double.toString(productList.get(i).getPrice()));
tr.addView(txtQty);
createView(tr, txtVal,"Value");
createView(tr, txtDisVal,"0.00");
createView(tr, txtDisQty,"0");
tr.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
System.out.println("Row Clicked with tag " + view.getTag());
}
});
tl.addView(tr);
}
}
}
public void createView(TableRow tr, TextView t, String viewdata) {
t.setText(viewdata);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t.setTextColor(Color.DKGRAY);
t.setPadding(1, 0, 0, 0);
tr.setPadding(0, 0, 0, 0);
tr.addView(t);
}
This is my implementation.I didn't implement pagination part yet.
How can I get it into the memory and load it?
Please help me
I suggest you create a function to get products with a param pageindex。
Then you can set a trigger which activated by pagination event。
And update you UI by a handler