How to use spinners in android? - java

I am new to android. I am trying to make simple unit converter. The layout contains EditText for input, two spinners (to and from), an ImageButton and EditText to display answer.
I want to add values to spinners and when these values are selected i want to perform action.
My xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".BitsBytes" >
<Spinner
android:id="#+id/SpinnerFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignRight="#+id/inputvalue"
android:layout_below="#+id/textView2"
android:entries="#array/bitsbytesfrom"
android:prompt="#string/from_prompt" />
<Spinner
android:id="#+id/SpinnerTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/SpinnerFrom"
android:layout_below="#+id/textView3"
android:entries="#array/bitsbytesto"
android:prompt="#string/to_prompt" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/SpinnerFrom"
android:layout_below="#+id/SpinnerFrom"
android:layout_marginTop="16dp"
android:text="To:"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Value: "
android:textSize="20dp" />
<EditText
android:id="#+id/inputvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#+id/textView6"
android:layout_toRightOf="#+id/textView6"
android:ems="10"
android:hint="Enter value"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/inputvalue"
android:layout_marginTop="16dp"
android:text="From:"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Bits/Bytes Conversion"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp" />
<ImageButton
android:id="#+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/SpinnerTo"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="#drawable/btnconvert"
android:clickable="true"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/answer"
android:layout_centerHorizontal="true"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
My java code:
package com.easy.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class BitsBytes extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitsbytes);
ImageButton ib = (ImageButton) findViewById(R.id.convertButton);
ib.setOnClickListener(new View.OnClickListener() {
EditText etValue = (EditText) findViewById(R.id.inputvalue);
Spinner spBitsBytesFrom = (Spinner) findViewById(R.id.SpinnerFrom);
Spinner spBitsBytesTo = (Spinner) findViewById(R.id.SpinnerTo);
String txtFromSpinner = spBitsBytesFrom.getSelectedItem().toString();
String txtToSpinner = spBitsBytesTo.getSelectedItem().toString();
TextView Answer = (TextView) findViewById(R.id.answer);
public void onClick(View v)
{
if (txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
});
}
}
I am filling the spinners using string array. I am not able to figure out whats wrong in my code because there is no error in logcat.

Change this,
(txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
to
if((txtFromSpinner.getSelectedItem().toString().equals("Bits")) && (txtToSpinner.getSelectedItem().toString().equals("Bytes")))
this will get the text from the item selected in the spinner.

Related

Android app keeps stopping while building using android studio

When I'm trying to transfer data between activities, I can't get my message and the app keeps crashing,
It show me 'app keeps stopping'
Code in MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Intent outIntent;
EditText edtPhone;
EditText edtMessage;
Button btnNext;
String tempText="";
public static final String PHONE = "PHONE";
public static final String MESSAGE = "MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
outIntent = new Intent(MainActivity.this, ActivityTwo.class);
edtPhone = (EditText) findViewById(R.id.edtPhone);
edtMessage = (EditText) findViewById(R.id.edtMessage);
tempText = edtPhone.getText().toString();
outIntent.putExtra(PHONE,tempText);
tempText = edtMessage.getText().toString();
outIntent.putExtra(MESSAGE,tempText);
startActivity(outIntent);
}
});
}
public void closeMethod(View view) {
finish();
}
}
ActivityTwo.java
package com.example.fir;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
Intent incomingIntent;
TextView txtPhone;
TextView txtMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
incomingIntent = getIntent();
txtPhone = (TextView) findViewById(R.id.txtPhone);
txtMessage = (TextView) findViewById(R.id.txtMessage);
txtPhone.setText(incomingIntent.getStringExtra(MainActivity.PHONE));
txtMessage.setText(incomingIntent.getStringExtra(MainActivity.MESSAGE));
}
}
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
Activitytwo.xml
////
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
The issue is when I don't put any value into the firstEditText or secondEditText or both of them and click on any button then the app crashes and a pop up shows "myapp keeps stopping".
I cannot get the text and am unsure of why the app keeps crashing.
you have defined your Edit texts in the first activity with id's named
edtPhone
edtMessage
but there was no edtPhone or edtMessage in your xml.
this issue also repeats in your second activity
you have defined txtPhone and txtMessage
but again there are no such ids in your second activity XML.
keep in mind that these might not be the whole problem as you didn't post any log for the errors. but if there were anything else, ask and we'll help you

