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();
Related
I'm trying to play around with two activities. Edit and View activity. I would like to get the inputs from the edit activity and show in the view activity. In the edit activity I have the ok/submit button, which approves the changes and take back to the view activity, in this case the input text fields should be updated with the entered data. If the cancel button is pressed, then obviously no changes being done and the user is being taken back to the view activity.
I've most of the implementations done right, but I can't get the entered data to be shown on the view activity. What am I missing?
This is my codes for edit and view activities.
ViewActivity.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 ViewActivity extends AppCompatActivity {
public static final String EXTRA_FNAME = "EXTRA_TEXT";
public static final String EXTRA_LNAME = "EXTRA_TEXT";
public static final String EXTRA_EMAIL = "EXTRA_TEXT";
String Fname, Lname, email;
EditText FNInput, LNInput, emailInput;
Button editButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
FNInput = (EditText) findViewById(R.id.FNInput);
LNInput = (EditText) findViewById(R.id.LNInput);
emailInput = (EditText) findViewById(R.id.emailInput);
editButton = (Button) findViewById(R.id.okButton);
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openEditActivity();
}
});
Fname = FNInput.getText().toString();
Lname = LNInput.getText().toString();
email = emailInput.getText().toString();
}
public void openEditActivity(){
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra(EXTRA_FNAME, Fname);
intent.putExtra(EXTRA_LNAME, Lname);
intent.putExtra(EXTRA_EMAIL, email);
startActivity(intent);
}
}
EditActivity.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 EditActivity extends AppCompatActivity {
public static final String EXTRA_FNAME = "EXTRA_TEXT";
public static final String EXTRA_LNAME = "EXTRA_TEXT";
public static final String EXTRA_EMAIL = "EXTRA_TEXT";
String Fname, Lname, email;
EditText FNInput, LNInput, emailInput;
Button okButton, cancelButton;
private static final String TAG = "EditActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
FNInput = (EditText) findViewById(R.id.FNInput);
LNInput = (EditText) findViewById(R.id.LNInput);
emailInput = (EditText) findViewById(R.id.emailInput);
okButton = (Button) findViewById(R.id.okButton);
cancelButton = (Button) findViewById(R.id.cancelButton);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateViewActivity();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FNInput.setText("");
LNInput.setText("");
emailInput.setText("");
finish();
}
});
}
public void updateViewActivity(){
Fname = FNInput.getText().toString();
Lname = LNInput.getText().toString();
email = emailInput.getText().toString();
FNInput.setText(Fname);
LNInput.setText(Lname);
emailInput.setText(email);
Intent intent = new Intent(this, ViewActivity.class);
intent.putExtra(EXTRA_FNAME, Fname);
intent.putExtra(EXTRA_LNAME, Lname);
intent.putExtra(EXTRA_EMAIL, email);
startActivity(intent);
}
}
activity_view.xml
<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=".ViewActivity">
<LinearLayout
android:layout_width="270dp"
android:layout_height="374dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="57dp"
android:layout_marginTop="75dp"
android:orientation="vertical">
<EditText
android:id="#+id/FNInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/LNInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/emailInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="info#mail.com"
android:inputType="textEmailAddress" />
<Button
android:id="#+id/okButton"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:text="Edit" />
</LinearLayout>
<TextView
android:id="#+id/viewTV"
android:layout_width="134dp"
android:layout_height="33dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="135dp"
android:layout_marginBottom="18dp"
android:text="View Activity"
tools:layout_editor_absoluteX="15dp"
tools:layout_editor_absoluteY="687dp" />
</RelativeLayout>
activity_edit.xml
<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=".EditActivity">
<LinearLayout
android:layout_width="270dp"
android:layout_height="374dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="57dp"
android:layout_marginTop="75dp"
android:orientation="vertical">
<EditText
android:id="#+id/FNInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/LNInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/emailInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="info#mail.com"
android:inputType="textEmailAddress" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/okButton"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:textColor="#03A9F4" />
<Button
android:id="#+id/cancelButton"
style="#style/Widget.AppCompat.Button.ButtonBar.AlertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAlignment="center" />
</TableRow>
</LinearLayout>
<TextView
android:id="#+id/viewTV"
android:layout_width="108dp"
android:layout_height="38dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="169dp"
android:layout_marginBottom="18dp"
android:text="Edit Activity" />
</RelativeLayout>
I'm sorry this post is going to be lengthy.
When you want to do something like this, you have to follow some steps and follow them properly. which are:-
First, making yourself clear what you want to do exactly, that mean your goal.
Secondly, try to understand what you should do to achieve that goal, like - what thing might have need to do that, resources, tutorial (for this scenario) etc.
Finally let's start searching and learn how to do that.
Here, I can tell you where the problem is, you started learning but didn't completed the learning. I can see you just copied and pasted into two different activities without a reason.
Well, I am sharing what problems I found out from the above code of yours :-
Your ViewActivity.java should be consist of some TextView where you're intended to show your data from your EditActivity.java, which is not there.
You're sending Data with same key every time (another proof of copy pasting, not knowing what is happening) which is -
public static final String EXTRA_FNAME = "EXTRA_TEXT"; // use it as EXTRA_FNAME public static final String EXTRA_LNAME = "EXTRA_TEXT"; // use it as EXTRA_LNAME public static final String EXTRA_EMAIL = "EXTRA_TEXT"; // use it as EXTRA_EMAIL
When you are sending data to your view activity, you need to receive what you were sending by using getIntent() something like :- String s = getIntent().getStringExtra("EXTRA_FNAME"); which will return the value assigned to this key from your previous activity while sending to the present activity.
After receiving the desired value populate your TextView in the next line like this :- textView.setText(s); // fetched from getIntent() previously
For more information you can check this tutorial, which has showed how to pass and view data from one activity to another. Hope you understand.
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>
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 get a LOT of errors from LogCat when I try running my app on an emulator. After looking around I think my main error is a the NullPointerException error which I assume is coming from calling the array of usernames from source and comparing them with the username posted on the form.
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
Context context = getApplicationContext();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void hitSubmit(View button){
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
String feedbackType = feedbackSpinner.getSelectedItem().toString();
if(feedbackType.equals("Please Select")){
Toast.makeText(this, "Please select a country.", Toast.LENGTH_SHORT).show();
return;
}
final EditText fnField = (EditText) findViewById(R.id.FN);
String FN = fnField.getText().toString();
if(FN.equals("")){
Toast.makeText(this, "Please enter your first name.", Toast.LENGTH_SHORT).show();
return;
}
final EditText lnField = (EditText) findViewById(R.id.LN);
String LN = lnField.getText().toString();
if(LN.equals("")){
Toast.makeText(this, "Please enter your last name.", Toast.LENGTH_SHORT).show();
return;
}
final EditText phoneField = (EditText) findViewById(R.id.PN);
String PN = phoneField.getText().toString();
if(PN.equals("")){
Toast.makeText(this, "Please enter your phone number.", Toast.LENGTH_SHORT).show();
return;
}
final EditText Email1Field = (EditText) findViewById(R.id.EA);
String EA1 = Email1Field.getText().toString();
if(EA1.equals("")){
Toast.makeText(this, "Please enter an email address.", Toast.LENGTH_SHORT).show();
return;
}
final EditText Email2Field = (EditText) findViewById(R.id.EA2);
String EA2 = Email2Field.getText().toString();
if(!EA2.equals(EA1)){
Toast.makeText(this, "Your email address' do not match", Toast.LENGTH_SHORT).show();
return;
}
final EditText userField = (EditText) findViewById(R.id.User);
String user = userField.getText().toString();
if(!user.equals("")){
Toast.makeText(this, "Please enter a username", Toast.LENGTH_SHORT).show();
return;
}
Resources res = getResources();
String[] usernameArray = res.getStringArray(R.array.usernames);
int userint = usernameArray.length;
for(int x=0;x<userint;x++){
if(user.equals(usernameArray[x])){
Toast.makeText(this, "Username already exists.", Toast.LENGTH_SHORT).show();
return;
}
}
RadioButton rb1, rb2;
rb1 = (RadioButton) findViewById(R.id.button1);
rb2 = (RadioButton) findViewById(R.id.button2);
if(!rb1.isChecked() && !rb2.isChecked()){
Toast.makeText(this, "Please select a gender.", Toast.LENGTH_SHORT).show();
return;
}
finish();
}
public void hitClear(View button){
finish();
startActivity(getIntent());
}
}
--the xml code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Homework 1</string>
<string name="FN">First Name</string>
<string name="LN">Last Name</string>
<string name="PN">Phone Number</string>
<string name="EA">Email Address</string>
<string name="REEA">Re-enter Email</string>
<string name="User">Username</string>
<string name="Pw">Password</string>
<string name="Gender">Gender</string>
<string name="Country">Country</string>
<string name="action_settings">.</string>
<string name="clear">Clear</string>
<string name="submit">Submit</string>
<string name="female">Female</string>
<string name="male">Male</string>
<string name="country_prompt">Choose a country</string>
<string name="countrytype1">Malaysia</string>
<string name="countrytype2">United States</string>
<string name="countrytype3">Indonesia</string>
<string name="countrytype4">France</string>
<string name="countrytype5">Italy</string>
<string name="countrytype6">Singapore</string>
<string name="countrytype7">New Zealand</string>
<string name="countrytype8">India</string>
<string-array name="country_arrays">
<item>Please Select</item>
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
<string-array name="usernames">
<item>Bryan</item>
<item>John</item>
<item>Matt</item>
<item>Mike</item>
</string-array>
</resources>
Thanks.
EDIT: Adding activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
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"
tools:context=".MainActivity" >
<EditText
android:id="#+id/FN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/FN"
android:inputType="textCapWords"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/LN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/LN"
android:inputType="textCapWords"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/PN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/PN"
android:inputType="phone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/EA"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:inputType="textEmailAddress"
android:hint="#string/EA"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/EA2"
android:layout_width="fill_parent"
android:inputType="textEmailAddress"
android:layout_height="49dp"
android:hint="#string/REEA"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/User"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/User"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/Pw"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="#string/Pw"
android:inputType="textPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="53dp"
android:layout_weight="1.00"
android:onClick="onRadioButtonClicked"
android:text="#string/female" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1.00"
android:onClick="onRadioButtonClicked"
android:text="#string/male" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView9"
android:layout_width="0dip"
android:layout_height="49dp"
android:layout_weight="1"
android:text="#string/Country"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/SpinnerFeedbackType"
android:layout_width="210dp"
android:layout_height="41dp"
android:entries="#array/country_arrays"
android:prompt="#string/country_prompt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="hitSubmit"
android:text="#string/clear" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:onClick="hitClear"
android:text="#string/submit" />
</LinearLayout>
</LinearLayout>
You don't use Context context = getApplicationContext(); in first line of your activity. it's wrong. If you need get context, you could use itself.
Just use this or this.getBaseContext()
Also, if you checked the error message it shown you special line where occurs error. ex. at com.example.test.MyActivity.(MyActivity.java:47) shows line 47
1st of all why you need getapplication() context in activity, if you can use "this" or MainActivity.class .If want to use Context context = getApplicationContext(); then put it in Oncreate() because it may contain the null value and can cause null point exception wherever used.But keep in mind which/how/where context to be used. Context object depends on where it came from originally.
Below is a table of the common places an application will receive a Context, and in each case what it is useful for: .
For details about context check outWhat is context
Joining to all comments, you can also move the line
context = getApplicationContext();
to the onCreate method
i.e.
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
}
i am very new to android i have created a form with 3 field. I want to display these field data in next screen or in the same screen in TextView but i failed....following are my xml code.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/feedbacktitle"
android:textSize="10pt">
</TextView>
<EditText
android:id="#+id/EditTextName"
android:layout_height="wrap_content"
android:hint="#string/feedbackname"
android:inputType="textPersonName"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/feedbackemail"
android:inputType="textEmailAddress"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/feedbackbody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="#+id/ButtonSendFeedback"
android:layout_height="wrap_content"
android:text="#string/feedbackbutton"
android:onClick="sendfeedback"
android:layout_width="fill_parent">
</Button>
<TextView
android:id="#+id/TextViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10pt">
</TextView>
</LinearLayout>
</ScrollView>
and following are Activity class code
package com.Delegence.FormHandling;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FormHandlingActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
TextView Text = (TextView)findViewById(R.id.TextViewResult);
public void sendFeedback(View button) {
EditText nameField = (EditText) findViewById(R.id.EditTextName);
String name = nameField.getText().toString();
EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();
Text.setText(name);
}
}
please guide me how to see these form field value.
Thanks in Advance
to show in same acitivty it should work...
TextView text = (TextView)findViewById(R.id.TextViewResult);
EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
text.setText(email );
to show in other activity you need to pass String like email to another activity by intent and set in textview similarly
1----------------
Use intent to send data to another activity .
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
2-----------------------------------
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// Do something with the data
}
http://www.vogella.com/articles/AndroidIntent/article.html