okay, i cannot post images yet :/
Basically, i have an application with three edittext fields, three check boxes, a calculation button and a text field to output the answer. i want the program to scan to see which value is missing, so that from this it know which calculation it should do. Another method would be for the user to check one of the checkbox's and then the program would only run the calculation for the corresponding checkbox.
for instance, if the user inputs Q = 10, T = 2, and checks the checkbox for I, the program would then do the math and display I = 20 ?
this make much sense yet
i spose in psuedo code it would look like
" if checkbox one is checked and values for Q and T have been entered, then calculate value for I and display in answers field" however i will be adding some error checks and validation methods in there.
if you need the xml file and java file then please ask, im just looking for a method at the moment as to how i could do it, i wouldnt expect you all to waste your free time writing a whole program for me.
cheers guys and girls
<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" >
<!--
The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->
<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->
<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="false"
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" />
okay, that was the xml, and this is the java so far
package com.kieran.whetherfieldphysicscalculator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
// i=q/t this will be the first calculation on the list
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
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);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}
public void CurrentHelp(View view) {
// Do something in response to button
Intent intent = new Intent(this, CurrentHelp.class);
startActivity(intent);
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am asking if there is an easier way to formate the program to find the values, rather than have to code each individual segment?
cheers
In your case I guess that you want the user to check only one checkbox and only answer corresponding to that checkbox should be displayed to the user.
so for that you would need to set OnClickListener on checkbox so that the corresponding operation is selected.
Read here on how to set listener on checkbox and use it. Paticularly look for the addListenerOnChkIos() method as it has the code you want.
Also set another OnClickListener on the calculate button and in that check which checkbox is checked depending on which you will perform the operation and update the textview.
Related
I know this question has been asked several times, and with different solutions, but I have tried all I could find here, and none worked. As the title suggest, in Android Studio, my EditText widget's field isn't showing any type of text, even what it has by default (like the hint and default text). The text is there if I print it out , but not displaying. It is for a simple login page.
This is the xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:text="Name : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:id="#+id/atcoNameTag" />
<TextView
android:text="Password : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/atcoNameTag"
android:layout_marginTop="30dp"
android:id="#+id/passTag"
android:layout_alignStart="#+id/atcoNameTag"/>
<TextView
android:text="Role : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/roleNameTag"
android:layout_below="#+id/passTag"
android:layout_alignStart="#+id/atcoNameTag"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:layout_above="#+id/passTag"
android:layout_centerHorizontal="true"
android:id="#+id/insertName"
android:layout_alignTop="#+id/atcoNameTag"
android:layout_toEndOf="#+id/atcoNameTag"
android:layout_alignStart="#+id/insertPass"
android:hint="username"
android:cursorVisible="true"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignBottom="#+id/passTag"
android:layout_toEndOf="#+id/passTag"
android:id="#+id/insertPass"
android:layout_alignTop="#+id/passTag"
android:hint="password"
android:cursorVisible="true"/>
<Spinner
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_alignTop="#+id/roleNameTag"
android:layout_toEndOf="#+id/roleNameTag"
android:layout_alignStart="#+id/insertPass"/>
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/loginButton"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
</RelativeLayout>
And this is the java file :
package mecals.mecalsapp;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
Button nameBtn = (Button) findViewById(R.id.loginButton);
nameBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText nameEntry = (EditText) findViewById(R.id.insertName);
nameEntry.setTextColor(Color.BLACK);
String name = nameEntry.getText().toString();
//TODO username checking, username not showing , need devC Team's work
if (name.length() > 0) {
Toast toastYes = Toast.makeText(getApplicationContext(), "Hi there, " + name + "!", Toast.LENGTH_SHORT);
toastYes.show();
Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
startActivity(intent);
} else {
Toast toastNo = Toast.makeText(getApplicationContext(), "Please insert name !", Toast.LENGTH_SHORT);
toastNo.show();
}
EditText passwordEntry = (EditText) findViewById(R.id.insertPass);
passwordEntry.setTextColor(Color.BLACK);
String pass = passwordEntry.getText().toString();
//TODO password checking , need devC Team's work
}
});
}
}
And these are the solutions I have tried , to no avail :
Difference between content_main.xml and activity_main.xml?
Android edittext typed text not showing
Android EditText typed value not showing
Text not displayed in Android editText when changing android:hint to android:text
EditText in Android doesn't show text when typing while using the on-screen keyboard
Edit Text hints not appearing on later version SDK
edittext not showing the typed text in android
In short, I have
-set the text colour to Black , with nameEntry.setTextColor(Color.BLACK);
-I have added android:windowSoftInputMode="adjustPan" in the manifest
-I have NOT used something like android:gravity , just android:layout
-the cursor is set to visible
-I even checked the height of the widget
-I checked the size of the text with android:ems to be 10
-and the input type is set to text for the first field and textPassword for the second
Help !
PS : before anyone asks , my Android Studio version is 2.2.3 , and the API I am using is min 21.
The problem is that the height of the EditText field is much to small to display the text. This is present because both the top and the bottom of the EditText field are set according to the TextView left of the EditText. But since the height of the TextView is smaller than the normal height of the EditText you force it to be very narrow (and so there is not enough space for the Text). If you remove this line
android:layout_alignTop="#+id/atcoNameTag"
you can see that the text gets displayed normally.
There are actually several ways to fix this:
you could increase the text size of the TextViews
you could add enough padding on top and on the bottom of the TextViews
you could decrease the text size of the EditText
you could decouple the height of the EditText from the TextView
It is probably a good idea to go with the last solution and put the Textview and EditText fields that belong together into a separate LinearLayout and remove the align calls like this:
<?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">
<LinearLayout
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:weightSum="100"
>
<TextView
android:layout_weight="30"
android:text="Name : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/atcoNameTag" />
<EditText
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:id="#+id/insertName"
android:hint="username"
android:cursorVisible="true"/>
</LinearLayout>
<LinearLayout
android:id="#+id/second"
android:layout_below="#+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:weightSum="100"
>
<TextView
android:layout_weight="30"
android:text="Password : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/passTag"/>
<EditText
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="#+id/insertPass"
android:hint="password"
android:cursorVisible="true"/>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:weightSum="100"
android:layout_marginTop="10dp"
>
<TextView
android:layout_weight="30"
android:text="Role : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/roleNameTag"/>
<Spinner
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"/>
</LinearLayout>
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/loginButton"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
</RelativeLayout>
I'm trying to use a Switch widget inside of a RecyclerView to set the particular component as active or not. How can I detect the switch toggling from the RecyclerView? My current OnItemClickListener intercepts the click on the Switch widget also.
Current Click Listener Code:
mRecyclerView.addOnItemTouchListener(
new RecyclerItemListener(getApplicationContext(), mRecyclerView,
new RecyclerItemListener.RecyclerTouchListener() {
public void onClickItem(View v, int position) {
Toast.makeText(MainActivity.this, "Clicked, position " + position + ". Name: " + GlobalData.totalAlarms.get(position).getAlarmName(), Toast.LENGTH_LONG).show();
}
public void onLongClickItem(View v, int position) {
}
}));
}
Layout for RecyclerView, including Switch:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/recycler_mainactivityName"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="16sp"
android:layout_marginTop="15dp"
android:paddingLeft="20dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/recycler_mainactivityTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/recycler_mainactivityDays"
android:paddingLeft="20dp"
android:textSize="12sp"
android:layout_marginBottom="5dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/recycler_mainactivityTime"
android:paddingTop="2dp"
android:textSize="12sp"
android:paddingLeft="20dp"
android:layout_below="#+id/recycler_mainactivityName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/recycler_mainactivityActive"
android:paddingRight="20dp"
android:layout_alignBaseline="#+id/recycler_mainactivityTime"
android:layout_alignBottom="#+id/recycler_mainactivityTime"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hapticFeedbackEnabled="true" />
</RelativeLayout>
When I remove the addOnItemTouchListener, the switches do toggle on click rather than being forced to swype.. Beyond that, how can I detect the touch? The addOnItemClickListener seems to intercept all clicks (unless I do a long click, then the switches toggle).
Add a OnCheckChangeListener of the switch inside your recyclerView adapter to get the state of the switch
I'm still learning how to make android apps and I'm practicing using the different controls, like EditText, radio buttons, dialog boxes, etc.
I'm trying to have the user enter some info into the app and then display that info into a dialog box when they press the "save" button. Whenever I press save, the app crashes.
I know it has something to do with my onClick method where I set all the input to strings so they can be written to the dialog box. It seems to happening when I try to convert the data from the spinner, radio button, and toggle button to string. When I comment those out, the dialog box works fine with just the edit text data.
Can anyone see what's wrong?
Java code
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;
import android.widget.Spinner;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.content.DialogInterface;
import android.app.AlertDialog;
import java.text.DecimalFormat;
import java.util.Calendar;
public class ActMain extends Activity {
//----------------------------------------------------------------
// Variables
//----------------------------------------------------------------
// Declare variables
EditText etAppName;
Spinner spCategory;
RadioGroup rgRating;
RadioButton rbGood;
RadioButton rbFair;
RadioButton rbBad;
ToggleButton tbGooglePlay;
EditText etPrice;
Button btnSave;
//----------------------------------------------------------------
// Activity overrides
//----------------------------------------------------------------
//----------------------------------------------------------------
// onCreate
//----------------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("### DEBUG ### onCreate started at " + currentTime() + ".");
super.onCreate(savedInstanceState);
setContentView(R.layout.laymain);
// Define edit text controls
etAppName = (EditText) findViewById(R.id.etAppName);
etPrice = (EditText) findViewById(R.id.etPrice);
// Define spinner adapter
String[] categories = {"Business", "Comics", "Education", "Finance", "Games", "Music", "News", "Tools", "Travel", "Weather"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Define spinner control
spCategory = (Spinner) findViewById(R.id.spCategory);
spCategory.setAdapter(adapter);
// Define spinner event
spCategory.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
System.out.println("Spinner: \"" +
parent.getItemAtPosition(position) +
"\" selected.");
}
public void onNothingSelected(AdapterView<?> parent)
{
System.out.println("Spinner: no item selected.");
}
});
// Define save button click event
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Mobile App Reviewer Message");
String appName = etAppName.getText().toString();
String price = etPrice.getText().toString();
String category = spCategory.getSelectedItem().toString();
int rbCheckedId = rgRating.getCheckedRadioButtonId();
RadioButton rbChecked = (RadioButton) findViewById(rbCheckedId);
String rating = rbChecked.getText().toString();
String onGooglePlay;
if(tbGooglePlay.isChecked())
onGooglePlay = "NO";
else
onGooglePlay = "YES";
builder.setMessage("Application: " + appName +
"\nCategory: " + category+
"\nRating: " + rating +
"\nOn Google Play? " + onGooglePlay +
"\nPrice: $" + price +
"\nSave this data?");
builder.show();
}
});
}
XML Code
<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:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Title rows - Text View
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<TextView
android:text="Software News"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_margin="4dp"/>
<TextView
android:text="Mobile App Reviewer"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_margin="4dp"/>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App info
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App name - Edit text
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Application: "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<EditText
android:id="#+id/etAppName"
android:textSize="16sp"
android:background="#33CCCC"
android:gravity="left"
android:layout_gravity="left"
android:layout_width="100dp"
android:layout_height="25dp"
android:padding="0dp"
android:layout_margin="4dp" />
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App Category - Spinner
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="6dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category: "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<Spinner
android:id="#+id/spCategory"
android:layout_width="160dp"
android:layout_height="wrap_content" />
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App Rating - Radio Buttons
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="6dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rating: "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<RadioGroup
android:id="#+id/rgRating"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rbGood"
android:text="Good"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/rbFair"
android:text="Fair"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/rbBad"
android:text="Bad"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Google Play? - Toggle Button
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="6dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On Google Play? "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<ToggleButton
android:id="#+id/tbGooglePlay"
android:textOn="NO"
android:textOff="YES"
android:textSize="12sp"
android:textStyle="bold"
android:layout_width="75dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Google Play? - Toggle Button
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="6dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price: $ "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<EditText
android:id="#+id/etPrice"
android:textSize="16sp"
android:background="#33CCCC"
android:inputType="numberDecimal"
android:gravity="left"
android:layout_gravity="left"
android:layout_width="100dp"
android:layout_height="25dp"
android:padding="0dp"
android:layout_margin="4dp" />
</LinearLayout>
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#A0CFEC"
android:gravity="center"
android:padding="4dp"
android:text="Save"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
rgRating is not assigned to anything. So you will get a nullPointerException when calling getCheckedRadioButtonId.
This would be obvious if you checked the stack trace or debugged the application.
To fix it, assign an ID to rgRating in the XML file and do a
rgRating = findViewById(...)
...before you try to call getCheckedRadioButtonId()
Try this:
AlertDialog dialog = builder.create();
dialog.show();
change this
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
to
AlertDialog.Builder builder = new AlertDialog.Builder(ActMain.this);
So after #DKIT pointed out my error with rgRating, I noticed that I had done the same thing with tbGooglePlay.
In the end, adding these lines of code to the Java file fixed my issue:
rgRating = (RadioGroup) findViewById(R.id.rgRating);
tbGooglePlay = (ToggleButton) findViewById(R.id.tbGooglePlay);
Im quite new to Android Dev, and well Programming also,
Im doing a Project for College by doing a Macro-Nutrient calculator which will give a user their Calorie needs for the day depending on their goal,
I have it almost finished (I will implement more features soon) Just want to get this basic feature done,
It all seems to work fine, but the textView named "displayValue" is not outputting the result,
Can someone please give me some advice?
ANDROID XML-LAYOUT
<?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"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="62dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="#drawable/bmr_logo" />
<EditText
android:id="#+id/weightInput"
android:layout_width="130dp"
android:layout_height="30dp"
android:layout_marginTop="175dp"
android:layout_marginLeft="75dp"
android:ems="10"
android:hint="#string/defaultText"
android:inputType="numberDecimal"
android:textSize="13sp" />
<Button
android:id="#+id/maintButton"
android:layout_width="75dp"
android:layout_height="30dp"
android:layout_marginTop="265dp"
android:layout_marginLeft="115dp"
android:text="Calculate"
android:textSize="12sp" />
<Button
android:id="#+id/bulkButton"
android:layout_width="75dp"
android:layout_height="30dp"
android:layout_marginTop="265dp"
android:layout_marginLeft="225dp"
android:text="Calculate"
android:textSize="12sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="85dp"
android:layout_height="15dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="220dp"
android:text="Gain Weight" />
<Button
android:id="#+id/cutButton"
android:layout_width="75dp"
android:layout_height="30dp"
android:layout_marginTop="265dp"
android:layout_marginLeft="15dp"
android:text="Calculate"
android:textSize="12sp" />
<EditText
android:id="#+id/heightInput"
android:layout_width="130dp"
android:layout_height="30dp"
android:layout_marginTop="135dp"
android:layout_marginLeft="75dp"
android:ems="10"
android:hint="#string/defaultText"
android:inputType="numberDecimal"
android:textSize="13sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="45dp"
android:layout_height="15dp"
android:layout_marginTop="180dp"
android:layout_marginLeft="15dp"
android:text="Weight:" />
<TextView
android:id="#+id/displayValue"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_marginTop="310dp"
android:layout_marginLeft="115dp"
android:textSize="12pt" />
<TextView
android:id="#+id/textView3"
android:layout_width="105dp"
android:layout_height="15dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="105dp"
android:text="Maintain Weight" />
<TextView
android:id="#+id/textView1"
android:layout_width="75dp"
android:layout_height="15dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="15dp"
android:text="Cut Weight" />
<EditText
android:id="#+id/ageInput"
android:layout_width="130dp"
android:layout_height="30dp"
android:layout_marginTop="90dp"
android:layout_marginLeft="75dp"
android:ems="10"
android:hint="#string/defaultText"
android:inputType="number"
android:textSize="13sp" />
<TextView
android:id="#+id/textHeight"
android:layout_width="45dp"
android:layout_height="15dp"
android:layout_marginTop="140dp"
android:layout_marginLeft="15dp"
android:text="Height:" />
<TextView
android:id="#+id/textAge"
android:layout_width="25dp"
android:layout_height="15dp"
android:layout_below="#+id/imageView1"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:text="Age:" />
</RelativeLayout>
JAVA CODE
package app.college.fitnessapp;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Macros extends Activity implements OnClickListener {
EditText ageInput;
EditText heightInput;
EditText weightInput;
Button cutButton;
Button maintButton;
Button bulkButton;
TextView displayValue;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.macro_layout);
// Find elements from the layout and Define them
ageInput = (EditText) findViewById(R.id.ageInput);
heightInput = (EditText) findViewById(R.id.heightInput);
weightInput = (EditText) findViewById(R.id.weightInput);
cutButton = (Button) findViewById(R.id.cutButton);
maintButton = (Button) findViewById(R.id.maintButton);
bulkButton = (Button) findViewById(R.id.bulkButton);
displayValue = (TextView) findViewById(R.id.displayValue);
//Set button listeners
cutButton.setOnClickListener(this);
maintButton.setOnClickListener(this);
bulkButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int ageNum = 0;
int weightNum = 0;
int heightNum = 0;
int result = 0;
int calculate = 0;
int percentAdd = 0;
// Test to see if fields are empty
if (TextUtils.isEmpty(ageInput.getText().toString())
|| TextUtils.isEmpty(heightInput.getText().toString())
|| TextUtils.isEmpty(weightInput.getText().toString())) {
return;
}
// This is to read the Edit Text fields from the layout and fill the Variables with Numbers
ageNum = Integer.parseInt(ageInput.getText().toString());
weightNum = Integer.parseInt(weightInput.getText().toString());
heightNum = Integer.parseInt(heightInput.getText().toString());
//Defines which button has been clicked and will calculate accordingly
switch (v.getId()) {
case R.id.cutButton:
calculate = (10*weightNum) + (6*heightNum) - (5*ageNum) + 5;
percentAdd = (calculate*1);
result = calculate + percentAdd;
break;
case R.id.maintButton:
calculate = (10*weightNum) + (6*heightNum) - (5*ageNum) + 5;
percentAdd = (calculate*1);
result = calculate + percentAdd;
break;
case R.id.bulkButton:
calculate = (10*weightNum) + (6*heightNum) - (5*ageNum) + 5;
percentAdd = (calculate*1);
result = calculate + percentAdd;
break;
default:
break;
}
//Displays the output
displayValue.setText(String.valueOf(result));
}
}
I have a feeling it is the last line with the displayValue output,
This Problem is Solved...
If i make my variables such as ageNum, weightNum etc into floats and parse the string into a Float i then get this error "The method setText(CharSequence) in the type TextView is not applicable for the arguments (float)"
But if I define them as Ints and parse the strings to Int i then get no error messgage but no output,
I would prefer to output as a float though, just incase a user would enter their weight at 78.5 or something,
This is my only problem now,
Im not getting any outputs :( in the displayValue Textview
Any suggestions please?
Thanks in advance!
then get this error "The method setText(CharSequence) in the type
TextView is not applicable for the arguments (float)"
You have to use the setText() method that accepts an object which implements the CharSequence interface (i.e like the String class). You can use the static method String.valueOf(float value).
displayValue.setText(String.valueOf(result));
The setText(...) takes as an argument a java.lang.CharSequence (e.g. a java.lang.String). You are passing an int.
Check out the String class for converting literals like int to String.
I am beginner to both Java and Android development, I am sure that I am doing this wrong; it’s just the way I've figured it out so far. That said, I have an app that will show something like a golf scorecard. There is a section (major row) for each of 4 players. Each player row comprises 3 sub-rows; one for Par (18 values in the row), one for Hole Handicap (18 values), one for Score (18 values). I want the user to click on a major row and have another activity appear based on the player named in the clicked row.
So, I created an XML layout with a single ListView. I created an XML layout for the page header with a GridView defining 12 columns each with a TextView for column headers, and I created an XML layout for the major row with a GridView comprising TextViews for 54 items (3 sub-rows of 18 items). I also added an onClick listener to the GridLayout declaration in the row def XML. Throw in a custom arrayAdapter to handle the 4 players and I get my scorecard on the screen.
Everything displays okay, and when I click a major row, I get to a debug breakpoint in the clickListener and I can fire off an Intent to kick off the activity showing player detail. But, I don’t know how to determine which player (major row) was clicked.
String item = ((TextView)view).getText().toString();
in the click handler crashes the app, I suppose because the clickListener was established on a GridLayout or some such thing.
So, what’s wrong with the setup and how can I get what I need to go forward?
Thanks
Here is relevant code. Sorry if it's a lot...
activity_display_scorecard.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Here is activity_sisplay_scorecard_row.xml (I edited out columns 4-12)
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnWidth="90dp"
android:columnCount="12"
android:id="#+id/SCRow"
android:onClick="displayScore"
>
<TextView
android:text=""
android:layout_column="0"
android:textSize="15sp"
android:layout_gravity="left"
/>
<TextView
android:text="Par"
android:layout_column="1"
android:textSize="15sp"
android:layout_gravity="right"
/>
<TextView
android:id="#+id/Par_Hole_1"
android:text="V_1"
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<!-- A bunch more for this row -->
<TextView
android:text=""
android:layout_column="0"
android:textSize="15sp"
android:layout_gravity="left"
/>
<TextView
android:text="HCP"
android:layout_column="1"
android:textSize="15sp"
android:layout_gravity="right"
/>
<TextView
android:id="#+id/HCP_Hole_1"
android:text="V_1"
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<!-- A bunch more for this row -->
<TextView
android:text=""
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<TextView
android:text="Name"
android:id="#+id/Row_Name"
android:layout_column="0"
android:textSize="15sp"
android:layout_gravity="left"
/>
<TextView
android:text="Bet"
android:layout_column="1"
android:textSize="15sp"
android:layout_gravity="right"
/>
<TextView
android:id="#+id/Bet_Hole_1"
android:text="B1"
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<!-- A bunch more for this row -->
<TextView
android:text=""
android:layout_column="0"
android:textSize="15sp"
android:layout_gravity="left"
/>
<TextView
android:text="Score"
android:layout_column="1"
android:textSize="15sp"
android:layout_gravity="right"
/>
<TextView
android:id="#+id/Score_Hole_1"
android:text="V_1"
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<!-- A bunch more for this row -->
</GridLayout>
and here is the activity DisplayScorecard
public class DisplayScorecard extends Activity {
private ListView listView1;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_scorecard);
CardRow scoreCard_data[] = new CardRow[]
{
new CardRow(0),
new CardRow(1),
new CardRow(2),
new CardRow(3)
};
CardRowAdapter adapter = new CardRowAdapter(this,
R.layout.activity_display_scorecard_row, scoreCard_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.activity_display_scorecard_header, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
/** Called when the user Selects a scorecard row */
public void displayScore(View view) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getApplicationContext(), "ABC" + item, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, DisplayScore.class);
startActivity(intent);
}
}