Why can't button display score on the screen? - java

I am trying create a simple quiz app where depending from user choices in checkBoxes, submitButton displays the result on the screen. When answer is right score increase 1 point, if answer is wrong score decrease 1 point.
I am totaly stuck!
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int score;
Button submitButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitButton = (Button) findViewById(R.id.submitAnswer);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submitButton.setText(score);
}
});
}
public void checkBoxes(View view) {
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
boolean hasCheckBox1 = checkBox1.isChecked();
CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
boolean hasCheckBox2 = checkBox2.isChecked();
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()){
case R.id.checkBox1:
Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
score ++;
break;
case R.id.checkBox2:
Toast.makeText(this,"Sorry, try again!", Toast.LENGTH_SHORT).show();
score --;
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.allyouask.practicebutton.MainActivity">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RightAnswer"
android:id="#+id/checkBox1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="179dp"
android:onClick="checkBoxes"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WrongAswer"
android:id="#+id/checkBox2"
android:layout_below="#+id/checkBox1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:onClick="checkBoxes"/>
<Button
android:id="#+id/submitAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onClick"/>
</RelativeLayout>

Your score in int type, you use setText(int) in this case. So you're trying to load resource with id==score.
Try to modify arguments, to use this version of the setText method:
submitButton.setText(String.valueOf(score))

you have to call an update method in order to update text on the Button or update its content other way, like this below
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int score;
Button submitButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitButton = (Button) findViewById(R.id.submitAnswer);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submitButton.setText(String.valueOf(score));
}
});
}
public void checkBoxes(View view) {
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
boolean hasCheckBox1 = checkBox1.isChecked();
CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
boolean hasCheckBox2 = checkBox2.isChecked();
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()){
case R.id.checkBox1:
Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
score ++;
break;
case R.id.checkBox2:
Toast.makeText(this,"Sorry, try again!", Toast.LENGTH_SHORT).show();
score --;
break;
}
//submitButton.setText(String.valueOf(score));
}
}

try this --> Replace int score; line your code with int score = 0; and check if it works.

you should use:
private int score=0

Related

How to get Hint and Text of a button In android studio

I am using this code to get the id of the button which is clicked and then use the id to get the text and hint of the same button
I don't know why but maybe this code this crashing the app after I use getID() method.
I am using Relative Layout.
The Java code
package com.example.firstapp;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity{
public AlertDialog.Builder dialogBuilder;
public AlertDialog dialog;
public EditText Name,Password;
public Button Save,Cancel,temp,ok,x;
public String file_location = "Passwords.txt";
public String name_x,pass_y;
public LinearLayout mylayout = null;
public RelativeLayout rl = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void View_Password(Button x){
ok = (Button) findViewById(R.id.button6);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
};
public void NewPassword(){
dialogBuilder = new AlertDialog.Builder(this);
final View NewPassSaveView = getLayoutInflater().inflate(R.layout.newpasspopup, null);
Name = (EditText) NewPassSaveView.findViewById(R.id.editTextTextPersonName);
Password = (EditText) NewPassSaveView.findViewById(R.id.editTextTextPersonName2);
Save = (Button) NewPassSaveView.findViewById(R.id.button4);
Cancel = (Button) NewPassSaveView.findViewById(R.id.button);
dialogBuilder.setView(NewPassSaveView);
dialog = dialogBuilder.create();
dialog.show();
Save.setOnClickListener(new View.OnClickListener() {
#SuppressLint("ResourceType")
#Override
public void onClick(View view) {
if (TextUtils.isEmpty(Name.getText().toString())) {
Name.setError("This Field is compulsory");
return;
} else if (TextUtils.isEmpty(Password.getText().toString())) {
Password.setError("This Field is compulsory");
return;
}
rl = (RelativeLayout) findViewById(R.id.layout2);
Toast.makeText(getApplicationContext(), "Password Saving...", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Password Saved", Toast.LENGTH_SHORT).show();
Button temp = new Button(MainActivity.this);
temp.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
));
rl.addView(temp);
temp.setText(Name.getText().toString());
temp.setHint(Password.getText().toString());
temp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int btn_id = v.getId();
btn2string(btn_id);
}
});
dialog.dismiss();
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
public void AddNew(View view) {
NewPassword();
}
public void btn2string(int x){
Button btn = findViewById(x);
name_x = btn.getText().toString();
pass_y = btn.getHint().toString();
See_Password(name_x,pass_y);
}
public void See_Password(String name,String pass){
Toast.makeText(getApplicationContext(), "Name: "+name+"\n"+"Password: "+pass, Toast.LENGTH_SHORT).show();
}
}
//The XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/heading"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView4"
android:layout_width="319dp"
android:layout_height="50dp"
android:text="Password Manager"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:onClick="AddNew"
android:text="Add new"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
app:layout_constraintVertical_bias="0.973" />
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="366dp"
android:layout_height="541dp"
android:layout_marginTop="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.488"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
app:layout_constraintVertical_bias="0.0"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Please tell if there is any other way to do this or tell the mistakes in the above code so that I can get the hint and text of that button which is clicked...
Thank You.
Inside,
NewPassword()
method just change the following code.
public void NewPassword(){
......
save.setOnClickListener(...){
........
temp.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
int btn_id = v.getId(); // remove this line.
btn2String(temp); //directly pass temp i.e. Button you created.
}
});
dialog.dismiss();
}
});
..........
}
And, pass Button type as a parameter in btn2String() method. as below:
public void btn2String(Button x){
Button btn = findViewById(x); // remove this line of code.
name_x = x.getText().toString();
pass_y = x.getHint().toString();
See_Password(name_x, pass_y);
}

