How to handle lot's of potential choices from user - java

I am in the process of learning android/java and I am creating an application that does conversions. The issue that I am running into is that there are a lot of different combinations that can be made as the user has to select a unit to convert from and one to convert to. I need a solution that can determine which unit has been chosen so I can do a calculation with values that are assigned to each unit. Here is the code.
Thanks in advance for any response.
Dom.
<?xml version="1.0" encoding="utf-8"?>
<android.widget.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/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Conversions"
android:textSize="20sp" />
<TextView
android:id="#+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:text="Weight"
android:textSize="20sp"
android:layout_marginLeft="10dp"/>
<Spinner
android:id="#+id/spin1"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="#+id/weight"
>
</Spinner>
<Spinner
android:id="#+id/spin2"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="#+id/weight"
android:layout_toRightOf="#id/to">
</Spinner>
<TextView
android:id="#+id/to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/weight"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toRightOf="#id/spin1"
android:text="-->"
android:textSize="25sp" />
<EditText
android:id="#+id/val1"
android:layout_width="75dp"
android:layout_height="60dp"
android:inputType="numberDecimal"
android:layout_below="#id/spin1"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#id/to"
/>
<TextView
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text=""
android:layout_toRightOf="#+id/val1"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/spin1"
/>
<Button
android:id="#+id/calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:textAllCaps="false"
android:layout_below="#id/spin2"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
/>
</android.widget.RelativeLayout>`
Java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = findViewById(R.id.spin1);
String[] items1 = {"Gram", "Kilogram", "Stone", "Tonne", "Pound", "Ounce",};
ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items1);
spin1.setAdapter(adapter1);
Spinner spin2 = findViewById(R.id.spin2);
String[] items2 = {"Gram", "Kilogram", "Stone", "Tonne", "Pound", "Ounce",};
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items2);
spin2.setAdapter(adapter2);
}
}

I'm envisioning an app that converts an input distance between different units of measurement (feet, miles, meters, etc...). Here's what I'd do:
Create two variables of type double, called inputConversionFactor and outputConversionFactor. When the user selects an option from each spinner, this sets the corresponding conversion factor value.
When an input distance is entered, internally convert this distance to meters using inputConversionFactor. Then use outputConversionFactor to convert from meters to the desired output unit.
By doing it this way, you're breaking down the problem into two discrete steps. You don't have to worry about how to convert between any two arbitrary units, since you're always converting the input to meters and then the meters to the output unit type.

Related

How to dynamically add a group of buttons to android app - With categories

I'm a beginner to android and am working on an app to organize a chore counting system and I'm having some issues with my next step. I am trying to give users the option to input different chores at their choice within the app (ideally they would start with no chores then input them on their own). For each chore they enter, 4 new elements need to show up (the name of chore, increase arrow, decrease arrow and chore count number). I am also trying to keep those new additions organized based on if they fall under "kitchen, bathroom or general" (user would select that when they add the chore). I tried to use groups and tags but couldn't figure out how to make it work.
`package com.example.testbasic;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
int integer = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void increase(View view) {
integer = integer + 1;
System.out.println(view.getTag());
display(integer);
}
public void decrease(View view) {
integer = integer - 1;
System.out.println(view.getTag());
display(integer);
}
private void display(int number) {
TextView displayInteger = findViewById(R.id.integer_number);
displayInteger.setText("" + number);
}
}
`
Snip of the XML layout
'
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical"
tools:ignore="UselessParent">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/welcome_to_the_chore_calculator"
android:textSize="20sp" />
<TextView
android:id="#+id/Kitchen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="50dp"
android:text="#string/kitchen" />
<androidx.constraintlayout.widget.Group
android:id="#+id/EmptyDishRackG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="tv_EmptyDishRack,IncreaseEDR,DecreaseEDR,integer_numberEDR" />
<TextView
android:id="#+id/tv_EmptyDishRack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Kitchen"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="15dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="140dp"
android:background="#android:drawable/editbox_background"
android:drawablePadding="16dp"
android:hint="#string/empty_dish_rack"
android:padding="12dp"
tools:ignore="UseCompatTextViewDrawableXml" />
<ImageButton
android:id="#+id/IncreaseEDR"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="#+id/tv_EmptyDishRack"
android:layout_alignParentEnd="true"
android:layout_marginStart="1dp"
android:layout_marginTop="-2dp"
android:layout_marginEnd="15dp"
android:onClick="increase"
android:tag="EmptyDishRack"
app:srcCompat="#android:drawable/arrow_up_float"
tools:ignore="UsingOnClickInXml" />
<ImageButton
android:id="#+id/DecreaseEDR"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="#+id/tv_EmptyDishRack"
android:layout_marginStart="-140dp"
android:layout_marginTop="-2dp"
android:layout_marginEnd="32dp"
android:layout_toEndOf="#+id/tv_EmptyDishRack"
android:onClick="decrease"
android:tag="EmptyDishRack"
app:srcCompat="#android:drawable/arrow_down_float"
tools:ignore="UsingOnClickInXml" />
<TextView
android:id="#+id/integer_numberEDR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tv_EmptyDishRack"
android:layout_alignBottom="#+id/tv_EmptyDishRack"
android:layout_marginStart="-31dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:layout_toStartOf="#+id/IncreaseEDR"
android:layout_toEndOf="#+id/DecreaseEDR"
android:text="#string/_0"
android:textAlignment="center"
android:textSize="30sp" />'
Here is how it looks so far
Up and down arrows
Other categories
^the chores that are currently there are just examples users may have different ones
Any help would be greatly appreciated.
As Faizan says, a RecyclerView is probably the best option here. RecyclerView lets you have a list of items (your chores in this case) stored in an ArrayList.
You set up how the items will display in the RecyclerView. This is done using a ViewHolder. In your case the ViewHolder will have the name of the chore, the up/down arrows and the count of chores.
Because the RecyclerView is linked to your ArrayList, each chore will display automatically in its own ViewHolder. You can then update the Array with new items as the user adds them. Here is a link to an example:
http://www.digitstory.com/wp-content/uploads/2017/02/android_recyclerview.jpg
For more details, have a look here:
https://developer.android.com/guide/topics/ui/layout/recyclerview
To get started, you could try YouTube and seach for "RecyclerView Android Studio". Work through a few example tutorials to get the concept, and try to adapt them to your needs.

TextInputLayout not animating when OnFocusChangeListener applied to wrapped EditText

Can't say it much more clearly than in the title. I have an EditText wrapped by a Text Input Layout. I'm trying to trigger an event when that EditText loses focus. However, once the event listener is applied, the TextInputLayout no longer animates the text, it just sits on the editText line until information is entered, at which time it does animate. Can anyone give me some insight as to why this is happening?
Edit: Layout 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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/card_drawable"
android:gravity="center"
android:padding="11dp"
android:text="The fields below will be used to calculate your training maxes. Please enter your best rep record, or 1RM, for each lift. Training maxes will be determined using the following formula provided in Beyond 5/3/1: \n(Weight Lifted x Reps X .0333 + Weight Lifted)" />
<LinearLayout
android:id="#+id/squat_container"
style="#style/linear_layout_style">
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="#+id/squatWeight_editText"
style="#style/editText_floating_label"
android:hint="Squat Weight" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/txtInputLayoutSquat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="#+id/squatReps_editText"
style="#style/editText_floating_label"
android:hint="Repetitons" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/bench_container"
style="#style/linear_layout_style">
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="#+id/bechpressWeight_editText"
style="#style/editText_floating_label"
android:hint="Bench Press Weight" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="#+id/benchpressReps_editText"
style="#style/editText_floating_label"
android:hint="Repetitons" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/deadlift_container"
style="#style/linear_layout_style">
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="#+id/deadliftWeight_editText"
style="#style/editText_floating_label"
android:hint="Deadlift Weight" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="#+id/deadliftReps_editText"
style="#style/editText_floating_label"
android:hint="Repetitons" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ohp_container"
style="#style/linear_layout_style">
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="#+id/ohpWeight_editText"
style="#style/editText_floating_label"
android:hint="Overhead Press Weight" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="#+id/ohpReps_editText"
style="#style/editText_floating_label"
android:hint="Repetitons" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
I had to deal with the same issue.
The workaround I used was using a custom EditText and adding a custom listener to it to be called when the onFocusChanged method is fired.
Custom EditText (Boiler plate code is not shown)
public class EditTextPlus extends AppCompatEditText {
//This is the custom listener you will use
public OnFocusChangeListener customListener;
#Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (customListener != null) {
customListener.onFocusChange(this, focused);
}
}
}
In Activity/Fragment
yourEditText.customListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
//Do your stuff
}
}
Don't forget to use the custom EditText in the xml as well and not the default EditText.
This doesn't solve the issue but does give some insight (I'll update if I figure out a solution), but seems like the animated hint is dependent on OnFocusChangedListener, which gets overwritten when you add your own listener.
https://code.google.com/p/android/issues/detail?id=175344
EDIT:
Per https://code.google.com/p/android/issues/detail?id=178693...
We're now using a different way to determine if the EditText is focused. For now do something like:
TextInputLayout inputLayout = ...;
EditText editText = inputLayout.getEditText();
final OnFocusChangeListener existing = editText.getOnFocusChangeListener();
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View view, boolean focused) {
existing.onFocusChange(view, focused);
// Your custom logic
}
});

How to use scrollTo() with Spinner?

I'm working on my first Android project, which means that I'm not that familiar with Android and Java, and I want to move scroll of ScrollView after a user presses the button for next action (IME_ACTION_SEND). My codes are as follows.
activity_add.xml
It basically consists of TextView, EditText, NumberPicker, Spinner, Button. I want to move the scroll after IME_ACTION_SEND on EditText, so that the NumberPicker is centered on the screen.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/addActivity_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<LinearLayout
android:id="#+id/layout_activity_add"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_addItem"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="40sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout"
android:layout_marginTop="50dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine = "true"
android:maxLength="20" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout3"
android:layout_marginTop="50dp">
<TextView
android:id="#+id/period_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_period"
android:layout_centerHorizontal="true" />
<NumberPicker
android:id="#+id/period_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/period_textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</NumberPicker>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout4"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_category"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_toStartOf="#+id/background_spinner"
android:layout_toLeftOf="#+id/background_spinner" />
<Spinner
android:id="#+id/background_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical">
</Spinner>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_submit"
android:layout_gravity="center_horizontal"
android:text="#string/label_submit" />
</LinearLayout>
</ScrollView>
AddItemActivity.java
I've just copied the part which I think is relevant.
public class AddSinceItemActivity extends ActionBarActivity {
EditText title;
Spinner spinner;
NumberPicker numberPicker;
String title_string;
ViewGroup linearLayout;
ScrollView addActivity_scrollview;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
addActivity_scrollview = (ScrollView) findViewById(R.id.addActivity_scrollview);
linearLayout = (ViewGroup) findViewById(R.id.layout_activity_add);
title = (EditText) findViewById(R.id.title_editText);
numberPicker = (NumberPicker) findViewById(R.id.period_number_picker);
spinner = (Spinner) findViewById(R.id.background_spinner);
/* For title */
title.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
// I think here is the most important part.
addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());
}
return false;
}
});
}
}
For addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());,
I've tried many possible codes, such as
addActivity_scrollview.smoothScrollTo(0, spinner.getTop());
addActivity_scrollview.smoothScrollTo(0, 300); (300 is just a random number)
addActivity_scrollview.smoothScrollBy(0, 300);
but the scroll is always stuck (it moves a little but it's always the same position with above codes) and the screen barely shows the selected number of NumberPicker. How can I achieve the goal to set scroll so that the screen shows the entire NumberPicker?
just add this line to your edittext code in xml :android:imeOptions="actionSend" and then just try this into your activityaddActivity_scrollview.smoothScrollTo(0, 480);
I should've added android:imeOptions="actionSend" to <EditText> in order to properly listen on onEditorAction(). I thought it'd be okay with android:singLine="true" because that option enabled "next" action on keyboard.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine="true"
android:maxLength="20"
android:imeOptions="actionSend" />

Limit listview, and display letters only

I have a listview that is displayed to the user, and I am figuring out ways to make the listview non-infinite, and limit it to 25 items. I am also trying restrict user from being able to display a number in the EditText. I have tried to add android:inputType="text|textNoSuggestions" , in list view but this has proven to be ineffective.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/white_wallpaper"
>
<ListView
android:id="#+id/listMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/divider"
android:divider="#null"
android:dividerHeight="0dp"
android:inputType="text|textNoSuggestions"
android:padding="0dip"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
tools:listitem="#layout/message_left" />
<RelativeLayout
android:id="#+id/divider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#color/off_white"
android:layout_above="#+id/relSendMessage" />
<RelativeLayout
android:id="#+id/relSendMessage"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:background="#ddd"
android:paddingLeft="10dp"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/messageBodyField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/sendButton"
android:layout_alignTop="#+id/sendButton"
android:layout_marginBottom="-4dp"
android:layout_marginRight="10dp"
android:inputType="text|textNoSuggestions"
android:layout_toLeftOf="#+id/sendButton"
android:background="#android:color/white"
android:hint="#string/message_elipses"
android:textColor="#000000"
android:textColorLink="#adefda"
android:textSize="14sp" />
<Button
android:id="#+id/sendButton"
android:layout_width="72dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_margin="4dp"
android:background="#drawable/button_send" />
</RelativeLayout>
<Button
android:id="#+id/iSchedule"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:alpha="0.7"
android:background="#C11B17"
android:text="Schedule"
android:textColor="#f2f2f2"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
</RelativeLayout>
Any suggestions would be greatly appreciated.
Update:
As far as limiting listview items, I have the below code:
Below is how the listview is displayed programmatically:
messagesList = (ListView) findViewById(R.id.listMessages);
messageAdapter = new MessageAdapter(this);
WOuld the below work:
#Override
public int getCount() {
messagesList.setAdapter(messageAdapter);
return 25;
}
So to handle your edit text problem, you can use this
<EditText
...
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/>
Now the above characters are the only one that can be entered into the EditText. Also you can change the keyboard type in the android:inputType.
Additionally you wanted to limit the number of entries in a ListView. In the BaseAdapter method of your code, change this:
#Override
public int getCount() {
return 25;
}
UPDATE:
Well what you can do is create a custom ArrayAdapter and modify the getCount method there. As getCount() is in a different class altogether, you just want to alter the maximum 25 there and then set the list view to that adapter. You may want to take a look here for more uses of ArrayAdapters.

If statements forcing program to crash

I have asked a few questions before, however I have decided to go back to basics.
I am creating a maths android application, each page has three TextFields that the user can input a number into, I have parsed these TextFields into a double. However, say a user inputs a number into 2 of the TextFields, I would like the program to tell me the value of the third Text field.
At the moment I can get the program to run one calculation, however when I add the if statements in for the remaining two TextFields, the program crashes when I click calculate.
The Key behind the program is to discover which TextField is empty, so that the program can do the correct calculation.
I am currently trying .getText().toString().isEmpty(), but this is not working, I have also tried .length() == 0, however this is not working either.
When I run the emulator and click the calculator button, the program crashes when I add more variables in, however it is fine with just one calculation.
Can anyone help me figure out why the If statements are not working, and if possible can anyone offer a solution?
Sorry if my formatting is off, I'm relatively new to Java, below is the Java
Button calc1 = (Button) findViewById(R.id.current_calculate);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText) findViewById(R.id.number_input_2);
EditText Time1 = (EditText) findViewById(R.id.number_input_3);
EditText current1 = (EditText) findViewById(R.id.number_input_1);
TextView Distances_answer = (TextView) findViewById(R.id.Distances_answer);
double charge = Double
.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
double current = Double.parseDouble(current1.getText()
.toString());
// Time is a class in Java
if (current1.getText().toString().isEmpty()) {
Distances_answer.setText("" + charge * time);
} else if (Time1.length() == 0) {
Distances_answer.setText(" " + charge / current);
}
}
});
I have also supplied the XML in case it is needed
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background1"
android:gravity="fill"
android:orientation="vertical"
android:textColor="#ffffff"
tools:context=".CurrentPage" >
<TextView
android:id="#+id/Current_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="33dp"
android:text="Welcome to the Current Calculation page.
Hint - Use the check boxes to find the values that are missing "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/Equation_letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/current_calculate"
android:layout_marginTop="37dp"
android:layout_toLeftOf="#+id/current_calculate"
android:text=" HELLO "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/Distances_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Equation_letter"
android:layout_alignBottom="#+id/Equation_letter"
android:layout_alignLeft="#+id/current_calculate"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<TextView
android:id="#+id/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Q"
android:layout_alignRight="#+id/Q"
android:layout_below="#+id/Q"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="t ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<CheckBox
android:id="#+id/custom3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/t"
android:layout_alignLeft="#+id/custom2"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<CheckBox
android:id="#+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/number_input_3"
android:layout_alignLeft="#+id/custom3"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Current_hint"
android:layout_alignRight="#+id/Current_hint"
android:layout_below="#+id/Equation_letter"
android:layout_marginTop="25dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/CurrentmainHelp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.31"
android:text="HELP! This equation can be used to calculate the current of an object in amps. The correct equation to discover amps is I=Q/t. Where I = current, Q = charge flowing past a point in the circuit, and t = time taken for the charge to flow. Please note, Q is measure in coulombs and t is measured in seconds, with I being measured in amperes.
Still need more help? "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/Yes_Please"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="36dp"
android:layout_weight="1.31"
android:onClick="CurrentHelp"
android:text="Yes Please"
android:textColor="#ffffff" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/current_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/Calculate_Current"
android:textColor="#ffffff" />
<EditText
android:id="#+id/number_input_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="#+id/t"
android:layout_alignLeft="#+id/number_input_1"
android:layout_alignRight="#+id/number_input_1"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/number_input_3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/t"
android:layout_alignLeft="#+id/number_input_2"
android:layout_alignRight="#+id/number_input_2"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" />
<CheckBox
android:id="#+id/custom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/number_input_1"
android:layout_marginLeft="45dp"
android:layout_toRightOf="#+id/number_input_1"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<EditText
android:id="#+id/number_input_1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_above="#+id/Q"
android:layout_alignRight="#+id/current_calculate"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/Q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Equation_letter"
android:layout_below="#+id/I"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="Q ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/I"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Q"
android:layout_alignRight="#+id/Q"
android:layout_below="#+id/Current_hint"
android:layout_marginTop="65dp"
android:gravity="right|top"
android:text="I ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
I believe the application crashes because you are parsing the contents of your EditText to doubles even though they may be empty. The Double.parseDouble function will throw a NumberFormatException if it cannot parse a double out of the given String. To fix this, perform a check for empty string before trying to parse, or catch the exception.
For example:
double charge = 0.0; // some default value
if (!Charge1.getText().toString().isEmpty()) {
charge = Double.parseDouble(Charge1.getText().toString());
}
double time = 0.0; // some default value
if (!Time1.getText().toString().isEmpty()) {
time = Double.parseDouble(Time1.getText().toString());
}
double current = 0.0; // some default value
if (!current1.getText().toString().isEmpty()) {
current = Double.parseDouble(current1.getText().toString());
}

Categories

Resources