how to get text of the textview in the dynamically generated table? - java

i have written something like this....
TableRow tr1;
TableRow tr2;
TextView txt9;
...
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT);
for (int i = 0; i < strarr.length-1; i++)
{
strinarr = fun1.split(strarr[i].trim(),"^");
tr1 = (TableRow) new TableRow(this);
txt1=new TextView(this);
txt9.setText(strinarr[0]);
txt9.setBackgroundColor(intblue);
txt9.setTextColor(intwhite);
txt9.setClickable(true);
txt9.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Log.i("pagename",strpagename);
String currenttext = txt9.getText().toString());
}
}
});
tr1.addView(txt9);
tl.addView(tr1,new TableLayout.LayoutParams(layoutParams));
}
i am able to get text on click but the stupid thing is that i am getting text of last textview in the table on all the textview click event... if somebody could tell me how to catch textview's text on focus or touch it would be really help full...

change String currenttext = txt9.getText().toString()); to String currenttext = ((TextView)v).getText().toString());

You should probably maintain a list of ID's in the table and a list of corresponding texts. When the user clicks on the widget with the given ID, simply look up the text in the textlist and set it.

ArrayList qty = new ArrayList();
for(int i = 1; i <= 10; i++)
{
qty.add(i);
}
for(int i = 0; i < 5; i++)
{
TableRow tr = new TableRow(this);
Spinner tvQtySpinner = new Spinner(this);
tvQtySpinner.setOnItemSelectedListener(this);
ArrayAdapter<String> aa =
new ArrayAdapter<String> this, android.R.layout.simple_spinner_item, qty);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
tvQtySpinner.setAdapter(aa);
tr.addView(tvQtySpinner);
TextView tvRate = new TextView(this);
tvRate.setText(value.getPrice().toString());
tvRate.setTextColor(Color.YELLOW);
tr.addView(tvRate);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
{
Integer value = (Integer) parent.getItemAtPosition(pos);
Float result = value*50;
tvRate.setText(String.valueOf(result));
}
Here I am having 5 rows, each row having 5 dynamic spinners and text views. I am getting the selected value from spinner and doing calculation and am trying to set that calculated result in particular rows TextView. But when I set the calculated value in TextView it will set on last rows TextView.

Related

Creating a string using onClick and getting the final value to search to the array

I'm new to android and I am currently having this problem on onClick():
I have dynamically created textViews within a TableLayout() and each textviews have onClickListener. The code is like this:
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//change the color of tapped textview
text.setTextColor(Color.WHITE);
text.setBackgroundColor(Color.parseColor("#c04485ed"));
String a = view.getTag().toString();
String b = text.getText().toString();
uTxt.setText(""+uTxt.getText().toString() + b);
if(uTxt.length() == 4){
if(checkAns(uTxt, list)){
Log.e("OUTPUT: " , uTxt.getText().toString().toLowerCase() + " IS ON THE ARRAY!");
}
else if (!checkAns(uTxt, list))
{
Log.e("OUTPUT: " , uTxt.getText().toString() + " IS NOT ON THE ARRAY!");
}
}
}
});
I also initialized a method called checkAns() and it goes like this:
private boolean checkAns(TextView uTxt, List<String> list) {
String word = uTxt.getText().toString().toLowerCase();
if (list.contains(word)) {
return true;
}
else {
return false;
}
}
The purpose of that method is to catch the created word and check if the formed word is on the array.
But that method always returns false. Is there anything wrong with my logic?
Any help, comments, and suggestions are welcome. :)
EDIT:
#k3b ask me where the list is populated. So here is the full code of the textView grid that i created:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TableLayout table = (TableLayout) findViewById(R.id.mainLayout);
table.setTag(1);
final Typeface font = Typeface.createFromAsset(getAssets(), "kg.ttf");
final Typeface font2 = Typeface.createFromAsset(getAssets(), "futura.ttf");
final String[] wordArray = new String[Category.size];
final TextView uTxt = (TextView)findViewById(R.id.userTxt);
for(int i = 0; i < Category.size; i++){
wordArray[i] = listahan[i];
}
final List<String> list = Arrays.asList(wordArray);
for (int i = 0; i < input.length; i++) {
final LinearLayout rowLayout = new LinearLayout(this);
rowLayout.setOrientation(LinearLayout.HORIZONTAL);
rowLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
final TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER_HORIZONTAL);
row.setTag(i);
int k = 0;
for (int j = 0; j < input.length; j++) {
final TextView text = new TextView(this);
Character temp = input[i][j];
text.setText(temp.toString());
text.setPadding(20, 20, 20, 20);
text.setTag(i + " " + j);
text.setTextSize(txtSize);
text.setTypeface(font);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//change the color of tapped textview
text.setTextColor(Color.WHITE);
text.setBackgroundColor(Color.parseColor("#c04485ed"));
String a = view.getTag().toString();
String b = text.getText().toString();
uTxt.setText(""+uTxt.getText().toString() + b);
if(uTxt.length() == 4){
if(checkAns(uTxt, list)){
Log.e("OUTPUT: " , uTxt.getText().toString().toLowerCase() + " IS ON THE ARRAY!");
}
else if (!checkAns(uTxt, list))
{
Log.e("OUTPUT: " , uTxt.getText().toString() + " IS NOT ON THE ARRAY!");
}
}
}
});
text.setGravity(Gravity.CENTER);
row.addView(text);
}
Thanks for all the help guys. I managed to fix the error. It seems that the code that initialized my array of words is not properly coded! Thanks guys!