The application has stopped unexpectedly. please try again "Force close" error in my android codes

I wrote below codes for Android in Eclipse:
package mohammad.negahdari.mystartup4;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyStartup4Activity extends Activity {
public int counter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
final Button btn1 = (Button) findViewById(R.id.txtCaption);
txtCaption.setText("Hooora");
txtCaption.setTextColor(Color.parseColor("#0000ff"));
txtCaption.setBackgroundColor(Color.parseColor("#ffffff"));
for (int i = 0; i <= 4; i++)
Toast.makeText(MyStartup4Activity.this, "Mohammad " + i, Toast.LENGTH_SHORT).show();
txtCaption.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("LOG", "Clicked");
Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
counter++;
txtCaption.setText("Number is : " + counter);
}
});
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("LOG", "Clicked");
Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
txtCaption.setVisibility(View.GONE);
}
});
}
}
and receive this error:
The application has stopped unexpectedly. please try again
But when I delete these lines:
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("LOG", "Clicked");
Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
txtCaption.setVisibility(View.GONE);
}
});
I have no error.
My xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff00ff"
android:gravity="center"
android:text="..."
android:textColor="#00ff00"
android:textSize="40dip" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
The application has stopped unexpectedly. please try again But when I
delete these lines: have no error.
Obviously,you are referring to the wrong id
final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
final Button btn1 = (Button) findViewById(R.id.txtCaption);
Check your button id in xml. You should assign different id to your button.
In your case, you should use
final Button btn1 = (Button) findViewById(R.id.btn1);
final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
final Button btn1 = (Button) findViewById(R.id.txtCaption);
Both txtCaption and btn1 point to the same id (R.id.txtCaption)
My xml codes :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff00ff"
android:gravity="center"
android:text="..."
android:textColor="#00ff00"
android:textSize="40dip" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

How to move a setText() to a new activity on Android Studio?

