i’m dooing this tutorial and at one point he uses the R.id.xxxx where x is the name/id of a control I’m using, if I understood it correctly.
Now I have two of those R thing’s -.-‘ and one is android.R and the other is dk.ilizane.android.temperatur.R which doesn’t contain any id at all so I kinda figured I will be using android.R.id but i’m looking for editText1, radio0, radio1 and it doesn’t contain any of those.
Is there anyone kind enough to try explain this to me? I’m trying to learn this so I would appreciate if the answer wasn’t just the correct code but an answer which I can
My code:
package dk.ilizane.android.temperatur;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class Omregn extends Activity
{
private EditText text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(android.R.id.editText1);
}
public void myClickHandler(View view) {
switch (view.getId())
{
case android.R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(dk.ilizane.android.temperatur.R);
RadioButton fahrenheitButton = (RadioButton) findViewById(android.R.id.radio1);
if (text.getText().length() == 0)
{
Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if(celsiusButton.isChecked()){
text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
}else {
text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
}
if(fahrenheitButton.isChecked()){
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
else
{
fahrenheitButton.setChecked(true);
celsiusButton.setChecked(false);
}
break;
}
}
private float convertFahrenheitToCelsius(float fahrenheit){
return ((fahrenheit - 32) * 5 / 9);
}
private float convertCelsiusToFahrenheit(float celsius){
return ((celsius *9) / 5) + 32;
}
}
It should be in dk.ilizane.android.temperatur.R.id.radio1. If you look at the tutorial in the main.xml file the ids of the RadioButtons are radio0 and radio1. Your projects custom resources will be compiled into a class named R in your package. In Eclipse there should be a gen src directory that contains the java file.