How to get checked radio button's id created dynamically in android

In my activity, I am creating a dynamic radiogroup. I have some questions and answers in Sqlite database, retrieving them in activity and then set in RadioGroup. This is working fine. But after that, I want to get all ID of selected radio button by user to store them in database. In general, we do something like this when we have option's ID :
id1=rdgroup1.getCheckedRadioButtonId();
que1=(RadioButton)findViewById(id1);
ans1=que1.getId()+""; // so here I will get radio button's ID.
So my questions is, how will I get selected radio button's ID. Here's my code for dynamically creating radiogroup.
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
c1=db.rawQuery("SELECT * FROM QueTable WHERE AgeGroup='10-20'", null);
c1.moveToFirst();
int i = c1.getCount();
if(i > 0)
{
Log.w("START", "start");
while (i > 0)
{
TextView title = new TextView(this);
questions = c1.getString(1);
title.setText(questions);
title.setTextColor(Color.BLACK);
mLinearLayout.addView(title);
// create radio button
answers=c1.getString(2);
String[] answer = answers.split(",");
rb = new RadioButton[5];
rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
int k = answer.length;
for (int j = 0; j < k; j++)
{
rb[j] = new RadioButton(this);
rg.addView(rb[j]);
rb[j].setText(answer[j]);
}
mLinearLayout.addView(rg);
c1.moveToNext();
i--;
}
}
The place where you create RadioButton set and Id to it
for (int j = 0; j < k; j++)
{
RadioButton rb = new RadioButton(this);
rb.setId(Somenumber + j)
rb[j] = rb;
rg.addView(rb[j]);
rb[j].setText(answer[j]);
}
Like this you can setId or also if you want you can add tag to process.
rb.setTag(SomeObject)
You need to set an ID for your programmatically created RadioButton in order to find it using findViewById as you show in your first code.
To set the ID for your RadioButton, you can use View.setId(). The documentation says:
Sets the identifier for this view. The identifier does not have to be
unique in this view's hierarchy. The identifier should be a positive
number.
The identifier does not have to be unique, but if it's not unique, findViewById will return the first occurence, so you can use the static method View.generateViewId added on API level 17.
If you are using lower APIs, you may want to write your own function to find a suitable (unused) ID. You can take a look at Android: View.setID(int id) programmatically - how to avoid ID conflicts?
Ok..So finally I got the solution. I am going to share complete code here that will create dynamically radiogroup and radiobuttons from database. Here is code below :
private static final int RB_ID = 100;
int k=0,len=0,r=0,p = 0,q=0, len = 0;
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
c1 = db.rawQuery("SELECT * FROM QueMotion WHERE AgeGroup ='"+Age+"' LIMIT 5", null);
c1.moveToFirst();
int i = c1.getCount();
rg = new RadioGroup[i];
rb = new RadioButton[i*5];
if (i > 0) {
Log.w("START", "start");
while (i > 0)
{
TextView title = new TextView(this);
questions = c1.getString(1);// retrive from database
title.setText(questions);
title.setTextColor(Color.BLACK);
title.setTypeface(null, Typeface.BOLD);
title.setPadding(0, 0, 0, 10);
mLinearLayout.addView(title);
answers = c1.getString(2);// retrive from database
String[] answer = answers.split(",");// will create options for radio button.
rg[p] = new RadioGroup(this);
rg[p].setOrientation(RadioGroup.VERTICAL);
len=len+answer.length;
for (int j = 0; j < answer.length; j++)
{
rb[q] = new RadioButton(this);
rg[p].addView(rb[q]);
rb[q].setId(k + RB_ID);
rb[q].setText(answer[j]);
k++;
q++;
}
mLinearLayout.addView(rg[p]);
c1.moveToNext();
i--;
p++;
}
//Submit button click
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (rg[0].getCheckedRadioButtonId() == -1)
{
Log.e("Nothing selected", "Nothing selected");
}
else
{
for (r = 0; r < len; r++) {
if (rb[r].isChecked()) {
RadioButton id = (RadioButton) findViewById(rb[r].getId());
String radioText = id.getText().toString();
c3.moveToFirst();
Log.e("RadioString", radioText);
} else {
Log.e("RadioString", "nothing");
}
}
}
}
});

Nesting a textview and an Imagebutton in a FrameLayout which is nested in a table row

