Creating a series of radio groups in Android Eclipse - java

Brilliant stuff Umesh, thanks! You've taught me a lot there!
I've one more question if you don't mind.
I would now like to choose one radio button from each radiogroup (for Example, Chinese, Level 1, Guess the City) and then have the message at the top read "You chose Chinese, Level 1, Guess the City". How can I write this in the Java script. so far I have:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;
private RadioGroup radioGroup3;
private RadioButton chinese, indian, russian;
private Button button;
private TextView textView;
public String string_first_radiogroup;
public String string_second_radiogroup;
public String string_third_radiogroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup1 = (RadioGroup) findViewById(R.id.radio1);// your radio group1
radioGroup2 = (RadioGroup) findViewById(R.id.radio2);// your radio group2
radioGroup3 = (RadioGroup) findViewById(R.id.radio3);// your radio group3
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.chinese) {
string_first_radiogroup="Chinese"; // String updated on radio check
} else if(checkedId == R.id.indian) {
string_first_radiogroup="Indian"; // String updated on radio check
} else {
string_first_radiogroup="Russian"; // String updated on radio check
}
}
});
chinese = (RadioButton) findViewById(R.id.chinese);
indian = (RadioButton) findViewById(R.id.indian);
russian = (RadioButton) findViewById(R.id.russian);
textView = (TextView) findViewById(R.id.text);
radioGroup1.check(R.id.chinese);// setting Chinese selected by default
string_first_radiogroup="Chinese"; //setting "Chinese" by default in string also
radioGroup2.check(R.id.difficulty1);
string_second_radiogroup="Level 1 (Starter)";
button = (Button)findViewById(R.id.choose);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("You chose " + string_first_radiogroup + " option");
textView.setText("You chose " + string_second_radiogroup + " option");
}
});
}
}

Do the same in onCreate() method for radioGroup2 and radioGroup3 .
Reduce your code by Declaring a public string And update it in radioGroup.onCheckedChangeListener
Where you are using toast
On button click just display that string .
No need to check in button clickListener.
For 3 radio groups use 3 strings.
public String string_first_radiogroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup1 = (RadioGroup) findViewById(R.id.radio1);// your radio group1
radioGroup2 = (RadioGroup) findViewById(R.id.radio2);// your radio group2
radioGroup3 = (RadioGroup) findViewById(R.id.radio3);// your radio group3
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.chinese) {
Toast.makeText(getApplicationContext(), "Choice: Chinese",
Toast.LENGTH_SHORT).show();
string_first_radiogroup="Chinese"; // String updated on radio check
} else if(checkedId == R.id.indian) {
Toast.makeText(getApplicationContext(), "Choice: Indian",
Toast.LENGTH_SHORT).show();
string_first_radiogroup="Indian"; // String updated on radio check
} else {
Toast.makeText(getApplicationContext(), "Choice: Russian",
Toast.LENGTH_SHORT).show();
string_first_radiogroup="Russian"; // String updated on radio check
}
}
});
chinese = (RadioButton) findViewById(R.id.chinese);
indian = (RadioButton) findViewById(R.id.indian);
russian = (RadioButton) findViewById(R.id.russian);
textView = (TextView) findViewById(R.id.text);
radioGroup.check(R.id.chinese);// setting Chinese slected by default
string_first_radiogroup="Chinese"; //setting "Chinese" by default in string also
button = (Button)findViewById(R.id.choose);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("You chose " + string_first_radiogroup + " option");
}
});
}

Related

getText and setText function not working in android studio

i tried to make my first converter in android studio with java but 2 functions setText and getText are not working
package com.example.unitconvertor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity<editTextTextPersonName> extends AppCompatActivity {
private Button button;
private editTextTextPersonName editTextTextPersonName;
private View editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "value saved", Toast.LENGTH_SHORT).show();
String s = editText.getText().toString();
int inch = Integer.parseInt(s);
double cm = 2.54 * inch;
editText.setText( "the value is" + cm);
}
});
}
}
i hope that i am clear
thankyou
Try this-
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.your_id);//ADD YOUR EDIT TEXT ID HERE
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "value saved", Toast.LENGTH_SHORT).show();
String s = editText.getText().toString();
int inch = Integer.parseInt(s);
double cm = 2.54 * inch;
editText.setText( "the value is" + cm);
}
});
}
}
The error is that your "edittext" is declared as view, correct it like this : private EditText edittext;
then, retrieve it by id like this in your onCreate method edittext = findViewById(R.id.your_id);

