How to identify a disabled switch in android - android

I am developing an android project where I am struck in a case, where I have to identify (ie.. to get some information about the 'disabled switch') so that I can use that particular disabled switch's information to change the database through PHP query. Please let me know your valuable suggestions.
Here is my code where I declared the switch -
mySwitch = new Switch[len];
flag = new int[len];
pnr = new String[len];
id = 0;
for( j = 0; j < len; j++ ) {
newlist = passenger[j].split("#");
pnr[j] = newlist[0];
hrow = new TableRow(this); //create row for each passenger
hrow.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
hrow.setPadding(0, 0, 0, 3);
Thanks

What your looking for is isClickable() (according to your comment)
http://developer.android.com/reference/android/view/View.html#isClickable()

Related

how to set properties and actionEvent on EditText on androud

I passed a EditText view to a table row addView() method. like this:
myTablerow.addView(new EditText (this))
Q1: How do I set properties like size, colour for the EditText etc
Q2: How do I get the input value from the EditText?
Full code from the author: (P.s. should update in the question instead of my answer)
(It looks like your post is mostly code; please add some more details.)
if (cursor.getCount() > 0) {
// looping through all rows and adding to list
for (int i = 0; i < numberOfRows; i++)
{
TableRow tr = new TableRow(this);
TableRow trheader = new TableRow(this);
if (i == 0)
trheader.setBackgroundColor(Color.parseColor("#344C58"));
else
tr.setBackgroundColor(Color.parseColor("#203F50"));
for (int j = 0; j < numberOfColumns; j++)
{
txtGeneric = new TextView(this);
TextView txtGenericHeader = new TextView(this);
//txtGenericHeader.setGravity(Gravity.LEFT);
if (i == 0) {
txtGeneric.setTextColor(Color.parseColor("#ffffff"));
for (int z = 0; z < numberOfColumns; z++) {
txtGenericHeader.setTextSize(12);
txtGenericHeader.setAllCaps(false);
//txtGeneric.setBackgroundColor(Color.parseColor("#768F9E"));
txtGenericHeader.setTextColor(Color.parseColor("#768F9E"));
txtGenericHeader.setText(cursor.getColumnName(j));
txtGenericHeader.setGravity(Gravity.CENTER_HORIZONTAL);
}
trheader.addView(txtGenericHeader);
}
txtGeneric.setTextSize(12);
txtGeneric.setTextColor(Color.parseColor("#768F9E"));
txtGeneric.setText(mArrayList.get(counter++));
txtGeneric.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(txtGeneric);
}
tr.addView(new EditText(this));
tr.addView(new EditText(this));
table.addView(trheader);
table.addView(tr);
cursor.moveToNext();
}
db.close();
sv.addView(table);
lay.addView(sv);
}//EOF PARENT IF
You can do this for setting the properties.
EditText edtText = new EditText(this);
edtText.setTextColor(someColor);
edtText.setTextSize(someSize);
myTablerow.addView(edtText);
And for getting the value from EditText you can use.
String edtTextValue = edtText.getText().toString();
you may refer to this
Q1: #setTextColor() , #setTextSize()
Q2: #getText()

Android Studio - removing table row programmatically

I have created a table dynamically from an array list
for (int i = 0; i < arrayList.size(); i++) {
TextView name = new TextView(this);
TextView time = new TextView(this);
TableRow row = new TableRow(this);
// Set event name and remaining time
name.setText(arrayList.get(i).name);
time.setText("...");
// Add text views to row
row.addView(time, layoutParams);
row.addView(name, layoutParams);
// Add row to table
tableLayout.addView(row, i);
}
Now if I want to edit or remove certain rows under a condition, I will have to access that row, but how?
outerloop:
for(int i = 0; i < arrayList.size(); i++ ) {
if(// row's time <= 0 ...) {
tableLayout.removeViewAt(i); // this doesn't seem to work properly
// it removes the wrong rows
}
else {
// ...
break outerloop;
}
}
For some reason tableLayout.removeViewAt(i); removes the incorrect row. Alternatively, I'd prefer to edit that row's name, but the principle applies, I just need to find a way to access the table row. Any way?
Create your table like this:
ArrayList<TableRow> rows = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
TextView name = new TextView(this);
TextView time = new TextView(this);
TableRow row = new TableRow(this);
rows.add(row);
// Set event name and remaining time
name.setText(arrayList.get(i).name);
time.setText("...");
// Add text views to row
row.addView(time, layoutParams);
row.addView(name, layoutParams);
// Add row to table
tableLayout.addView(row, i);
}
When you want to remove a row now you can do that by it's index in the ArrayList:
row = rows.get(i);
table.removeView(row);
rows.remove(i);
When you want to remove multiple rows this could be a solution for you:
ArrayList<TableRow> removeRows = new ArrayList<>();
for(int i = 0; i < rows.size(); i++) {
if(....) {
removeRows.add(rows.get(i));
}
}
// now delete those rows
for(TableRow remove : removeRows) {
tableLayout.removeView(row);
rows.remove(remove);
}
I hope this works for you!