I am tying to make a basket and want depending on what button is pressed the price that is shown on the screen then appear in the basket (new activity) in TextView09 ( section of the table) but at the moment what is appearing is android.support.v7.widget.App...
Here is the activity page for the product:
package com.example.emily.woodensigns;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.LayoutInflater;
public class Letters extends AppCompatActivity {
int txtSize = 14;
EditText Wood;
Button bSize, bSize1, bSize2, bSize3, bBasket;
public int count = 5;
private LayoutInflater mInflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_letters);
Wood = (EditText) findViewById(R.id.Wood);
Button bSize = (Button) findViewById(R.id.bSize);
Button bSize1 = (Button) findViewById(R.id.bSize1);
Button bSize2 = (Button) findViewById(R.id.bSize2);
Button bSize3 = (Button) findViewById(R.id.bSize3);
Button bBasket = (Button) findViewById(R.id.bBasket);
final TextView price = (TextView) findViewById(R.id.price);
bSize.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Wood.setTextSize(40);
price.setText("£10");
TextView price = (TextView) findViewById(R.id.price);
String pri = price.getText().toString();
Intent intent2 = new Intent(Letters.this, Letters.class);
intent2.putExtra("£",pri);
}
});
bSize1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Wood.setTextSize(60);
price.setText("£20");
}
});
bSize2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Wood.setTextSize(100);
price.setText("£35");
}
});
bSize3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Wood.setTextSize(150);
price.setText("£50");
}
});
bBasket.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Wood = (EditText) findViewById(R.id.Wood);
String str = Wood.getText().toString();
if (str.length() == 0) {
Wood.requestFocus();
Wood.setError("FIELD CANNOT BE EMPTY");
} else if (str.length() >= 2) {
Wood.requestFocus();
Wood.setError("You can only type one letter!");
} else {
Intent intent2 = new Intent(Letters.this, Basket.class);
intent2.putExtra("MY_INFO",str + " - Letters" + price );
startActivity(intent2);
}
}
});
}
}
Activity for the Basket:
package com.example.emily.woodensigns;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;
public class Basket extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_basket);
TextView TextView08 = (TextView) findViewById(R.id.TextView08);
TextView TextView09 = (TextView) findViewById(R.id.TextView09);
Button bCheckout=(Button) findViewById(R.id.bCheckout);
String strFromActivity = getIntent().getStringExtra("MY_INFO");
TextView08.setText(strFromActivity);
String priFromActivity = getIntent().getStringExtra("£" );
TextView09.setText(priFromActivity);
Toast.makeText(getBaseContext(),priFromActivity, Toast.LENGTH_LONG).show();
bCheckout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent2 = new Intent(Basket.this, customerDetails.class);
startActivity(intent2);
}
});
}
}
Basket xml file:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="259dp"
android:shrinkColumns="*" android:stretchColumns="*" android:background="#ffffff">
<!-- Row 1 with single column -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text=""
android:layout_span="3"
android:padding="18dip"
android:background="#b0b0b0"
android:textColor="#000"/>
</TableRow>
<!-- Row 2 with 3 columns -->
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/TextView04"
android:layout_weight="1"
android:background="#dcdcdc"
android:textColor="#000000"
android:padding="20dip"
android:gravity="left"/>
<TextView
android:id="#+id/TextView05"
android:text="Product"
android:layout_weight="1"
android:background="#dcdcdc"
android:textColor="#000000"
android:padding="20dip"
android:gravity="center"/>
<TextView
android:id="#+id/TextView06"
android:text="Price"
android:layout_weight="1"
android:background="#d3d3d3"
android:textColor="#000000"
android:padding="20dip"
android:gravity="center"/>
</TableRow>
<TableRow>
<TextView
android:id="#+id/TextView07"
android:layout_weight="1"
android:background="#dcdcdc"
android:textColor="#000000"
android:padding="20dip"
android:gravity="left"/>
<TextView
android:id="#+id/TextView08"
android:text="Product"
android:layout_weight="1"
android:background="#dcdcdc"
android:textColor="#000000"
android:padding="20dip"
android:gravity="center"/>
<TextView
android:id="#+id/TextView09"
android:text="Price"
android:layout_weight="1"
android:background="#d3d3d3"
android:textColor="#000000"
android:padding="20dip"
android:gravity="center"/>
</TableRow>
</TableLayout>
thank you
Your btn click code should like below:
bBasket.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Wood = (EditText) findViewById(R.id.Wood);
String str = Wood.getText().toString();
if (str.length() == 0) {
Wood.requestFocus();
Wood.setError("FIELD CANNOT BE EMPTY");
} else if (str.length() >= 2) {
Wood.requestFocus();
Wood.setError("You can only type one letter!");
} else {
Intent intent2 = new Intent(Letters.this, Basket.class);
intent2.putExtra("MY_INFO",str + " - Letters" + price );
TextView price = (TextView) findViewById(R.id.price);
String pri = price.getText().toString();
intent2.putExtra("£",pri);
startActivity(intent2);
}
}
});
Hope this will help you.

How to hide next button and show when radio group button is selected

