Simple multiplication app in Android - android

So like so many others i am new to android. I have looked everywhere for an explanation of how to do this but have found none.
this is the app im trying to make.
http://i.stack.imgur.com/JGhJP.png
Im assuming i have to take the input from the top two edittexts and put them into stings and them multiply them and set that answers equal to the bottom edittext?
Im just confused as to how i would correctly do that.
Here is what ive tried so far.
package com.wattzen.testcalculation;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
public class Main extends Activity {
private static final String total_Made = "total_Made";
private double tmmade;
private int hedittext;
private int etmade;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
tmmade = 0.0;
} else {
tmmade = savedInstanceState.getDouble(total_Made);
}
}
{
{
hedittext = (EditText) findViewById(R.id.ethours);
etmade = (EditText) findViewById(R.id.etmade);
etmade.addTextChangedListener(etmadeWatcher);}
}
private void updateStandard() {
double tmmade = hedittext * etmade;
tmmade.addTextChangedListener(tmmadeWatcher);
double toTal= tmmade;
}
}
and here is the xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hours Worked" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/ethours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="Amount made per hour" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/etmade"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/bcalculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Calculate" />
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Money Made" />
</TableRow>
<EditText
android:id="#+id/tmmade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="number" />
</TableLayout>
I greatly appreciate any help you guys can give me. thanks.

You should be getting a ton of syntax errors that should clue you in on lines that need changing.
Since hedittext and etmade are ints, you cannot set them equal to an EditText, as you do in onCreate().
First you need to get the actual content of the EditTexts. You can do this by calling the edit text's getText() methods.
Then you need to parse the result of getText as an integer. You can do this using the Integer wrapper class' parseInt() method.
It should look more like this:
EditText editTextA = (EditText) findViewById(R.id.ethours);
int hours = Integer.parseInt(editTextA.getText().toString())

Your first step is to assign an onClick listener the calculate button. Once you do this, reference both the editTexts within onCreate. After this, you can get the text from the edit texts using the getText() function.
Here is a basic mockup of the code. Id suggest checking if the input is valid first and then multiplying
Button bcalculate = (Button)findViewById(R.id.bcalculate);
EditText hedittext = (EditText)findViewById(R.id.ethours);
EditText etmade = (EditText)findViewById(R.id.etmade);
EditText tmmade = (EditText)findViewById(R.id.edittext3);
bcalculate.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
try{
double e1 = Double.parseDouble(hedittext.getText().toString());
double e2 = Double.parseDouble(etmade.getText().toString());
tmmade.setText(e1*e2);
} catch (NumberFormatException e) {
}
});

Related

Why couldn't I use XML onClick attribute for Event Handling?

I am writing a simple computing machine which basically shows in the text view the sum of 2 numbers I entered in two edit text. Theoretically, it should be right, but in my case I feel like something went wrong, perhaps about missing components. At first, I thought the problem had come from the edit text but later I changed it and nothing new happened. That makes me doubt about the use of onClick attribute. The program automatically stops when I pressed the button
Specifically, I have my java code
package com.example.computing_machine;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Computing_Machine extends Activity {
private TextView text;
protected void onCreate(Bundle x) {
super.onCreate(x);
setContentView(R.layout.activity_computing_machine);
text = (TextView) findViewById(R.id.textView3);
}
public void sum(View v) {
/* EditText e1= (EditText) findViewById(R.id.editText1);
EditText e2= (EditText) findViewById(R.id.editText2);
int a = Integer.parseInt(e1.getText() + "");
int b = Integer.parseInt(e2.getText() + ""); */
text.setText(5);
}
}
And this is my XML file
<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.computing_machine.Computing_Machine" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/number1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignStart="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="23dp"
android:text="#string/number2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_toRightOf="#+id/textView1"
android:layout_toEndOf="#+id/textView1"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignStart="#+id/editText1"
android:layout_below="#+id/editText1"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/editText2"
android:layout_alignEnd="#+id/editText2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:onClick="sum"/>
Try this:
Code:
int a = Integer.parseInt(""+e1.getText().toString());
int b = Integer.parseInt(""+e2.getText().toString());
Try
text.setText("" + 5);
setText takes a string as its argument. You are passing an int.

Disabling focus on EditText

