I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.
Related
Context: My Style Activity corresponds to a layout with 4 imageButtons and a regular button. I want the user to only be able to select one imageButton at a time. Upon the click of the regular button, I want to send the data regarding which imageButton is selected to my ReviewActivity while simultaneously opening my ReflectionActivity.
I have 2 questions. First, how do I dry up my code surrounding OnClick's and disabled imageButtons? Second, how do I set a variable based on which imageButton was selected and send to another activity with an intent? I am fairly sure I've done this the long/hard way. All suggestions greatly appreciated!
public class StyleActivity extends AppCompatActivity {
Button btn_open_reflection;
ImageButton style1;
ImageButton style2;
ImageButton style3;
ImageButton style4;
public static final String style_selection = "com.example.application.hearttoart.style_selection";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_style );
// set up image buttons for the onClick function
style1 = (ImageButton)findViewById(R.id.style1);
style2 = (ImageButton)findViewById(R.id.style2);
style3 = (ImageButton)findViewById(R.id.style3);
style4 = (ImageButton)findViewById(R.id.style4);
// TODO: DRY up when possible, lots of repeated code here
style1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style2.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style1";
}
});
style2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style2";
}
});
style3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style2.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style3";
}
});
style4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style2.setEnabled(false);
style3.setEnabled(false);
String style_selection = "#string/style4";
}
});
btn_open_reflection =(Button) findViewById(R.id.btn_open_style);
btn_open_reflection.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
// Open Style Activity - Navigate to Activity from the Click
openReflection();
sendStyle();
}
});
}
public void sendStyle() {
Intent styleIntent = new Intent(StyleActivity.this, ReviewActivity.class );
styleIntent.putExtra("style", style_selection);
}
public void openReflection() {
Intent intent = new Intent( this, ReflectionActivity.class );
startActivity( intent );
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_open_reflection;
ImageButton style1;
ImageButton style2;
ImageButton style3;
ImageButton style4;
String style_selection = "com.example.application.hearttoart.style_selection";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set up image buttons for the onClick function
style1 = (ImageButton)findViewById(R.id.style1);
style2 = (ImageButton)findViewById(R.id.style2);
style3 = (ImageButton)findViewById(R.id.style3);
style4 = (ImageButton)findViewById(R.id.style4);
btn_open_reflection =(Button) findViewById(R.id.btn_open_style);
btn_open_reflection.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
// Open Style Activity - Navigate to Activity from the Click
openReflection();
}
});
}
public void openReflection() {
Intent intent = new Intent( MainActivity.this, OtherActivity.class );
intent.putExtra("style", style_selection);
startActivity(intent);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.style1:
//disable other buttons
style2.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style1";
break;
case R.id.style2:
style1.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style2";
break;
case R.id.style3:
style1.setEnabled(false);
style2.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style4";
break;
case R.id.style4:
style1.setEnabled(false);
style2.setEnabled(false);
style3.setEnabled(false);
style_selection = "#string/style4";
break;
}
}
}
This link might help.
I've just started with android and I'm trying to make simple program to take string from user and display the result as Toast
EditText e = (EditText) findViewById(R.id.editText);
final String result = e.getText().toString();
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
Now If I type anything in editText it's supposd to print that value but instead it's printing the default text value and even if I edit that it prints the same value
As you can see in the picture on pressing the button it shows "Name" on toast
and even upon changing it shows the same thing that is "Name". I want it too show the value that I typed later.
What should I do?
You store the text when you setup the views. Thats why you get the default text.
move
final String result=e.getText().toString();
into the onClick
#Override
public void onClick(View v) {
final String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
and it should get the text when the button is pressed.
Reason is your variable String result is not taking latest value when button is clicked.
Try this:
final EditText e = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
your requirement is show EditText Text in Toast
first make EditText object as globle
class ActivityClassFileName extend AppCompactActivity
{
EditText ToastText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
//initialize EditText object
ToastText = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//showing Toast
Toast.makeText(getApplicationContext(), ToastText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
}
Sometimes you have to display toast outside of onclick,by creating new method.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = findViewById(R.id.button);
editText=findViewById(R.id.editTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String testString=editText.getText().toString();
showToast(testString);
}
});
}
private void showToast(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
I'm using the following class and ive been trying for the last 24 hours i cant figure out why its not working. when i press the button it does nothing.
I'm creating this so i can verify the use login information
My class
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG);
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
});
}}
I Added my XML file on https://codeshare.io/5XWxOE
You are creating Toast but do not call it to show its content try code blew :
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG).show();
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
you forgot to use show()
Please try this solution
Toast.makeText(login.this,"it
clicked",Toast.LENGTH_LONG).show;
Instead of the getApplicationContext() you have.
Try this ...
TextView mt;
Button clickButton;
mt=(TextView) findViewById(R.id.messy);
clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "Button click toast", Toast.LENGTH_SHORT).show();
mt.setText("Your text here");
}
});
When something is going wierd.. you can check basic log. try this one~
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setText("Button Found");
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
clickButton.setText("Button Clicked");
}
});
}}
There is another way to write code for button clicking
public class login extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button firstButton = (Button) findViewById(R.id.loginButton);
firstButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginButton: {
Toast.makeText(login.this, "Button click toast", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.messy);
textView.setText("Your text here");
break;
}
default: {
Toast.makeText(login.this, "Something happens", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
I hope this will resolve your issue.
so a quick question. In my app, the users go through multiple activities that provides them with radio-buttons to choose from.. at the final activity, based on there options, the will be shown which character they are etc... Now the problem is I don't know how to write a code in order to do that. Here is what I have
First activity
public class Quiz1 extends Activity {
Button btn;
RadioGroup rg1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
btn = (Button) findViewById(R.id.nextBtn1);
rg1= (RadioGroup) findViewById(R.id.rg1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg1.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle = getIntent().getExtras();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second activity
Button btn;
RadioGroup rg2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz2);
btn = (Button) findViewById(R.id.nextBtn2);
rg2= (RadioGroup) findViewById(R.id.rg2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg2.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz3.class);
Bundle bundle = getIntent().getExtras();
int id = rg2.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg2", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
This continues for about 7 activities
Final activity (where the result and the character are shown)
public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}
Now, in the final activity, I want to have a code where if for example:
if in activity1(Quiz1) options 1 OR 2 are chosen ( as in the radio-buttons selected),
Quiz2: options 2 or 4
Quiz3: options 1 or 4
Quiz2: options 2 or 3
and so on...
then change a textview to something specific like "your character is x"
I have already carried all the information to the final class, I just don't know how to approach this problem, even-though it sounds simple.
Any help would be appreciated, thank you <3
EDIT:
TextView textviewResult = (TextView) findViewById(R.id.textViewResult);
if(bundle.getString("rg").equals("A")||(bundle.getString("rg").equals("B")&& bundle.getString("rg1").equals("B")&& bundle.getString("rg2").equals("Long range weapons")
&& bundle.getString("rg3").equals("C") || bundle.getString("rg3").equals("D") && bundle.getString("rg4").equals("A")||bundle.getString("rg4").equals("B")
|| bundle.getString("rg4").equals("CC") && bundle.getString("rg5").equals("A") || bundle.getString("rg5").equals("E")
&& bundle.getString("rg6").equals("Yes"))) {
textviewResult.setText("x");
}else{
textviewResult.setText("not x");
}
The problem with this is, even if I choose another option for rg (so not the "A" or "B" options), but then for the rest I choose the ones in the If statement, it still ends up saying x(but it should be saying Not x)
You can use the String equals() method inside your if statement. It returns true if both Strings are equal.
E.g.:
if(bundle.getString("rg").equals("YourRadioButtonText")){
...
}
Your code could look something like this:
`public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
// NEW
TextView textviewResult = (TextView) findViewById(R.id.resultTV);
if(bundle.getString("rg").equals("C")){
textviewResult.setText("It is C");
}
else if(bundle.getString("rg1").equals("A") || bundle.getString("rg2").equals("B")){
textviewResult.setText("It is A or B");
}
else if(bundle.getString("rg1").equals("C") && bundle.getString("rg2").equals("D")){
textviewResult.setText("It is C and D");
}
else if(!bundle.getString("rg1").equals("A")){
textviewResult.setText("It is not A");
}
else {
textviewResult.setText("Mhhh");
}
// END
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}`
I started a simple quiz application as below it consists of TextView to show the questions and a RadioGroup consisting of 3 RadioButtons for 3 options. When you finish the quiz it shows number of correct answers and number of wrong answers and your degree. But I have some problems as below Please help me to solve it.
the problems:
When i click on next question its transfer me to result activity not to next questions.
The Result activity page did not show the total result correctly
MainActivity
public class MainActivity extends ActionBarActivity {
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton rb1,rb2,rb3;
String questions[]={"qqqqq","dddddd"};
String ans[]={"",""};
String opt[]={"","","","","",""};
int flag=0;
public static int marks,correct,wrong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvque);
btnNext = (Button) findViewById(R.id.button1);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
rb1 = (RadioButton) findViewById(R.id.radio0);
rb2 = (RadioButton) findViewById(R.id.radio1);
rb3 = (RadioButton) findViewById(R.id.radio2);
tv.setText(questions[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
Toast.makeText(this,"Nigative mark", 1000).show();
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText=uans.getText().toString();
if(ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
}
else
{
wrong++;
}
flag++;
if(flag<questions.length)
{
tv.setText(questions[flag]);
rb1.setText(opt[flag*3]);
rb2.setText(opt[(flag*3)+1]);
rb3.setText(opt[(flag*3)+2]);
}
else
{
marks=correct;
}
Intent i=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(i);
}
});
}
}
ResultActivity
public class ResultActivity extends ActionBarActivity {
TextView tv;
Button btRestart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv=(TextView) findViewById(R.id.textView1);
btRestart=(Button) findViewById(R.id.button1);
StringBuffer sb=new StringBuffer();
sb.append("Correct ANS:"+MainActivity.correct);
sb.append("Wrong ANS:"+MainActivity.wrong);
sb.append("Final Score:"+MainActivity.marks);
tv.setText(sb);
MainActivity.correct=0;
MainActivity.wrong=0;
btRestart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
}
}
When i click on next question its transfer me to result activity not to next questions.
You are telling it to go to the ResultsActivity in your Intent
// 2nd param is the target Activity
Intent i=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(i);
To fix that, you would need to use the name of the Activity where your next question is to replace ResultActivity. Unless you store them in an Array or other type of list then you could just replace the question TextView text with the next question.
The Result activity page did not show the total result correctly
This is nearly impossible to answer without knowing your expected and actual results.