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 !
Related
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 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'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.
I created a tictactoe android game, and when i try to link the strings.xml file to activity.java I receive an error.
Here is my activity.java
package com.tictactoeoyna.www;
import android.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TicTacToeOynaActivity extends Activity {
private TicTacToeOyun mGame;
private Button mBoardButtons[];
private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;
private int mHumanCounter = 0;
private int mTieCounter = 0;
private int mAndroidCounter = 0;
private boolean mHumanFirst = true;
private boolean mGameOver = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBoardButtons = new Button[TicTacToeOyun.getBOARD_SIZE()];
mBoardButtons[0] = (Button) findViewById(R.id.one);// it should be blue but not
mBoardButtons[1] = (Button) findViewById(R.id.two);//same
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);//until here
mInfoTextView = (TextView) findViewById(R.id.information);
mHumanCount = (TextView) findViewById(R.id.humanCount);
mTieCount = (TextView) findViewById(R.id.tiesCount);
mAndroidCount = (TextView) findViewById(R.id.androidCount);
mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGame = new TicTacToeOyun();
startNewGame();
}
private void startNewGame()
{
mGame.clearBoard();
for (int i = 0; i < mBoardButtons.length; i++)
{
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
}
if (mHumanFirst)
{
mInfoTextView.setText(R.string.first_human);
mHumanFirst = false;
}
else
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
mHumanFirst = true;
}
}
private class ButtonClickListener implements View.OnClickListener
{
int location;
public ButtonClickListener(int location)
{
this.location = location;
}
public void onClick(View view)
{
if (!mGameOver)
{
if (mBoardButtons[location].isEnabled())
{
setMove(mGame.HUMAN_PLAYER, location);
int winner = mGame.checkForWinner();
if (winner == 0)
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
winner = mGame.checkForWinner();
}
if (winner == 0)
mInfoTextView.setText(R.string.turn_human);
else if (winner == 1)
{
mInfoTextView.setText(R.string.result_tie);
mTieCounter++;
mTieCount.setText(Integer.toString(mTieCounter));
mGameOver = true;
}
else if (winner == 2)
{
mInfoTextView.setText(R.string.result_human_wins);
mHumanCounter++;
mHumanCount.setText(Integer.toString(mHumanCounter));
mGameOver = true;
}
else
{
mInfoTextView.setText(R.string.result_android_wins);
mAndroidCounter++;
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGameOver = true;
}
}
}
}
}
private void setMove(char player, int location)
{
mGame.setMove(player, location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == mGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.GREEN);
else
mBoardButtons[location].setTextColor(Color.RED);
}
}
The error is on one, two, three...nine and information count, human count etc. Here is my fragment file, so why can't I see one, two, three....nine labeled blue? Why is it not recognized? What have I done wrong ? I received the errors after findviewbyid.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/playArea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="one"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/one"
android:textSize="70dp" />
<Button
android:id="#+id/two"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/two"
android:textSize="70dp" />
<Button
android:id="#+id/three"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/three"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/four"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/four"
android:textSize="70dp" />
<Button
android:id="#+id/five"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/five"
android:textSize="70dp" />
<Button
android:id="#+id/six"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/six"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/seven"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/seven"
android:textSize="70dp" />
<Button
android:id="#+id/eight"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/eight"
android:textSize="70dp" />
<Button
android:id="#+id/nine"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/nine"
android:textSize="70dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:text="#string/info"
android:textSize="25dp" />
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/human"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/human" />
<TextView
android:id="#+id/humanCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/ties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ties" />
<TextView
android:id="#+id/tiesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/android" />
<TextView
android:id="#+id/androidCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</LinearLayout>
import com.tictactoeoyna.www.R;
and removeimport android.R;
When you got this type of error at anywhere and you have import android.R; then just remove import android.R;.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to enter data in my EditText using my own buttons instead of default keyboard (I mean like in calculator apps). Please write me some method or show suitable example. Thanks.
I initialize veriable, even tried to make some algorithm. But there is no reason to go further without knowing how to input data.
And of course I was looking for the answer.
public class MainActivity extends Activity implements OnClickListener{
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bPlus,bMin,bDiv,bMult,bEqual;
String sN1,sN2,func,result;
EditText etEnter;
SoundPool pool; int shot = 0;
TextView tvShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
initVars();
}
private void initVars(){
etEnter =(EditText)findViewById(R.id.etCalc);
tvShow = (TextView)findViewById(R.id.tvShow);
b0 = (Button)findViewById(R.id.button0);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
b6 = (Button)findViewById(R.id.butt6);
b7 = (Button)findViewById(R.id.butt7);
b8 = (Button)findViewById(R.id.butt8);
b9 = (Button)findViewById(R.id.butt9);
bMin = (Button)findViewById(R.id.bMinus);
bPlus = (Button)findViewById(R.id.bPlus);
bDiv = (Button)findViewById(R.id.bDiv);
bMult = (Button)findViewById(R.id.bMult);
bEqual = (Button)findViewById(R.id.bEqual);
b0.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
bMin.setOnClickListener(this);
bPlus.setOnClickListener(this);
bDiv.setOnClickListener(this);
bMult.setOnClickListener(this);
bEqual.setOnClickListener(this);
pool = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
shot = pool.load(this, R.raw.shot, 1);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button0:
break;
case R.id.button1:
break;
case R.id.button2:
break;
case R.id.button3:
break;
case R.id.button4:
break;
case R.id.button5:
break;
case R.id.butt6:
break;
case R.id.butt7:
break;
case R.id.butt8:
break;
case R.id.butt9:
break;
case R.id.bMinus:
func = "-";
break;
case R.id.bPlus:
func = "+";
break;
case R.id.bDiv:
func = "/";
break;
case R.id.bMult:
func = "*";
break;
case R.id.bEqual:
pool.play(shot, 1, 1, 0, 0, 1);
if(func.contentEquals("+")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1+l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("-")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1-l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("/")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1/l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("*")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1*l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}
break;
}
}
and XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/rainbow"
>
<LinearLayout android:paddingTop="22dp" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<EditText
android:id="#+id/etCalc"
android:layout_width="260dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="right"
android:textSize="30dp"
android:inputType="number" />
</LinearLayout>
<LinearLayout android:paddingRight="33dp" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvShow"
android:layout_width="100dp"
android:layout_height="45dp"
android:text=":-)"
android:textSize="35dp"
android:paddingRight="10dp"
android:layout_gravity="right"
android:background="#drawable/rect_buttons"
android:gravity="right"
></TextView>"
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dp" >
<Button
android:id="#+id/bPlus"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:gravity="center"
android:text="+" />
<Button
android:id="#+id/bMinus"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="-" />
<Button
android:layout_gravity="center"
android:id="#+id/bEqual"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="=" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1"
android:textSize="30dp" />
<Button
android:id="#+id/button2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2"
android:textSize="30dp" />
<Button
android:id="#+id/button3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/button4"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="4"
android:textSize="30dp" />
<Button
android:id="#+id/button5"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5"
android:textSize="30dp" />
<Button
android:id="#+id/butt6"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/butt7"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="7"
android:textSize="30dp" />
<Button
android:id="#+id/butt8"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8"
android:textSize="30dp" />
<Button
android:id="#+id/butt9"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/bDiv"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="/" />
<Button
android:layout_gravity="center"
android:id="#+id/button0"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="0" />
<Button
android:id="#+id/bMult" android:layout_width="70dp" android:layout_height="50dp" android:textSize="30dp" android:text="*" />
</LinearLayout>
</LinearLayout>
You have to make your editText non focusable/editable and modify the text inside when you click on your buttons. Easy !