code for image run in arraylist - java

i do array list with images thet i want them to run with 2 buttons thet go next pic and beck pic i dont know the code for this i thx all for the time you spend for help.
ackage com.example.hanansanag.mytourneyccreator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by ssh on 25/12/2016.
*/
public class Players extends AppCompatActivity implements View.OnClickListener {
protected Button btnNext, btnBack;
protected String fname;
protected String Lname;
protected String team;
protected ImageView iv;
protected ArrayList array_image;
int i = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_pic);
ArrayList<Integer> array_image = new ArrayList<Integer>();
array_image.add(R.drawable.bacelona);
array_image.add(R.drawable.athlethiko);
array_image.add(R.drawable.arsenak);
array_image.add(R.drawable.chelsea);
array_image.add(R.drawable.dortmond);
array_image.add(R.drawable.city);
array_image.add(R.drawable.bayernunchen);
array_image.add(R.drawable.intermilan);
array_image.add(R.drawable.psj);
array_image.add(R.drawable.realmadrid);
array_image.add(R.drawable.leverpool);
array_image.add(R.drawable.milan);
array_image.add(R.drawable.juventus);
array_image.add(R.drawable.ashkelon);
array_image.add(R.drawable.macabiheifa);
array_image.add(R.drawable.macabitelaviv);
array_image.add(R.drawable.beitaryeroshlaim);
array_image.add(R.drawable.apoelbersheva);
iv = (ImageView) findViewById(R.id.imageView);
btnNext = (Button) findViewById(R.id.btnNextPic);
btnBack = (Button) findViewById(R.id.btnBeckPic);
btnBack.setOnClickListener(this);
btnNext.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (i <= 0 || i >= array_image.length) {
return;
}
if (btnNext == v) {
iv.setImageResource(array_image.get(i++));
} else if (btnBack == v) {
iv.setBackgroundResource(array_image.get(i--));
}
}
}
**what can i do here in this line of code thet the pic will move : **
#Override
public void onClick(View v) {
if (i <= 0 || i >= array_image.length) {
return;
}
if (btnNext == v) {
iv.setImageResource(array_image.get(i++));
} else if (btnBack == v) {
iv.setBackgroundResource(array_image.get(i--));
}
}
this is the xml;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/teamimage">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="12dp"
android:id="#+id/textView2" />
<ImageView
android:layout_height="80dp"
android:id="#+id/imageView"
android:layout_width="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:id="#+id/Lname"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/imageView" />
<TextView
android:text="Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="12dp"
android:id="#+id/textView3" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true">
<Button
android:layout_width="20dp"
android:layout_height="15dp"
android:id="#+id/btnBeckPic"
android:background="#drawable/btnbeck"
android:layout_marginStart="30dp"
android:layout_alignBottom="#+id/Lname"
android:layout_toEndOf="#+id/textView2" />
<Button
android:background="#drawable/btnnext"
android:layout_width="20dp"
android:layout_height="15dp"
android:id="#+id/btnNextPic"
android:layout_below="#+id/imageView"
android:layout_toEndOf="#+id/btnBeckPic"
android:layout_marginStart="14dp" />
</GridLayout>
</RelativeLayout>

int currentImage = 0;
String[] strArr = new String[array_image.size()];
strArr = array_image.toArray(stockArr);
#Override
public void onClick(View v) {
if (btnNext == v) {
currentImage++;
currentImage = currentImage % strArr .length;
iv.setImageResource(strArr .length[currentImage]);
} else if (btnBack == v) {
currentImage--;
currentImage = (currentImage + strArr .length) % strArr .length;
iv.setImageResource(strArr[currentImage]);
}
}

Related

How to fix sharedpreference to save my data and skip an activity?