I'm using the setOnFocusChangeListener but I'm looking for something more viable for my application. Currently this is what I'm using.
if(primaryCode == CodeCC)
{
mKeyboardView= (KeyboardView)mHostActivity.findViewById(R.id.keyboardview);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, R.xml.symbol2keyboard));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(symbol2Keyboard);
final EditText edit1 = (EditText) mHostActivity.findViewById(R.id.row1);
final EditText edit2 = (EditText) mHostActivity.findViewById(R.id.row2);
final EditText edit3 = (EditText) mHostActivity.findViewById(R.id.row3);
final EditText edit4 = (EditText) mHostActivity.findViewById(R.id.row4);
edit1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
edit2.setEnabled(false);
}else
edit2.setEnabled(true);
}
});
I'm wondering if there is something like, if I click edit1, edit2 will be disabled. But when I click edit2, edit2 will be enabled back but edit3 will be disabled etc.
Right now, when edit2 is enabled, I can't even click it or it doesn't allow the focus to be on edit2. Is it fixed that way or is there another way that I can work around with?
Thanks
Layout Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/backselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/back" />
<Button
android:id="#+id/nextselection"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/next" />
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/textModeTab"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/row1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text" />
<EditText
android:id="#+id/row2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#id/row1"
android:layout_marginTop="20dp"
android:inputType="text" />
<EditText
android:id="#+id/row3"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#id/row2"
android:layout_marginTop="20dp"
android:inputType="text" />
<EditText
android:id="#+id/row4"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#id/row3"
android:layout_marginTop="20dp"
android:inputType="text" />
<CheckBox
android:id="#+id/row4CB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row2"
android:layout_alignBottom="#+id/row2"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp" />
<CheckBox
android:id="#+id/row3CB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row1"
android:layout_alignBottom="#+id/row1"
android:layout_alignLeft="#+id/row4CB"
android:layout_marginTop="20dp" />
<CheckBox
android:id="#+id/row1CB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row3"
android:layout_alignBottom="#+id/row3"
android:layout_alignLeft="#+id/row4CB"
android:layout_marginTop="20dp" />
<CheckBox
android:id="#+id/row2CB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row4"
android:layout_alignBottom="#+id/row4"
android:layout_alignLeft="#+id/row1CB"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/row3CB"
android:text="Scroll"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/graphicsModeTab"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/animationModeTab"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
I don't think it is possible. If you disable an element, it won't respond to any events, including focus events. You may still get focus on another element and edit2 should be enabled then, as edit1 will loose its focus. That works as designed.
I would not recommend to do that, but you can try to extend Button class to override its disabled behaviour. It should draw itself in disabled state, while being actually enabled. I can't provide any code example as I failed to implement it myself fast.
Check sources of View class and Drawable implementations.
This is assuming that you absolutely necessarily want this feature and cannot do without it:
What you can do is change your layout a bit.Place a transparent relative layout on top of each edit text.When an onclick of the edit text is triggerred,you can disable everything you want accordingly and also make the relative layout invisible and request focus on your edit text.Its slightly tedious but layout masking opens a a large number of possibiltiies for the sort of things you are trying.
If you want a code example,please add your layout code and comment on my answer.I'll update it.
EDIT: added code
there you go.I dont like the hack much,but it does what you want.You might have to tweak the keyboard opening to make it better.
The activity
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView row1, row2;
private EditText row1Et, row2Et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
row1 = (TextView) findViewById(R.id.row1cover);
row2 = (TextView) findViewById(R.id.row2cover);
row1Et = (EditText) findViewById(R.id.row1);
row2Et = (EditText) findViewById(R.id.row2);
row1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
row1.setVisibility(View.INVISIBLE);
row2.setVisibility(View.VISIBLE);
row1Et.setEnabled(true);
row2Et.setEnabled(false);
row1Et.requestFocus();
row1Et.performClick();// to open the key
Log.d("disabled", "1 is disabled?:" + row1Et.isEnabled());
Log.d("disabled", "2 is disabled?:" + row2Et.isEnabled());
}
});
row2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
row2.setVisibility(View.INVISIBLE);
row1.setVisibility(View.VISIBLE);
row1Et.setEnabled(false);
row2Et.setEnabled(true);
row2Et.requestFocus();
row2Et.performClick();
Log.d("disabled", "2 is disabled?:" + row2Et.isEnabled());
Log.d("disabled", "1 is disabled?:" + row1Et.isEnabled());
}
});
OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
} else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
};
row1Et.setOnFocusChangeListener(focusListener);
row2Et.setOnFocusChangeListener(focusListener);
}
}
and layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<Button
android:id="#+id/backselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="back" />
<Button
android:id="#+id/nextselection"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="next" />
<RelativeLayout
android:id="#+id/textModeTab"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/row1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text" />
<TextView
android:id="#+id/row1cover"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/row1"
android:layout_alignTop="#id/row1" />
<EditText
android:id="#+id/row2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#id/row1"
android:layout_marginTop="20dp"
android:inputType="text" />
<TextView
android:id="#+id/row2cover"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/row2"
android:layout_alignTop="#id/row2" />
<CheckBox
android:id="#+id/row1CB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row1"
android:layout_alignBottom="#+id/row1"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/row1" />
<CheckBox
android:id="#+id/row2CB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row2"
android:layout_alignBottom="#+id/row2"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/row2" />
</RelativeLayout>