Hi all hope i do a better job explaining what i need below.. thanks in advance for your help..
What im trying to do: Im trying to dynamically create a table of image buttons. When each image button is clicked, a counter badge/bubble (like the update icon u see on apps when there is an update in iphones). This badge should show one the top left corner of the image button.
My logic: To do that i thought that i need to create a tablerow, then in that tablerow put a FrameLayout, and in that FrameLayout but in the Image button and a text view that will display the badge image. Reason i use a FrameLayout is because i need to have the TextView overlap the Imagebutton to get the effect i want...
The Problem: I wrote the code below and it runs with no errors but no counter badge!! It seems like the application ignores the badge and only creates the image buttons for me.... Need help to make it work
HELP !!!
ContentView(R.layout.activitymenu);
TableLayout table =(TableLayout) findViewById(R.id.MenuButtonTable);
for (int row=0; row<NUM_Array_Rows;row++) {
//Set new Row in Table
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 0.1F));
table.addView(tableRow);
for (int col = 0; col < NUM_Array_Clns; col++) {
int x, y;
final int FINAL_COL = col;
final int FINAL_ROW = row;
final int FINAL_BtnCounter = BtnCounter;
final String SW_NAME;
float w;
final ImageButton ImButton = new ImageButton(this);
final FrameLayout FrmLayout = new FrameLayout(this);
final TextView txtView = new TextView(this);
FrameLayout.LayoutParams layout1 =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams. WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
FrmLayout.setLayoutParams(layout1);
ViewGroup.LayoutParams layout2 =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP _CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
txtView.setLayoutParams(layout2);
txtView.setGravity(Gravity.TOP | Gravity.RIGHT);
txtView.setBackgroundResource(R.drawable.badge);
txtView.setText("2");
FrmLayout.setBackgroundResource(R.drawable.ic_laun cher);
ImButton.setLayoutParams(new TableRow.LayoutParams(BtnWidth, BtnHight,0.1F));
Resources res = getResources();
SW_NAME = MenuArray[col][1]; //SW NAme
temp = MenuArray[col][3];
int id = res.getIdentifier(temp, "drawable", MenuActivity.this.getPackageName());
Drawable drawable = res.getDrawable(R.drawable.sw03);
ImButton.setBackground(drawable);
BtnCounter = BtnCounter + 1;
ImButtonArray[row][col] = BtnCounter; // just storing button positions in this array
ImButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int SW_ID;
SW_ID = BtnCounter;
onPrepareOptionsMenu(FINAL_ROW, FINAL_COL);
}
});
tableRow.addView(FrmLayout);
tableRow.addView(ImButton);
FrmLayout.addView(txtView);

How can I know, how many EditText user add?

I inflate an EditText and I setId(i++) | static int i = 0;
and this is the code :
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addOne = layoutInflater.inflate(R.layout.row, null);
final EditText textOut = (EditText)addOne.findViewById(R.id.textout);
Button buttonRemove = (Button)addOne.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addOne.getParent()).removeView(addOne);
}
});
container.addView(addOne);
textOut.setId(i++); }});
so, if I add an EditText, the Id will be 0 and if I add another EditText, the Id will be 1 .. etc
so, the user can select how many EditText he want, right ?
if the user add 3 EditText for example
How can my code knows ?
for example, if I want sum all the values of EditText, and the user add 3 EditTixt
I will do this
( EditText one = (EditText)findViewById(0) ) + ( EditText dodo = (EditText)findViewById(1) ) + ( EditText dodo = (EditText)findViewById(2) )
But, if I don't know how many EditTixt the user will add !
what can I do ?
thank you.
int count = 0;
int childcount = container.getChildCount();
for (int i=0; i < childcount; i++){
View v = container.getChildAt(i);
if(view.getTag.equals("edittext"){
count ++;
}
}
and change this line in your code
final View addOne = layoutInflater.inflate(R.layout.row, null);
addOne.setTag("edittext");
count return you total edittext added by user
take look on this code it will count edit text for you in your layout container
first you need to making changed in your code like this and it worked with me fine :
final ViewGroup addOne = (ViewGroup) layoutInflater.inflate(R.layout.test, null);
int count = 0 ;
for (int i = 0; i < addOne .getChildCount(); i++) {
if(addOne .getChildAt(i).getClass().getSimpleName().equals("EditText"))
count ++ ;
}

TextView variable name + string combining

question with either easy or impossible answer, dunno. I'm new and I want to create new TextView's within a loop, but I'm having problem with TextView's variable name. I need it to be unique.. Thanks.
int i = 1;
while (i<=10) {
String asd = String.valueOf(i);
TextView textView+asd = new TextView(this);
//new textView+asd.setText("asdd");
i++;
}
You can't do it like you posted. You need to create array of TextView's. TextView textViews[] = new TextView[10]; for(int i=0; i<textViews.length; i++) { textViews[i] = new TextView(this); }
You will need to use your layout and do something like this
final int x = 5; // # of TextViews you want
int i = 0;
final TextView[] textViews = new TextView[x];
while (i < x) {
TextView newTextView = new TextView(this); // this needs to be the proper context
newTextView.setText("Whatever");
layout.addView(newTextView); // Need to bring in your layout before this
textViews[i] = newTextView;
i++;
}

Categories

Resources