I want to use "Shared preference" for saving my username and password and it saved but do not work on second time i open the app.
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.icu.text.IDNA;
import android.support.v4.media.RatingCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = (TextView)findViewById(R.id.tv_user);
tv_pass = (TextView)findViewById(R.id.tv_pass);
tx_user = (EditText)findViewById(R.id.tx_user);
tx_pass = (EditText)findViewById(R.id.tx_pass);
btn_login = (Button)findViewById(R.id.btn_login);
img_ghorme = (ImageView)findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page2);
findall();
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String user = tx_user.getText()+"";
String pass = tx_pass.getText()+"";
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
save = getSharedPreferences("user", MODE_PRIVATE);
save = getSharedPreferences("pass", MODE_PRIVATE);
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.home.seesion10.Login_page"
android:layoutDirection="rtl"
android:textDirection="rtl"
android:background="#color/textShiri">
<ImageView
android:id="#+id/img_ghorme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/cirlcleghormesabzi"
android:layout_marginTop="1500dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="100dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.902"
android:layout_marginStart="100dp" />
<TextView
android:id="#+id/tv_user"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:text="نام کاربری"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/tx_user"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="#drawable/tx_bg"
android:drawableLeft="#drawable/ic_user"
android:ems="10"
android:hint="نام کاربری"
android:inputType="textPersonName"
android:padding="10dp"
android:text=""
android:textColor="#color/textShiri"
android:textColorHint="#color/textShiri"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_user" />
<TextView
android:id="#+id/tv_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:text="رمز عبور"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tx_user" />
<EditText
android:id="#+id/tx_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="#drawable/tx_bg"
android:drawableLeft="#drawable/ic_pass"
android:ems="15"
android:hint="........."
android:inputType="textPassword"
android:padding="10dp"
android:text=""
android:textColor="#color/textShiri"
android:textColorHint="#color/textShiri"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_pass" />
<Button
android:id="#+id/btn_login"
android:layout_width="122dp"
android:layout_height="50dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_bg"
android:text="ورود"
android:textColor="#color/textShiri"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/img_ghorme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tx_pass" />
</android.support.constraint.ConstraintLayout>
i expected that when i write admin admin for username and password, it saved it and for the second time i open the app it skip that login page but its not work and show the login page again
You are not initialising the sharedPreference before using it and defining two sharedPreferences for each key-pair. Change your code as below.
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = findViewById(R.id.tv_user);
tv_pass = findViewById(R.id.tv_pass);
tx_user = findViewById(R.id.tx_user);
tx_pass = findViewById(R.id.tx_pass);
btn_login = findViewById(R.id.btn_login);
img_ghorme = findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findall();
save = getSharedPreferences("userInfo", MODE_PRIVATE);
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
String user = tx_user.getText().toString();
String pass = tx_pass.getText().toString();
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}

How do i exit runnable and switch another activity

