Flash Card App
I have multiple static strings stored on strings.xml and I would like to navigate between them using the prev_Clicked/next_Clicked buttons.
I also would like to be able to store strings for Questions and Answers while the app is running, and access those via prev/next buttons
Thank you for your time, follows my code:
.java
public class MainActivity extends splashActivity {
Button closeButton, addButton, prevButton, nextButton;
TextView QAText, answerText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashcard);
QAText = (TextView) findViewById(R.id.fcfront_textView);
answerText = (TextView) findViewById(R.id.fcfront_textView); //not used anywhere atm
QAText.setOnClickListener(new View.OnClickListener()
{
//function to toggle between Q/A
public void onClick(View v)
{
if(QAText.getText().toString().equals(getString(R.string.fc_front))){
QAText.setText(R.string.fc_back);
Toast.makeText(getApplicationContext(), R.string.fc_front , Toast.LENGTH_LONG).show();
}else{
QAText.setText(R.string.fc_front);
Toast.makeText(getApplicationContext(), "SWAPPING: back-to-front", Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//gets click event directly from xml
public void next_Clicked(View view)
{
//test:
Toast.makeText(getApplicationContext(), "Next button works!!", Toast.LENGTH_LONG).show();
}
public void prev_Clicked(View view)
{
//test:
Toast.makeText(getApplicationContext(), "Prev button works!!", Toast.LENGTH_LONG).show();
}
} //end of main
layout.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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity"
android:clickable="true"
android:id="#+id/background"
android:background="#ff80b5ff">
<TextView
android:text="#string/fc_front"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fcfront_textView"
android:layout_centerVertical="true"
android:textSize="30sp"
android:layout_centerHorizontal="true"
android:linksClickable="true"
android:textIsSelectable="false"
android:singleLine="true"
android:onClick="OnClick"
android:clickable="true" />
<!--<TextView-->
<!--android:text="#string/fc_back"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_below="#+id/fcfront_textView"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:id="#+id/fcback_textView"-->
<!--android:textSize="30sp"-->
<!--android:linksClickable="true"-->
<!--android:textIsSelectable="false"-->
<!--android:singleLine="true"-->
<!--android:clickable="true" />-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_prev"
android:id="#+id/prevbutton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="bottom|center"
android:layout_weight="1"
android:textSize="30sp"
android:layout_alignParentStart="true"
android:onClick="prev_Clicked"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_next"
android:id="#+id/nextbutton"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="bottom|center"
android:layout_weight="1"
android:textSize="30sp"
android:hint="Browse +"
android:onClick="next_Clicked"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:id="#+id/closebutton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:textSize="30sp"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/plusbutton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:textSize="30sp"
android:layout_alignParentStart="true" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CSCE305HW1</string>
<string name="fc_front">FLASHCARD: FRONT</string>
<string name="fc_back">FLASHCARD: BACK</string>
<string name="Q1">Question1</string>
<string name="A1">Answer1</string>
<string name="Q2">Question2</string>
<string name="A2">Answer2 </string>
<string name="action_settings">Nothing here yet</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_goodbye">goodbye</string>
<string name="main_next">Next</string>
<string name="main_prev">Previous</string>
</resources>
define your strings as an array in xml like this :
<string-array name="your_string_array">
<item>string1</item>
<item>string1</item>
</string-array>
and use them as an array in your code like this :
String[] some_array = getResources().getStringArray(R.array.your_string_array);
int index = 0 ;
...
prevButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
...
#Override
public void onClick(View v) {
switch(v.getId) {
case R.id.your_next_button_id :
index++;
break;
case R.id.your_previous_button_id :
index--;
break;
}
yourTextView.setText(some_array[index]);
}
Put the strings in an array. You will be able to use the arrays index to navigate backwards, forwards or jump straight to a given string.
Related
I am new in Android studio development and errors like this have really messed up my day. I am looking for a solution 2 days now many people had the same problem with me but each one of them had a very different solution. Even in Stackoverflow the solutions varied, so I would really appreciate if you could help me overpass this error so I can go on.
The error I am getting is this:
java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
I have modified the .xml file and the .java file a lot with no chance.
Here is the .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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:clickable="true">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#id/tabHost"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#+id/TabsHost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/creatortab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Contact Creator"
android:id="#+id/ContactCreator"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="#+id/name"
android:layout_below="#+id/ContactCreator"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp"
android:layout_alignParentEnd="true"
android:hint="name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/phone"
android:layout_below="#+id/name"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/name"
android:hint="Phone"
android:text="Phone"
android:layout_marginTop="44dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/email"
android:layout_below="#+id/phone"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/phone"
android:text="E-mail"
android:layout_marginTop="54dp"
android:hint="email" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Contact"
android:id="#+id/addcontact"
android:enabled="false"
android:allowUndo="false"
android:clickable="false"
android:hint="add contact"
android:nestedScrollingEnabled="false"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginTop="74dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/address"
android:text="Address"
android:layout_above="#+id/addcontact"
android:layout_alignParentStart="true"
android:layout_marginTop="64dp"
android:layout_alignEnd="#+id/email"
android:hint="address" />
</LinearLayout>
<LinearLayout
android:id="#+id/ListTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
and the .java:
EditText nameTxt, phoneTxt, emailTxt, addressTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameTxt = (EditText)findViewById(R.id.name);
phoneTxt = (EditText)findViewById(R.id.phone);
emailTxt = (EditText)findViewById(R.id.email);
addressTxt = (EditText)findViewById(R.id.address);
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tabSpec = tabHost.newTabSpec("creator");
tabSpec.setContent(R.id.creatortab);
tabSpec.setIndicator("Creator");
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("list");
tabSpec.setContent(R.id.ListTab);
tabSpec.setIndicator("List");
tabHost.addTab(tabSpec);
final Button addBtn = (Button)findViewById(R.id.addcontact);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Your contact has been added", Toast.LENGTH_SHORT).show();
}
});
nameTxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
addBtn.setEnabled(!nameTxt.getText().toString().trim().isEmpty());//if nameTxt is equals to nothing,
//to trim koitaei gia kena pisw kai mprosta
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I would really appreciate any recommendations.
Your error (Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs') says it all:
Change this
<TabWidget
android:id="#+id/TabsHost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></TabWidget>
to
<TabWidget
android:id="#+id/android.R.id.tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</TabWidget>
Change this
<TabWidget
android:id="#+id/tabs"
</TabWidget>
to
<TabWidget
android:id="#+id/android.R.id.tabs"
</TabWidget>
Change this
<TabWidget
android:id="#+id/tabs"
</TabWidget>
to
<TabWidget
android:id="#android:id/tabs"
</TabWidget>
and same thing with framelayout maybe
So I am making an app for an assignment.
Basically convert Miles to KM or KM to Miles.
I've now got the radio buttons working but when I add the onclick event, upon clicking the edittext fields I get an exception. :
java.lang.IllegalArgumentException:
Expected receiver of type a00891437.set3s.comp3975.convertunits.MainActivity,
but got android.support.v7.internal.widget.TintContextWrapper
Here is the code
package a00891437.set3s.comp3975.convertunits;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onRadioButtonClicked(final View view) {
View mText = findViewById(R.id.MilesText);
View kmText = findViewById(R.id.KilometersText);
if (view.getId() == R.id.KmToMButton) {
mText.setEnabled(false);
kmText.setEnabled(true);
}
if (view.getId() == R.id.MToKmButton) {
kmText.setEnabled(false);
mText.setEnabled(true);
}
}
public void onClickedText(final View view) {
Log.d("MainActivity", "Clicked");
}
}
The layout
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/screen">
TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="110dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/KilometersText"
android:layout_above="#+id/button"
android:layout_marginBottom="134dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:enabled="false"
android:onClick="onClickedText"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/MilesText"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/KilometersText"
android:layout_alignStart="#+id/KilometersText"
android:enabled="false"
android:onClick="onClickedText"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Km"
android:id="#+id/Kilometers"
android:layout_alignTop="#+id/KilometersText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="M"
android:id="#+id/Miles"
android:layout_alignTop="#+id/MilesText"
android:layout_alignRight="#+id/Kilometers"
android:layout_alignEnd="#+id/Kilometers" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kilometers to Miles"
android:id="#+id/KmToMButton"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/MToKmButton"
android:layout_alignStart="#+id/MToKmButton"
android:checked="false"
android:onClick="onRadioButtonClicked"/>/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Miles to Kilometers"
android:id="#+id/MToKmButton"
android:layout_below="#+id/KmToMButton"
android:layout_alignLeft="#+id/Kilometers"
android:layout_alignStart="#+id/Kilometers"
android:checked="false"
android:onClick="onRadioButtonClicked"/>/>
</RadioGroup>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp" />
</RelativeLayout>
You are specifying the onClick event method through XML, and it looks like this does not work with support library AppCompatActivity.
You should just get the reference to the EditText and call setOnClickListener().
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText kilometers = (EditText) findViewById(R.id.KilometersText);
kilometers.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onClickedText(v);
}
});
// repeat this for the miles EditText
// you'll need to add an id for the radio group for this to work:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangedListener(new RadioGroup.OnCheckedChangedListener() {
// I leave the code inside here for you to do as an exercise
});
}
I'm trying to develop a riddle game with levels. First level is in "OneActivity.java" and "activity_one.xml" 2nd level on "TwoActivity.java" and "activity_two.xml" and so on. After the user types the answer to the question in level one, a toast display with "Correct" is displayed if the answer is correct and "Wrong" if it's incorrect. Now, how do I automatically go to next level if the answer is correct but remain on same level until the user inputs the correct answer. Here's my OneActivity.java and activity_one.xml
OneActivity.java:
package com.golo.user.gaunkhanekatha;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class OneActivity extends Activity {
public Button check;
public EditText typeh;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
check = (Button)findViewById(R.id.check); //R.id.button is the id on your xml
typeh = (EditText)findViewById(R.id.typeh); //this is the EditText id
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//Here you must get the text on your EditText
String Answer = (String) typeh.getText().toString(); //here you have the text typed by the user
//You can make an if statement to check if it's correct or not
if(Answer.equals("4") || (Answer.equals("four")))
{
//Create a Toast because it's correct
Toast.makeText(OneActivity.this, "Correct!",
Toast.LENGTH_LONG).show();
}
else{
//It's not the correct answer
Toast.makeText(OneActivity.this, "Wrong! Try Again",
Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_aboutus, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_one.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"
android:background="#drawable/background"
android:weightSum="1"
android:textAlignment="center"
android:id="#+id/oneque">
<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:text="What is 2+2?"
android:id="#+id/que"
android:width="255dp"
android:textSize="30dp"
android:layout_margin="50dp"
android:textStyle="italic"
android:gravity="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/typeh"
android:layout_gravity="center_horizontal"
android:text="Type Here" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Answer"
android:id="#+id/check"
android:layout_gravity="center_horizontal" />
</LinearLayout>
activity_level.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:background="#drawable/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_weight="1"
android:onClick="moveToOneActivity"
android:background="#drawable/one" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToTwoActivity"
android:background="#drawable/two" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToThreeActivity"
android:background="#drawable/three" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_weight="1"
android:onClick="moveToFourActivity"
android:background="#drawable/four" />
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToFiveActivity"
android:background="#drawable/five" />
<Button
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToSixActivity"
android:background="#drawable/six" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_weight="1"
android:onClick="moveToSevenActivity"
android:background="#drawable/seven" />
<Button
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToEightActivity"
android:background="#drawable/eight" />
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToNineActivity"
android:background="#drawable/nine" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/button10"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:layout_weight="1"
android:onClick="moveToTenActivity"
android:background="#drawable/ten" />
<Button
android:id="#+id/button11"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToElevenActivity"
android:background="#drawable/eleven" />
<Button
android:id="#+id/button12"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="30dip"
android:layout_weight="1"
android:onClick="moveToTwelveActivity"
android:background="#drawable/twelve" />
</LinearLayout>
</LinearLayout>
if(Answer.equals("4") || (Answer.equals("four")))
{
//Create a Toast because it's correct
Toast.makeText(OneActivity.this, "Correct!",
Toast.LENGTH_LONG).show();
//Here create intent to the new activity - for example
//If you wish the user will have time to see the toast, you can use a Handler with post delayed
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent newLevel= new Intent(OneActivity.this, TwoActivity.class);
startActivity(newLevel);
finish(); //finish the activity of the current level
}
}, 3000); //the code inside run() will be executed after 3 seconds so the user can see the toast
}
else
{
//It's not the correct answer
Toast.makeText(OneActivity.this, "Wrong! Try Again",
Toast.LENGTH_LONG).show();
}
To go to the second level you should add this Intent inside your correct Toast as follows :
Intent i = new Intent(OneActivity.this, TwoActivity.class);
startActivity(i);
finish();
This should be your code :
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//Here you must get the text on your EditText
String Answer = (String) typeh.getText().toString(); //here you have the text typed by the user
//You can make an if statement to check if it's correct or not
if(Answer.equals("4") || (Answer.equals("four")))
{
//Create a Toast because it's correct
Toast.makeText(OneActivity.this, "Correct! Going to Level 2...",
Toast.LENGTH_LONG).show();
Intent i = new Intent(OneActivity.this, TwoActivity.class);
startActivity(i);
finish();
}
else{
//It's not the correct answer
Toast.makeText(OneActivity.this, "Wrong! Try Again",
Toast.LENGTH_LONG).show();
}
}
});
EDIT
This edit will cancel the Toast when you are on your TwoActivity.class, you have to change some stuff, I'll exaplain to you.
1.- Create a global Toast variable.
private Toast toast;
2.- Initialize it on your onCreate() like this :
toast = Toast.makeText(OneActivity.this, "", Toast.LENGTH_SHORT);
3.- Then change your two Toast messages to this :
//Correct Toast
toast.setText("Correct! Going to Level 2...");
toast.show();
//Incorrect Toast
toast.setText("Wrong! Try Again");
toast.show();
4.- You want to make a finish() to avoid the back button to return to OneActivity() so you will call it, and it calls onDestroy() so you have to add this method aswell to cancel the Toast
#Override
protected void onDestroy() {
super.onDestroy();
if(toast!= null) {
toast.cancel();
}
}
You can use intents to switch to another activity.
Intent myIntent = new Intent(this, MyActivity.class);
startActivity(myIntent);
You could simply have an if statement. If the answer is correct, create an intent and start activity two, otherwise reloop in activity one or do whatever other behaviour you want to do if the answer is wrong.
I have a problem with creating things using LayoutInflater:
package com.tip.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements View.OnClickListener {
EditText billamount, percent, people;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
billamount = (EditText)findViewById(R.id.billtext);
percent = (EditText)findViewById(R.id.percenttext);
people = (EditText)findViewById(R.id.numberpeople);
Button unevensplitbutton = (Button)findViewById(R.id.unevensplit);
Button calculates = (Button)findViewById(R.id.calculate);
unevensplitbutton.setOnClickListener(this);
calculates.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.calculate:
TableLayout calculatenumberinflated = (TableLayout)findViewById(R.id.numbertable2);
View view = getLayoutInflater().inflate(R.layout.calculatenumbertable,calculatenumberinflated,false);
calculatenumberinflated.addView(view);
break;
case R.id.unevensplit:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the XML of calculatenumbertable:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/numbertable2">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Each Person Pays"
android:id="#+id/numberpeople"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/eachpersonedit"
android:layout_column="6" />
</TableRow></TableLayout>
And the XML of fragment_main:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bill amount"
android:id="#+id/bill"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/billtext"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage %"
android:id="#+id/percentage"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/percenttext"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of people"
android:id="#+id/textView"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/numberpeople"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Split Unevenly"
android:id="#+id/unevensplit"
android:layout_column="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="#+id/calculate"
android:layout_column="6" />
</TableRow>
</TableLayout>
</ScrollView>
It returns an error where:
12-16 21:22:56.825 28459-28459/com.tip.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.tip.calculator.MainActivity.onClick(MainActivity.java:35)
Where line 35 is the calculatenumberinflated.addView(view); line of code.
I feel lost right now, can someone please clarify what I am doing wrong? Thanks.
You are getting Nullpointer error at this line TableLayout calculatenumberinflated = (TableLayout)findViewById(R.id.numbertable2); which is not at all available in your fragment_main layout. It in the layout calculatenumbertable file and as you are inflating fragment_main layout so its not getting the correct id of your TableLayout.
Try to add your layout in TableRow besides TableLayout. Create separate row in your calculatenumbertable layout and then try add the layout in that row. You can not add layout directly in TableLayout.
Try as below:
In your calculatenumbertable add row in last as below:
<TableRow
android:id="#+id/tablerow"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableRow>
In your onClick add the layout as below:
public void onClick(View v) {
switch(v.getId()){
case R.id.calculate:
TableRowcalculatenumberinflated = (TableRow)findViewById(R.id.tablerow);
View view = getLayoutInflater().inflate(R.layout.calculatenumbertable,calculatenumberinflated,false);
calculatenumberinflated.addView(view);
break;
case R.id.unevensplit:
break;
}
}
Just move android:id="#+id/numbertable2" from calculatenumbertable.xml to fragment_main.xml in the tableLayouts.
Try this
LayoutInflater inflater=getLayoutInflater();
View view= inflater.inflate(R.layout.calculatenumbertable, null);
calculatenumberinflated.addView(view);
I have a method name set using onClick in my XML file but the application can't seem to find it. I think it is because the method should be called using the button of an alert dialog. Any ideas would be appreciated!
Main Activity:
public class MainActivity extends Activity {
String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void recordExpenditure(View v) {
Log.v(TAG, "User clicked Record Expenditure button");
// custom dialog
final Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.add_expenditure_dialog_layout);
dialog.setTitle("Add new expenditure");
dialog.show();
}
public void viewExpenditure(View v) {
Log.v(TAG, "User clicked View Expenditure button");
}
public void saveExpenditure(View v) {// <---- Can't find this
DatabaseHandler db = new DatabaseHandler(this);
// Inserting Contacts
Log.d(TAG, "Inserting new expenditure into database");
Log.v(TAG, "User clicked Save Expenditure button");
EditText saveExpenditureTitleText = (EditText)findViewById(R.id.expenditureTitleBox);
EditText saveExpenditureValue = (EditText)findViewById(R.id.expenditureValueBox);
String expenditureText = saveExpenditureTitleText.getText().toString();
String expenditureValue = saveExpenditureValue.getText().toString();
db.addExpenditure(new Expenditure(expenditureText, expenditureValue));
Log.v(TAG, "Recording expenditure details...\nExpenditure Title: " + expenditureText + "\nExpenditure Value: " + expenditureValue);
}
public void cancelExpenditure(View v) {
Log.v(TAG, "User clicked Cancel Expenditure button");
}
}
Main Layout.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=".MainActivity" >
<Button
android:id="#+id/recordExpenditure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Add Expenditure"
android:onClick="recordExpenditure" />
<Button
android:id="#+id/viewExpenditure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/recordExpenditure"
android:layout_below="#+id/recordExpenditure"
android:layout_marginTop="45dp"
android:text="View Expenditure"
android:onClick="viewExpenditure" />
</RelativeLayout>
Dialog Box.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" >
<TextView
android:id="#+id/expenditureValueText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/expenditureTitleBox"
android:text="Expenditure Value" />
<TextView
android:id="#+id/expenditureTitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Expenditure Title" />
<EditText
android:id="#+id/expenditureTitleBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/expenditureTitleText"
android:ems="10" />
<EditText
android:id="#+id/expenditureValueBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/expenditureValueText"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="#+id/saveExpenditureDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/expenditureValueBox"
android:text="Save"
android:onClick="saveExpenditure" /> <---- Can't find this
<Button
android:id="#+id/cancelExpenditureDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/saveExpenditureDetails"
android:layout_alignBottom="#+id/saveExpenditureDetails"
android:layout_alignRight="#+id/expenditureValueBox"
android:text="Cancel"
android:onClick="cancelExpenditure" />
</RelativeLayout>
Error:
09-15 21:12:37.411: E/AndroidRuntime(818): FATAL EXCEPTION: main
09-15 21:12:37.411: E/AndroidRuntime(818): java.lang.IllegalStateException: Could not find a method saveExpenditure(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'saveExpenditureDetails'
09-15 21:12:37.411: E/AndroidRuntime(818): at android.view.View$1.onClick(View.java:3578)
E/AndroidRuntime(818): java.lang.IllegalStateException: Could not find a method saveExpenditure(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'saveExpenditureDetails'
You will notice that the error message indicates that Android is looking for saveExpenditure(View) in android.view.ContextThemeWrapper, which is not your class. That's because your layout is being used by a dialog, not your activity directly. AFAIK, you will need to use setOnClickListener() to associate a listener with the Button in a Dialog.