Edittext - copy values - android

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

Related

Alignment setting in Dynamic TableLayout

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.

OnClickListener on dynamic table layout

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

Textview text color not changing on click

I have an activity in which I have created a table dynamically. The table is populated from a SQLite database. Onclick the table header the table is getting sorted accordingly.
Everything is working fine, now my requirement is to change the text color of the table header textview when it is clicked. The text color is not getting changed.
This is the a method which is populating the table:
//Time ascending method
public void populateTableTimeAscending()
{
getTimeNameTimeAsc();
getStudentNameTimeAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_asc.iterator();
Iterator itr2 = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
Inside time_name_title.setOnClickListener(new OnClickListener() I have tried to change the text color which did not happen.
----------------Edit full code----------------------------------
public class TableActivity extends Activity {
TableLayout follow_up_table;
TableRow followup_tr_data;
List<String> time_name_list_time_asc;
List<String> student_name_list_time_asc;
List<String> time_name_list_time_dsc;
List<String> student_name_list_time_dsc;
List<String> time_name_list_studname_asc;
List<String> student_name_studname_asc;
List<String> time_name_list_studname_dsc;
List<String> student_name_list_studname_dsc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table);
follow_up_table=(TableLayout) findViewById(R.id.follow_up_table);
//getTime();
// database handler
deleteRows();
insertData("Morning", "2013-04-23 10:00:00", "Suresh Kumar");
insertData("Morning", "2013-04-23 11:30:00", "Pravat Das");
insertData("Evening", "2013-04-23 16:16:00", "Namita Roy");
insertData("Evening", "2013-04-23 17:30:00", "Rakesh Mitra");
insertData("After noon", "2013-04-23 14:27:00", "Anil Sarma");
// getTimeNameTimeAsc();
// getStudentNameTimeAsc();
// getTimeNameTimeDsc();
// getStudentNameTimeDsc();
// getTimeNameSyudentAsc();
// getStudentNameStudentAsc();
// getTimeNameStudentDsc();
// getStudentNameStudentDsc();
populateTableTimeAscending();
}
//==================================================================================//
//=============Functions for Table View=====================================================//
//==================================================================================//
//Time ascending method
public void populateTableTimeAscending()
{
getTimeNameTimeAsc();
getStudentNameTimeAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_asc.iterator();
Iterator itr2 = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Time descending method
public void populateTableTimeDescending()
{
getTimeNameTimeDsc();
getStudentNameTimeDsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click setOnClickListener---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
populateTableTimeAscending();
}
});
//----------------------On click setOnClickListener---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_dsc.iterator();
Iterator itr2 = student_name_list_time_dsc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Name ascending method
public void populateTableNameAscending()
{
getTimeNameSyudentAsc();
getStudentNameStudentAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameDescending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_studname_asc.iterator();
Iterator itr2 = student_name_studname_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Name descending method
public void populateTableNameDescending()
{
getTimeNameStudentDsc();
getStudentNameStudentDsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_studname_dsc.iterator();
Iterator itr2 = student_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//==================================================================================//
//=============Functions for Table View=====================================================//
//==================================================================================//
//==================================================================================//
//=============Functions for DB=====================================================//
//==================================================================================//
//=================Time name ascending=================================
public void getTimeNameTimeAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_time_asc = db.getTimeNameTimeAsc();
Iterator itr = time_name_list_time_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameTimeAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_time_asc = db.getStudentNameTimeAsc();
Iterator itr = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Time name ascending=================================
//=================Time name descending=================================
public void getTimeNameTimeDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_time_dsc = db.getTimeNameTimeDsc();
Iterator itr = time_name_list_time_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameTimeDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_time_dsc = db.getStudentNameTimeDsc();
Iterator itr = student_name_list_time_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Time name descending=================================
//=================Student name ascending=================================
public void getTimeNameSyudentAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_studname_asc = db.getTimeNameSyudentAsc();
Iterator itr = time_name_list_studname_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameStudentAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_studname_asc = db.getStudentNameStudentAsc();
Iterator itr = student_name_studname_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Student name ascending=================================
//=================Student name descending=================================
public void getTimeNameStudentDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_studname_dsc = db.getTimeNameStudentDsc();
Iterator itr = time_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameStudentDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_studname_dsc = db.getStudentNameStudentDsc();
Iterator itr = student_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Student name descending=================================
//method to insert data into local database
public void insertData(String timeName, String time, String studentName)
{
DatabaseHandler db = new DatabaseHandler(TableActivity.this);
ContentValues values = new ContentValues();
//db.createDataBase();
values.put("timeName",timeName);
values.put("time",time);
values.put("studentName",studentName);
db.insertValues(timeName, time, studentName);
db.close();
}
//method to delete all rows
public void deleteRows()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
db.deleteAllData();
db.close();
}
//==================================================================================//
//=============Functions for DB=====================================================//
//==================================================================================//
//method to show toast message
public void makeAToast(String str) {
//yet to implement
Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
You can also use this:
textview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
textview.setTextColor(Color.BLUE);
break;
case MotionEvent.ACTION_UP:
textview.setTextColor(Color.BLACk);
break;
}
return false;
}
});
Another way change text color on click is:
Add resource file:
XML file saved at res/color/button_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
And set as textColor for you widget:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:textColor="#color/button_text" />
For more dital look at google guide
Insure that you're trying to change text color in proper TextView and try to call setTextColor after populateTableTimeDescending();
final TextView tv = new TextView(this);
tv.setText("Sample TextView");
rl = (RelativeLayout) findViewById(R.id.rl);
rl.addView(tv);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
populateTableTimeDescending();//since you are setting the color of the TextView back to same(white) again in this method, set the color of the textView after calling the method.
tv.setTextColor(Color.parseColor("#FF0000")); //**edited**
}
});
You are trying to set the same color. Check it out.
Please change the color first in any of the method.
--------------edit-------------
then do textview.setColor(Color.BLUE); before populateTableTimeDescending(); in your onClick();
Here, after four digits, you can set whichever color you'd like using a hexadecimal color code:
time_name_title.setTextColor(0xffffffff);

tablelayout delete tablerow in android

final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TableRow row = new TableRow(this);
TextView t2 = new TextView(this);
t2.setText("test");
row.addView(t2);
Button bu = new Button(this);
bu.setBackgroundResource(R.drawable.del);
bu.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//I need to delete the tablerow
//how to do?
}
});
row.addView(bu);
table.addView(row, new TableLayout.LayoutParams(WC, WC));
**
i want to delete tablerow in bu.setOnClickListener
how to do removeViewAt() ,i cant find indexId
**
use removeView for removing tablerow as:
table.removeView(row);
NOTE: If they don't have unique id then use:
table.removeView(rowIndex);
and by using removeViewAt
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}

How to get list of product in memory in Android

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

Categories

Resources