I have a textview timer, i want it stops after 10 seconds of showing on display and after 10 seconds that my current activity jumps to an other activity
Runnable runnable = new Runnable()
{
#Override
public void run()
{
while(Running)
{
try{
Thread.sleep(1000);
}catch(InterruptedException e)
{
e.printStackTrace();
}
handler.post(new Runnable()
{
#Override
public void run()
{
number+=1;
tvTimer.setText(String.valueOf(number)); //With setting value of number on textfield ITS POSSIBLE TO SEE THE TIMER
if(number==10)
{
Running = false;
handler.removeCallbacks(this);//that does not works for me to stop
Intent i = new Intent(getApplicationContext(), SonucMenu.class);
startActivity(i); //Starting my second class
}
}
});
}
}
My Second activity starts but the layout from second activity comes more than one time, what i want is that it comes only one time, and i think this problem depends on that i cant stop runnable
Here My Complete original Code
MainActivity.class
package com.marburg.leftright;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import com.marburg.leftright.R;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
static Random rnd=new Random();
static int rndN = rnd.nextInt(2);
TextView inf,tvSc, tvTimer;
Button btnL, btnR;
int ScoreRhtg = 0;
int ScoreFls = 0;
String right = "RIGHT";
String left = "LEFT";
private int number;
private Handler handler;
private boolean Running = true;
MediaPlayer mySound;
public void sendData()
{
String rData = Integer.toString(ScoreRhtg); //Score Of Right Answer Data setting
String wData = Integer.toString(ScoreFls);
Intent i = new Intent(getApplicationContext(), SonucMenu.class);
i.putExtra("DATA", rData);
i.putExtra("DATA2", wData);
startActivity(i);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lr);
Intent intent = getIntent(); //Get Intent from SonucMenu.class to turn back MainActivity.java from New Button (This Class)
inf = (TextView)findViewById(R.id.tvInfo);
btnL = (Button)findViewById(R.id.btnL);
btnR = (Button)findViewById(R.id.btnR);
tvSc = (TextView)findViewById(R.id.tvScore);
//Set text left or right according new random number
if(rndN==1){
inf.setText(right);
}
else
{
inf.setText(left);
}
//Set 0 for Left
btnL.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{ //Set 0 for Left
if(rndN==0)
{
ScoreRhtg++;
tvSc.setText(ScoreRhtg+" Right");
rndN=rnd.nextInt(2); //Renew the random number
}
else
{
ScoreFls++;
tvSc.setText(ScoreFls+" False");
rndN=rnd.nextInt(2); //Renew the random number
}
//Set text left or right according new random number
if(rndN==1){
inf.setText(right);
}
else
{
inf.setText(left);
}
}
});
//Set 1 For Right
btnR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(rndN==1)
{
ScoreRhtg++;
tvSc.setText(ScoreRhtg+" Right");
rndN=rnd.nextInt(2); //Renew the random number
}
else
{
ScoreFls++;
tvSc.setText(ScoreFls+" False");
rndN=rnd.nextInt(2); //Renew the random number
}
//Set text left or right according a new random number
if(rndN==1){
inf.setText(right);
}
else
{
inf.setText(left);
}
}
});
//FOR TIMER RESPONSIBLE PART OF CODE
tvTimer = (TextView)findViewById(R.id.tvTimer);
handler = new Handler();
Runnable runnable = new Runnable()
{
#Override
public void run()
{
while(Running)
{
try{
Thread.sleep(1000);
}catch(InterruptedException e)
{
e.printStackTrace();
}
handler.post(new Runnable()
{
#Override
public void run()
{
number+=1;
tvTimer.setText(String.valueOf(number)); //With setting value of number on textfield ITS POSSIBLE TO SEE THE TIMER
if(number==9)
{
mySound=MediaPlayer.create(MainActivity.this, R.raw.click);
mySound.start();
Running = false;
handler.removeCallbacks(this);//Doesnt works for me
sendData(); //sendData function startActivity(i) included
}
}
});
}
}
};
new Thread(runnable).start();
//END OF FOR TIMER RESPONSIBLE PART OF CODE
}
#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 my Second Class SonucMenu.java
package com.example.timerdeneme2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SonucMenu extends MainActivity {
public TextView tvCorr, tvWro;
public Button btnNew, btnCls;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sonuc_menu);
Intent i = getIntent();
Bundle dataBundle = i.getExtras();
String dataRString = dataBundle.getString("DATA");
String dataWString = dataBundle.getString("DATA2");
TextView tv = (TextView)findViewById(R.id.tvCorr);
TextView tvW = (TextView)findViewById(R.id.tvWro);
tv.setText(dataRString);
tvW.setText(dataWString);
btnNew = (Button)findViewById(R.id.btnNew);
btnNew.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(SonucMenu.this, MainActivity.class);
//myIntent.putExtra("key", value); //Optional parameters
SonucMenu.this.startActivity(myIntent);
}
});
btnCls=(Button)findViewById(R.id.btnEx);
btnCls.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
}
}
And XML Layouts lr.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:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.marburg.leftright.MainActivity" >
<TextView
android:id="#+id/tvResCorr"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tvResCorr"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/tvResCorr" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnL"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="L"
android:textSize="70dp" />
<Button
android:id="#+id/btnR"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="50dp"
android:text="R"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
<TextView
android:id="#+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableLayout1"
android:layout_alignRight="#+id/tableLayout1"
android:layout_below="#+id/tableLayout1"
android:layout_marginLeft="10dp"
android:layout_marginTop="200dp"
android:gravity="center"
android:text="Info"
android:textSize="24dp" />
<TextView
android:id="#+id/tvScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableLayout1"
android:layout_alignRight="#+id/tableLayout1"
android:layout_below="#+id/tvInfo"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Score"
android:textSize="24dp" />
<TextView
android:id="#+id/tvTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tableLayout1"
android:layout_centerHorizontal="true"
android:text="Time" />
</RelativeLayout>
And sonuc_menu.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.marburg.leftright.MainActivity" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="36dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvResCorr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Correctness:" />
<TextView
android:id="#+id/tvCorr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:text="X" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvResWro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wrongness" />
<TextView
android:id="#+id/tvWro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:text="X" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableLayout1"
android:layout_below="#+id/tableLayout1"
android:layout_marginTop="113dp" >
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnNew"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="NEW" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BEST SCORES" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<Button
android:id="#+id/btnEx"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="EXIT" />
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</RelativeLayout>
I think for this you can use following code , It worked for me.
It will start your activity after 10 seconds.
new Handler().postDelay(new Runnable() {
#Override
public void run() {
Start Your activity here.
}
}, 10000);
but one of the best solution will be (I just found it from develoer site):
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
strat your activity here.
}
}.start();
Use View.post(Runnable) and View.postDelayed(Runnable, long), no need to create additional thread.
private TextView tvTimer;
private final Runnable mCountDownRunnable = new Runnable() {
private int mCount = 0;
#Override
public void run() {
mCount++;
if (mCount < 10) {
tvTimer.setText(Integer.toString(mCount));
tvTimer.postDelayed(this, 1000);
} else {
Intent i = new Intent(YourActivity.this, SonucMenu.class);
YourActivity.this.startActivity(i);
}
}
};
to start runnable, call tvTimer.post(mCountDownRunnable);