Android Studio Java code

I'm trying to replace a part of the text after clicking the bottom but I can't make it work. When I click the bottom without entering a name the message should say "Hello Username", and if I enter a name the "Username" should be replace by the name. Here is the code so far. Thanks
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class HelloAndroid extends AppCompatActivity implements OnClickListener {
private EditText name;
private TextView display;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_android);
name = (EditText) findViewById(R.id.name);
display = (TextView) findViewById(R.id.display);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_hello_android, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view) {
if (button == view) {
String message = "Hello, Username " + name.getText().toString() + "!";
if (button == view) {
String username = "Hello " + name.getText().toString() + "";
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
}
}
}
}
You are enable to do this because may be you are not getting click here . Here is my code change it .
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.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class HelloAndroid extends AppCompatActivity implements View.OnClickListener {
private EditText name;
private TextView display;
private Button button;
String message ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
name = (EditText) findViewById(R.id.name);
display = (TextView) findViewById(R.id.display);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button :
if (name.getText().toString().isEmpty())
{
message = "Hello username" ;
}
else {
message = "Hello "+name.getText().toString() ;
}
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
break;
}
}
}

Counter not incrementing for radio button in android

am using an if statement to increment the answer for a quiz app in android. i think for loop or while loop should work berrter. But i hae also tested that . am now getting it well.
Below is my code. Thank you
package newton.quizapplication;
import android.app.Activity;
import android.database.CursorJoiner;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyActivity extends Activity {
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;
private RadioGroup radioGroup3;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private Button btnSubmit;
private int Ans = 0;
private TextView Total;
// private int wrong = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
String All;
Total = (TextView)findViewById(R.id.Score);
addListenerOnButton();
}
public void addListenerOnButton() {
radioGroup1 = (RadioGroup) findViewById(R.id.rg1);
radioGroup2 = (RadioGroup) findViewById(R.id.rg2);
radioGroup2 = (RadioGroup) findViewById(R.id.rg3);
btnSubmit = (Button) findViewById(R.id.submit);
btnSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId1 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton1 = (RadioButton) findViewById(selectedId1);
// get selected radio button from radioGroup
int selectedId2 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton2 = (RadioButton) findViewById(selectedId2);
// get selected radio button from radioGroup
int selectedId3 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton3 = (RadioButton) findViewById(selectedId3);
if (radioButton1.getText().equals("Hyper Text Markup Language")) {
Ans++;
}
else if (radioButton2.getText().equals("The World Wide Web Consortium")) {
// Toast.makeText(MyActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show();
Ans++;
}
else if (radioButton2.getText().equals("Specific places within a particular page")) {
Ans++;
}
Total.setText("You Got " + Ans + " Answer correct out of 3");
Ans =0;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You should change your conditions to :
if (radioButton1.getText().equals("Hyper Text Markup Language")) {
Ans++;
}
if (radioButton2.getText().equals("The World Wide Web Consortium")) {
// Toast.makeText(MyActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show();
Ans++;
}
if (radioButton3.getText().equals("Specific places within a particular page")) {
Ans++;
}
In your original code of if (...) ... else if (...) ... else if (...) ..., only one condition can be met (since the first time a condition evaluates to true, no other conditions would be checked), so Ans can only be 0 or 1.
In addition, I'm assuming your last if should check radioButon3.

Null pointer exception when setting results in a textview

The application is crashing when pressing the Computecost button
package com.example.hw_3;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
public class ShoppingExpensesPage extends Activity
{
TextView et;
Button computecost;
Button save;
Button cancel;
RadioGroup drinks;
RadioButton drink;
int tottalcost=0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
et=(TextView)findViewById(R.id.tv11);
Bundle extra = getIntent().getExtras();
String val1 = extra.getString("value");
et.setText(val1);
computecost=(Button)findViewById(R.id.btn11);
computecost.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
String val;
int selectedId = drinks.getCheckedRadioButtonId();
drink = (RadioButton)findViewById(selectedId);
val=(String) drink.getText();
if(val=="Juice")
{tottalcost=tottalcost+3;
}
else if (val=="Cola")
{
tottalcost=tottalcost+2;
}
et.setText(Integer.toString(tottalcost));
}
});
save=(Button)findViewById(R.id.btn21);
save.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
String str=Integer.toString(tottalcost);//(String) et.getText();
returnIntent.putExtra("return", str);
setResult(RESULT_OK,returnIntent);
finish();
}
});
}
}
You haven't set the view for drinks.
int selectedId = drinks.getCheckedRadioButtonId();
Find a view for it, before computecost.setOnClickListener:
drinks = (RadioGroup) findViewById(...);
This is the wrong way to compare Strings
if(val=="Juice")
In Java, "==" compares the reference of the Objects but not their values. You need to use .equals()
if("Juice".equals(val))
{ // do something }
If this doesn't solve your problem then please post the logcat from the error but this still needs to be changed.

Working with checkbox. Checkbox string value will transfer to edittext when checked

Supposedly i have 3 checkboxes, Each checkboxes has a corresponding string value. What i want is that when i check checkbox 1 and 2, their corresponding strings or value will be all put over a edittext.
Example:
[✓] Checkbox 1 (Egg)
[✓] Checkbox 2 (Hotdog)
[ ] Checkbox 3 (Cheese)
When I check boxes 1 and 2. It will go to edittext and view as "Egg"\n + "Hotdog" or similar.
Output:
Edittext:
Egg
Hotdog
Is that possible? Thanks for your help!
package com.example.checkboxes;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private CheckBox egg, hotdog, cheese;
private OnClickListener checkboxclicklistener;
private EditText display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
egg = (CheckBox) findViewById(R.id.a);
cheese = (CheckBox) findViewById(R.id.c);
hotdog = (CheckBox) findViewById(R.id.b);
display = (EditText) findViewById(R.id.display);
createListener(egg);
createListener(hotdog);
createListener(cheese);
}
public void createListener(CheckBox checkbox) {
checkbox.setOnClickListener( new OnClickListener() {
StringBuilder checkeditems = new StringBuilder();
#Override
public void onClick(View v) {
//is checkbox checked?
if (((CheckBox) v).isChecked()) {
String checkboxname = ((CheckBox) v).getText().toString();
Toast.makeText(getApplicationContext(), checkboxname, Toast.LENGTH_LONG).show();
if(checkboxname.equalsIgnoreCase("egg"))
{
display.setText(display.getText().toString()+"\nEgg");
}
else if(checkboxname.equalsIgnoreCase("hotdog"))
{
display.setText(display.getText().toString()+"\nHotdog");
}
else if(checkboxname.equalsIgnoreCase("cheese"))
{
display.setText(display.getText().toString()+"\nCheese");
}
}
else if (((CheckBox) v).isChecked()==false)
{
String checkboxname = ((CheckBox) v).getText().toString();
if(checkboxname.equalsIgnoreCase("egg"))
{
display.setText(display.getText().toString().replace("\nEgg", ""));
}
else if(checkboxname.equalsIgnoreCase("hotdog"))
{
display.setText(display.getText().toString().replace("\nHotdog", ""));
}
else if(checkboxname.equalsIgnoreCase("cheese"))
{
display.setText(display.getText().toString().replace("\nCheese", ""));
}
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You could use setOnClickListener like so
CheckBox chkEgg;
EditText yourEditText;
String yourText;
public void addListenerOnChkIos() {
chkEgg = (CheckBox) findViewById(R.id.checkbox1);
chkEgg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is chkEgg checked?
if (((CheckBox) v).isChecked()) {
yourText = yourText + "Egg";
yourEditText.setText(yourText);
}
}
});
}
I only did it for one
Hope this helps

Categories