how can I get the value from programmatically radio button/check box?

I have a form that the question comes from web service (maybe have check boxes or text field or radio button-made programmatic-ally ) and send the value to the local database.
The first part (call the web service and showing them) is correct but the second part (getting the values) is not!
how can I get the value from programmatic-ally radio button/check box ?
(the important part is i have two "FOR" for radio button or check boxes)
I do not know how can i get the value!??!??!
this is my code, made question!
PLZ help me.
for (int i = 0; i < output2.length; i++) {
if (output2[i].contains("#")) {
part1 = output2[i].split("#");
int i2 = 0;
do {
// Question Id ->part1[0]
// FK_MASTERNAZAR ->partinfo_id[0]
// Tilte ->part1[1]
TextView tv2 = new TextView(page2.this);
tv2.setId(i2);
tv2.setText(part1[1]);
tv2.setGravity(Gravity.RIGHT);
lmain2.addView(tv2);
// Answers ->part1[2]
// Type ->part1[3]
switch (Integer.valueOf(part1[3])) {
case 1:
// single selection
part2 = part1[2].split(",");
radioGroup1 = new RadioGroup(page2.this);
radioGroup1.setGravity(Gravity.RIGHT);
radioGroup1.setOrientation(RadioGroup.HORIZONTAL);
for (int i3 = 0; i3 < part2.length; i3++) {
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
rb6 = new RadioButton(page2.this);
rb6.setId(i3);
rb6.setText(part2[i3]);
rb6.setGravity(Gravity.RIGHT);
radioGroup1.addView(rb6, 0, layoutParams);
}
lmain2.addView(radioGroup1);
break;
case 2:
et_Num = new EditText(page2.this);
et_Num.setId(i2);
et_Num.setHint("insert your answer…");
et_Num.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
lmain2.addView(et_Num);
break;
case 3:
// multi selection
part2 = part1[2].split(",");
LinearLayout lcheck = new LinearLayout(page2.this);
lcheck.setGravity(Gravity.RIGHT);
lcheck.setOrientation(LinearLayout.HORIZONTAL);
for (int i3 = 0; i3 < part2.length; i3++) {
ch6 = new CheckBox(page2.this);
ch6.setId(i3);
ch6.setText(part2[i3]);
ch6.setGravity(Gravity.RIGHT);
lcheck.addView(ch6);
lmain2.addView(lcheck);
break;
}
// T ->part2[4]
// insert line
TextView tv6 = new TextView(page2.this);
tv6.setId(i2);
tv6.setText("**********");
tv6.setGravity(Gravity.CENTER);
tv6.setTextColor(Color.parseColor("#33B5E5"));
lmain2.addView(tv6);
Question_Id = Integer.valueOf(part1[0]);
Integer qtype = Integer.valueOf(part1[4]);
// copy FormQuestion web service to the database
database = new MySQLiteHelper(getApplicationContext());
FormQuestionClass formquestionclass = new FormQuestionClass(
Question_Id, Fk_masternazar, part1[2], part1[3], qtype);
database.InsertQuestion(formquestionclass);
database.close();
// end insertting to db.
} while (i2 == part1.length);
}
}
Use the added controls as fields instead of local variables.
That way you can check later on for null, and if they're not null, use
RadioGroup.getCheckedRadioButtonId()
EditText.getText()

Dynamically Update TableLayout

Edit: as Blumer pointed out, I was not adding the items to the table, so that this question appeared just because I was careless and I didn't see my mistake.
I am trying to create a dynamic TableLayout, as I have to receive results from the server and add rows based on the results, but the table is not updating. (Also, the TableLayout already has 1 initial row, the header row).
This is my code:
Room[] rooms = State.rooms;
TableLayout tblBookDetails = (TableLayout) findViewById(R.id.tblbookdetails);
for(int i = 0; i < rooms.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LayoutParams layout_wrapwrap = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_wrapwrap.rightMargin = 10; //TODO: Convert into DP
Resources res = getResources();
TextView txt1 = new TextView(this);
txt1.setLayoutParams(layout_wrapwrap);
txt1.setTextColor(res.getColor(android.R.color.black));
txt1.setText(rooms[i].name);
TextView txt2 = new TextView(this);
txt2.setLayoutParams(layout_wrapwrap);
txt2.setTextColor(getResources().getColor(android.R.color.black));
txt2.setText(rooms[i].price + " " + rooms[i].currency);
EditText edit1 = new EditText(this);
edit1.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit1.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit1.setEms(3);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
EditText edit2 = new EditText(this);
edit2.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit2.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit2.setEms(3);
edit2.setInputType(InputType.TYPE_CLASS_NUMBER);
Spinner spinner = new Spinner(this);
layout_wrapwrap.rightMargin = 0;
spinner.setLayoutParams(layout_wrapwrap);
Integer[] numbers = new Integer[rooms[i].count];
for(int j = 0; j < numbers.length; j++) {
numbers[j] = i + 1;
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
BookActivity.this, R.layout.spinner_textview, numbers);
spinner.setAdapter(adapter);
tblBookDetails.addView(tr);
}
//Another exquisite beauty of Java.
Log.d("USR", Integer.valueOf(tblBookDetails.getChildCount()).toString());
tblBookDetails.invalidate();
tblBookDetails.refreshDrawableState();
To prevent any confusion, the Room[] array is just a simple property-holder class.
This code looks enormously convoluted, and the table is not updating. I've searched quite a bit on the Internet, and I could not find any solution for this problem.
Thank you in advance.
I see where you add tr to tblBookDetails, but I don't see anywhere where you put txt1, txt2, edit1, etc. in tr. Try adding those views to the row, and I think that should get you there, because right now you appear to be adding the TableRow, but there's nothing in it.

how to store multiple dynamic edittext value in android

I have multiple dynamic editTextBox. I want to know how can I store all its edit textbox value?
what should I write in all edit box that should be I want a store on button click event.
My code is below:
for(i = 0 ;i < 6; i++) {
TableRow tr = new TableRow(this);
TableRow tr1 = new TableRow(this);
TextView tv1 = new TextView(this);
editText1 = new EditText(this);
editText1.setWidth(300);
tr.addView(tv1);
tbl.addView(tr);
tr1.addView(editText1);
tbl.addView(tr1);
}
Here is the code.
EditText[] editText1 = new EditText[6];
for(i = 0 ;i < 6; i++) {
TableRow tr = new TableRow(this);
TableRow tr1 = new TableRow(this);
TextView tv1 = new TextView(this);
editText1[i] = new EditText(this);
editText1[i].setWidth(300);
tr.addView(tv1);
tbl.addView(tr);
tr1.addView(editText1[i]);
tbl.addView(tr1);
}
public void onClick(View v)
{
for(int i=0;i<6;i++)
{
Log.i("----value of EditText:"+i,"."+editText1[i].getText().toString());
}
}
A direct answer to your question would be:
remember your textviews when you create them (add them to an ArrayList for example)
the onPause() method of your activity is the place to retain any information you want available later (android framework can finish() your activity anytime when it's paused).
in an overrided onPause() method, cycle through your ArrayList<TextView>, retrieve each TextView content and store them for later use. (If you have a fixed number of TextViews, you may consider using SharedPreferences, but take a look at the remainder of this article to see if something better suits your needs.
You can then use onCreate() or onResume() method to repopulate your TextViews before your application shows up.
An indirect, possibly better, answer would be to separate data from UI by using a data management class that would handle saving restoring and modifying your data and a customized Adapter which would be responsible of filling the TextViews. This way you can easily change the later to address different widgets.
I wrote these codes without using editor, check it before using
String sValues="";
for (int i = 0; i < tbl.getChildCount(); i++) {
TableRow row = (TableRow)tbl.getChildAt(i);
for (int j = 0; j < row.getChildCount(); j++) {
if(row.getChildAt(j) instanceof EditText){
EditText editText1 = (EditText)row.getChildAt(j);
sValues += editText1.getText().toString();
}
}
}

Categories

Resources