Multiple Checkboxes to Run Different Methods

Here is my .java:
package com.example.clarkrubberfoam;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class PriceEstimator extends ActionBarActivity implements OnCheckedChangeListener {
double total = 0;
double md_foam25 = 0.31, md_foam50 = 0.63, md_foam75 = 0.81, md_foam100 = 1.01, md_foam125 = 1.25, md_foam150 = 1.34;
double hd_foam25 = 0.34, hd_foam50 = 0.66, hd_foam75 = 0.91, hd_foam100 = 1.19, hd_foam125 = 1.48, hd_foam150 = 1.53;
double ye_foam25 = 0.59, ye_foam50 = 0.94, ye_foam75 = 1.41, ye_foam100 = 1.67, ye_foam125 = 2.09, ye_foam150 = 2.34;
double oe_foam25 = 0.70, oe_foam50 = 1.37, oe_foam75 = 1.69, oe_foam100 = 2.20, oe_foam125 = 2.64, oe_foam150 = 3.16;
CheckBox chkMD = (CheckBox) findViewById(R.id.checkBox1);
CheckBox chkHD = (CheckBox) findViewById(R.id.checkBox2);
CheckBox chkYE = (CheckBox) findViewById(R.id.checkBox3);
CheckBox chkOE = (CheckBox) findViewById(R.id.checkBox4);
/* Called when first opening the app */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_price_estimator);
//Attach listeners to the checkboxes
chkMD.setOnCheckedChangeListener(this);
chkHD.setOnCheckedChangeListener(this);
chkYE.setOnCheckedChangeListener(this);
chkOE.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView == chkMD) {
if(isChecked) {
mediumDensityPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkHD){
if(isChecked) {
highDensityPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkYE) {
if(isChecked) {
yellowEnduroPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkOE) {
if(isChecked) {
orangeEnduroPrice();
} else {
total = total + 0;
}
}
}
public void mediumDensityPrice() {
Toast.makeText(getBaseContext(), "Medium Density Selected", Toast.LENGTH_LONG).show();
}
public void highDensityPrice() {
Toast.makeText(getBaseContext(), "High Density Selected", Toast.LENGTH_LONG).show();
}
public void yellowEnduroPrice() {
Toast.makeText(getBaseContext(), "Yellow Enduro Selected", Toast.LENGTH_LONG).show();;
}
public void orangeEnduroPrice() {
Toast.makeText(getBaseContext(), "Orange Enduro Selected", Toast.LENGTH_LONG).show();;
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_price_estimator,
container, false);
return rootView;
}
}
Here is my .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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.clarkrubberfoam.PriceEstimator$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:text="#string/price_estimator"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="#string/medium_density" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkBox1"
android:layout_below="#+id/checkBox1"
android:text="#string/high_density" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkBox1"
android:layout_alignBottom="#+id/checkBox1"
android:layout_marginLeft="27dp"
android:layout_toRightOf="#+id/checkBox1"
android:text="#string/enduro_yellow" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkBox3"
android:layout_below="#+id/checkBox3"
android:text="#string/enduro_orange" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkBox2"
android:layout_below="#+id/checkBox2"
android:layout_marginTop="10dp"
android:text="#string/enter_width" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/numberPicker1"
android:layout_below="#+id/numberPicker1"
android:layout_marginTop="30dp"
android:onClick="totalPrice"
android:text="#string/calculate_me" />
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="16dp" />
<NumberPicker
android:id="#+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/numberPicker1"
android:layout_centerHorizontal="true" />
<NumberPicker
android:id="#+id/numberPicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView4"
android:layout_alignTop="#+id/numberPicker2" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/numberPicker1"
android:layout_alignRight="#+id/numberPicker2"
android:text="#string/enter_length" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/numberPicker1"
android:layout_alignParentRight="true"
android:text="#string/enter_thickness" />
My problem is that when I run the program, it crashes immediately. I'm not sure what's going wrong. I am currently trying to adapt another multiple checkbox program to work for my needs: 'http://pulse7.net/android/android-check-box-example/'.
Essentially I have four grades of foam, and multiple thicknesses, so each grade, if checked in the checkbox, will need to lead to a different method. Currently, the Toast.makeText parts are just a placeholder for what will go there eventually once this part gets figured out :)
Cheers!
You must findViewById() in onCreate() method after setContentView() call.
public class MainActivity extends Activity {
CheckBox chkMD;
CheckBox chkHD;
CheckBox chkYE;
CheckBox chkOE;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_price_estimator);
chkMD = (CheckBox) findViewById(R.id.checkBox1);
chkHD = (CheckBox) findViewById(R.id.checkBox2);
chkYE = (CheckBox) findViewById(R.id.checkBox3);
chkOE = (CheckBox) findViewById(R.id.checkBox4);
// Attach listeners to the checkboxes
chkMD.setOnCheckedChangeListener(this);
chkHD.setOnCheckedChangeListener(this);
chkYE.setOnCheckedChangeListener(this);
chkOE.setOnCheckedChangeListener(this);
}
}

