First, I'm new with android. I've trying to make a survey on android. My problem is that i want that the next question and possible answers appear when radiobutton is selected, and the same thing for all the question i have. I make it function but accidentally i erased the project. Here is the code i developed
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView texto1;
TextView texto2;
RadioGroup Selecopc;
int i=1;
private TextView num;
private TextView pregu;
private TextView rep1;
private TextView rep2;
private TextView rep3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.area_de_preguntas);
num = (TextView)findViewById(R.id.Numero);
pregu = (TextView)findViewById(R.id.Pregunta);
rep1 = (TextView)findViewById(R.id.Respuesta1);
rep2 = (TextView)findViewById(R.id.Respuesta2);
rep3 = (TextView)findViewById(R.id.Respuesta3);
Selecopc = (RadioGroup)findViewById(R.id.opcion);
Selecopc.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId)
{
i++;
String n = String.valueOf(i);
num.setText(n);
String pregseg ="R.string."+"preg"+i;
pregu.setText(pregseg);
}
});
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="action_settings">Settings</string>
<string name="numero">1</string>
<string name="preg1">Cua...</string>
<string name="preg2">Cu..</string>
<string name="preg3">...</string>
<string name="preg4">...</string>
<string name="preg5">...</string>
<string name="preg6">...</string>
String id resource names is just a static field. I suppose you can do it using reflection but it's not the better choice in your case. The better way is to predefine array of identifiers and use them. Sample:
private int[] pregs = new int[]{
R.string.preg1,
R.string.preg2,
R.string.preg3
};
And use it later like this:
public void onCheckedChanged(RadioGroup group, int checkedId)
{
i++;
String n = String.valueOf(i);
num.setText(n);
pregu.setText(pregs[i]);
}
Related
I am trying to make an simple button that will cooperate with TextView, array and method changer (in public class Change). However the application uses the method changer only twice (below). It should work on every click. The changer method is used for the change in displayed array.
1-click: 1 |
2-click: 4 |
3-click: 1 |
4-click: 1 |
...-click: 1 |
I don't know where is the problem.
My target solution should be 1,4,1,4,1,4 ...
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String[] numbers = {"1", "2","3","4"};
private Button mButton;
int i = 0;
Change ch = new Change();
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button2);
text = (TextView) findViewById(R.id.tv1);
ch = new Change();
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text.setText(numbers[0]);
numbers = ch.changer(numbers);
i++;
}
});
}
}
Change.java
public class Change {
String[] arraynew = new String[4];
public String[] changer(String[] array){
arraynew[0]=array[3];
arraynew[1]=array[2];
arraynew[2]=array[1];
arraynew[3]=array[0];
return arraynew;
}
}
The arraynew variable in your code is global - so it will not create a new array every time you call the changer method, only rearranges its contents.
Here comes the tricky part: When you call the method for the second time, the parameter is the same object as arraynew. So when you do arraynew[0]=array[3] it changes 4 to 1 on the first slot; but arraynew[3]=array[0] will copy that newly changed 1 to the third index (where it is still the same 1 you put there). It basically eliminates the 4, making it impossible to recover.
Solution: Make arraynew local. If you declare it in the method, it will never be the same array, so the issue won't appear.
This question already has answers here:
Can't resolve symbol 'setText' error
(4 answers)
Closed 4 years ago.
I would like to change the text in a button, but the setText method doesn't work, it marks it in red and says symbol not resolved. How can i fix this? Here is my code.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class GameActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
int partA = 9;
int partB = 9;
int correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer++;
int wrongAnswer2 = correctAnswer--;
}
TextView textObjectA = (TextView)findViewById(R.id.textPartA);
TextView textObjectB = (TextView)findViewById(R.id.textPartB);
Button buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
Button buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
Button buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
//this part doesn't work
buttonObjectChoice1.setText("" + partA);
}
Move your code
TextView textObjectA = (TextView)findViewById(R.id.textPartA);
TextView textObjectB = (TextView)findViewById(R.id.textPartB);
Button buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
Button buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
Button buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
//this part doesn't work
buttonObjectChoice1.setText("" + partA);
into onCreate() method.
I have a problem with a TextView showing letters instead of numbers.
I am trying to make a converter app.
Here is my code:
package com.rickhuisman.converter;
import android.icu.text.DecimalFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class LengthActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_length);
}
public void onClickConvertLength(View view) {
// variables
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1Length);
Spinner spinner2 = (Spinner)findViewById(R.id.spinner2Length);
Button button = (Button)findViewById(R.id.convertButtonLength);
// spinner 1
String spinner1Value = String.valueOf(spinner1.getSelectedItem());
// spinner 2
String spinner2Value = String.valueOf(spinner2.getSelectedItem());
EditText inputChecker = (EditText)findViewById(R.id.InputLength);
TextView showText = (TextView)findViewById(R.id.showTextLength);
String inputCheckerText = inputChecker.getText().toString();
EditText input = (EditText)findViewById(R.id.InputLength);
// If input == 0
if(inputCheckerText.equals("")) {
inputCheckerLength();
}
// Millimeters && Millimeters
else if(spinner1Value.equals("Millimeters") && spinner2Value.equals("Millimeters")) {
// Get the input
Double formule = new Double(input.getText().toString());
methodLength(formule);
}
// Millimeters && Centimeters
else if(spinner1Value.equals("Millimeters") && spinner2Value.equals("Centimeters")) {
// Get the input
Double formule = new Double(input.getText().toString());
methodLength(formule);
}
}
// If the input == 0
public void inputCheckerLength() {
TextView showText = (TextView)findViewById(R.id.showTextLength);
CharSequence toastText = "Nothing to convert!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, toastText, duration);
toast.show();
}
// The method
public void methodLength(double formule) {
TextView showText = (TextView)findViewById(R.id.showTextLength);
showText.setText(Double.valueOf(formule).toString());
}
}
image of the code: Code
The problem is that if I put to many numbers in the EditText the TextView will show letters, is there a way I can make the TextView show the whole number and not letters?
Image of the problem:Image
Sorry for the bad English and thank you!
The problem is that you are using the Double.toString() method to output your double value, which uses the scientific notation format.
If you want to display the plain number you can use String.format() instead:
showText.setText(String.format("%.0f", formule));
In case you want to show decimals you can use e.g. %.2f for two decimals instead of %.0f.
I'm a noob Android studio programmer (this is hour 2 of learning!) and I expect this is a real rookie error I'm making!
I've got a Plain Text field in my application and I would like to set the text of this dynamically. I've given the plain text field the ID of: "resultText". Here's what I try;
public void calcnums(View v)
{
int x=firstNum + seondNum;
resultText.setText("Result: " + x);
}
For some reason I get 'resultText' highlighted in red and the hover over message is; Cannot resolve symbol 'resultText'.
I have the feeling that I'm doing something wrong by using the ID, but I'm lost!
Full code as suggested in comments;
import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class AddNumbers extends AppCompatActivity {
private int firstNum;
private int seondNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_numbers);
}
public void calcnums(View v)
{
int x=firstNum + seondNum;
resultText.setText(String.format("Result: %d", x);
}
public void setNums(View v)
{
TextView tx= (TextView) findViewById(R.id.text);
Random r = new Random();
int x=r.nextInt(2) + 1; // r.nextInt(2) returns either 0 or 1
firstNum = x;
r = new Random();
x=r.nextInt(2) + 1;
seondNum = x;
num1.setText(""+firstNum);
num2.setText(""+seondNum);
}
}
It seems you need to declare the View resultText
Like,
EditText resultText = (EditText) findViewById(R.id.resultText);
Also make sure that the name you are using for the resultText View in xml is written correctly as provided in the method findViewById()
usually cannot resolve symbol means some problems in variable declarations.
I am too learning android and I stumbled upon this problem as well.
I don't have the whole snippet of your code.But the thing you might be missing is to point your EditText object to view in xml.
EditText resultText = (EditText) v.findViewById(R.id.result_edit_text);
<EditText
android:id="#+id/result_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/result_edit_text"
/>
Let's assume that we have an layout with an edittext
<EditText
android:id="#id/txt_user_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
It has an ID. In your actitivy you must find and cast the EditText as follow:
EditText txtUserEmail = (EditText) findViewById(R.id.txt_user_email);
txtUserEmail.setText("klaus.dieter#lusty-swingers.de");
You need to find your text view in onCreate(), like that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_numbers);
TextView tx= (TextView) findViewById(R.id.text);
}
i recently started programming in Android and i came across a little problem.
What i'm trying to do is:
I have NewsActivity and a NewsRows.class (in the same package). So the news activites just creates a new NewsRows object and tells it to fill the TableLayout with new rows.
It works fine as long as i try to add an image from a resource... The app just keeps crashing.
The debugger tells me it can't find the resource but i can't find out why!
My code is here:
News Acitivty
package de.myapp.app.activites.news;
import de.myapp.app.R;
import android.app.Activity;
import android.os.Bundle;
public class News extends Activity {
NewsRows rowClass = new NewsRows();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
NewsRows.createNewsEntries(this);
}
}
NewsRows.class
package de.myapp.app.activites.news;
import de.myapp.app.R;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class NewsRows {
static TextView title;
static TableRow tRow;
static TableLayout tLayout;
public NewsRows() {
}
public static void createNewsEntries(Activity contextActivity) {
ImageView image = new ImageView(contextActivity);
image.setBackgroundColor(R.drawable.myimage);
tLayout = (TableLayout) contextActivity.findViewById(R.id.NewsTable);
for(int a = 0; a < 100; a++) {
tRow = new TableRow(contextActivity);
title = new TextView(contextActivity);
//tRow.addView(image);
title.setText("This is a test.");
tRow.addView(title);
tLayout.addView(tRow);
}
}
}
EDIT:
The line
image.setBackgroundColor(R.drawable.myimage);<br />
Is actually supposed to be:
image.setImageResource(R.drawable.myimage);
You're trying to set an image into a background color:
Change this:
image.setBackgroundColor(R.drawable.myimage);
to this:
image.setBackgroundResource(R.drawable.myimage);
Fun fact:
TableLayout tLayout = (TableLayout) findViewById(R.id.NewsTable);
TableRow tRow = new TableRow(this);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.myimage);
tRow.addView(image);
tLayout.addView(tRow);
If i put this code right in the News.Activity it works...
You can't set a drawable for as a background color of your ImageView, that's why you have the resource not found exception on your logcat!!
image.setBackgroundColor(R.drawable.myimage);
change it to this:
image.setBackgroundResource(R.drawable.myimage);
Also try to change this
public static void createNewsEntries(Activity contextActivity)
with
public static void createNewsEntries(Context contextActivity)