Android app is lagging when trying to use any input

For some reason I am experiencing massive lag when I try to use the input fields and buttons in my app. The lag is present both in the emulator that comes with Android Studio and my own One Plus Two. I really don't have much code at all so I find this very strange. The app is also using about 150Mb of RAM despite the images used are below 1Mb in total size. The app sometimes even stop responding. I am new to Android development but familiar with programming.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#drawable/bg">
<ImageView
android:contentDescription="#string/logoCS"
android:layout_width="125dp"
android:layout_height="125dp"
android:id="#+id/logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:src="#drawable/logo" />
<Button
android:layout_width="145dp"
android:layout_height="50dp"
android:text="#string/sign_in_with"
android:id="#+id/facebookButton"
android:layout_below="#+id/logo"
android:layout_alignStart="#+id/emailField"
android:layout_marginTop="20dp"
android:textAlignment="viewStart"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:textSize="12sp"
android:background="#color/signature_gray" />
<Button
android:layout_width="145dp"
android:layout_height="50dp"
android:id="#+id/googleButton"
android:text="#string/sign_in_with"
android:src="#drawable/google"
android:layout_below="#+id/logo"
android:layout_alignTop="#+id/facebookButton"
android:adjustViewBounds ="true"
android:layout_alignEnd="#+id/emailField"
android:textAlignment="viewStart"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:textSize="12sp"
android:background="#color/signature_gray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/or"
android:textColor="#color/signature_gray"
android:id="#+id/orTextView"
android:layout_marginTop="20dp"
android:layout_below="#+id/googleButton"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="320dp"
android:layout_height="50dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/emailField"
android:layout_below="#id/orTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#color/white"
android:hint="#string/email"
android:textColorHint="#color/signature_gray"
android:textColor="#color/signature_gray"
android:paddingEnd="10dp"
android:paddingStart="50dp"
android:maxLines="1" />
<EditText
android:layout_width="320dp"
android:layout_height="50dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordField"
android:layout_marginTop="10dp"
android:layout_below="#+id/emailField"
android:layout_alignStart="#+id/emailField"
android:background="#color/white"
android:hint="#string/password"
android:textColorHint="#color/signature_gray"
android:textColor="#color/signature_gray"
android:paddingEnd="10dp"
android:paddingStart="50dp"
android:maxLines="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/forgot_password"
android:textColor="#color/white"
android:id="#+id/forgotPasswordText"
android:layout_below="#+id/passwordField"
android:layout_alignEnd="#+id/passwordField"
android:layout_marginTop="5dp"
android:onClick="forgotPassword" />
<TextView
android:text="#string/sing_in_error"
android:layout_width="320dp"
android:id="#+id/errorText"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_below="#+id/forgotPasswordText"
android:layout_alignEnd="#+id/forgotPasswordText"
android:textColor="#android:color/holo_red_light"
android:visibility="gone" />
<ImageView
android:contentDescription="#string/userIconCS"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/userIcon"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_above="#+id/passwordField"
android:layout_alignStart="#+id/emailField"
android:background="#drawable/user" />
<ImageView
android:contentDescription="#string/pwIconCS"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/passwordIcon"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_above="#+id/signInButton"
android:layout_alignStart="#+id/passwordField"
android:background="#drawable/password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/not_a_member"
android:id="#+id/notMemberField"
android:textColor="#color/white"
android:layout_marginTop="20dp"
android:layout_below="#+id/signInButton"
android:layout_alignEnd="#+id/orTextView"
android:layout_marginEnd="6dp" />
<TextView
android:text="#string/register_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/registerText"
android:textColor="#color/white"
android:onClick="register"
android:layout_alignBaseline="#+id/notMemberField"
android:layout_alignBottom="#+id/notMemberField"
android:layout_marginStart="-3dp"
android:layout_toEndOf="#+id/notMemberField" />
<Button
android:layout_width="320dp"
android:layout_height="50dp"
android:id="#+id/signInButton"
android:layout_marginTop="65dp"
android:background="#color/signature"
android:text="#string/sing_in"
android:textColor="#color/white"
android:onClick="signIn"
android:layout_below="#+id/passwordField"
android:layout_alignEnd="#+id/passwordField" />
<ImageView
android:contentDescription="#string/googleLogoCS"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/googleImage"
android:elevation="2dp"
android:background="#drawable/google"
android:layout_alignTop="#+id/googleButton"
android:layout_alignEnd="#+id/googleButton"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp" />
<ImageView
android:contentDescription="#string/facebookLogoCS"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/facebookImage"
android:elevation="2dp"
android:background="#drawable/facebook"
android:layout_alignTop="#+id/facebookButton"
android:layout_alignEnd="#+id/facebookButton"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp" />
</RelativeLayout>
MainActivity.java
import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button facebookButton;
private Button googleButton;
private EditText emailField;
private EditText passwordField;
private Button loginButton;
private TextView registerText;
private TextView errorText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebookButton = (Button) findViewById(R.id.facebookButton);
googleButton = (Button) findViewById(R.id.googleButton);
emailField = (EditText) findViewById(R.id.emailField);
passwordField = (EditText) findViewById(R.id.passwordField);
loginButton = (Button) findViewById(R.id.signInButton);
registerText = (TextView) findViewById(R.id.registerText);
errorText = (TextView) findViewById(R.id.errorText);
registerText.setPaintFlags(registerText.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
}