How would i make a button to switch where points go in a counter?

I am currently a high school student who decided to try and take up Android dev for fun, but I am stumped. I have an image button for blue team and for red team. The score goes up automatically for blue team. What i dont know how to do is when you hit the red button, the image buttons make the red teams score go up and vice versa.
Here is my java code
package com.example.puremmacompetitionjiujitsuscorer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
class MainActivity extends Activity {
private int blueScore = 0;
private int redScore = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton sweep = (ImageButton) findViewById(R.id.imageButton5);
final ImageButton pass = (ImageButton) findViewById(R.id.imageButton8);
final ImageButton mount = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton backMount = (ImageButton) findViewById(R.id.imageButton3);
final ImageButton kneeOnBelly = (ImageButton) findViewById(R.id.imageButton7);
final ImageButton takeDown = (ImageButton) findViewById(R.id.imageButton6);
final TextView blueScoreCount = (TextView) findViewById(R.id.blueScore1);
takeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
sweep.setOnClickListener(new View.OnClickListener(){
public void onClick(View w) {
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
pass.setOnClickListener(new View.OnClickListener(){
public void onClick(View q){
blueScore += 3;
blueScoreCount.setText("" + blueScore);
}
});
mount.setOnClickListener(new View.OnClickListener(){
public void onClick(View t){
blueScore += 4;
blueScoreCount.setText("" + blueScore);
}
});
backMount.setOnClickListener(new View.OnClickListener(){
public void onClick(View s){
blueScore += 4;
blueScoreCount.setText("" + blueScore);
}
});
kneeOnBelly.setOnClickListener(new View.OnClickListener(){
public void onClick(View g){
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
};
#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;
}
Here is my XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/purebig" >
<TextView
android:id="#+id/redScore1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint"
android:maxLength="2"
android:textIsSelectable="false" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/stop" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/play"/>
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton2"
android:layout_alignParentLeft="true"
android:src="#drawable/kneeonbelly"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageButton7"
android:src="#drawable/backmount"
android:onClick="addFour" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton3"
android:layout_alignParentRight="true"
android:src="#drawable/mount"
android:onClick="addFour" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton7"
android:layout_centerHorizontal="true"
android:src="#drawable/pass"
android:onClick="addThree" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/imageButton8"
android:src="#drawable/takedown"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton8"
android:layout_below="#+id/imageButton8"
android:src="#drawable/sweep"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/imageButton9"
android:src="#drawable/red" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/imageButton1"
android:src="#drawable/blue" />
<TextView
android:id="#+id/blueScore1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/hint"
android:maxLength="2"
android:textIsSelectable="false"/>
<requestFocus />
</RelativeLayout>
I think I possibly understand you. If I do, you can use a flag to see which Button was pressed. So the flow would be like
Red button pressed -> flag = red -> Score Button pressed -> red score += score
In code it would be like
String flag = "";
public void onRedBtnClick(View v)
{
flag = "red";
}
public void scoreBtnClick(View v)
{
if (!flag.equals(""));
{
if ("red".equals(flag))
{
redScore += score;
}
if ("blue".equals(flag))
{
blueScore += score;
}
}
This is obviously done really quick and you will have to adjust your variables to your needs. But if I understand what you want then this would give you the basic logic

Moving TextView Box made my android app crash

I have had this problem crop up a few times and I can't figure out why it should happen. What happened was after moving some stuff around my TextView box ended up on top of an EditText box, which is no good. So I went and moved the TextView box to the bottom of the screen. When I did that, the app would crash when I tried to access the piggybank. However, if I move the TextView box up to the top again, it works fine.. I really don't get it. Anyways, this is the error that I got
06-22 09:06:41.928: E/AndroidRuntime(10958): java.lang.RuntimeException: Unable to
start activity ComponentInfo{net.finalexam/net.finalexam.Piggy}:
java.lang.ClassCastException: android.widget.RadioButton
This is piggy xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/piggy" >
<EditText
android:id="#+id/txtQuarters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:ems="10"
android:hint="Number of quarters"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtDimes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtQuarters"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Number of dimes"
android:inputType="number" />
<EditText
android:id="#+id/txtNickles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtDimes"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Number of nickles"
android:inputType="number" />
<EditText
android:id="#+id/txtPennies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtNickles"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Number of pennies"
android:inputType="number" />
<EditText
android:id="#+id/txtDollars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPennies"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Number of Dollars"
android:inputType="number" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtDollars"
android:layout_centerHorizontal="true" >
<RadioButton
android:id="#+id/radAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Add" />
<RadioButton
android:id="#+id/radSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract" />
</RadioGroup>
<Button
android:id="#+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup1"
android:layout_centerHorizontal="true"
android:text="Calculate" />
<TextView
android:id="#+id/txtResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCalculate"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:textSize="40sp" android:textStyle="bold" android:textColor="#000000"/>
</RelativeLayout>
This is the Piggy Java file
package net.finalexam;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class Piggy extends Activity
{
double quartersValue = .25;
double dimesValue = .10;
double nicklesValue = .05;
double penniesValue = .01;
double dollarsValue = 1;
double quartersMoney;
double dollarsMoney;
double dimesMoney;
double nicklesMoney;
double penniesMoney;
double totalMoney;
double newTotalMoney;
double oldTotalMoney = 0;
int numberOfQuarters;
int numberOfDimes;
int numberOfNickles;
int numberOfPennies;
int numberOfDollars;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.piggybank);
final EditText quarters = (EditText) findViewById(R.id.txtQuarters);
final EditText dimes = (EditText) findViewById(R.id.txtDimes);
final EditText nickles = (EditText) findViewById(R.id.txtNickles);
final EditText pennies = (EditText) findViewById(R.id.txtPennies);
final EditText dollars = (EditText) findViewById(R.id.txtDollars);
Button calculate = (Button) findViewById(R.id.btnCalculate);
final TextView results = ((TextView) findViewById(R.id.txtResults));
final RadioButton add = (RadioButton) findViewById(R.id.radAdd);
final RadioButton subtract = (RadioButton) findViewById(R.id.radSubtract);
calculate.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (quarters.getText().toString().equals(""))
{
numberOfQuarters = 0;
}
else
{
numberOfQuarters = Integer.parseInt(quarters.getText().toString());
}
if (dimes.getText().toString().equals(""))
{
numberOfDimes = 0;
}
else
{
numberOfDimes = Integer.parseInt(dimes.getText().toString());
}
if (nickles.getText().toString().equals(""))
{
numberOfNickles = 0;
}
else
{
numberOfNickles = Integer.parseInt(nickles.getText().toString());
}
if (pennies.getText().toString().equals(""))
{
numberOfPennies = 0;
}
else
{
numberOfPennies = Integer.parseInt(pennies.getText().toString());
}
if (dollars.getText().toString().equals(""))
{
numberOfDollars = 0;
}
else
{
numberOfDollars = Integer.parseInt(dollars.getText().toString());
}
quartersMoney = numberOfQuarters * quartersValue;
dimesMoney = numberOfDimes * dimesValue;
nicklesMoney = numberOfNickles * nicklesValue;
penniesMoney = numberOfPennies * penniesValue;
dollarsMoney = numberOfDollars;
totalMoney = quartersMoney + dimesMoney + nicklesMoney + penniesMoney + dollarsMoney;
DecimalFormat currency = new DecimalFormat("$###,###.##");
if (add.isChecked())
{
if (totalMoney > 0)
{
newTotalMoney = oldTotalMoney + totalMoney;
oldTotalMoney = newTotalMoney;
results.setText(currency.format(newTotalMoney));
}
else
{
Toast.makeText(Piggy.this, "You need to do more chores!!", Toast.LENGTH_LONG).show();
}
}
if (subtract.isChecked())
{
newTotalMoney = oldTotalMoney - totalMoney;
}
if (newTotalMoney > 0)
{
oldTotalMoney = newTotalMoney;
results.setText(currency.format(newTotalMoney));
}
else
{
Toast.makeText(Piggy.this, "Save more money kido!!", Toast.LENGTH_LONG).show();
};
}
});
}
}
I have searched for an answer but they all seem to be slightly different situations. Any ideas? Thanks. And like I said, this works fine if the TextView box is overlapping the quarters EditText box.
Edit 1 Here is the screen before the piggy bank
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/ic_launcher_money"
android:layout_width="100px"
android:layout_height="100px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="2px"
android:src="#drawable/ic_launcher_money"></ImageView>
<TextView
android:id="#+id/bankses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/bankses"
android:textSize="25sp">
</TextView>
</LinearLayout>
Main.java
package net.finalexam;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Main extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String[] banks ={"Piggy Bank","Adult Bank"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.main, R.id.bankses, banks));
}
protected void onListItemClick(ListView l, View v, int position, long id){
switch(position){
case 0:
startActivity(new Intent(Main.this,Piggy.class));
break;
case 1:
startActivity(new Intent(Main.this,Adultbank.class));
break;
}
}
}
Try cleaning your project on Eclipse. It often fixes that kind of problems.

Categories