I use a menu inflater and in every row I give the textview and seekbar an id of i(I use a for loop to give them there id). When I get the id of the seekbar and I want to insert text into the textview. The problem I am having is how do I set the text in the textview that has the same value as the seekbar.
public static void setNewTipAmount(SeekBar barChanged, double amountForTip) {
int getID = barChanged.getId(); //get seekbar id
TextView textView = tipPerPerson.findViewById(getID);// get the textview with same id
textView.setText("Hello");
}
}
Here is the code to how I assign the Id's
for (int i = 0; i < InBetweenUIAndBusinessLogic.getGuests(); i++) {
View v = in.inflate(R.layout.row_per_person, table, false);
double tipPerIndividual = (Math.round(InBetweenUIAndBusinessLogic
.getTipPerPerson() * 100.0) / 100.0);
String tip = Double.toString(tipPerIndividual);
tipPerPerson = (TextView) v.findViewById(R.id.tipPerPerson);
tipPerPerson.setId(i);
tipPerPerson.setText(tip);
rows.add(tipPerPerson);
percentageTip = (SeekBar) v.findViewById(R.id.seekBar1);
percentageTip.setOnSeekBarChangeListener(new myListener());
double guests = InBetweenUIAndBusinessLogic.getGuests();
percentageTip.setProgress((int) (100 / guests));
percentageTip.setId(i);
table.addView(v);
bars.add(percentageTip);
}
I suggest creating a simple mapping between TextView id and Seekbar id. This way each id will be different and you can still get the corresponding item id. Here is my solution:
mGuestCount = InBetweenUIAndBusinessLogic.getGuests();
for (int i = 0; i < InBetweenUIAndBusinessLogic.getGuests(); i++) {
View v = in.inflate(R.layout.row_per_person, table, false);
double tipPerIndividual = (Math.round(InBetweenUIAndBusinessLogic
.getTipPerPerson() * 100.0) / 100.0);
String tip = Double.toString(tipPerIndividual);
tipPerPerson = (TextView) v.findViewById(R.id.tipPerPerson);
tipPerPerson.setId(i);
tipPerPerson.setText(tip);
rows.add(tipPerPerson);
percentageTip = (SeekBar) v.findViewById(R.id.seekBar1);
percentageTip.setOnSeekBarChangeListener(new myListener());
double guests = InBetweenUIAndBusinessLogic.getGuests();
percentageTip.setProgress((int) (100 / guests));
percentageTip.setId(i + mGuestCount);
table.addView(v);
bars.add(percentageTip);
}
Now to find the textview:
public static void setNewTipAmount(SeekBar barChanged, double amountForTip) {
int getID = barChanged.getId(); //get seekbar id
TextView textView = tipPerPerson.findViewById(getID - mGuestCount);// get the textview with corresponding id
textView.setText("Hello");
}
Related
I have a array list with three element stored in it.In an if condition i have removed one element ,it removing fine but when i check the size of the array list outside the if condition it return zero value.Please anyone help me.i have followed these steps...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_moves = (TextView) findViewById(R.id.tv_moves);
tv_time = (TextView) findViewById(R.id.tv_time);
ArrayList<Integer> arl = new ArrayList<Integer>();
arl.add(1);
arl.add(2);
arl.add(3);
ArrayList<Integer>remain=new ArrayList<>();
mGrid = (GridLayout) findViewById(R.id.grid_layout);
inflater = LayoutInflater.from(this);
for (int i = 0; i < NBR_ITEMS; i++) {
final View itemView = inflater.inflate(R.layout.item_grid, mGrid, false);
final TextView text = (TextView) itemView.findViewById(R.id.text);
if (i == 4) {
itemView.setEnabled(false);
itemView.setBackgroundColor(Color.parseColor("#999999"));
int index = randomGenerator.nextInt(arl.size());
Integer item1 = arl.get(index);
text.setText(item1.toString());
array[4] = item1.toString();
arl.remove(index);
System.out.println("array element list==="+arl);
remain.add(0,arl.get(0));
remain.add(1,arl.get(1));
checkSubmit();
text.setPadding(60, 30, 10, 10);
}
if (i == 3) {
System.out.println("size of array list at 3=="+arl.size());//here i got the size 3 but i think it should be 2
itemView.setEnabled(false);
itemView.setBackgroundColor(Color.parseColor("#999999"));
int index = randomGenerator.nextInt(arl.size());
Integer item2 = arl.get(index);
text.setText(item2.toString());
array[3] = item2.toString();
checkSubmit();
text.setPadding(60, 30, 10, 10);
}
I used the answer to this question to get the basic value of the TextView clicks into a single TextView .This code gives a per click value, I want to know how to add the existing value of TextView.
Note: m1aa and m1ab are number variables in a previous activity and received through intent on this one.
UPDATE This question should have been "how do you convert a string to a decimal."
//Original non working code.
private int productTotal1;
private int productTotal2;
private int overallTotalproduct;
private TextView m1a,
private TextView m1aa,
//and then way down the page
productTotal1 = 0;
productTotal2 = 0;
overallTotalproduct = 0;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
textView1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal1++;
overallTotalproduct = productTotal1 + productTotal2;
textView1.setText(String.valueOf(productTotal1));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
textView2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal2++;
overallTotalproduct = productTotal1 + productTotal2;
textView2.setText(String.valueOf(productTotal2));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
***UPDATE***WORKS FINE :)
AFTER GETTING HELP AND "THINKING" ABOUT WHAT I WAS WRITING I END UP WITH...
//Working version
private double overallTotalproduct;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final String stringm1aa = textView1.getText().toString();
final double intm1aa = Double.parseDouble(stringm1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
final String stringm1ab = textView2.getText().toString();
final double intm1ab = Double.parseDouble(stringm1ab);
You can use following code to get a value of TextView and convert it to integer
String s = textView.getText().toString();
int n = Integer.parseInt(s);
Remember NumberFormatException will be thrown if the string does not contain a parsable integer.
Is this code completely correct or not?
myRLayout = (RelativeLayout) findViewById( R.id.rel_layout );
for(int i = 0; i < myRLayout.getChildCount(); i++) {
View v = myRLayout.getChildAt(i);
if(v instanceof EditText) {
TextView res = (TextView) findViewById(R.id.result_text);
String str1 = ((EditText) findViewById(R.id.edittext_1)).getText().toString();
String str2 = ((EditText) findViewById(R.id.edittext_2)).getText().toString();
res.setText(str1+str2);
}
}
Why I ask? For each iteration only one EditText gets in action or all together at once?
use v instance of Child to get value from all EditText and use TextView.append for showing values in TextView. try it as:
TextView res = (TextView) findViewById(R.id.result_text);
res.setText(""); //<< clear textview here
for(int i = 0; i < myRLayout.getChildCount(); i++) {
View v = myRLayout.getChildAt(i);
if(v instanceof EditText) {
String str1 = v.getText().toString(); //<< get text from ith EditText
res.append(str1); // append value to result_text TextView
}
}
how to create the textview in code not in xml file. It is because number of textviews will be changing in my application according to some integer.
This is the code to create TextView Dynamically
LinearLayout layout = (LinearLayout ) findViewById(R.id.llayout);
for (int i = 0; i < 3; i++) {
TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
dynamicTextView.setText("NewYork");
layout.addView(tstate);
}
Maybe thats what you need:
LinearLayout lin = (LinearLayout) findViewById(R.id.myLinear);
for (int i = 0; i <= 10 ; i++)
{
TextView myText = new TextView(this);
myText.setText("textview# "+ i);
lin.addView(myText);
}
Something like the following should be what you need:
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is TextView #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
private LinearLayout ll;
private TextView tv;
// in oncreate()
onCreate()
{
int WrapWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
int WrapHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
tv = new TextView(this);
ll.addView(tv,WrapWidth,WrapHeight);
}
Code is here
final int c = 12;
final TextView[] mtext = new TextView[c];
for (int i = 0; i < c; i++) {
TextView rowtxt = new TextView(this);
rowtxt.setText("Hello" + i);
myLinearLayout.addView(rowtxt);
myTextViews[i] = rowtxt;
myTextViews[i].setOnClickListener(onclicklistener);//textview click
}
OnClickListeners code is here
OnClickListener onclicklistener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == myTextViews[0]){
//do whatever you want....
}
}
};
Hope it is helpful for you
You use TextView textView = new TextView(CurrentActivity.this);
and then you add setter arguments that come with the TextView class
is there anyway to change which view you are editing based upon the value of a int?
Without using a if statement?
for example:
int counter = 1;
public void attach(){
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
TextView text1 = (TextView) findViewById(R.id.text1)
TextView text2 = (TextView) findViewById(R.id.text2)
TextView text3 = (TextView) findViewById(R.id.text3)
TextView text4 = (TextView) findViewById(R.id.text4)
if (counter < 5){
if (sp.getInt("test" + counter , 0) == counter){
text + counter .setText("test" + counter);
}else{counter = counter + 1; attach();}}
}
Simplest way is to define an array of possible view IDs.
int[] viewIds = new int[] {
R.id.text1,
R.id.text2,
R.id.text3,
R.id.text4
}
//some code
int i = /*whatever*/;
TextView tv = (TextView)findViewById(viewIds[i]);
tv.setText("test"+i);