content of spinner+edittext to another page(textview)(android)

so im really new to android programming(and java) and im trying to do basic stuff.I followed tutorial on the internet but I cant manage to do what I want.I have spinners with edittext, and a button.When I click the button, I want the selected items in the spinners and numbers in the edittext to go in another textview(load a new page with a big textview).So far I did this:
(Mainactivity.java)
package com.example.spinnertest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
Spinner s1,s2,s3,s4;
TextView txt2;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner) findViewById(R.id.spinner1);
s2= (Spinner) findViewById(R.id.spinner2);
s3 = (Spinner) findViewById(R.id.spinner3);
txt2 = (TextView) findViewById(R.id.textView2);
/////////CODE PART FOR TESTING ONLY,NO LINK WITH MY QUESTION/////
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String val = s1.getSelectedItem().toString();
txt2.setText(val);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
s3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String val = s3.getSelectedItem().toString();
txt2.setText(val);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String val = s2.getSelectedItem().toString();
txt2.setText(val);
}
//////////////////END OF TESTING CODE PART///////////
// OnClickListener of Button
button.setOnClickListener(new OnClickListener() { ///theres few errors here?
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Getting values of text and spinners
//selectedDay = daySpinner.getSelectedItem().toString(); //this code doesnt fit the way I define my spinners;see below
//selectedMonth = monthSpinner.getSelectedItem().toString(); //this code doesnt fit the way I define my spinners;see below
//text = editText.getText().toString(); //this code doesnt fit the way I define my spinners;see below
// Creating an Intent to open new Activity(Screen) and sending
// the details
Intent intent = new Intent(MainActivity.this, Affichage.class);
// intent.putExtra("day", selectedDay);//this code doesnt fit the way I define my spinners;see below
//intent.putExtra("month", selectedMonth);//this code doesnt fit the way I define my spinners;see below
//intent.putExtra("text", text); //this code doesnt fit the way I define my spinners;see below
startActivity(intent);
}
});
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
};}}
seconde java file activity:
package com.example.spinnertest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Spinner;
import android.widget.TextView;
public class Display extends ActionBarActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Spinner s1;
setContentView(R.layout.activity_display);
textView = (TextView) findViewById(R.id.disp);
s1 = (Spinner) findViewById(R.id.spinner1);
Intent intent = getIntent();
if(intent!=null)
{
String val = s1.getSelectedItem().toString();
textView.setText(val);
//String month = intent.getStringExtra("month"); //example from the internet;doesnt fit my code
//String text = intent.getStringExtra("text"); //example from the internet,doesnt fit my code
}
else
{
textView.setText("Intent is null");
}
}}
layout of the second page with the textview:
<?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:id="#+id/afficher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
layout were my spinners and stuff are defined:
<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.spinnertest.MainActivity" >
<Spinner
android:id="#+id/spinner6"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_alignLeft="#+id/spinner5"
android:layout_alignTop="#+id/editText3"
android:entries="#array/Type" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignParentLeft="true"
android:text="Description de la recette de fabrication"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentBottom="true"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="Fabriquer" />
<Spinner
android:id="#+id/spinner3"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/spinner2"
android:entries="#array/spin1" />
<EditText
android:id="#+id/editText3"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/spinner3"
android:layout_toRightOf="#+id/spinner3"
android:ems="10"
android:inputType="number" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_alignLeft="#+id/spinner1"
android:layout_below="#+id/spinner4"
android:entries="#array/spin1" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_above="#+id/editText6"
android:layout_alignLeft="#+id/textView1"
android:layout_marginBottom="29dp"
android:entries="#array/spin1" />
<EditText
android:id="#+id/editText2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_above="#+id/spinner3"
android:layout_toRightOf="#+id/spinner3"
android:ems="10"
android:inputType="number" />
<EditText
android:id="#+id/editText6"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_alignTop="#+id/spinner6"
android:ems="10"
android:inputType="number" />
<EditText
android:id="#+id/editText1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/editText2"
android:layout_alignTop="#+id/spinner1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText5"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_above="#+id/spinner6"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:ems="10"
android:inputType="number" />
<Spinner
android:id="#+id/spinner5"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_alignTop="#+id/editText5"
android:layout_toLeftOf="#+id/editText6"
android:entries="#array/Type" />
<EditText
android:id="#+id/editText4"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText5"
android:layout_alignTop="#+id/spinner4"
android:ems="10"
android:inputType="number" />
<Spinner
android:id="#+id/spinner4"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_above="#+id/spinner5"
android:layout_toRightOf="#+id/button1"
android:entries="#array/Type" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/spinner1"
android:layout_alignParentTop="true"
android:text="Production en cours:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Spinner
android:id="#+id/spinner7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/editText1"
android:layout_marginLeft="22dp"
android:entries="#array/Production" />
<EditText
android:id="#+id/editText7"
android:layout_width="50dp"
android:layout_height="25dp"
android:layout_above="#+id/editText1"
android:layout_toRightOf="#+id/spinner4"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner7"
android:layout_toRightOf="#+id/button1"
android:text="Quantité produite:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/textView2"
android:text="TextView" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView5"
android:layout_alignBottom="#+id/textView5"
android:layout_toRightOf="#+id/textView5"
android:text="TextView" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#+id/textView6"
android:layout_toRightOf="#+id/editText3"
android:text="TextView" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView7"
android:layout_alignBottom="#+id/textView7"
android:layout_toRightOf="#+id/textView7"
android:text="TextView" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/spinner4"
android:layout_below="#+id/textView1"
android:text="TextView" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView9"
android:layout_alignBottom="#+id/textView9"
android:layout_toRightOf="#+id/textView9"
android:text="TextView" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView10"
android:layout_alignBottom="#+id/textView10"
android:layout_toRightOf="#+id/textView10"
android:text="TextView" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentBottom="true"
android:text="TextView" />
</RelativeLayout>
content of my spinner are defined in array.xml(in values) like this:
<resources>
<array name="spin1">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</array>
<array name="Type">
<item> A</item>
<item> B</item>
<item>c</item>
<item>d</item>
</array>
<array name="Production">
<item>motorbike</item>
</array>
</resources>
So I want the selected items of my spinners + edittext to go in a textview on a new page, and I dont know how to do that, But I have a good start I think?
thank you
You should be fine sending the data over an intent with myIntent.setExtra("Data", data); as you have above. Why do you define your spinners to listen to every change when all you want is the final value? You can use mySpinner.getSelectedItem().toString(); instead. Can you please elaborate on the doesn't fit my code please? (I would comment this but i don't have 50 rep)

