I want to send two textview by intents. I am sending intents but I am not getting the desired result. I get two $12.99 as my output. Why doesn't it show the pizza name? Please help. I want to get my pizza name and then price in one line on my app. Here is my code:
XML
<TextView android:id="#+id/pizza1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Mr. Meat Lover Pizza"
android:textSize="25dp"
android:textColor="#2c2349" />
<TextView android:id="#+id/price1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/rectangle"
android:text="$12.99+"
android:textColor="#2c2349"
android:layout_alignParentEnd="true"
android:paddingStart="15dp"
android:textSize="25dp" />
<Button android:id="#+id/ckeckout1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#drawable/rectangle"
android:text="Add to Cart"
android:layout_marginTop="5dp"
android:layout_below="#+id/price1"
android:textColor="#2c2349"
android:backgroundTint="#ff00"
android:layout_alignParentEnd="true"
android:textSize="15dp" />
<LinearLayout android:id="#+id/layout4"
android:layout_below="#+id/layout3"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/textview1"
android:textSize="20dp"
android:layout_width="230dp"
android:layout_height="wrap_content" />
<TextView android:id="#+id/textview2"
android:textSize="20dp"
android:layout_width="130dp"
android:layout_height="wrap_content" />
</LinearLayout>
JAVA
TextView1 = (TextView) findViewById(R.id.pizza1);
TextView2 = (TextView) findViewById(R.id.price1);
My get intent is:
Intent intent1 = new Intent(SplPizzas.this, Cart.class);
String message1 = TextView1.getText().toString();
intent1.putExtra(Message1, message1);
String message2 = TextView2.getText().toString();
intent1.putExtra(Message2, message2);
startActivity(intent1);
break;
Activity 2
Intent intent = getIntent();
String message1 = intent.getStringExtra(SplPizzas.Message1);
String message2 = intent.getStringExtra(SplPizzas.Message2);
TextView output1 = (TextView) findViewById(R.id.textview1);
output1.setText(message1);
TextView output2 = (TextView) findViewById(R.id.textview2);
output2.setText(message2);
#ferrari The way you wrote the code is bit terrible :P
I formatted it a bit and it runs on my end.
As much I am able to guess I think you have mistakenly assigned same "String" to your SplPizzas.Message1 and SplPizzas.Message2.
Thats why you are getting same value i.e. $12.99 in both cases.
Please verify.
Here is the code:
MainActivity.java --> SplPizzas.java from your code
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static String MESSAGE1 = "msg1";
public static String MESSAGE2 = "msg2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startNextActivity(View view){
TextView TextView1 = (TextView) findViewById(R.id.pizza1);
TextView TextView2 = (TextView) findViewById(R.id.price1);
Intent intent1 = new Intent(MainActivity.this, Main2Activity.class);
String message1 = TextView1.getText().toString();
intent1.putExtra(MESSAGE1, message1);
String message2 = TextView2.getText().toString();
intent1.putExtra(MESSAGE2, message2);
startActivity(intent1);
}
}
Main2Activity.java --> Cart.java from your code
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String message1 = intent.getStringExtra(MainActivity.MESSAGE1);
String message2 = intent.getStringExtra(MainActivity.MESSAGE2);
TextView output1 = (TextView) findViewById(R.id.textview1);
output1.setText(message1);
TextView output2 = (TextView) findViewById(R.id.textview2);
output2.setText(message2);
}
}
activity_main.xml --> First screen of your code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="in.proged.mytempapp2.MainActivity">
<TextView
android:id="#+id/pizza1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Mr. Meat Lover Pizza"
android:textSize="25dp"
android:textColor="#2c2349"/>
<TextView
android:id="#+id/price1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="$12.99+"
android:textColor="#2c2349"
android:layout_alignParentEnd="true"
android:paddingStart="15dp"
android:textSize="25dp"/>
<Button
android:id="#+id/ckeckout1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Add to Cart"
android:layout_marginTop="5dp"
android:layout_below="#+id/price1"
android:textColor="#2c2349"
android:backgroundTint="#ff00"
android:layout_alignParentEnd="true"
android:textSize="15dp"
android:onClick="startNextActivity"/>
</RelativeLayout>
activity_main2.xml --> Second screen of your code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
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="in.proged.mytempapp2.Main2Activity">
<LinearLayout
android:id="#+id/layout4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview1"
android:textSize="20dp"
android:layout_width="230dp"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textview2"
android:textSize="20dp"
android:layout_width="130dp"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
Related
I'm using an ImageView to fetch the data from a TextView and use it to call the ACTION_DIAL. Whenever the app is run, it is unable to launch the dialer to display the number. It shows that the getPackageManger is null. I've added the code with the XML below. If I remove the else statement below the app will crash.
Code:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class PostDetailActivity extends AppCompatActivity {
TextView mTitleTv, mDetailTv, mCategoryTv, mCallTv;
ImageView mImageIv;
private static final String TAG = "PostDetailActivity";
String contact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_detail);
//Action Bar
ActionBar actionBar = getSupportActionBar();
// ActionBar title
actionBar.setTitle("Coupon Details");
// Set Back Button
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
// Initialise
mTitleTv = findViewById(R.id.titleTv);
mDetailTv = findViewById(R.id.descriptionTv);
mImageIv = findViewById(R.id.imageView);
mCategoryTv = findViewById(R.id.categoryTv);
mCallTv = findViewById(R.id.callTv);
// get data from intent
String image = getIntent().getStringExtra("image");
String title = getIntent().getStringExtra("title");
String desc = getIntent().getStringExtra("description");
String cate = getIntent().getStringExtra("category");
String call = getIntent().getStringExtra("contact");
contact = call;
// Set Data to views
mTitleTv.setText(title);
mDetailTv.setText(desc);
mCategoryTv.setText(cate);
mCallTv.setText(call);
Picasso.get().load(image).into(mImageIv);
}
// Handle onBack pressed to go back to the previous activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
public void dialNumber(View view) {
TextView textView = (TextView) findViewById(R.id.callTv);
// Use format with "tel:" and phone number to create phoneNumber.
String phoneNumber = String.format("",
textView.getText().toString().trim());
// String phoneNumber = "1234567890";
// Create the intent.
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
// Set the data for the intent as the phone number.
dialIntent.setData(Uri.parse(phoneNumber));
// If package resolves to an app, send intent.
if (dialIntent.resolveActivity(getPackageManager()) != null) {
startActivity(dialIntent);
} else {
Log.e(TAG, "Can't resolve app for ACTION_DIAL Intent.");
}
}
}
The XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="5dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp"
tools:context=".PostDetailActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#color/cardview_light_background" />
<TextView
android:id="#+id/titleTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descriptionTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_alignTop="#id/titleTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<TextView
android:id="#+id/categoryTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_alignTop="#id/descriptionTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<TextView
android:id="#+id/callTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_alignTop="#id/descriptionTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<ImageView
android:id="#+id/imgCall"
android:layout_width="19dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="83dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="255dp"
android:clickable="true"
android:onClick="dialNumber"
app:srcCompat="#drawable/ic_call_black" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Replace
String phoneNumber = String.format("", textView.getText().toString().trim());
with
String phoneNumber = String.format("tel:%s", textView.getText().toString().trim());
As the first statement will produce an empty string.
Recently I decided to develop an app in Android. I have used this forum and other tools online to teach myself how to develop a simple app that asks the user for their name and answer few questions. My aim to allow the user to send the answers to any email of their choice. So far I have been able to achieve this:
From: ********#gmail.com
To: ********#gmail.com
Subject: Survey result for John Doe
Name: John Doe
Question 1: true
End of Survey.
My aim is to include in the email not the state of question 1 (i.e. "True") but instead I want to show the answer of their question. For Example, Question 1: B. 26-35.
I have searched online for the past couple of days but I have been having issues to find some help as I'm still new to programming. The closest thing I came across was this SO Question but it is not what I'm looking for. Any help or guidance would be highly appreciated.
MainActivity.java
package com.example.android.sampleupdrs;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
}
public void emailResult (View view)
{
EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
RadioGroup radioQ1 = (RadioGroup) findViewById(R.id.radio_Q1);
final RadioButton checkedRadioQ1 = (RadioButton) radioQ1.findViewById(radioQ1.getCheckedRadioButtonId());
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
radioQ1.setOnCheckedChangeListener (new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged (RadioGroup group, int checkedQ1Id)
{
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedQ1Id);
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
if (isCheckedRadioQ1)
{
tv.setText("Checked: " +checkedRadioButton.getText());
}
}
});
String resultsEmail = createEmailSummary(name, isCheckedRadioQ1);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Survery result for " + name);
intent.putExtra(Intent.EXTRA_TEXT, resultsEmail);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
private String createEmailSummary (String name, boolean isCheckedRadioQ1) {
String resultsEmail = "Name: " + name;
resultsEmail += "\nQuestion 1 " + isCheckedRadioQ1;
resultsEmail += "\nEnd of Survey";
return resultsEmail;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/name_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Name"
android:inputType="textCapWords"/>
<TextView
android:text="#string/q1string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/textView1"/>
<RadioGroup
android:id="#+id/radio_Q1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/q1b0_url"
android:text="#string/q1b0string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp" />
<RadioButton
android:id="#+id/q1b1_url"
android:text="#string/q1b1string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b0_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b2_url"
android:text="#string/q1b2string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b1_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b3_url"
android:text="#string/q1b3string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b2_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b4_url"
android:text="#string/q1b4string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b3_url"
android:layout_marginBottom="30dp"
android:gravity="start" />
</RadioGroup>
<TextView
android:text="#string/q2string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/textView2"/>
<RadioGroup
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nextQ1"
android:onClick="emailResult"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="right" />
</LinearLayout>
</ScrollView>
strings.xml
<resources>
<string name="app_name">Survey</string>
<string name="q1string">1. Select age group</string>
<string name="q1b0string">A. 18-25</string>
<string name="q1b1string">B. 26-35</string>
<string name="q1b2string">C. 36-45</string>
<string name="q1b3string">D. 46-59</string>
<string name="q1b4string">E. 60+</string>
</resources>
In MainActivity class,
you can get the text of the Radio button in this way:
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
text = "";
if (isCheckedRadioQ1) {
text = checkedRadioQ1.getText().toString();
tv.setText("Checked: " +checkedRadioButton.getText());
}
In method createEmailSummary do
resultsEmail += "\nQuestion 1 " + text;
instead of
resultsEmail += "\nQuestion 1 " + isCheckedRadioQ1
You'll have to send text as a parameter to createEmailSummary instead of isCheckedRadioQ1
If I understand correctly, what you need is checkedRadioButton.getText().toString() and not the boolean isCheckedRadioQ1.
MainActivity's code
How can I get values from the text boxes and the qty box in that user will input the value. price will come from server and than i want to calculate the bill and show it in next layout. plz give me some easy explanation as i am not expert in it. thanks in advance.
MainActivity .java
package com.example.myone;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
private Button done;
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
done = (Button) findViewById(R.id.cal);
tv= (TextView) findViewById(R.id.tv);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
//(int1);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
}
}
MainActivity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#drawable/mainbg"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/textin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter your item here"
/>
<Button
android:id="#+id/cal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/textin"
android:text="Done!"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/add"
android:text="Add"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#+id/cal"
/>
</RelativeLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#drawable/borderbg"
android:orientation="horizontal"
android:id="#+id/rel"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Item"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
<EditText
android:id="#+id/qty"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignTop="#+id/price"
android:layout_toRightOf="#+id/price"
android:layout_marginLeft="50dp"
android:textSize="12sp"
android:hint="Qty" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textout"
android:layout_alignBottom="#+id/textout"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/textout"
android:hint="Price" />
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="remove" />
</RelativeLayout>
Simply I want to create a basic shopping app that have 20 products. the price and product name will come from server and quantity will input by user so how do I that?
You are looking for something like this
int count = container.getChildCount();
for (int i = 0; i < count; i++) {
final View row = container.getChildAt(i);
TextView textOut = (TextView)row.findViewById(R.id.textout);
String data = textOut.getText().toString();
}
Where container is your LinearLayout in which you have added your inflated rows.
Try this:-
To take user input from text box:-
int qty = Integer.parseInt(qtyTextBox.getText().toString());
Caluculate bill:-
double bill = price*qty;
Send bill value to next activity:-
Intent in = new Intent();
in.putExtra("Bill", bill);
startActivity(in);
Get bill value in next activity:-
Intent in = getIntent();
int mBill = in.getExtra("Bill");
string bill = String.valueOf(mBill);
Now you can set the value of 'bill' to the TextView where you want to display it.
I want to display following output:
NAME : _
USN : _
ADDRESS : _
I want to display it in a single text view. When I tried using 2 text views it worked. But to use a single text view I used String builder, But when i click submit button, My application gets crashed. Why it's happening?
/*main.xml*/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/name"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="43dp"
android:text="#string/name" />
<EditText
android:id="#+id/edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_name" />
<TextView
android:id="#+id/usn"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="30dp"
android:paddingLeft="52dp"
android:text="#string/usn" />
<EditText
android:id="#+id/edit_usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_name"
android:layout_marginLeft="95dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_usn" />
<TextView
android:id="#+id/add"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="40dp"
android:paddingLeft="20.3dp"
android:text="#string/add" />
<EditText
android:id="#+id/edit_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_usn"
android:layout_marginLeft="95dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_add" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_centerHorizontal="true"
android:text="#string/submit"/>
</RelativeLayout>
/*layout file*/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Student Application Form</string>
<string name="name">NAME :</string>
<string name="edit_name">Enter your Name</string>
<string name="usn">USN :</string>
<string name="edit_usn">Enter the Usn</string>
<string name="add">ADDRESS :</string>
<string name="edit_add">Enter the Address</string>
<string name="submit">Submit</string>
<string name="number">1.</string>
<string name="action_settings">Settings</string>
<string name="title_activity_details">Details</string>
<string name="hello_world">Hello world!</string>
</resources>
/*Activity_details.xml*/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="#string/number"
android:textStyle="bold" />
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:paddingLeft="43dp"
android:text="#string/name"
android:textStyle="bold" />
<TextView
android:id="#+id/usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="15dp"
android:paddingLeft="52dp"
android:text="#string/usn"
android:textStyle="bold"/>
<TextView
android:id="#+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="20dp"
android:paddingLeft="20.3dp"
android:text="#string/add"
android:textStyle="bold" />
</RelativeLayout>
/*Main Activity.java*/
package com.example.assignment4;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button btn;
EditText etext1;
EditText etext2;
EditText etext3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext1 = (EditText) findViewById(R.id.edit_name);
etext2 = (EditText) findViewById(R.id.edit_usn);
etext3 = (EditText) findViewById(R.id.edit_add);
/** Called when the user clicks the Submit button */
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this, DetailsActivity.class);
intent.putExtra("name", etext1.getText().toString());
intent.putExtra("usn", etext2.getText().toString());
intent.putExtra("address", etext3.getText().toString());
startActivity(intent);
}
}
I passed the intent from this class to Details class. But when I click submit,it gets crashed. But Before it was working properly. After I added string builder, it started giving problem.
/*Details.java*/
package com.example.assignment3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class Details extends ActionBarActivity {
TextView text1;
TextView text2;
TextView text3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
//Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
//String Builder to append strings
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
StringBuilder sb3=new StringBuilder();
//Receiving the intent
Intent intent=getIntent();
//Extracting the string
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("address");
//get the string from a textview
String stext1=getString(R.id.name);
sb1.append(stext1);
sb1.append(Name);
text1.setText(sb1);
String stext2=getString(R.id.usn);
sb2.append(stext2);
sb2.append(Usn);
text2.setText(sb2);
String stext3=getString(R.id.add);
sb3.append(stext3);
sb3.append(Address);
text3.setText(sb3);
}
}
I'm getting Run Time Exception and Null Pointer exception in Logcat
you forgot to initialize textviews.
change your code like below:
public class Details extends ActionBarActivity {
TextView text1;
TextView text2;
TextView text3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
text1 = (TextView)findViewById(R.id.name);
text2 = (TextView)findViewById(R.id.usn);
text3 = (TextView)findViewById(R.id.add);
//Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
//String Builder to append strings
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
StringBuilder sb3=new StringBuilder();
//Receiving the intent
Intent intent=getIntent();
//Extracting the string
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("address");
//get the string from a textview
String stext1= getResources().getString(R.string.name);
text1.setText(Html.fromHtml("<b>" + stext1+ "</b>" +Name));
String stext2= getResources().getString(R.string.usn);
text2.setText(Html.fromHtml("<b>" + stext2+ "</b>" +Usn));
String stext3= getResources().getString(R.string.add);
text3.setText(Html.fromHtml("<b>" + stext3+ "</b>" +Address));
}
}
As Simple you have just declared your TextView s, not initialize it
TextView text1;
TextView text2;
TextView text3;
So initialize it on onCreate() method
text1 = (TextView)findViewById(R.id.name);
text2 = (TextView)findViewById(R.id.usn);
text3 = (TextView)findViewById(R.id.add);
try changing these:
String stext1=getString(R.id.name);
to
String stext1=(findViewById(R.id.name)).getText().toString();
String stext2=getString(R.id.usn);
to
String stext2=(findViewById(R.id.usn)).getText().toString();
String stext3=getString(R.id.add);
to
String stext3=(findViewById(R.id.add)).getText().toString();
What we wanted to do is to display the information of the selected checkbox in Desserts Menu into the List Of Final Order (LOFO) activity.
Here is our desserts.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#ffff"
tools:context=".MainMenu" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/dessertsmenu"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="27dp"
android:text="#string/meal_ok"
android:onClick="onClicdesserts_ok" />
<CheckBox
android:id="#+id/chkpeach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/chkpie"
android:layout_below="#+id/chkpie"
android:defaultValue="false"
android:text="#string/peachcrumbbars"
android:onClick="onClickpeachcrumbbars_ok" />
<CheckBox
android:id="#+id/chksquares"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/chkpeach"
android:layout_below="#+id/chkpeach"
android:defaultValue="false"
android:text="#string/snickerysquares"
android:onClick="onClicksnicjerysquares_ok" />
<CheckBox
android:id="#+id/chkpie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="22dp"
android:layout_marginTop="18dp"
android:defaultValue="false"
android:text="#string/blackberrypiebars"
android:onClick="onClickblackberrypiebars_ok" />
And here is our DessertsMenu.java:
package net.xadtv.yoursmenu;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class DessertsMenu extends Activity{
Button button1;
CheckBox chkpie;
CheckBox chkpeach;
CheckBox chksquares;
public static final String MENU_NAME = "MyMenuFile";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.desserts);
button1 = (Button) findViewById(R.id.button1);
chkpie = (CheckBox) findViewById(R.id.chkpie);
chkpeach = (CheckBox) findViewById(R.id.chkpeach);
chksquares = (CheckBox) findViewById(R.id.chksquares);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chkpie.isChecked());
SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences.Editor pie = settings1.edit();
pie.putString("pietext", "black berry pie bars");
pie.commit();
if(chkpeach.isChecked());
SharedPreferences settings2 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences.Editor peach = settings2.edit();
peach.putString("peachtext", "peach crumbbars");
peach.commit();
if(chksquares.isChecked());
SharedPreferences settings3 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences.Editor squares = settings3.edit();
squares.putString("squarestext", "snickery squares");
squares.commit();
Intent intent = new Intent(getApplicationContext(),LOFO.class);
startActivity(intent);
}
});
}
}
This is where we want to put the clicked checkbox information, The List of Final Order Activity.
Here is our lofo.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#277c9b"
tools:context=".MainMenu" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView4"
android:layout_below="#+id/textView4"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/textView5"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
And here is our LOFO.java:
package net.xadtv.yoursmenu;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class LOFO extends Activity{
public static final String MENU_NAME = "MyMenuFile";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lofo);
TextView tvpeach = (TextView)findViewById(R.id.textView1);
TextView tvsquares = (TextView)findViewById(R.id.textView2);
TextView tvpie = (TextView)findViewById(R.id.textView3);
SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences settings2 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences settings3 = getSharedPreferences(MENU_NAME, 0);
tvpeach.setText(settings1.getString("peach", ""));
tvsquares.setText(settings2.getString("squares", ""));
tvpie.setText(settings3.getString("pie", ""));
}
}
The problem now is, once we run it and then check one of the checkbox, an error will occur (Source not Found).
Any help is greatly appreciated. :)
Couple of things:
you have have: if(chkpie.isChecked()); See that semi-colon at the end? that means that the if statement is useless
you also have two declarations of FILE_MENU. That's sloppy and bad practice. Remove one. Reference the declaration in the second file like so: DessertsMenu.FILE_MENU
same with the three declarations in LOFO.java of shared preferences
I'm also going to guess that the "source not found" error you are talking about is an exception because you are trying to reference your check box objects in the click listener. This can't be done like this.
Instead try this:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0);
switch v.getId()
{
case R.id.chkpie:
settings1.edit().putString("pietext", "black berry pie bars").commit();
case R.id.chkpeach:
//.... you see where I'm going with this right?
}
Intent intent = new Intent(getApplicationContext(),LOFO.class);
startActivity(intent);
}
});
}