How to I hide the next button and will only show when the user choose a radio group button
I have this XML code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/assembly"
android:checked="false"
/>
<RadioButton
android:id="#+id/csharp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/java"
android:checked="false" />
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:onClick="OnClick"
/>
</RadioGroup>
</LinearLayout>
and this class code
package com.example.quiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
* Created by leste on 3/5/2016.
*/
public class CS_Category extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cs_category);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
if (radioSexButton.getId() == R.id.assembly) {
Intent i = new Intent(CS_Category.this, CS_Assembly.class);
startActivity(i);
} else {
if (radioSexButton.getId() == R.id.csharp) {
Intent i = new Intent(CS_Category.this, CS_Csharp.class);
startActivity(i);
} else {
if (radioSexButton.getId() == R.id.java) {
Intent i = new Intent(CS_Category.this, CS_Java.class);
startActivity(i);
}
}
Toast.makeText(CS_Category.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
What should I do to hide the next button and show only if there is a selected radio button
In your button xml you should use:
android:visibility="gone"
In java you should use:
btnDisplay.setVisibility(View.VISIBLE);
First of all I would take the button out of the radio group and just have it be below it. Then in my java code I would have this function:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.assembly:
if (checked)
make_button_visible();
break;
case R.id.csharp:
if (checked)
make_button_visible();
break;
}
}
Make sure that each radio button has onClick="onRadioButtonClicked" set.
Then have a function called make_button_visible() that has these lines:
Button mButton=(Button)findViewById(R.id.btnDisplay);
mButton.setVisibility(View.VISIBLE);//This will make it visible
add below code in your onCreate method
addListenerOnButton();
btnDisplay.setVisibility(View.INVISIBLE);
radioSexGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (btnDisplay.getVisibility() == View.INVISIBLE)
btnDisplay.setVisibility(View.VISIBLE);
}
});

Unfortunately (My App) Has Stopped Working

Okay, this happened when I created a simple ImageButton. I don't see what I'm doing wrong though... Here's my code:
(My Activity.Xml File):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/tapbgh"
android:orientation="vertical" >
<TextView
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:text="#string/level01"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/Score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:text="#string/click0"
android:textColor="#FFFFFF"
android:textSize="16pt" />
<Chronometer
android:id="#+id/Timer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_horizontal"
android:text="01:00"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/Tap"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.81"
android:background="#drawable/buttonbg" />
<Button
android:id="#+id/Menu"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Menu" />
<ImageButton
android:id="#+id/Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/resetbtn"
android:src="#drawable/resetbtn" />
</LinearLayout>
(My .Java File):
package my.first.app;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
//Click Events Below This Line
public class Tap_Game_Activity extends Activity {
private Button tapBtn;
private Button rstBtn;
int scr = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Menu = (Button) findViewById(R.id.Menu);
registerForContextMenu(Menu);
tapBtn = (Button) findViewById(R.id.Tap);
rstBtn = (Button) findViewById(R.id.Reset);
rstBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView Score = (TextView) findViewById(R.id.Score);
Score.setText("0");
scr = 0;
}
});
tapBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scr = scr + 1;
TextView Score = (TextView) findViewById(R.id.Score);
Score.setText(String.valueOf(scr));
}
});
}
// Options Menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderTitle("Menu");
menu.add(0, view.getId(), 0, "Resume");
menu.add(0, view.getId(), 0, "Quit");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Invoke Context Function 1") {
contextFunction1(item.getItemId());
}
else if(item.getTitle()=="Invoke Context Function 2") {
contextFunction2(item.getItemId());
}
else {
return false;
}
return true;
}
public void contextFunction1(int id){
Toast.makeText(this, "function 1 invoked!", Toast.LENGTH_SHORT).show();
}
public void contextFunction2(int id){
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Tap_Game_Activity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
I feel like it may be in this line:
rstBtn = (Button) findViewById(R.id.Reset);
But I still don't understand, any help would be great, thank you.
API tells me, that we can't cast ImageButton to Button. Try to declare rstBtn as an ImageButton and adjust the cast.
rstBtn = (Button) findViewById(R.id.Reset);
You are casting ImageButton to Button when ImageButton does not inherit from Button.
http://developer.android.com/reference/android/widget/ImageButton.html
BTW: Please choose a title that better describes your problem and include every error messages you get in your question.
private ImageButton rstBtn;
....
rstBtn = (ImageButton) findViewById(R.id.Reset);
rstBtn.setOnClickListener(........

Categories

Resources