How to set textview as method output in android?
I just recently started using Android Studio and am currently working on a Roman numeral translator app. The application’s interface looks like this: Application interface
The user will use the keyboard to enter an integer that will be displayed on the TextView shown above. When they hit the convert button, it will take the integer they entered and convert it (if it contains strings or characters, the program will be able to capture the input). The application then resets the TextView to the result of the user clicking the Convert button.
Currently, my main activity consists of onClickListeners for buttons and separate translator methods for translation. My problem is the “Convert” button, I’m not sure how to get input from the translator method and set it to TextView when the conversion is done. Here is my code example:
“Convert” button listener -‘
convert.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
TextView numeralInput = (TextView) findViewById(R.id.textView);
String intValue = numeralInput.getText().toString();
try{
int integer = Integer.parseInt(intValue);
if (integer > 0 && integer <= 4999){
translator(integer);
}else{
numeralInput.setText("Please enter an integer between 0 and 4,999.");
}
}catch(NumberFormatException e){
numeralInput.setText("Invalid input try again.");
}
}
}
);
`
Translator method -‘
public static void translator(int integer) {
LinkedList<String> stack = new LinkedList<String>();
if (integer > 0 && integer <= 4999) {
ArrayList<Integer> placement = new ArrayList<Integer>();
int place = (int) Math.log10(integer);
for (int i = 0; i <= place; i++) {
while(integer > 0){
System.out.println(integer);
int placeOfValue = integer % 10;
stack.push(placeOfValue);
System.out.print(stack);
System.out.print(placeOfValue +":" + i);
String placement = "";
switch (i) {
case 0:
placement = ones(placeOfValue);
break;
case 1:
placement = tens(placeOfValue);
break;
case 2:
placement = hundreds(placeOfValue);
break;
case 3:
placement = thousands(placeOfValue);
break;
default:
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES. GINGERBREAD) {
stack.push(placement);
}
integer = integer / 10;
System.out.print(placement);
System.out.println(placement.size());
//}
for(int j = 0; j < placement.size(); j++){
double tenthPower = Math.floor(Math.log10(placement.get(j)));
double place = Math.pow(10, tenthPower);
System.out.println(place);
//
// }
// }
while (!stack.isEmpty()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES. GINGERBREAD) {
System.out.print(stack.pop());
}
}
} else {
System.out.println("Please enter an integer between 0 and 4,999.");
// }
}
}
`
Other methods in Translator are like a library of Roman numerals, where each Roman numeral contains a number for each bit value, as shown below.
Thousand Laws -‘
public static String thousands(int integer) {
String thouValue = "";
switch (integer) {
case 1:
thouValue = "M";
System.out.print("M");
break;
case 2:
thouValue = "MM";
System.out.print("MM");
break;
case 3:
thouValue = "MMM";
System.out.print("MMM");
break;
case 4:
thouValue = "MMMM";
System.out.print("MMMM");
break;
default:
thouValue = "";
break;
}
return thouValue;
}
`
Solution
Have your translator()
method return a string containing the final output.
So before the while
statement in the method, declare a string, eg
String result = null;
And append the pop-up value to this variable in the loop, like result += stack.pop().
Now, where the translator(integer)
method is called, execute numeralInput.setText
(translator(integer)) instead of translator(integer).