How to use ListView from class1 in class2 in android project?

I was browsing android forums on the internet and this is the place I find most acurate and professional answers to questions regarding Android.
I'm learning programming for Android and I'm working on one application.
I have implemented SQLite database with read and write, shwoing data in EditBoxes and similar things, but I can't implement ListView.
I have some example with ListView showing data from database.
I have managed to adapt that to show data from my database and it works as separate application.
My question is:
How can I add that modified example to my aplication?
That example has class:
public class DataListView extends ListActivity
and it's using another class for database access from my application:
public class DatabaseManager extends SQLiteOpenHelper
But, my own application has following class:
public class MyWorkActivity extends Activity implements OnClickListener
How can I call and use DataListView class in my own project?
Please help me, because I'm working on this ListView for over one week with no progress.
This is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/honeycomb2"
android:shrinkColumns="*"
android:stretchColumns="9" >
<TableRow
android:id="#+id/tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:stretchColumns="6" >
<TextView
android:id="#+id/lblVrijemDolaska"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/lblVrijemeDolaska"
android:textSize="20sp" />
<requestFocus />
<TextView
android:id="#+id/lblVrijemeOdlaska"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/lblVrijemeOdlaska"
android:textSize="20sp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#+id/tvVrijemeDolaska"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:shadowColor="#00ccff"
android:shadowDx="2"
android:shadowRadius="2"
android:text="Vr. dolaska"
android:textSize="30sp" />
<TextView
android:id="#+id/tvVrijemeOdlaska"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="center"
android:shadowColor="#00ccff"
android:shadowDx="2"
android:shadowRadius="2"
android:text="Vr. odlaska"
android:textSize="30sp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#+id/lblDorucak3"
android:layout_gravity="center"
android:gravity="right"
android:text="#string/lblDorucak3" />
<TextView
android:id="#+id/lblPauza3"
android:layout_gravity="center"
android:gravity="right"
android:text="#string/lblPauza3" />
</TableRow>
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="9"
android:gravity="center" >
<EditText
android:id="#+id/txtDorucak3"
android:gravity="center"
android:layout_span="1"
android:layout_width="60dp"
android:inputType="number" />
<TextView
android:id="#+id/lblRazmak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
<EditText
android:id="#+id/txtPauza3"
android:layout_width="60dp"
android:layout_span="1"
android:gravity="center"
android:inputType="number" />
</TableRow>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/btnDodaj"
android:layout_width="75dp"
android:layout_height="85dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="61dp"
android:background="#drawable/dodaj"
android:scaleType="fitXY"
android:text="#string/btnDodaj" />
<Button
android:id="#+id/btnIzbrisi"
android:layout_width="75dp"
android:layout_height="85dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="68dp"
android:background="#drawable/izbrisi"
android:scaleType="fitXY"
android:text="#string/btnIzbrisi" />
</RelativeLayout>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableRow>
<ScrollView
android:id="#+id/scrollViewDatabase"
android:layout_width="fill_parent"
android:layout_height="49dp" >
<TextView
android:id="#+id/tvZaBazu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<ListView
android:id="#+id/lista"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:textSize="5sp" >
</ListView>
</TableLayout>
And this is ListView example that works with my database as separate app but nt in my project:
import java.util.ArrayList;
import com.saigmn.DatabaseManager.DatabaseInfo;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
public class DataListView extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private SQLiteDatabase newDB;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
//TextView tView = new TextView(this);
//tView.setText("This data is retrieved from the database and only 4 " +
//"of the results are displayed");
//getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
newDB = this.openOrCreateDatabase(DatabaseInfo.IME_BAZE, MODE_PRIVATE, null);
Cursor c = newDB.rawQuery("SELECT * FROM " + DatabaseInfo.IME_TABELE, null);
if (c !=null && c.getCount() > 0) {
c.moveToFirst();
int Column1 = c.getColumnIndex("datum");
int Column2 = c.getColumnIndex("dolazak");
int Column3 = c.getColumnIndex("odlazak");
int Column4 = c.getColumnIndex("r_vrijeme");
do {
String datum = c.getString(Column1);
String dolazak = c.getString(Column2);
String odlazak = c.getString(Column3);
String r_vrijeme = c.getString(Column4);
results.add(datum + " * " + dolazak + " * " + odlazak + " -> " + r_vrijeme);
}while (c.moveToNext());
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Nije moguće kreirati ili otvoriti bazu");
} finally {
if (newDB != null)
//newDB.execSQL("DELETE FROM " + DatabaseInfo.IME_TABELE);
newDB.close();
}
}
}
You are implementing onClick listener in MyWorkActivity... so u have clickable objects..
on clicking one of the buttons.. you can make list view appear in your screen..in yourMyWorkActivity you have this part..
button.SetOnClickListener(this);
OnclickListener(View v)
{
switch(v.getGetId());
{
case R.id.x:<--- some button.. on clicking which listview should appear
Intent i=new Intent(MyWorkActivity.this,DataListView.class)
startActivity(i);
// by using these lines your application will display the datalistview activity you created..
}
}
}

