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
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 am implementing a program that uses database and interacts with different layouts of entries and editing users.
I'm working with RelativeLayout on all screens. In one of thelayouts, I insert a perfectly aligned button and give the command android:visibility="gone" for him to show upon request.
The problem is that when I need to use it at command editarBt.setVisibility(View.VISIBLE), the button appears out of alignment and overlaps the fields for entering information.
Is there any way to keep the position of command by button?
I will not put the entire code because it has 7 classes, so I'll just put the classes that interest.
EnterPatientActivity Class
package br.luizhmu.aulas_android_sqlite;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by LuizHMU on 2/17/15.
*/
public class EnterPatientActivity extends Activity {
private Paciente paciente = new Paciente();
private EditText nomeEt;
private EditText emailEt;
private EditText senhaEt;
private Button salvarBt;
private Button editarBt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inserir_paciente);
nomeEt = (EditText) findViewById(R.id.editTextNome);
emailEt = (EditText) findViewById(R.id.editTextEmail);
senhaEt = (EditText) findViewById(R.id.editTextSenha);
salvarBt = (Button) findViewById(R.id.buttonSalvar);
editarBt = (Button) findViewById(R.id.buttonEditar);
Intent intent = getIntent();
if(intent != null){
Bundle bundle = intent.getExtras();
if(bundle != null){
paciente.setId(bundle.getLong("id"));
paciente.setNome(bundle.getString("nome"));
paciente.setEmail(bundle.getString("email"));
nomeEt.setText(paciente.getNome());
emailEt.setText(paciente.getEmail());
senhaEt.setVisibility(View.GONE);
salvarBt.setVisibility(View.GONE);
editarBt.setVisibility(View.VISIBLE);
}
}
}
public void salvar(View view){
paciente.setNome(nomeEt.getText().toString());
paciente.setEmail(emailEt.getText().toString());
paciente.setSenha(senhaEt.getText().toString());
DataBase bd = new DataBase(this);
bd.inserir(paciente);
Toast.makeText(this, "Paciente inserido com sucesso!", Toast.LENGTH_SHORT).show();
}
public void editar(View view){
paciente.setNome(nomeEt.getText().toString());
paciente.setEmail(emailEt.getText().toString());
DataBase bd = new DataBase(this);
bd.atualizar(paciente);
Toast.makeText(this, "Paciente \""+paciente.getNome()+"\" atualizado com sucesso.", Toast.LENGTH_SHORT).show();
}
}
activity_inserir_paciente.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="15dp"
android:paddingRight="15dp"
android:background="#ffffea0a"
tools:context=".EnterPatientActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Novo paciente"
android:id="#+id/textView3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ff1727ff"
android:textSize="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="*Nome"
android:ems="10"
android:id="#+id/editTextNome"
android:layout_below="#+id/textView3"
android:layout_alignRight="#+id/buttonSalvar"
android:layout_alignEnd="#+id/buttonSalvar" />
<EditText
android:hint="Telefone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/editTextTelefone"
android:layout_below="#+id/editTextNome"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:hint="*E-mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editTextEmail"
android:layout_below="#+id/editTextTelefone"
android:layout_alignLeft="#+id/editTextTelefone"
android:layout_alignStart="#+id/editTextTelefone" />
<EditText
android:hint="*Senha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editTextSenha"
android:layout_below="#+id/editTextEmail"
android:layout_alignLeft="#+id/editTextEmail"
android:layout_alignStart="#+id/editTextEmail" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Salvar"
android:id="#+id/buttonSalvar"
android:onClick="salvar"
android:layout_below="#+id/textView4"
android:layout_alignRight="#+id/editTextSenha"
android:layout_alignEnd="#+id/editTextSenha" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Editar"
android:id="#+id/buttonEditar"
android:layout_alignTop="#+id/buttonSalvar"
android:layout_toLeftOf="#+id/buttonSalvar"
android:layout_toStartOf="#+id/buttonSalvar"
android:visibility="gone"
android:onClick="editar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="* Campos de preenchimento obrigatório"
android:textColor="#000000"
android:id="#+id/textView4"
android:layout_below="#+id/editTextSenha"
android:layout_alignLeft="#+id/editTextSenha"
android:layout_alignStart="#+id/editTextSenha" />
</RelativeLayout>
In a RelativeLayout, you can prevent views from overlapping by using the layout_toLeftOf, layout_toRightOf, layout_above and layout_below attributes, which expect a view id as value.
Also, you might want to use View.INVISIBLE instead of View.GONE: The former will consider the view during layouting, but hide it. The latter will pretend the view does not exist, therefore altering your layout result.
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();
I have an app and want to have a contact us for where the user inputs their name, address, phone number, and a comment section. Then they will click on compose mail button and it will load the text into the email automaticlly. have some of the code worked out but not sure how to get the text from the edit text into my email msg. anyone have any suggestions on how i can do this. I have attached the code from my xml file and my java file.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Contactform extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);
Button email = (Button) findViewById(R.id.button30);
email.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//TODO Auto-generated method stub
Intent email = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
email.setType("plain/text");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ladsoffice#lads-to-leaders.org"});
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Lads to Leaders/Leaderettes Questions and/or Comments");
email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");
/* Send it off to the Activity-Chooser */
startActivity(Intent.createChooser(email, "Send mail..."));
}
});
}
}
and here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#bf311a" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="#string/name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="37dp"
android:layout_toRightOf="#+id/textView1"
android:inputType="textPersonName" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="35dp"
android:text="#string/addy"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentRight="true"
android:inputType="textPostalAddress" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText2"
android:layout_marginTop="30dp"
android:textColor="#000000"
android:text="#string/cell"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/editText2"
android:inputType="phone" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textColor="#000000"
android:text="#string/questions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_marginTop="25dp"
android:inputType="textMultiLine" />
<Button
android:id="#+id/button30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:background="#drawable/composemail" />
</RelativeLayout>
and here is a ss of what my form looks like if that helps
Thank you for any help you can give. Still learning a bit on how to do some of this stuff so i apologize if this has been asked before but been searching for two days and can't seem to find what i need to do this.
Ok for those who want to know here is the corrected code. The only code i needed to modify was the code for the Java file so thats the only code i will post.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Contactform extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText addy = (EditText) findViewById(R.id.editText2);
final EditText cell = (EditText) findViewById(R.id.editText3);
final EditText questions = (EditText) findViewById(R.id.editText4);
Button email = (Button) findViewById(R.id.button30);
email.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//TODO Auto-generated method stub
Intent email = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
email.setType("plain/text");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ladsoffice#lads-to-leaders.org"});
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Lads to Leaders/Leaderettes Questions and/or Comments");
email.putExtra(android.content.Intent.EXTRA_TEXT,
"name:"+name.getText().toString()+'\n'+"address:"+addy.getText().toString()+'\n'+"phone:"+cell.getText().toString()+'\n'+'\n'+questions.getText().toString()+'\n'+'\n'+"Sent from the Lads to Leaders/Leaderettes Android App.");
/* Send it off to the Activity-Chooser */
startActivity(Intent.createChooser(email, "Send mail..."));
}
});
}
}
Ok so just a quick explaintion. you have to declare your edit text in the code after your oncreate method. It must say final in front of it as well. Then you input your strings into the intent for EXTRA TEXT. also i added the '\n' in single quotes. this action will act as an enter button giving each item its own line so for example i only wanted name on one line so i did name+mystring+'\n'. this will make your name and string appear on one line and then go down one line for the next line. Hope this makes since and it helps someone else out.
You can put all data inside email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");
First Get all EditText by ID here in oncreate Like
EditText name = (EditText) findViewById(R.id.editext1);
EditText address = (EditText) findViewById(R.id.editext2);
EditText phone = (EditText) findViewById(R.id.editext3);
Then put all data
email.putExtra(android.content.Intent.EXTRA_TEXT,
"name:"+name.getText().toString()+"address:"+address.getText().toString()+"phone:
"+phone.getText().toString());
Enjoy :)