Multiplication between two text fields - Android Java

I am new to Android and programming and having a bit of difficulties with my code. I tried to research this but couldn't find out what was wrong, would appreciate the help. I am basically trying to multiply two values but every time I run my emulator and click on the button (calculate) it crashes:
Java Code:
package com.example.cantcook;
import java.math.BigDecimal;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends Activity {
EditText costofmeal, amountofpeople;
TextView costperperson;
Button Calculate;
BigDecimal costNum, percentNum;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
costofmeal = (EditText) findViewById(R.id.editText1);
amountofpeople = (EditText) findViewById(R.id.editText2);
Calculate = (Button) findViewById(R.id.button1);
costperperson = (TextView) findViewById(R.id.textView2);
//costperperson.setText("£0.00");
Calculate.setOnClickListener(new View.OnClickListener()
{
public void onClick (View v)
{
costNum = new BigDecimal(costofmeal.getText().toString());
percentNum = new BigDecimal(amountofpeople.getText().toString());
costperperson.setText(costNum.multiply(percentNum).toString());
}
});
}
XML code:
<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/background"
tools:context=".Calculator" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="£"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="112dp"
android:text=" Total spent on Shopping"
android:textAlignment="center"
android:textSize="18dp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text=" Amount of People"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginBottom="25dp"
android:onClick="Btncalculate"
android:text="Calculate" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="numberSigned" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="£" />
</RelativeLayout>
In your xml layout you have android:onClick="Btncalculate" in button1. What this does is when a click happens Android tries to look for Btncalculate method in your activity. And I'm assuming you don't have it there, since you're using an OnClickListener for button1(which, in my opinion, is better then using android:onClick).
Remove android:onClick="Btncalculate" from the xml file and it should work(unless there are other errors, in which case it would be great to see the stack trace).
Update
From your stacktrace:
java.lang.NumberFormatException: £5
Looks like you're code is trying to put "£5" into BigDecimal, which is not allowed, since it is not a number. To fix this you can either set android:inputType to a number in your editText1 and remove android:text="£", or use a separate method to filter out the £ sign from input.