Android: Why does my EditText keep its value, even on orientation change?

this is a question out of curiosity.
I recently just learned that i have to care about keeping my activity state on orientation changes myself.
But in one of my Layouts, the Edittext just keeps its value and i do not get why?
Obviously i would like to reproduce this behaviour for other EditTexts...
Heres the Layout, the edittext is android:id="#+id/createlistactivity_listname_edittext"
<?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" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/title_bar"
android:gravity="center"
android:text="Enter a Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/createlistactivity_listname_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:hint="Name..."
android:singleLine="true" >
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/CreateListActivity_HeaderLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/title_bar"
android:gravity="center"
android:text="Add Products to your new List"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:footerDividersEnabled="true" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="No Products available. Insert some Products in the Products menu first" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/CreateListActivity_BottomBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
style="#android:style/ButtonBar" >
<ImageButton
android:id="#+id/CreateListActivity_SaveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#android:color/transparent"
android:onClick="saveButtonClicked"
android:src="#drawable/tick_32" />
<ImageButton
android:id="#+id/CreateListActivity_CancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#android:color/transparent"
android:onClick="cancelButtonClicked"
android:src="#drawable/block_32_2" />
</LinearLayout>
</LinearLayout>
and here is the onCreate for the activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createlistactivity_layout);
_selectedItems = new HashMap<Integer, Integer>();
_productDAO = new ProductDAO(getApplication());
_listDAO = new ListDAO(getApplication());
_productCursor = _productDAO.getAllProductsCursor();
startManagingCursor(_productCursor);
setUpListView();
final EditText listNameEditText = (EditText)this.findViewById(R.id.createlistactivity_listname_edittext);
listNameEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(listNameEditText
.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
}
this is the code of another activity which contains edittexts that do not preserve their state
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="#+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/productImage"
android:background="#android:drawable/alert_light_frame" >
<TableRow
android:id="#+id/productview_toprow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >
<EditText
android:id="#+id/productview_productname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="Product Name"
android:singleLine="true" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="#string/productview_pricelabel"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/black" />
<EditText
android:id="#+id/productview_price_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="0.0"
android:singleLine="true"
android:inputType="numberDecimal" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="#string/productview_unitlabel"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/black" />
<EditText
android:id="#+id/productview_unit_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="kg|ml|..." />
</TableRow>
</TableLayout>
and the onCreate...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productview);
_productName = (EditText)findViewById(R.id.productview_productname);
_productPrice = (EditText)findViewById(R.id.productview_price_edittext);
_productUnit = (EditText)findViewById(R.id.productview_unit_edittext);
_productImage = (ImageButton)findViewById(R.id.productImage);
_productDAO = new ProductDAO(getApplication());
//set data from database
_product = _productDAO.getProduct(getIntent().getExtras().getString(DataBaseKeys.PRODUCT_NAME));
if(_product != null)
{
_productName.setText(_product.getName());
_productImage.setImageBitmap(_product.getIcon());
_productPrice.setText(""+_product.getPrice());
_productUnit.setText(_product.getUnit());
_productIcon = _product.getIcon();
}
}
Well the Views in android do keep some of its state. Like EditText keep its text so you dont have to save it onSaveInstanceState. Other examples are ListView keep the scroll position. EditText also keeps scroll position if it multi-line text.
But some of the thing a user has to takecare like if you are having a Note App, you have to store Note ID which is being edited so that on orientation change you know what you are working on.
This is a standard way of Android, nothing unusual here. For more details refer.
Saving Activity State
Regarding one of your activity not behaving as aspected is because of code:
_product = _productDAO.getProduct(getIntent().getExtras().getString(DataBaseKeys.PRODUCT_NAME));
if(_product != null)
{
_productName.setText(_product.getName());
_productImage.setImageBitmap(_product.getIcon());
_productPrice.setText(""+_product.getPrice());
_productUnit.setText(_product.getUnit());
_productIcon = _product.getIcon();
}
You are overriding the values of the edit text. Try this log statement before if statement.
Log.i("EDIT_NOT_WORKING", "VALUE OF EDIT: " + _productName.getText());
You should see that it will print the entered value.
If you don't give an id to your editText, it won't save the value on screen change. You have to give an id so that the value is saved, even if the screen changes orientation.

