I'm currently developing an mobile grocery app. I just want to ask you why my code in layout Y cant throw the details from different activity, but layout X can! As you can see, I used checkbox to checked all items you want to purchase, but in Layout Y, if u checked the item you want and click the "add to cart" button it crashed.
Tho I reedit the code based on respective information and copy to layout Y.
Here is my Code
Baby Diaper (Layout X) Java
public class Baby_Diaper extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoCart;
Intent i = new Intent(this, Shopping_List.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baby__diaper);
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_diaper);
}
public void SelectItem (View view) {
boolean checked = ((CheckBox) view) .isChecked();
switch (view.getId())
{
case R.id.pampers:
if(checked)
{selection.add("Pampers");}
else
{
selection.remove ("Pampers");
}
break;
case R.id.huggies:
if(checked)
{selection.add("Huggies");}
else
{
selection.remove ("Huggies");
}
break;
case R.id.johnsons:
if(checked)
{selection.add("Johnsons");}
else
{
selection.remove ("Johnsons");
}
break;
case R.id.supreme:
if(checked)
{selection.add("Supreme");}
else
{
selection.remove ("Supreme");
}
break;
}
}
public void ocaddtocart(View view){
String final_shopping_selection = "";
for (String Selections : selection){
final_shopping_selection = final_shopping_selection + Selections + "\n";
}
final_text.setText(final_shopping_selection);
final_text.setEnabled(true);}
public void ocgtshoppinglist (View view){
Intent x = new Intent(Baby_Diaper.this, Shopping_List.class);
x.putExtra("items", final_text.getText().toString());
startActivity(x);
}
Baby Food (Layout Y) Java
public class Baby_Food extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoFood;
Intent i = new Intent(this, Shopping_List.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addtoFood = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
setContentView(R.layout.activity_baby__food);
}
public void SelectItem (View view) {
boolean checked = ((CheckBox) view) .isChecked();
switch (view.getId())
{
case R.id.coryandgate:
if(checked)
{selection.add("Cory & Gate");}
else
{
selection.remove ("Cory & Gate");
}
break;
case R.id.gerber:
if(checked)
{selection.add("Gerber");}
else
{
selection.remove ("Gerber");
}
break;
case R.id.hipp:
if(checked)
{selection.add("Hipp");}
else
{
selection.remove ("Hipp");
}
break;
}
}
public void ocaddtocart(View view){
String final_shopping_selection = "";
for (String Selections : selection){
final_shopping_selection = final_shopping_selection + Selections + "\n";
}
final_text.setText(final_shopping_selection);
final_text.setEnabled(true);
}
public void ocgtshoppinglist (View view){
Intent x = new Intent(Baby_Food.this, Shopping_List.class);
x.putExtra("items", final_text.getText().toString());
startActivity(x);
}
Baby Diaper (Layout X) XML
<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"
tools:context="com.example.admin.mobile_grocery.Baby_Diaper"
android:id="#+id/baby_diaper">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pampers"
android:id="#+id/pampers"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:checked="false"
android:onClick="SelectItem"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/huggies"
android:id="#+id/huggies"
android:layout_below="#+id/pampers"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/johnsons"
android:id="#+id/johnsons"
android:layout_below="#+id/huggies"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
android:inputType="textNoSuggestions"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/supreme"
android:id="#+id/supreme"
android:layout_below="#+id/johnsons"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
android:inputType="textNoSuggestions"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/addtocart"
android:id="#+id/addtocart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:inputType="textNoSuggestions"
android:onClick="ocaddtocart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello Shoppers!"
android:id="#+id/final_shopping_diaper"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO SHOPPING LIST"
android:id="#+id/gt_shopping_list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="ocgtshoppinglist"
/>
Baby Food (Layout Y) XML
<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"
tools:context="com.example.admin.mobile_grocery.Baby_Food"
android:id="#+id/baby_food">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cory & Gate"
android:id="#+id/coryandgate"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:checked="false"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gerber"
android:id="#+id/gerber"
android:layout_below="#+id/coryandgate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hipp"
android:id="#+id/hipp"
android:layout_below="#+id/gerber"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD TO CART / REMOVE"
android:id="#+id/addtocart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="ocaddtocart"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello Shoppers!"
android:id="#+id/final_shopping_food"
android:layout_below="#+id/hipp"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO SHOPPING LIST"
android:id="#+id/gt_shopping_list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="ocgtshoppinglist" />
When I clicked the link on Logcat it pointed me to
final_text.setText(final_shopping_selection); of layout Y
try to change it in Y
addtoFood = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
setContentView(R.layout.activity_baby__food);
to
setContentView(R.layout.activity_baby__food);
addtoFood = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
I think your text view is null in addtocart method.
Related
xml mainactivity screen 1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#00008b"
android:id="#+id/page"
>
<TextView
android:id="#+id/game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The Random Game"
android:textStyle="bold|italic"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:textColor="#006400"
android:outlineSpotShadowColor="#2196F3"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/buttonstart"
android:text="Start"
android:textStyle="bold|italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/game"
android:layout_centerInParent="true"
android:textColor="#006400"
android:background="#8b0000"
/>
<TextView
android:id="#+id/TVshowmessage"
android:text="the number is"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonstart"
android:layout_centerInParent="true"
android:textColor="#006400"
/>
<Button
android:id="#+id/buttonplus"
android:text="+"
android:textStyle="bold|italic"
android:textSize="#android:dimen/app_icon_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/TVshowmessage"
android:layout_centerInParent="true"
android:textColor="#006400"
android:background="#8b0000"
/>
<TextView
android:id="#+id/TVcurrentnum"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonplus"
android:layout_centerInParent="true"
android:textColor="#006400"
android:background="#8b0000"
/>
<Button
android:id="#+id/buttonminus"
android:text="-"
android:textSize="#android:dimen/app_icon_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/TVcurrentnum"
android:layout_centerInParent="true"
android:textColor="#006400"
android:background="#8b0000"
/>
<TextView
android:id="#+id/tvcounter"
android:text="counter"
android:layout_width="wrap_content"
android:layout_height="66dp"
android:layout_centerInParent="true"
android:layout_marginBottom="300dp"
android:background="#8b0000"
android:textColor="#006400"
android:textStyle="bold|italic"
android:textAlignment="center"
android:layout_below="#id/buttonrestart"
/>
<Button
android:id="#+id/buttoncheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:textColor="#006400"
android:background="#8b0000"
android:textStyle="bold|italic"
android:outlineSpotShadowColor="#008B02"
android:text="Check"
android:layout_below="#id/buttonminus"
/>
<Button
android:id="#+id/buttonrestart"
android:text="restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#006400"
android:background="#8b0000"
android:textStyle="bold|italic"
android:layout_below="#id/buttoncheck"
/>
</RelativeLayout>
foundnum xml screen 2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00008b"
android:id="#+id/grats"
>
<TextView
android:id="#+id/tvfound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="congrats"
android:textColor="#006400"
android:background="#8b0000"
android:textStyle="bold|italic"
android:layout_above="#+id/buttonrestart1"
android:layout_centerInParent="true"
/>
<Button
android:id="#+id/buttonrestart1"
android:text="restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#006400"
android:background="#8b0000"
android:textStyle="bold|italic"
/>
</RelativeLayout>
main activity java.
package com.example.findrandomnumber;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button buttonstart, buttonplus, buttonminus, buttoncheck, buttonrestart,buttonrestart1;
private TextView TVcurrentnum, TVshowmessage, tvcounter,tvfoundnum;
private String TAG = "gilog",currentnum,counter;
private Controllerguessnum myC;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: hi");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonstart =(Button) findViewById(R.id.buttonstart);
buttonplus = (Button)findViewById(R.id.buttonplus);
buttonminus =(Button) findViewById(R.id.buttonminus);
buttoncheck = (Button)findViewById(R.id.buttoncheck);
buttonrestart = (Button)findViewById(R.id.buttonrestart);
buttonrestart1 = (Button)findViewById(R.id.buttonrestart1);
TextView tvcounter= findViewById(R.id.tvcounter);
TextView TVshowmessage= findViewById(R.id.TVshowmessage);
TextView TVcurrentnum= findViewById(R.id.TVcurrentnum);
TextView tvfoundnum= findViewById(R.id.tvfound);
findViewById(R.id.page);
findViewById(R.id.grats);
buttonstart.setOnClickListener(this);
buttonrestart.setOnClickListener(this);
buttoncheck.setOnClickListener(this);
buttonplus.setOnClickListener(this);
buttonminus.setOnClickListener(this);
Log.d(TAG, "onCreate: set clickers worked");
}
public void startGame() {
myC = new Controllerguessnum();
Log.d(TAG, "startGame: random number is " + myC.crandom());
}
public void showmessage(){
TextView TVshowmessage= findViewById(R.id.TVshowmessage);
Log.d(TAG, "onClick: button check was pressed");
if (myC.ccheck() == "smaller"){
Log.d(TAG, "onClick: check is ? "+ myC.ccheck() +" numbers: "+myC.ccurrentnum()+ "<"+ myC.crandom());
TVshowmessage.setText("number is smaller than random number" + 0);
}
else
if (myC.ccheck() == "equal"){
Log.d(TAG, "onClick: check is ? "+ myC.ccheck() + "="+ myC.crandom());
TVshowmessage.setText("congrats you found the number, "+String.valueOf(myC.ccounter()/2)+" tries,"+ " press restart to play again" );
}
else {
if(myC.ccheck() == "bigger") {
Log.d(TAG, "onClick: check is ? "+ myC.ccheck() + ">"+ myC.crandom());
TVshowmessage.setText("number you chose is bigger than random number" + 0);
}
}
}
public void changepage(){
TextView tvfoundnum= findViewById(R.id.tvfound);
RelativeLayout grats= findViewById(R.id.grats);
RelativeLayout page= findViewById(R.id.page);
TextView TVshowmessage= findViewById(R.id.TVshowmessage);
if (myC.ccheck() == "equal"){
page.setVisibility(View.GONE);
setContentView(grats);
}
else{
setContentView(page);
}
}
#Override
public void onClick(View v) {
buttonstart =(Button) findViewById(R.id.buttonstart);
buttonplus = (Button)findViewById(R.id.buttonplus);
buttonminus =(Button) findViewById(R.id.buttonminus);
buttoncheck = (Button)findViewById(R.id.buttoncheck);
buttonrestart = (Button)findViewById(R.id.buttonrestart);
TVcurrentnum= findViewById(R.id.TVcurrentnum);
tvcounter= findViewById(R.id.tvcounter);
if (v.getId() == R.id.buttonstart || v.getId() == R.id.buttonrestart) {
startGame();
}
if (v.getId() == R.id.buttonrestart1){
findViewById(R.id.page).setVisibility(View.VISIBLE);
findViewById(R.id.grats).setVisibility(View.GONE);
startGame();
}
//button check
if (v.getId() == R.id.buttoncheck) {
showmessage();
myC.ccntplus();
tvcounter.setText(String.valueOf((int)(myC.ccounter())+ 0));
Log.d(TAG, "onClick: counter returned"+ tvcounter.getText());
changepage();
}
if (v.getId() == R.id.buttonplus) {
Log.d(TAG, "onClick: button plus was pressed");
myC.cplus();
TVcurrentnum.setText(String.valueOf((int)(myC.ccurrentnum()) + 0));
} else if (v.getId() == R.id.buttonminus) {
Log.d(TAG, "onClick: button minus was pressed");
myC.cminus();
TVcurrentnum.setText(String.valueOf((int)(myC.ccurrentnum()) + 0));
}
}
}
there is more code in two other java files but these are not relevant to changing screen but I will add it below.
controller.java
package com.example.findrandomnumber;
public class Controllerguessnum {
private Modelguessnum myModel1;
public double counter=0;
private int random;
public Controllerguessnum(){
myModel1= new Modelguessnum();
cstartGame();
this.counter=0;
}
public void cstartGame(){
myModel1.mStartGame();
this.counter=0; }
public void crestartgame(){
myModel1.mStartGame();
this.counter=0;
}
public void cplus(){
myModel1.mplus();
}
public void cminus(){
myModel1.mminus();
}
public void ccntplus(){
this.counter++;
}
public String ccheck(){
String res = myModel1.mcheck();
return res;
}
public double ccurrentnum(){
int cnum=myModel1.getCurrentnum();
return cnum;
}
public double ccounter(){
double cnt= this.counter;
return cnt;
}
public int crandom(){
this.random = myModel1.getRandomnum();
return this.random;
}
}
model.java
package com.example.findrandomnumber;
import java.util.Random;
public class Modelguessnum {
private int currentnum;
private int randomnum;
private int counter=0;
public Modelguessnum() {
}
public void mStartGame(){
// this.randomnum= new Random().nextInt( 100);
// this.currentnum= new Random().nextInt(100);
this.randomnum=5;
this.currentnum=5;
}
public int getCurrentnum(){
int mnum= currentnum;
return mnum;
}
public int getRandomnum(){
return randomnum;
}
public void mplus(){
this.currentnum++;
}
public void mminus()
{
this.currentnum--;
}
public String mcheck() {
//-1 is smaller
//0 equal
//1 is bigger
if (this.currentnum > this.randomnum) {
return "bigger";
}
else
if (this.currentnum==this.randomnum){
return "equal";
}
else
if(this.currentnum < this.randomnum){
return "smaller";
}
else
return "neither";
}
}
the math part of the code worked fine- any improvements are welcome. - how do I solve the errors so that when I find the number, when the random number = current number and when I press check, I want it to bring me to the second screen/layout(foundnum.xml) - foundnum.xml
the error I get from this is the following:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.findrandomnumber, PID: 480
java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
at android.view.ViewGroup.addView(ViewGroup.java:3718)
at android.view.ViewGroup.addView(ViewGroup.java:3700)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:687)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:175)
at com.example.findrandomnumber.MainActivity.changepage(MainActivity.java:95)
at com.example.findrandomnumber.MainActivity.onClick(MainActivity.java:129)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)>
The exception you getting is that you: Cannot add a null child view to a ViewGroup.
A ViewGroup is a view that can contain other views. The ViewGroup is the base class for Layouts in android, like LinearLayout , RelativeLayout , FrameLayout etc. In other words, ViewGroup is generally used to define the layout in which views(widgets) will be set/arranged/listed on the android screen.
You have two ViewGroups: one activity_main.xml and foundnum.xml
Within your code (MainActivity), you set activity_main.xml as your viewGroup by this line of code setContentView(R.layout.activity_main); in your oncreate method.
What causes the error??
The error is caused by taking action(setting text or setting onclick action) on tvfoundnum and buttonrestart1:
TextView tvfoundnum= findViewById(R.id.tvfound)
buttonrestart1 = (Button)findViewById(R.id.buttonrestart1);
Any action you set to them will result into an error because those views are not part of the set ViewGroup. In otherwards they are not contained in activity_main.xml and they are not yet known
Solution Options:
There are 2 ways you can be able to achieve what you want:
By hiding and displaying only the views you want to display: Meaning you will have to combine foundnum.xml's code to activity_main.xml. Such that you have one layout.
By using fragments. With fragments, you can have one fragment manage more than one ViewGroup (layouts).
Since you are using activity, I will demonstrate solution option (1) bellow
Solution Option One.
This is the new layout(it contains code from activity_main.xml & foundnum.xml): activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<RelativeLayout
android:id="#+id/page"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:outlineSpotShadowColor="#2196F3"
android:text="The Random Game"
android:textColor="#006400"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="#+id/buttonstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/game"
android:layout_centerInParent="true"
android:background="#8b0000"
android:text="Start"
android:textColor="#006400"
android:textStyle="bold|italic"
/>
<TextView
android:id="#+id/TVshowmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonstart"
android:layout_centerInParent="true"
android:text="the number is"
android:textColor="#006400"
/>
<Button
android:id="#+id/buttonplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/TVshowmessage"
android:layout_centerInParent="true"
android:background="#8b0000"
android:text="+"
android:textColor="#006400"
android:textSize="#android:dimen/app_icon_size"
android:textStyle="bold|italic"
/>
<TextView
android:id="#+id/TVcurrentnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonplus"
android:layout_centerInParent="true"
android:background="#8b0000"
android:text="0"
android:textColor="#006400"
/>
<Button
android:id="#+id/buttonminus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/TVcurrentnum"
android:layout_centerInParent="true"
android:background="#8b0000"
android:text="-"
android:textColor="#006400"
android:textSize="#android:dimen/app_icon_size"
/>
<TextView
android:id="#+id/tvcounter"
android:layout_width="wrap_content"
android:layout_height="66dp"
android:layout_below="#id/buttonrestart"
android:layout_centerInParent="true"
android:layout_marginBottom="300dp"
android:background="#8b0000"
android:text="counter"
android:textAlignment="center"
android:textColor="#006400"
android:textStyle="bold|italic"
/>
<Button
android:id="#+id/buttoncheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonminus"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:background="#8b0000"
android:outlineSpotShadowColor="#008B02"
android:text="Check"
android:textColor="#006400"
android:textStyle="bold|italic"
/>
<Button
android:id="#+id/buttonrestart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttoncheck"
android:layout_centerInParent="true"
android:background="#8b0000"
android:text="restart"
android:textColor="#006400"
android:textStyle="bold|italic"
/>
</RelativeLayout>
<!-- code from foundnm.xml-->
<RelativeLayout
android:id="#+id/grats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
<TextView
android:id="#+id/tvfound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonrestart1"
android:layout_centerInParent="true"
android:background="#8b0000"
android:text="congrats"
android:textColor="#006400"
android:textStyle="bold|italic"
/>
<Button
android:id="#+id/buttonrestart1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#8b0000"
android:text="restart"
android:textColor="#006400"
android:textStyle="bold|italic"
/>
</RelativeLayout>
</RelativeLayout>
Note that, when you run the app take note that some views are hidden, that is
<RelativeLayout>
<!--page relative layout--> This is visible (visibilty = visible )
<!--grats relative layout--> This is invisible (visibility = gone)
</RelativeLayout>
After doing whatever its that you want to do(checking if number is in the counter.....), set visible to <!--grats relative layout--> and set visibility gone for <!--page relative layout-->
You need to modify the the if statement in this function: changepage(), to something like this
public void changepage(){
if (myC.ccheck() == "equal"){
page.setVisibility(View.GONE);
grats.setVisibility(View.VISIBLE);
}
else{
grats.setVisibility(View.GONE);
page.setVisibility(View.VISIBLE);
}
}
I hope you find this helpful. If you have issues, please comment, I will help you out.
I want to make a quiz app that can calculate the sum of the number in radio button that user click. How to caluclate the sum and how can I display the result in another layout after user click the button to generate the result?
Question page Result page
Here is the code for question layout (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/instruction_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/instruction" />
// Question 1
<TextView
android:id="#+id/question1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question1" />
<RadioGroup
android:id="#+id/question1_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question1_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question1_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<RadioButton
android:id="#+id/question1_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question1_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question1_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question1_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question1_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 2
<TextView
android:id="#+id/question2_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question2" />
<RadioGroup
android:id="#+id/question2_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question2_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question2_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question2_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question2_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question2_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question2_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question2_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 3
<TextView
android:id="#+id/question3_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question3" />
<RadioGroup
android:id="#+id/question3_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question3_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question3_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question3_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question3_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question3_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question3_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question3_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 4
<TextView
android:id="#+id/question4_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question4" />
<RadioGroup
android:id="#+id/question4_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question4_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question4_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question4_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question4_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question4_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question4_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question4_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 5
<TextView
android:id="#+id/question5_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question5" />
<RadioGroup
android:id="#+id/question5_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question5_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question5_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question5_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question5_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question5_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question5_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question5_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
<Button
android:id="#+id/jana_keputusan_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jana Keputusan" />
</LinearLayout>
Here is the code for the question activity (MainActivity.java):
package com.android.resilien.resilienminver;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
startActivity(generate_result_intent);
}
});
}
// Question 1 Radio Button clicked
public void onRadioButtonQuestion1Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question1_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question1_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question1_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question1_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question1_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question1_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question1_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 2 Radio Button clicked
public void onRadioButtonQuestion2Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question2_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question2_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question2_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question2_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question2_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question2_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question2_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 3 Radio Button clicked
public void onRadioButtonQuestion3Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question3_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question3_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question3_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question3_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question3_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question3_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question3_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 4 Radio Button clicked
public void onRadioButtonQuestion4Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question4_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question4_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question4_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question4_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question4_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question4_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question4_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 5 Radio Button clicked
public void onRadioButtonQuestion5Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question5_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question5_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question5_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question5_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question5_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question5_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question5_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
}
First i would strongly recommend to sum up the click method for all RadioButtons to one single method, e.g. onRadioAction:
The layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/instruction_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/instruction" />
// Question 1
<TextView
android:id="#+id/question1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question1" />
<RadioGroup
android:id="#+id/question1_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question1_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question1_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<RadioButton
android:id="#+id/question1_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question1_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question1_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question1_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question1_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 2
<TextView
android:id="#+id/question2_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question2" />
<RadioGroup
android:id="#+id/question2_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question2_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question2_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question2_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question2_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question2_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question2_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question2_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 3
<TextView
android:id="#+id/question3_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question3" />
<RadioGroup
android:id="#+id/question3_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question3_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question3_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question3_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question3_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question3_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question3_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question3_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 4
<TextView
android:id="#+id/question4_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question4" />
<RadioGroup
android:id="#+id/question4_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question4_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question4_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question4_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question4_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question4_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question4_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question4_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 5
<TextView
android:id="#+id/question5_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question5" />
<RadioGroup
android:id="#+id/question5_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question5_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question5_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question5_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question5_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question5_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question5_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question5_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
<Button
android:id="#+id/jana_keputusan_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jana Keputusan" />
</LinearLayout>
The activity:
package com.android.resilien.resilienminver;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
startActivity(generate_result_intent);
}
});
}
public void radioButtonAction(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question5_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question5_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question5_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question5_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question5_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question5_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question5_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
To extend your app i would also recommend, that you generate the layout for questions dynamically in your source code, not as hardcoded layout xml.
Finally to display the result e.g. add a Button to the bottom and set the following code to display a dialog with the achieved score. As alternative you can also start a new Activity and show it there via Intent and Bundle.
private onClickedButtonResult(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Your score")
//set message
.setMessage("You achieved a score of " + String.valueOf(score) + "!")
//set positive button
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Call finish to close your app
finish();
}
})
.show();
}
Alternative:
Show score in another Activity
Create an Intent for the other Activity and save the value of score in the intent:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
Bundle extras = generate_result_intent.getExtras();
extras.putExtra("SCORE", score);
startActivity(generate_result_intent);
}
});
}
And in your other Activity you can read the passed value of score and show it to the user:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
Intent mIntent = getIntent();
if (i.hasExtra("SCORE")) {
int score = mIntent.getIntExtra("SCORE", 0);
// Do something to display the score
}
}
Finally, be aware that the user can increase his own score by clicking multiple times on a RadioButton. That should be addressed in your application logic, because the scoring would be invalid.
You can go to another activity and display result by INTENT. You can pass your score to another activity by using putExtra.
MainActivity:
okAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("Score", score);
startActivity(intent);
}
});
Another Activity:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
int score = bundle.getInt("Score");
restltText.setText("Your score is: "+ score);
}
To get text from RadioButton I offer you to use:
int indexOfButton = radioGroup.indexOfChild(findViewById(radioGroup.getCheckedRadioButtonId()));
String scoreS = (indexOfButton > -1)? ((RadioButton) radioGroup.getChildAt(indexOfButton)).getText().toString() : "";
score += Integer.parseInt(scoreS);
This methods is getting checked RadioButton id and by it finds index in that group. By the index you can get RadioButton text.
To show the score in other activity you can send data throw Intent extras. Like this:
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("result", score);
startActivity(intent);
On ResultActivity.class you can get score from extras like this:
Integer result = 0;
Intent i = getIntent();
if (i.hasExtra("result")) {
result = i.getIntExtra("result");
}
Then you can display it in TextView or in other possible ways.
If you don't understand something, feel free to ask.
I hope it helps you...
I'm trying to show automaticly "This is the correct answer" or "Try again" just right after the radio button is pressed.
My question is:
How to add two strings
<string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
to this textView
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
so I can proceed this
for (boolean radioAnswer : answer)
correct = correct && radioAnswer;
if (correct)
tv.setText(R.string.Good_answer);
else
tv.setText(R.string.Wrong_answer);
And here is setOnClick... (at first it was with checkbutton 'mbuton')
mbuton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean check = true;
boolean correct = true;
// To check if all questions have been answered
for (boolean radioChecked : checked)
check = check && radioChecked;
if (check) {
// To check if all questions have been answered correctly
for (boolean radioAnswer : answer)
correct = correct && radioAnswer;
if (correct)
tv.setText(R.string.Good_answer);
else
tv.setText(R.string.Wrong_answer);
}
else
tv.setText("Answer all questions");
}
});
Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 1"/>
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:layout_marginTop="16dp"
android:layout_below="#+id/rg1"
android:id="#+id/rg2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 2"/>
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And string
<string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
// i have modified your code, please check it.
// i have display message if user does not select any radio button,
//another wise it display correct answer count on textview.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class Main2Activity2 extends Activity {
TextView tv;
Button mbuton;
RadioGroup rg1,rg2;
ArrayList<Integer> arrayListOfRadioGroupId =new ArrayList<Integer>();
int noAnswerCount= 0;
int correctAnswerRadioButtonCount= 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textViewAnswer);
mbuton = (Button) findViewById(R.id.mbuton);
rg1 = (RadioGroup)findViewById(R.id.rg1);
rg2 = (RadioGroup)findViewById(R.id.rg2);
// Store Radio group id to arraylist
arrayListOfRadioGroupId.add(rg1.getId());
arrayListOfRadioGroupId.add(rg2.getId());
mbuton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noAnswerCount= 0;
correctAnswerRadioButtonCount= 0;
for(int radioGroupId: arrayListOfRadioGroupId)
{
RadioGroup objRadioGroup = (RadioGroup) findViewById(radioGroupId);
int checkedId=objRadioGroup.getCheckedRadioButtonId();
// get Selected Radio button id.
if(checkedId>-1) {
RadioButton rB = (RadioButton) findViewById(checkedId);
String strRadioButtonText = rB.getText().toString();
// get Selected Radio button Text.
if(strRadioButtonText.equals("Correct Option"))
{
correctAnswerRadioButtonCount ++;
noAnswerCount --;
}
}
else
{
noAnswerCount++;
}
}
if(noAnswerCount > 0)
{
tv.setText("Answer all questions");
}
else
{
tv.setText("Correct Answer Count is: " +correctAnswerRadioButtonCount);
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButton5) {
RadioButton rB = (RadioButton) findViewById(checkedId);
String strRadioButtonText = rB.getText().toString();
// get Selected Radio button Text.
if(strRadioButtonText.equals("Correct Option")) {
tv.setText("Correct Answer");
}
else
{
tv.setText("Wrong Answer");
}
}
}
});
}
}
// my Xml code is.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 1"/>
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:layout_marginTop="16dp"
android:layout_below="#+id/rg1"
android:id="#+id/rg2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 2"/>
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/mbuton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MButton"
android:layout_below="#id/textViewAnswer"/>
</RelativeLayout>
If your problem is just about setting those string in the TextView then, try this:
//For correct answer.
String CorrectAnswer = getString(R.string.Good_answer);
tv.setText(CorrectAnswer);
//For wrong answer.
String WrongAnswer = getString(R.string.Wrong_answer);
tv.setText(WrongAnswer);
The Code is still unclear (I can't Figure out what mButton is) .
However can you also share your layout xml I would suggest having a Radio Group comprising of X number of Radio Buttons (options) that you like and put the on checkedChangeListener
<RadioGroup
android:id="#+id/rgroup"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/test_image"
android:button="#null" />
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/test_image"
android:button="#null" />
</RadioGroup>
Your Activity should implement the OnCheckedChangeListener and you can then define your Radio Group RadioGroup Options = (RadioGroup) findViewById(R.id.rgroup);
Then you can implement a method in your activity where if a option inside that Radio Button is clicked you can use the below method.
#Override
public void onCheckedChanged(RadioGroup group,
int checkedId)
{
switch (checkedId)
{
case R.id.option1:
// settextview to correct here
break;
case R.id.option2:
//set textview to wrong here
break;
default:
break;
}
}
Assuming you might have answers at dynamic positions you might want to code some logic in the on checked change method . Hope this helps
Layout file; look at those questions and answers. I've hardcoded the string here. You can set it in string resource file and fetch it here using #string or set it dynamically using yourTextView.setText() method.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your name?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Melisa" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alisha" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lolita" />
</RadioGroup>
<RadioGroup
android:id="#+id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rg1"
android:layout_marginTop="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What are you learning?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<RadioButton
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HTML" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rg2"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
And in your activity.
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroupFirst, radioGroupSecond;
private String[] realAnswers = {"Melisa", "Android"};
private String[] userAnswers;
private TextView tv;
private boolean status = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroupFirst = (RadioGroup) findViewById(R.id.rg1);
radioGroupSecond = (RadioGroup) findViewById(R.id.rg2);
tv = (TextView) findViewById(R.id.textViewAnswer);
userAnswers = new String[2];
radioGroupFirst.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
tv.setText("");
RadioButton nameRadio = (RadioButton) findViewById(checkedId);
Log.e("ID", "" + nameRadio.getText().toString());
userAnswers[0] = nameRadio.getText().toString();
}
});
radioGroupSecond.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton learningRadio = (RadioButton) findViewById(checkedId);
Log.e("ID", "" + learningRadio.getText().toString());
userAnswers[1] = learningRadio.getText().toString();
if (checkStatus(userAnswers))
tv.setText(getString(R.string.Good_answer));
else
tv.setText(getString(R.string.Wrong_answer));
}
});
}
private boolean checkStatus(String[] userAnswers) {
status = true;
for (int i = 0; i < userAnswers.length; i++) {
Log.e(userAnswers[i], realAnswers[i]);
if (!userAnswers[i].equals(realAnswers[i]))
status = false;
}
return status;
}
}
I haven't use the string resources here, if you want to set the text view with the string from resources too, look it in my first answer.
So, I think this will help you.
I'm using broadcast receiver to show a dialog.So the flow of code is something like:
Step1 Getting the requestCode value
Step2 Based on this requestCode the broadCast receiver goes to if or else if or else part
Step3 If the value that i entered using some scanner into the EditText(i.e Scan) doesn't matches it shows a Toast "Item Not Available".
Step 4 Once "Item Not Available" toast comes the focus changes to the Listview which is my problem.
Step5 Again if i pass value to the Scan EditText the Listview get click automatically.
So my question is "How to remove focus from the Listview" and set it to the EditText(i.e Scan).
For Reference I'm attaching the snap with code snippet and the layout.xml.Please have a look and drop your suggestions why the focus is going to the listview.
.java snippet
final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
loc = mspinner.getItemAtPosition(mspinner.getSelectedItemPosition())
.toString();
final String ItemNo;
final String Desc;
final String StockUnit;
final String PickSeq;
final String qtyCount;
final String qtyonHand;
final Button mok;
final Button mcancel;
final Button mplus;
final Button mminus;
final EditText medtQtyCount;
final EditText medtItem;
final EditText medtdesc;
final EditText medtuom;
final DatabaseHandler dbHandler;
final String[] UOM = null;
int requestCode;
LayoutInflater li = LayoutInflater.from(InventoryCount.this);
View promptsView = li.inflate(R.layout.quantityupdate, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
InventoryCount.this);
alertDialogBuilder.setView(promptsView);
//requestCode=Integer.parseInt(intent.getStringExtra("idx"));
requestCode=intent.getIntExtra("idx", -1);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
dbHandler = new DatabaseHandler(InventoryCount.this);
medtuom = (EditText) promptsView.findViewById(R.id.edt_mseshipuom_mic);
mok = (Button) promptsView.findViewById(R.id.btn_mseshipOk_mic);
mcancel = (Button) promptsView.findViewById(R.id.btn_mseshipCancel_mic);
mplus = (Button) promptsView.findViewById(R.id.btn_mseshipIncr_mic);
mminus = (Button) promptsView.findViewById(R.id.btn_mseshipDecr_mic);
medtQtyCount = (EditText) promptsView
.findViewById(R.id.edt_shipShiped_mic);
medtdesc = (EditText) promptsView
.findViewById(R.id.edt_mseshipQtyOrd_mic);
medtItem = (EditText) promptsView
.findViewById(R.id.edt_mseshipItemNo_mic);
if (requestCode == 1) {
}
else if (requestCode == 0) {
// ItemNo
/*if (resultCode == RESULT_OK) {
Log.i("Scan resul format: ",
intent.getStringExtra("SCAN_RESULT_FORMAT"));
*/
String itNo = intent.getStringExtra("SCAN_RESULT");
dbhelper.getReadableDatabase();
MIC_Inventory mic_inventory = dbhelper.getMicInventoryDetails(
loc, itNo);
dbhelper.closeDatabase();
if (mic_inventory != null) {
loc = mspinner.getItemAtPosition(
mspinner.getSelectedItemPosition()).toString();
ItemNo = mic_inventory.getItemno();
Desc = mic_inventory.getItemdescription();
PickSeq = mic_inventory.getPickingseq();
StockUnit = mic_inventory.getStockunit();
qtyonHand = mic_inventory.getQoh();// This value gives
// QOHand
qtyCount = mic_inventory.getQc();
medtItem.setText(ItemNo);
medtdesc.setText(Desc);
medtQtyCount.setText(qtyCount);
medtuom.setText(StockUnit);
mplus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String a = medtQtyCount.getText().toString();
int b = Integer.parseInt(a);
b = b + 1;
a = a.valueOf(b);
medtQtyCount.setText(a);
}
});
mminus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = Integer.parseInt(medtQtyCount.getText()
.toString());
c = c - 1;
medtQtyCount.setText(new Integer(c).toString());
}
});
mok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*
* UOM[mspinnerUom.getSelectedItemPosition()] =
* medtQtyCount .getText().toString();
*/
MIC_UOMInternal mic_uom = new MIC_UOMInternal();
mic_uom.setLocation(loc);
mic_uom.setItemno(ItemNo);
String updatedqtyCount = medtQtyCount.getText()
.toString();
if (!qtyCount.equals(updatedqtyCount)) {
mic_uom.setQc(Double
.parseDouble(updatedqtyCount));
mic_uom.setUom(StockUnit);
MIC_Inventory mic_Inventory = new MIC_Inventory();
mic_Inventory.setItemdescription(Desc);
mic_Inventory.setItemno(ItemNo);
mic_Inventory.setLocation(loc);
mic_Inventory.setPickingseq(PickSeq);
mic_Inventory.setQc(updatedqtyCount);
mic_Inventory.setQoh(qtyonHand);
mic_Inventory.setStockunit(StockUnit);
dbHandler.getWritableDatabase();
String result = dbHandler
.insertIntoInternal(mic_uom);
if (result.equals("success")) {
result = dbHandler.updateMIC(mic_Inventory);
}
dbHandler.closeDatabase();
}
Intent i = new Intent(InventoryCount.this,
InventoryCount.class);
i.putExtra("et", 1);
i.putExtra("LOCATION", loc);
// i.putExtra("ID", ID);
startActivity(i);
// InventoryCount.this.finish();
}
});
mcancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.cancel();
}
});
// show it
alertDialog.show();
} else {
/*
* Toast.makeText(this, "Item not available",
* Toast.LENGTH_LONG).show();
*/
toastText.setText("Item not available");
Toast toast = new Toast(getBaseContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
msearchtext.setText("");
/*msearchtext.setFocusableInTouchMode(true);
msearchtext.requestFocus();*/
/*msearchtext.setSelection(0);
lstView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
*/msearchtext.requestFocus();
}
else if (requestCode == 2) {
}
else
{
toastText.setText("Problem in Scanning");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
}
}
Layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border_green"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/txt_InvTitle"
style="#style/pageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/invTitle" />
<View
android:id="#+id/txt_InvView"
android:layout_width="match_parent"
android:layout_height="2dip"
android:layout_below="#+id/txt_InvTitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#2E9AFE" />
<LinearLayout
android:id="#+id/invLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/txt_InvView"
android:layout_marginTop="16dp" >
<TextView
android:id="#+id/txtLoc"
style="#style/textRegular"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center"
android:text="#string/location" />
<Spinner
android:id="#+id/sploc"
style="#style/SpinnerItemAppTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:editable="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/invScanType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/invLocation"
android:layout_gravity="center"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="18dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/edt_Search_mic"
style="#style/EditTextAppTheme_Scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_weight=".15"
android:gravity="center"
android:hint="#string/scan" />
<RadioGroup
android:id="#+id/radioScanBasedOn_mic"
style="#style/RadioButtonAppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radioInum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:button="#drawable/radiobutton_selector"
android:checked="true"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/itemno" />
<RadioButton
android:id="#+id/radioNum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/manfno" />
<RadioButton
android:id="#+id/radioUpc_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/upc" />
</RadioGroup>
</LinearLayout>
<HorizontalScrollView
android:id="#+id/scroll_full_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/invScanType" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="25dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lay_fullTitle_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
style="#style/textRegular_list"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="#string/itemno"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="#string/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/pick_seq"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qoh"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/uom"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/lst_msefull_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/ListViewAppTheme.White" >
</ListView>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/lay_PO_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="41dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:visibility="gone" >
<Button
android:id="#+id/btn_OrderLstImport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExit_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Add a textwatcher to edit text and check when text is not blank and it is not equal to expected text then only switch the focus.
/* Set Text Watcher listener */
yourEditText.addTextChangedListener(passwordWatcher);
and check for text once user enter text
private final TextWatcher passwordWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
if (s.length() != 0 && passwordEditText.getText().equals("Your expected text")) {
// show your toast and change focus
}
}
}
You should make your listview not focusable by using setFocusable(false) when not required and when you get response correctly from barcode scanner then you can again make your listview focusable.
I am making a quiz android app using Eclipse and I made it so that every question has his own activity, now everything works ok if i answer qustions slow but if i do it faster the xml stays open in the background I even added onPause method but it is still open. I do not know how to use threads but someone told me it would make the app faster so the xml would close. I hope there is an easy fix for my problem if not, can anyone explain me how to use threads.
Here is one of my xml layouts:
<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:background="#drawable/qh4"
tools:context=".POV6" >
<TextView
android:id="#+id/povrat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="150dp"
android:layout_marginTop="50dp"
android:paddingLeft="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_vertical_margin"
android:text="The highest peak in North America is ?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button2"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_below="#+id/povrat"
android:layout_centerHorizontal="true"
android:onClick="tocan"
android:text="Mount Mckinley"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button3"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/button2"
android:onClick="netocanodgovor"
android:text="Mount Everest"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button4"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button3"
android:layout_below="#+id/button3"
android:onClick="netocanodgovor"
android:text="Mount Logan"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button5"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_below="#+id/button4"
android:onClick="netocanodgovor"
android:text="Mount Rainier"
tools:ignore="HardcodedText" />
<SlidingDrawer
android:id="#+id/slidingDrawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pull up to use Jokers !"
tools:ignore="HardcodedText" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/rekord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:paddingBottom="20dp"
android:text="This joker remowes one wrong answer !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker1text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/rekord"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="joker1"
android:text="Use this joker !"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:paddingBottom="20dp"
android:text="This joker will sometimes give you the right answer (30% of all cases) ! "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker2text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/textView3"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker2odgovor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/joker2text"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:text="I think the right answer is Mount Mckinley "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:visibility="invisible"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="joker2"
android:text="Use this joker !"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:paddingBottom="20dp"
android:text="This joker will skip this question. But you will get no points for it !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker3text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/textView4"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="joker3"
android:text="Use this joker !"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:paddingBottom="20dp"
android:text="This joker will give you the right answer !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker4text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/textView5"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="joker4"
android:text="Use this joker !"
tools:ignore="HardcodedText" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
Here is my java code:
package com.peky.smartornot;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
public class POV6 extends Activity {
Sql ulaz = new Sql(this);
Sqlrecords rekordi = new Sqlrecords(this);
TextView joke4text;
TextView joke3text;
TextView joke2text;
TextView joke1text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pov6);
sve();
}
public void sve() {
// TODO Auto-generated method stub
ulaz.open();
int joker1 = ulaz.procitaj(), joker2 = ulaz.procitaj2(), joker3 = ulaz
.procitaj3(), joker4 = ulaz.procitaj4();
ulaz.close();
TabHost joker = (TabHost) findViewById(R.id.tabhost);
joker.setup();
TabSpec izgled = joker.newTabSpec("tag1");
izgled.setContent(R.id.tab1);
izgled.setIndicator("Joker 1");
joker.addTab(izgled);
izgled = joker.newTabSpec("tag2");
izgled.setContent(R.id.tab2);
izgled.setIndicator("Joker 2");
joker.addTab(izgled);
izgled = joker.newTabSpec("tag3");
izgled.setContent(R.id.tab3);
izgled.setIndicator("Joker 3");
joker.addTab(izgled);
izgled = joker.newTabSpec("tag4");
izgled.setContent(R.id.tab4);
izgled.setIndicator("Joker 4");
joker.addTab(izgled);
joke1text = (TextView) findViewById(R.id.joker1text);
joke1text.setText("You have " + joker1 + " jokers !");
joke2text = (TextView) findViewById(R.id.joker2text);
joke2text.setText("You have " + joker2 + " jokers !");
joke3text = (TextView) findViewById(R.id.joker3text);
joke3text.setText("You have " + joker3 + " jokers !");
joke4text = (TextView) findViewById(R.id.joker4text);
joke4text.setText("You have " + joker4 + " jokers !");
}
public void joker1(View view) {
Button netocan = (Button) findViewById(R.id.button5);
Button netocan2 = (Button) findViewById(R.id.button4);
Button netocan3 = (Button) findViewById(R.id.button3);
ulaz.open();
int joker1 = ulaz.procitaj(), joker2, joker3, joker4;
ulaz.close();
if (joker1 != 0) {
if (netocan.getVisibility() == View.VISIBLE) {
netocan.setVisibility(View.INVISIBLE);
ulaz.open();
joker1 = joker1 - 1;
joker2 = ulaz.procitaj2();
joker3 = ulaz.procitaj3();
joker4 = ulaz.procitaj4();
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joke1text = (TextView) findViewById(R.id.joker1text);
joke1text.setText("You have " + joker1 + " jokers !");
} else if (netocan2.getVisibility() == View.VISIBLE) {
netocan2.setVisibility(View.INVISIBLE);
ulaz.open();
joker1 = joker1 - 1;
joker2 = ulaz.procitaj2();
joker3 = ulaz.procitaj3();
joker4 = ulaz.procitaj4();
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joke1text = (TextView) findViewById(R.id.joker1text);
joke1text.setText("You have " + joker1 + " jokers !");
} else if (netocan3.getVisibility() == View.VISIBLE) {
netocan3.setVisibility(View.INVISIBLE);
ulaz.open();
joker1 = joker1 - 1;
joker2 = ulaz.procitaj2();
joker3 = ulaz.procitaj3();
joker4 = ulaz.procitaj4();
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joke1text = (TextView) findViewById(R.id.joker1text);
joke1text.setText("You have " + joker1 + " jokers !");
} else {
Toast imasodgovor = Toast.makeText(getApplicationContext(),
"You can not use more JOKERS1 on this question !",
Toast.LENGTH_SHORT);
imasodgovor.show();
}
} else {
Toast nemasjokera = Toast.makeText(getApplicationContext(),
"Not enought JOKERS1 !", Toast.LENGTH_SHORT);
nemasjokera.show();
}
}
public void joker4(View view) {
ulaz.open();
int joker1, joker2, joker3, joker4 = ulaz.procitaj4();
ulaz.close();
Button netocan = (Button) findViewById(R.id.button5);
Button netocan2 = (Button) findViewById(R.id.button4);
Button netocan3 = (Button) findViewById(R.id.button3);
if (joker4 != 0) {
if (netocan.getVisibility() == View.VISIBLE
|| netocan2.getVisibility() == View.VISIBLE
|| netocan3.getVisibility() == View.VISIBLE) {
netocan.setVisibility(View.INVISIBLE);
netocan2.setVisibility(View.INVISIBLE);
netocan3.setVisibility(View.INVISIBLE);
ulaz.open();
joker1 = ulaz.procitaj();
joker2 = ulaz.procitaj2();
joker3 = ulaz.procitaj3();
joker4 = joker4 - 1;
;
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joke4text = (TextView) findViewById(R.id.joker4text);
joke4text.setText("You have " + joker4 + " jokers !");
} else {
Toast imasodgovor = Toast.makeText(getApplicationContext(),
"You can not use more JOKERS4 on this question !",
Toast.LENGTH_SHORT);
imasodgovor.show();
}
} else {
Toast jokertext = Toast.makeText(getApplicationContext(),
"Not enought JOKERS4", Toast.LENGTH_SHORT);
jokertext.show();
}
}
public void joker3(View view) {
ulaz.open();
int joker3 = ulaz.procitaj3(), joker1, joker2, joker4;
ulaz.close();
if (joker3 != 0) {
ulaz.open();
joker1 = ulaz.procitaj();
joker2 = ulaz.procitaj2();
joker4 = ulaz.procitaj4();
joker3 = joker3 - 1;
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
Random crazy = new Random();
switch (crazy.nextInt(4)) {
case 0:
Intent pokreni = new Intent(this, POV3.class);
startActivity(pokreni);
finish();
break;
case 1:
Intent pokreni1 = new Intent(this, POV1.class);
startActivity(pokreni1);
finish();
break;
case 2:
Intent pokreni11 = new Intent(this, POV4.class);
startActivity(pokreni11);
finish();
break;
case 3:
Intent pokreni111 = new Intent(this, POV5.class);
startActivity(pokreni111);
break;
}
}
}
public void joker2(View view) {
TextView joker2odgovor = (TextView) findViewById(R.id.joker2odgovor);
ulaz.open();
int joker1, joker2 = ulaz.procitaj2(), joker3, joker4;
ulaz.close();
if (joker2 != 0 && joker2odgovor.getVisibility() == View.INVISIBLE) {
ulaz.open();
joker1 = ulaz.procitaj();
joker2 = joker2 - 1;
joker3 = ulaz.procitaj3();
joker4 = ulaz.procitaj4();
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joker2odgovor.setVisibility(View.VISIBLE);
joke2text = (TextView) findViewById(R.id.joker2text);
joke2text.setText("You have " + joker2 + " jokers !");
} else {
Toast odgovor = Toast.makeText(getApplicationContext(),
"Not enought JOKERS2 or already used on this question !",
Toast.LENGTH_SHORT);
odgovor.show();
}
}
public void tocan(View view) {
Toast josip = Toast.makeText(getApplicationContext(), "Right answer !",
Toast.LENGTH_SHORT);
josip.show();
int rekord = 0, ukupno = 0;
rekordi.open();
ukupno = rekordi.procitajukupno();
rekord = rekordi.procitaj() + 10;
rekordi.spremi(rekord, ukupno);
rekordi.close();
Random crazy = new Random();
switch (crazy.nextInt(4)) {
case 0:
Intent pokreni = new Intent(this, POV3.class);
startActivity(pokreni);
finish();
break;
case 1:
Intent pokreni1 = new Intent(this, POV1.class);
startActivity(pokreni1);
finish();
break;
case 2:
Intent pokreni11 = new Intent(this, POV4.class);
startActivity(pokreni11);
finish();
break;
case 3:
Intent pokreni111 = new Intent(this, POV5.class);
startActivity(pokreni111);
break;
}
}
public void netocanodgovor(View view) {
Intent gotovo = new Intent(this, Records.class);
startActivity(gotovo);
finish();
Toast josip = Toast.makeText(getApplicationContext(),
"Incorrect answer !", Toast.LENGTH_SHORT);
josip.show();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
If you need more information, or even the whole app i can send it to you !