How to make Views with an Invisible attribute 'Visible' after clicking a button

I have several Views, text views, and a button that have the android:visibility="invisible" attribute. My goal is to click a button that resides above these 'invisible' widgets, so that these widgets will become visible. I created another java class called 'VisibilityActivity.java" and tried the following method. But for some reason when I run the app, the button doesn't do anything. I don't know what I'm missing.
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VisibilityActivity extends Activity {
private View mVictim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mVictim = findViewById(R.id.horizontalRule1);
mVictim = findViewById(R.id.TextView03);
mVictim = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_background"
android:isScrollContainer="true"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/ratingsInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/yourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp" />
<Button
android:id="#+id/submitRatingButton"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_button"
android:text="#string/submitRating"
android:textColor="#color/black1" />
<View
android:id="#+id/horizontalRule1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/summaryInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1"
android:visibility="invisible" />
<View
android:id="#+id/horizontalRule2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/ourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/overallRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar3"
android:color="#color/black1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="40dp"
android:visibility="invisible" />
<Button
android:id="#+id/saveContinueButton3"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:background="#drawable/custom_button"
android:text="#string/saveContinue"
android:textColor="#color/black1"
android:onClick="onSaveAndContinue3Clicked"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Thanks. Help would be greatly appreciated!
I am updating user936414's answer.
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
if( mText.getVisibility() == View.INVISIBLE )
mText.setVisibility(View.VISIBLE);
else
mText.setVisibility(View.INVISIBLE);
if( mRule1.getVisibility() == View.INVISIBLE )
mRule1.setVisibility(View.VISIBLE);
else
mRule1.setVisibility(View.INVISIBLE);
if( mRule2.getVisibility() == View.INVISIBLE )
mRule2.setVisibility(View.VISIBLE);
else
mRule2.setVisibility(View.INVISIBLE);
}
};
Also you might want to experiment with View.GONE.
findViewById(R.id.ratingBar3).setVisibility(View.VISIBLE);
findViewById(R.id.saveContinueButton3).setVisibility(View.VISIBLE);
you made it invisible view invisible again.. try the above code
Try
public class VisibilityActivity extends Activity {
private TextView mText;
private View mRule1, mRule2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mText= (TextView)findViewById(R.id.horizontalRule1);
mRule1 = findViewById(R.id.TextView03);
mRule2 = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mText.setVisibility(View.VISIBLE);
mRule1.setVisibility(View.VISIBLE);
mRule2.setVisibility(View.VISIBLE);
}
};
}
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(new View.onClickListener)
{
#Override
public void onClick(View v)
{
//Insert your code here
}
}

Categories

Resources