Problems from Editable (EditText) to Integers

I am having problems getting the integer values for a simple math program out of the edittext boxes. I can get a string out of it but when I try to convert it to a integer it crashes. I have tried:
int a = Intger.parseInt(X2.getText().toString());
and it still crashes. Any idea's would be greatly appreciated.
Here is source code.
package siu.edu.cs215;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Quadratic extends Activity {
private TextView answer;
private EditText X2, X, K;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quad);
Button1 = (Button)findViewById(R.id.quad);
Button2 = (Button)findViewById(R.id.backwards);
answer = (TextView)findViewById(R.id.B1);
X2 = (EditText)findViewById(R.id.xsquared);
X = (EditText)findViewById(R.id.xcoeff);
K = (EditText)findViewById(R.id.kconst);
}
public Button Button1 = null;
public Button Button2 = null;
public void QA (View view){
//double sol1=0.0, sol2=0.0, disc=0.0;
int a = Integer.parseInt(X2.getText().toString());
//int b = Integer.getInteger(X.getText().toString());
//int c = Integer.getInteger(K.getText().toString());
//disc = Math.pow(b, 2) - 4.0*a*c;
//sol1 = (-b + Math.sqrt(disc)) / (2.0 * a);
//sol2 = (-b - Math.sqrt(disc)) / (2.0 * a);
answer.setText(a);
};
public void BACK (View view){
Intent i = new Intent(this, ExtraCreditActivity.class);
startActivity(i);
}
}
Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".6" >
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<EditText
android:id="#+id/xsquared"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="X^2 +"
android:textSize="20dp" />
<EditText
android:id="#+id/xcoeff"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="X + "
android:textSize="20dp" />
<EditText
android:id="#+id/kconst"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:inputType="number" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.2" >
<Button
android:id="#+id/quad"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="QA"
android:text="Quadratic" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".8" >
<TextView
android:id="#+id/B1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2pt"
android:text="#string/app_name" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/backwards"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="BACK" />
</TableRow>
</TableLayout>
</LinearLayout>
Most likely you're problem is in trying to parse an empty string. Perhaps you can simply avoid performing any calculations unless all fields can be correctly parsed. Put your parsing into a try/catch block.

Categories

Resources