I want to add something like scrolling from top to bottom and vice versa but i don't know how to add this function, i was trying with ScrollView but that gave nothing.
MainActivity.java :
package com.example.marek.przelicznikpaliw;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private EditText a1;
private EditText b1;
private EditText c1;
private EditText d1;
private TextView tv;
private TextView tv2;
private TextView tv3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//pola
a1 = (EditText) findViewById(R.id.cel);
b1 = (EditText) findViewById(R.id.pali);
c1 = (EditText) findViewById(R.id.litr);
d1 = (EditText) findViewById(R.id.koszt);
// wynik
tv = (TextView) findViewById(R.id.wynikKoszt);
tv2 = (TextView) findViewById(R.id.wynikKilometry);
tv3 = (TextView) findViewById(R.id.wynikKosztx2);
findViewById(R.id.button).setOnClickListener(onClickListener);
findViewById(R.id.button2).setOnClickListener(onClickListener);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
float a = 0;
float b = 0;
float c = 0;
float d = 0;
float wynik = 0;
float wynik2 = 0;
float wynik3 = 0;
try {
a = Float.parseFloat(a1.getText().toString());
b = Float.parseFloat(b1.getText().toString());
c = Float.parseFloat(c1.getText().toString());
d = Float.parseFloat(d1.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}
if (view.getId() == R.id.button) {
wynik = ((b / 100) * a) * c;
tv.setText(String.valueOf(wynik));
wynik2 = (((b / 100) * a) * c)*2;
tv3.setText(String.valueOf(wynik2));
} else if (view.getId() == R.id.button2) {
wynik3 = d / ((b * c) / 100);
tv2.setText(String.valueOf(wynik3));
}
}
};
}
activity_main.xml :
<LinearLayout 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:orientation="vertical"
android:onClick="onButtonClick"
android:weightSum="1">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cel"
android:hint="Ilosc km do celu to:"
android:layout_centerHorizontal="true"
android:numeric="integer|decimal" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pali"
android:hint="Ile litrow pali na 100km"
android:layout_centerHorizontal="true"
android:layout_below="#+id/cel"
android:numeric="integer|decimal" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/litr"
android:hint="Cena za litr"
android:layout_below="#+id/pali"
android:layout_centerHorizontal="true"
android:numeric="integer|decimal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_below="#+id/litr"
android:layout_centerHorizontal="true"
android:hint="Licz zlotowki"
android:text="Licz koszty"
android:clickable="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/koszt"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="Za ile zl zostalo zatankowane gaz/paliwo?"
android:numeric="integer|decimal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Licz kilometry"
android:id="#+id/button2"
android:layout_below="#+id/koszt"
android:layout_centerHorizontal="true"
android:clickable="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dojedziesz w wybrany cel za(zł):"
android:id="#+id/textView" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/emptystring"
android:id="#+id/wynikKoszt"
android:textIsSelectable="false" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Oraz spowrotem za(zł):"
android:id="#+id/textView2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/emptystring"
android:id="#+id/wynikKosztx2"
android:layout_weight="0.06" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Za tę kwotę przejedziesz(km):"
android:id="#+id/textView3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/emptystring"
android:id="#+id/wynikKilometry" />
Related
I am trying to calculate the rectangle area in my application. I have created a simple layout but for some reason, data is not parsed into the application. I tried to print it in the console but nothing is shown.
This is my Main Class:
package com.example.week3labcode;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button calcButton;
EditText number1, number2, resultFinal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calcButton = (Button)findViewById(R.id.button1);
number1 = (EditText) findViewById(R.id.edtext1);
number2 = (EditText) findViewById(R.id.edtext2);
resultFinal = (EditText) findViewById(R.id.edtext3);
}
#Override
public void onClick(View v){
if(v == calcButton){
number1 = (EditText) findViewById(R.id.edtext1);
number2 = (EditText) findViewById(R.id.edtext2);
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
calcRectangle(num1, num2);
}
}
public void calcRectangle(double x, double y){
double result = x * y;
System.out.println(result);
resultFinal = (EditText) findViewById(R.id.edtext3);
resultFinal.setText(Double.toString(result));
System.out.println('H' + result);
}
}
And the following code in my XML file that provides layout for the application:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/label1"
android:layout_centerHorizontal="true"
android:layout_marginVertical="20px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rectangle Area Calculation" />
<TextView
android:id="#+id/label2"
android:layout_below="#+id/label1"
android:layout_centerHorizontal="true"
android:layout_marginVertical="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Length" />
<EditText
android:id="#+id/edtext1"
android:layout_toRightOf="#+id/label2"
android:layout_centerHorizontal="true"
android:layout_alignBaseline="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/label3"
android:layout_below="#+id/label2"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter breadth" />
<EditText
android:id="#+id/edtext2"
android:layout_toRightOf="#+id/label3"
android:layout_alignBaseline="#+id/label3"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/button1"
android:layout_below="#+id/label3"
android:layout_centerHorizontal="true"
android:layout_marginTop="50px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate" />
<TextView
android:id="#+id/label4"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginVertical="20px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Area in cm2" />
<EditText
android:id="#+id/edtext3"
android:layout_toRightOf="#+id/label4"
android:layout_centerHorizontal="true"
android:layout_alignBaseline="#+id/label4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/button2"
android:layout_below="#+id/label4"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Entries" />
</RelativeLayout>
You have not set OnClickListener to your Button so calcRectangle() method is never called.
calcButton = (Button)findViewById(R.id.button1);
calcButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
number1 = (EditText) findViewById(R.id.edtext1);
number2 = (EditText) findViewById(R.id.edtext2);
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
calcRectangle(num1, num2);
}
});
OR
calcButton.setOnClickListener(this);
I'm in trouble, this code is not working.
I think my error is in the class, that has the function of the sum in class check1a4. It should be a 'Fragment'?
I tried searching in some tutorials but I did not find a solution.
MainActivity
package com.journaldev.navigationdrawer;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
android.support.v7.app.ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
setupToolbar();
DataModel[] drawerItem = new DataModel[3];
drawerItem[0] = new DataModel(R.drawable.connect, "Check List (1 a 4)");
drawerItem[1] = new DataModel(R.drawable.fixtures, "Check List (1 a 19)");
drawerItem[2] = new DataModel(R.drawable.table, "Sobre a sove");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.list_view_item_row, drawerItem);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerListener(mDrawerToggle);
setupDrawerToggle();
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ConnectFragment();
break;
case 1:
fragment = new FixturesFragment();
break;
case 2:
fragment = new TableFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
void setupToolbar(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
void setupDrawerToggle(){
mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.app_name, R.string.app_name);
//This is necessary to change the icon of the Drawer Toggle upon state change.
mDrawerToggle.syncState();
}
}
**java **
package com.journaldev.navigationdrawer;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.widget.*;
import android.view.*;
public class check1a4 extends Activity {
/** Called when the activity is first created. */
CheckBox a,s,d,f,g,h,j,k,q,w,e,rr,t,y,u,i,z,x,c,v,b,n,m, aa,ss,dd,ff;
EditText resultado;
EditText hh,jj,kk,fr,qq,ww,ee;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check1a4);
a = (CheckBox) findViewById(R.id.a);
s = (CheckBox) findViewById(R.id.s);
d = (CheckBox) findViewById(R.id.d);
f = (CheckBox) findViewById(R.id.f);
g = (CheckBox) findViewById(R.id.g);
h = (CheckBox) findViewById(R.id.h);
j = (CheckBox) findViewById(R.id.j);
k = (CheckBox) findViewById(R.id.k);
q = (CheckBox) findViewById(R.id.q);
w = (CheckBox) findViewById(R.id.w);
e = (CheckBox) findViewById(R.id.e);
t = (CheckBox) findViewById(R.id.t);
y = (CheckBox) findViewById(R.id.y);
u = (CheckBox) findViewById(R.id.u);
i = (CheckBox) findViewById(R.id.i);
z = (CheckBox) findViewById(R.id.z);
x = (CheckBox) findViewById(R.id.x);
c = (CheckBox) findViewById(R.id.c);
v = (CheckBox) findViewById(R.id.v);
b = (CheckBox) findViewById(R.id.b);
n = (CheckBox) findViewById(R.id.n);
m = (CheckBox) findViewById(R.id.m);
rr = (CheckBox) findViewById(R.id.rr);
aa = (CheckBox) findViewById(R.id.aa);
ss = (CheckBox) findViewById(R.id.ss);
dd = (CheckBox) findViewById(R.id.dd);
ff = (CheckBox) findViewById(R.id.ff);
resultado = (EditText) findViewById(R.id.total);
hh = (EditText) findViewById(R.id.hh);
jj = (EditText) findViewById(R.id.jj);
kk = (EditText) findViewById(R.id.kk);
fr = (EditText) findViewById(R.id.fr);
qq = (EditText) findViewById(R.id.qq);
ww = (EditText) findViewById(R.id.ww);
ee = (EditText) findViewById(R.id.ee);
Button bttotal = (Button) findViewById(R.id.btsomar);
bttotal.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
double total = 0;
if (a.isChecked()) {
total += 10.0;
}
if (s.isChecked()) {
total += 5.00;
}
if (d.isChecked()) {
total += 5.00;
}
if (f.isChecked()) {
total += 4.00;
}
if (g.isChecked()) {
total += 1.00;
}
if (h.isChecked()) {
total += 1.00;
}
if (j.isChecked()) {
total += 1.00;
}
if (k.isChecked()) {
total += 2.00;
}
if (q.isChecked()) {
total += 1.00;
}
if (w.isChecked()) {
total += 2.00;
}
if (e.isChecked()) {
total += 2.00;
}
if (rr.isChecked()) {
total += 1.00;
}
if (t.isChecked()) {
total += 3.50;
}
if (y.isChecked()) {
total += 3.50;
}
if (u.isChecked()) {
total += 1.00;
}
if (z.isChecked()) {
total += 4.50;
}
if (x.isChecked()) {
total += 6.00;
}
if (c.isChecked()) {
total += 4.50;
}
if (v.isChecked()) {
total += 1.00;
}
if (b.isChecked()) {
total += 1.00;
}
if (z.isChecked()) {
total += 4.50;
}
if (n.isChecked()) {
total += 6.00;
}
if (m.isChecked()) {
total += 4.00;
}
if (aa.isChecked()) {
total += 1.50;
}
if (ss.isChecked()) {
total += 2.00;
}
if (dd.isChecked()) {
total += 2.50;
}
if (ff.isChecked()) {
total += 2.00;
}
double num1 = Double.parseDouble(hh.getText().toString());
double num2 = Double.parseDouble(jj.getText().toString());
double num3 = Double.parseDouble(kk.getText().toString());
double num4 = Double.parseDouble(qq.getText().toString());
double num5 = Double.parseDouble(ww.getText().toString());
double num6 = Double.parseDouble(ee.getText().toString());
double num7 = Double.parseDouble(fr.getText().toString());
////
resultado.setText(String.valueOf(num1+num2+num3+num4+num5+num6+num7+total));
}
});
}}
R.layout.check1a
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:focusable="false"
android:layout_height="match_parent" >
<ScrollView 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:orientation="vertical"
android:focusable="false"
android:soundEffectsEnabled="false"
android:splitMotionEvents="false">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="false"
android:addStatesFromChildren="false"
android:background="#android:color/background_light"
android:backgroundTint="#android:color/background_dark"
android:isScrollContainer="true"
android:orientation="vertical"
android:weightSum="1"
tools:isScrollContainer="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Red Lojas 1 a 4"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Disponibilidade Sua loja possui"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CC 2L + CC LT 350 ML" />
<CheckBox
android:id="#+id/s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CC Ls 1l + CCRefPet 2L" />
<CheckBox
android:id="#+id/d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CC mini pet 250 ml + Ft Lar. 2l" />
<CheckBox
android:id="#+id/f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Del valle kapo+ TP 200ml ou lt 350ml + Necta 1L + Crystal individual" />
<CheckBox
android:id="#+id/g"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="energetico" />
<CheckBox
android:id="#+id/h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heineken LT 350ml" />
<CheckBox
android:id="#+id/j"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heineken LN 330 ml" />
<CheckBox
android:id="#+id/k"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kaiser Latao Ou LT 350 ml" />
<CheckBox
android:id="#+id/q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sol LN" />
<TextView
android:layout_width="372dp"
android:layout_height="wrap_content"
android:editable="false"
android:text="Ativação de sua loja"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textIsSelectable="false"
android:textSize="18sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pop Externo ou entrada da loja" />
<CheckBox
android:id="#+id/e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pop Interno Familia" />
<CheckBox
android:id="#+id/rr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Presença Refre Retornavel (minimo 3 frentes)" />
<CheckBox
android:id="#+id/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Presença Refre Retornavel gelado (minimo 3 frentes)" />
<CheckBox
android:id="#+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rack entrada da loja de retornável" />
<CheckBox
android:id="#+id/u"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rack de suco ou ponto entra (minimo 20 frentes)" />
<CheckBox
android:id="#+id/i"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Presença de pack fechado de cerveja na gondola" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Preço correto"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CC Ref Pet 2l + CC LS 1L" />
<CheckBox
android:id="#+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CC 2l" />
<CheckBox
android:id="#+id/c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CC LT 350 ML" />
<CheckBox
android:id="#+id/v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heineken LN 330 Ml" />
<CheckBox
android:id="#+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cerveja mainstream Lt 350 ml ou Lt 473 ml" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Comunicação de preço"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/n"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tudo certo maluco!"
android:textAppearance="#style/TextAppearance.AppCompat" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Geladeira"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 porta com refri" />
<CheckBox
android:id="#+id/aa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 porta com ncarbs" />
<CheckBox
android:id="#+id/ss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GDM não invadida" />
<CheckBox
android:id="#+id/dd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gdm 80% abastecida" />
<CheckBox
android:id="#+id/ff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GDM com 50% de colas" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sua Sove em loja"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sovi refrigerante nota: 3.5" />
<EditText
android:id="#+id/hh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sovi gelado refrigerante nota: 3.0" />
<EditText
android:id="#+id/jj"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sovi refrigerante individual nota:4.5" />
<EditText
android:id="#+id/kk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sovi Ncarbs nota: 1.0" />
<EditText
android:id="#+id/fr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sovi Gelado Ncarbs nota: 1.0" />
<EditText
android:id="#+id/qq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sovi Ncarbs individual nota: 2.0" />
<EditText
android:id="#+id/ww"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sovi refrigerante retornavel nota: 7.0" />
<EditText
android:id="#+id/ee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="0" />
<Button
android:id="#+id/btsomar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Verificar nota da loja" />
<EditText
android:id="#+id/total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="110"
android:inputType="textPersonName"
android:text="0" />
</LinearLayout>
</ScrollView>
**activity_main xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.hynes.equations.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please select what type of equation to solve"
android:id="#+id/textView2" />
<Button
android:text="Check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3"
android:layout_marginRight="230dp"
android:layout_marginEnd="230dp"
android:id="#+id/button2" />
<TextView
android:layout_centerVertical="true"
android:id="#+id/textView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="50dp"
android:layout_width="250dp"
android:layout_alignRight="#+id/textView2"
android:layout_alignEnd="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_above="#+id/button2"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:layout_marginRight="170dp"
android:layout_marginEnd="170dp"
android:layout_marginBottom="36dp"
android:id="#+id/editText2" />
<RadioGroup android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:text="Addition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="26dp"
android:id="#+id/addition"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Subtraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:id="#+id/subtraction"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:id="#+id/division"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Multiplication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:id="#+id/multiplication"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
`
MAIN ACTIVITY
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;`
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int x;
private int y;
public int a;
public int b;
Random random = new Random();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View view){
TextView display = (TextView) findViewById(R.id.textView5);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int btnID = radioGroup.getCheckedRadioButtonId();
switch(btnID){
case R.id.addition:
x = random.nextInt(100);
y = random.nextInt(100);
display.setText(x + "+" + y + "=");
break;
case R.id.subtraction:
x = random.nextInt(100);
y = random.nextInt(100);
display.setText(x + "-" + y + "=");
break;
case R.id.division:
y = random.nextInt(5);
x = y*2;
display.setText(x + "/" + y + "=");
break;
case R.id.multiplication:
x = random.nextInt(10);
y = random.nextInt(10);
display.setText(x + "*" + y + "=");
break;
}
public void onClickCheck(View view){
TextView result = (TextView) findViewById(R.id.textView5);
EditText ans = (EditText) findViewById(R.id.editText2);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int btnID = radioGroup.getCheckedRadioButtonId();
if(boolean(RadioButton) findViewById(R.id.addition)) ){
int key = x+y;
int try = 0;
while(!ans.equals(key)){
try = try + 1;
if(try ==0){
result.setText("Incorrect, try again!");
try++
}else if (try>2){
result.setText("Incorrect, the answer is : " + key);
}
if(ans.equals(key)){
result.setText("You are correct !");
}
}
}
}
}
}
}
This is a android studio app that's suppose to randomly generate a math equation upon creation which I've done. There's addition, subtraction, division, and multiplication. My random equation generator works perfectly. But I'm stuck at my onClickCheck method which i want to get the user's answer/input and check to see if it's correct or incorrect and display a message accordingly. My onClick method is wrong but it's a start. I don't know what methods i need to get my code to accept inputs properly. Any help would be appreciated.
It looks like you aren't assigning an OnClickListener to the button.
You need to put:(in onCreate)
Button button = (Button)findViewById(R.id.button2);
button.setOnClickListener(onClickCheck);
or
Button button = (Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickCheck(view);
}
});
Same for the radio button.
I created a tictactoe android game, and when i try to link the strings.xml file to activity.java I receive an error.
Here is my activity.java
package com.tictactoeoyna.www;
import android.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TicTacToeOynaActivity extends Activity {
private TicTacToeOyun mGame;
private Button mBoardButtons[];
private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;
private int mHumanCounter = 0;
private int mTieCounter = 0;
private int mAndroidCounter = 0;
private boolean mHumanFirst = true;
private boolean mGameOver = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBoardButtons = new Button[TicTacToeOyun.getBOARD_SIZE()];
mBoardButtons[0] = (Button) findViewById(R.id.one);// it should be blue but not
mBoardButtons[1] = (Button) findViewById(R.id.two);//same
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);//until here
mInfoTextView = (TextView) findViewById(R.id.information);
mHumanCount = (TextView) findViewById(R.id.humanCount);
mTieCount = (TextView) findViewById(R.id.tiesCount);
mAndroidCount = (TextView) findViewById(R.id.androidCount);
mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGame = new TicTacToeOyun();
startNewGame();
}
private void startNewGame()
{
mGame.clearBoard();
for (int i = 0; i < mBoardButtons.length; i++)
{
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
}
if (mHumanFirst)
{
mInfoTextView.setText(R.string.first_human);
mHumanFirst = false;
}
else
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
mHumanFirst = true;
}
}
private class ButtonClickListener implements View.OnClickListener
{
int location;
public ButtonClickListener(int location)
{
this.location = location;
}
public void onClick(View view)
{
if (!mGameOver)
{
if (mBoardButtons[location].isEnabled())
{
setMove(mGame.HUMAN_PLAYER, location);
int winner = mGame.checkForWinner();
if (winner == 0)
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
winner = mGame.checkForWinner();
}
if (winner == 0)
mInfoTextView.setText(R.string.turn_human);
else if (winner == 1)
{
mInfoTextView.setText(R.string.result_tie);
mTieCounter++;
mTieCount.setText(Integer.toString(mTieCounter));
mGameOver = true;
}
else if (winner == 2)
{
mInfoTextView.setText(R.string.result_human_wins);
mHumanCounter++;
mHumanCount.setText(Integer.toString(mHumanCounter));
mGameOver = true;
}
else
{
mInfoTextView.setText(R.string.result_android_wins);
mAndroidCounter++;
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGameOver = true;
}
}
}
}
}
private void setMove(char player, int location)
{
mGame.setMove(player, location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == mGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.GREEN);
else
mBoardButtons[location].setTextColor(Color.RED);
}
}
The error is on one, two, three...nine and information count, human count etc. Here is my fragment file, so why can't I see one, two, three....nine labeled blue? Why is it not recognized? What have I done wrong ? I received the errors after findviewbyid.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/playArea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="one"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/one"
android:textSize="70dp" />
<Button
android:id="#+id/two"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/two"
android:textSize="70dp" />
<Button
android:id="#+id/three"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/three"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/four"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/four"
android:textSize="70dp" />
<Button
android:id="#+id/five"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/five"
android:textSize="70dp" />
<Button
android:id="#+id/six"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/six"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/seven"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/seven"
android:textSize="70dp" />
<Button
android:id="#+id/eight"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/eight"
android:textSize="70dp" />
<Button
android:id="#+id/nine"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/nine"
android:textSize="70dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:text="#string/info"
android:textSize="25dp" />
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/human"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/human" />
<TextView
android:id="#+id/humanCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/ties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ties" />
<TextView
android:id="#+id/tiesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/android" />
<TextView
android:id="#+id/androidCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</LinearLayout>
import com.tictactoeoyna.www.R;
and removeimport android.R;
When you got this type of error at anywhere and you have import android.R; then just remove import android.R;.
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.