I have developed a simple form within an activity.
My requirement is that when I click a send button, the form data is displayed in same the form layout
But this program is not displaying any field data.
No error is thrown.
Main_Activity.java:
public class MainActivity extends ActionBarActivity {
// private static final String EXTRA_MESSAGE = "com.example.";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
EditText editText1 =(EditText) findViewById(R.id.name);
String message1 =editText1.getText().toString();
Log.v("ThisApp", message1);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message1);
}
}
activity_main.xml:-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/name"
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="Name"/>
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Am I doing something wrong?
Put TextView in same layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/name"
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="Name"/>
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
android:layout_gravity="center_horizontal" />
<TextView
android:id="#+id/showText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="..."
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="..."/>
</LinearLayout>
Then reference it from MainActivity:
public class MainActivity extends ActionBarActivity {
// private static final String EXTRA_MESSAGE = "com.example.";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton=(Button)findViewById(R.id.sendButton);
EditText editText1 =(EditText) findViewById(R.id.name);
TextView textView = (TextView) findViewById(R.id.showText);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
String message1 =editText1.getText().toString();
textView.setText(message1);
}
});
}
}
Related
here's my sample task app, I have six clases Employee, Department, EmployeeActivity , ViewEmployeeInfo, ViewDepartmentInfo,
When user click on add employee it must be navigate to EmployeeActivity, and the same thing when click on department it's navigate to Department class, I created an employee object on EmployeeActivity to send it to ViewEmployeeInfo activity and it must view it on the five EditText that I created it, the problem is that when I Click on Submit button to send data via Intent with putExtra method that takes Serializable object I see no values on EditTexts on anther activity, I tried to send it with regual putExtra method as int,String,String,double,String also got NullPointerException
Here's my code, first this the Employee class
package com.companyactivityexample.companyactivityexample;
import android.widget.Button;
import android.widget.EditText;
import java.io.Serializable;
/**
* Created by MML on 24/12/2017.
*/
public class Employee implements Serializable {
private int id;
private String name;
private String address;
private double salary;
private String job;
public Employee(int id, String name, String address, double salary, String job) {
this.id = id;
this.name = name;
this.address = address;
this.salary = salary;
this.job = job;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
this activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.companyactivityexample.companyactivityexample.MainActivity"
android:orientation="vertical"
>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="10dp"
android:text="Add employee"
android:textSize="25sp"
android:textAllCaps="false"
/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:text="Add department"
android:textSize="25sp"
android:textAllCaps="false"
/>
</LinearLayout>
and this Main Class
`package com.companyactivityexample.companyactivityexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button addEmployee;
private Button addDepartment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addEmployee = (Button) findViewById(R.id.button1);
addDepartment = (Button) findViewById(R.id.button2);
addEmployee.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,EmployeeActivity.class);
startActivity(i);
}
});
addDepartment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,Department.class);
startActivity(i);
}
});
}
}
the employee_activity xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<EditText
android:id="#+id/empID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="ID"
android:textSize="25sp"
android:textColorHint="#color/black"
android:inputType="number"
/>
<EditText
android:id="#+id/empName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="Name"
android:textSize="25sp"
android:textColorHint="#color/black"
android:inputType="text"
/>
<EditText
android:id="#+id/empAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="Address"
android:textSize="25sp"
android:textColorHint="#color/black"
android:inputType="text"
/>
<EditText
android:id="#+id/empSalary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="Salary"
android:textSize="25sp"
android:textColorHint="#color/black"
android:inputType="numberDecimal"
/>
<EditText
android:id="#+id/empJob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="Job"
android:textSize="25sp"
android:textColorHint="#color/black"
android:inputType="text"
/>
<Button
android:id="#+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:text="Submit"
android:textSize="25sp"
android:textAllCaps="false"
/>
</LinearLayout>
</ScrollView>
the EmployeeActivity class
package com.companyactivityexample.companyactivityexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by MML on 27/12/2017.
*/
public class EmployeeActivity extends AppCompatActivity {
private EditText editTextID;
private EditText editTextName;
private EditText editTextAddress;
private EditText editTextSalary;
private EditText editTextJob;
private Button submitButton;
private Employee employee1;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.employee_activity);
editTextID = (EditText) findViewById(R.id.empID);
editTextName = (EditText) findViewById(R.id.empName);
editTextAddress = (EditText) findViewById(R.id.empAddress);
editTextSalary = (EditText) findViewById(R.id.empSalary);
editTextJob = (EditText) findViewById(R.id.empJob);
submitButton = (Button) findViewById(R.id.submit);
if(isEmpty(editTextID) || isEmpty(editTextSalary)){
editTextID.setText("0");
editTextSalary.setText("0");
}
employee1 = new Employee(Integer.parseInt(editTextID.getText().toString())
, editTextName.getText().toString(), editTextAddress.getText().toString()
, Double.parseDouble(editTextSalary.getText().toString())
, editTextJob.getText().toString());
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(EmployeeActivity.this, ViewEmployeeInfo.class);
i.putExtra("emp",employee1);
startActivity(i);
}
});
}
/*i.putExtra("id",id);
i.putExtra("name",name);
i.putExtra("address",address);
i.putExtra("salary",salary);
i.putExtra("job",job);*/
private static boolean isEmpty(EditText etText) {
if (etText.getText().toString().trim().length() > 0)
return false;
return true;
}
}
the view_employee_info activity xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
tools:context=".ViewEmployeeInfo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="ID"
android:textColor="#color/black"
android:textSize="25sp"
/>
<EditText
android:id="#+id/editTextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="Name"
android:textColor="#color/black"
android:textSize="25sp"
/>
<EditText
android:id="#+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="Address"
android:textColor="#color/black"
android:textSize="25sp"
/>
<EditText
android:id="#+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="Salary"
android:textColor="#color/black"
android:textSize="25sp"
/>
<EditText
android:id="#+id/editTextSalary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="Job"
android:textColor="#color/black"
android:textSize="25sp"
/>
<EditText
android:id="#+id/editTextJob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp" />
</LinearLayout>
<Button
android:id="#+id/editButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:text="Edit"
android:textAllCaps="false"
android:textSize="25sp" />
</LinearLayout>
</ScrollView>
the ViewEmployeeInfo class
package com.companyactivityexample.companyactivityexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by MML on 24/12/2017.
*/
public class ViewEmployeeInfo extends AppCompatActivity {
private EditText editID;
private EditText editName;
private EditText editAddress;
private EditText editSalary;
private EditText editJob;
private Button editButton;
private Employee employee;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_employee_info);
Intent i = getIntent();
employee = (Employee) i.getSerializableExtra("emp");
editID = (EditText) findViewById(R.id.editTextID);
editID.setText(employee.getId() + "");
editName = (EditText) findViewById(R.id.editTextName);
editName.setText(employee.getName());
editAddress = (EditText) findViewById(R.id.editTextAddress);
editAddress.setText(employee.getAddress());
editSalary = (EditText) findViewById(R.id.editTextSalary);
editSalary.setText(employee.getSalary() + "");
editJob = (EditText) findViewById(R.id.editTextJob);
editJob.setText(employee.getJob());
editButton = (Button) findViewById(R.id.editButton);
/*Intent i = getIntent();
int getID = i.getIntExtra("id", 0);
String getName = i.getStringExtra("name");
String getAddress = i.getStringExtra("address");
double getSalary = i.getDoubleExtra("salary", 0.0);
String getJob = i.getStringExtra("job");
editID.setText(getID);
editName.setText(getName);
editAddress.setText(getAddress);
editSalary.setText(getSalary + "");
editJob.setText(getJob);*/
}
}
You put the extra in with key of "emp1" and are trying to retrieve it using a key of "emp" in getSerializableExtra("emp"). Simple typo.
Edit: Not a typo. Instead of putExtra("emp", employee1) , cast like putExtra("emp", (serializable) employee1)
Pass the serializable list using Bundle.Serializable
Intent i = new Intent(EmployeeActivity.this, ViewEmployeeInfo.class);
i.putSerializable("emp",employee1);
startActivity(i);
Instead
Intent i = new Intent(EmployeeActivity.this, ViewEmployeeInfo.class);
i.putExtra("emp",employee1);
startActivity(i);
Receive
Intent i = getIntent();
employee = (Employee) i.getSerializableExtra("emp");
The problem was resolved, it was supposed to put the new object of employee1
inside the action of submit button
employee1 = new Employee(Integer.parseInt(editTextID.getText().toString())
, editTextName.getText().toString(), editTextAddress.getText().toString()
, Double.parseDouble(editTextSalary.getText().toString())
, editTextJob.getText().toString());
I'm creating a simple calculator in Android Studio but the equal button didn't work. When I clicked the equal button, the app crash but it didn't show any error message. Other buttons work fine, I couldn't figure out why my equal button didn't work.
This is my MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private EditText showNum;
private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private Button zero;
private Button divide;
private Button multiple;
private Button minus;
private Button equal;
private Button dot;
private Button plus;
private Button clear;
private Boolean addButton, multipleButton, minusButton, divideButton;
Float value, currentValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showNum=(EditText) findViewById(R.id.editView);
one = (Button)findViewById(R.id.button1);
two = (Button)findViewById(R.id.button2);
three = (Button)findViewById(R.id.button3);
four = (Button)findViewById(R.id.button4);
five = (Button)findViewById(R.id.button5);
six = (Button)findViewById(R.id.button6);
seven = (Button)findViewById(R.id.button7);
eight = (Button)findViewById(R.id.button8);
nine = (Button)findViewById(R.id.button9);
zero = (Button)findViewById(R.id.button0);
divide = (Button)findViewById(R.id.buttonDivide);
multiple = (Button)findViewById(R.id.buttonMultiple);
minus = (Button)findViewById(R.id.buttonMinus);
equal = (Button)findViewById(R.id.buttonEqual);
dot = (Button)findViewById(R.id.buttonDot);
plus = (Button)findViewById(R.id.buttonPlus);
clear = (Button)findViewById(R.id.buttonC);
clear.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText("");
}
});
zero.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"0");
}
});
one.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"1");
}
});
two.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"2");
}
});
three.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"3");
}
});
four.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"4");
}
});
five.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"5");
}
});
six.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"6");
}
});
seven.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"7");
}
});
eight.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"8");
}
});
nine.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"9");
}
});
dot.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+".");
}
});
divide.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
divideButton=true;
showNum.setText(null);
}
});
multiple.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if(showNum==null){
showNum.setText("");
}
else{
value = Float.parseFloat(showNum.getText()+"");
multipleButton=true;
showNum.setText(null);
}
}
});
plus.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
addButton=true;
showNum.setText(null);
}
});
minus.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
minusButton=true;
showNum.setText(null);
}
});
equal.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
currentValue = Float.parseFloat(showNum.getText()+"");
if (divideButton) {
showNum.setText(value / currentValue + "");
divideButton = false;
Log.i("gina", "b"+divideButton);
}
if (multipleButton){
showNum.setText(value * currentValue + "");
multipleButton = false;
}
if (addButton){
showNum.setText(value+currentValue + "");
addButton = false;
}
if (minusButton){
showNum.setText(value-currentValue + "");
minusButton = false;
}
}
});
}
}
Below is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iu.hsiaoyuanwang.proficiency2.MainActivity">
<EditText
android:id="#+id/editView"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:padding="35dp"
android:textSize="30sp"
android:gravity="top|center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/editView"
android:orientation="vertical"
android:id="#+id/linear1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
android:textSize="20dp" />
<Button
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
android:textSize="20dp"
/>
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="9" />
<Button
android:id="#+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="รท"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textSize="20dp" />
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
android:textSize="20dp"
/>
<Button
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="6" />
<Button
android:id="#+id/buttonMultiple"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="x"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textSize="20dp" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textSize="20dp"
/>
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="3" />
<Button
android:id="#+id/buttonMinus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:textSize="20dp" />
<Button
android:id="#+id/buttonDot"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."
android:textSize="20dp"
/>
<Button
android:id="#+id/buttonEqual"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="=" />
<Button
android:id="#+id/buttonPlus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/buttonC"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
As Kuti suggested, its better to keep only one OnClickListener, so keep his advice in mind.
A Boolean is an object, is not true or false, so the sentence if(addButton) when addButton is a Boolean is always false. Try using addButton.booleanValue() or use a boolean instead of a Boolean
I am trying to generate Multiplication Table of any number that is input by user. I have developed the interface for the application but cannot understand where to start with the logical part(coding). What i want is when a user inputs a number into the EditText then in the TextView (id: printArea) should show the table of the input number in the format as given in image 2. [Just to show you example i used TextView in the printArea part and i do not know what to use instead]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Please Enter a Number: "
android:textSize="20sp" />
<EditText
android:id="#+id/num"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/log"
android:onClick="submitNumber"
android:text="Get Table" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:id="#+id/printArea" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/clear"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="1dp"
android:text="Clear" />
<Button
android:id="#+id/credits"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="Credits" />
<Button
android:id="#+id/exit"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="2dp"
android:text="Exit" />
</LinearLayout>
</LinearLayout>
and the MainActivity.java is:
package com.example.tara.multiplicationtable;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num;
Button credits;
Button calculate;
Button clear;
Button exit;
TextView printArea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = new EditText(this);
clear = new Button(this);
calculate = new Button(this);
credits = new Button(this);
calculate = new Button(this);
printArea = new TextView(this);
num = (EditText) findViewById(R.id.num);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
printArea = (TextView) findViewById(R.id.printArea);
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//change the integer value into string
int num1 = Integer.parseInt(num.getText().toString());
// Perform action on click
for (int i = 1; i <= 10; i++) {
printArea.setText(num1 + "X" + i + "=" + i * num1);
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Credits.class);
startActivity(i);
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
}
when application is launched it should show like thisThe initial state of application
and i want to make the application show the table like this if user input is 2 The Final Result
Make a Listview and then in its adapter add your table data.
Make adapter's layout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
Here are the codes that i used to build the Multiplication Table that i imagined to build. The code for activity_main.xml file will be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="2dp"
android:text="#string/Enter_number"
android:textSize="16sp" />
<EditText
android:id="#+id/num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="2dp"
android:layout_marginStart="1dp"
android:digits="0123456789"
android:hint="#string/hidden_text"
android:inputType="number"
android:maxLength="3"
android:textSize="16sp" />
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:imeOptions="actionDone"
android:textSize="16sp"/>
<!--android:onClick="submitNumber"-->
<TextView
android:id="#+id/printArea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:background="#drawable/backimage" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="2dp"
android:textSize="16sp"
android:text="#string/clear"
/>
<Button
android:id="#+id/credits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="1dp"
android:textSize="16sp"
android:text="#string/credits" />
<Button
android:id="#+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginStart="1dp"
android:layout_weight="1"
android:textSize="16sp"
android:text="#string/exit" />
</LinearLayout>
</LinearLayout>
The Code for the MainActivity.java file will be:
package com.example.tara.multiplicationtable;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText num;
Button credits;
Button calculate;
Button clear;
Button exit;
TextView printArea;
//private static final String result = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = new EditText(this);
clear = new Button(this);
calculate = new Button(this);
credits = new Button(this);
calculate = new Button(this);
printArea = new TextView(this);
num = (EditText) findViewById(R.id.num);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
calculate = (Button) findViewById(R.id.calculate);
printArea = (TextView) findViewById(R.id.printArea);
calculate.setOnClickListener(new View.OnClickListener() {
// Perform action on Get Table Button click
public void onClick(View v) {
if (num.getText().length() == 0 ){
printArea.setText(R.string.err_msg);
}
else {
//change the integer value into string
printArea.setText("");
int num1 = Integer.parseInt(num.getText().toString());
String result;
for (int i = 1; i <= 10; i++) {
result = (num1 + " X " + i + " = " + i * num1);
printArea.append("\n" + result);
}
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num.setText("");
printArea.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Credits.class));
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Good Bye, User!", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
Thank you everyone for the guidance.
I have this error in eclipse.My code is this.
public class MainActivity extends ActionBarActivity {
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView) findViewById(R.id.textView1);
t1.setText("Hello");
}
}
XML code
<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_marginLeft="46dp"
android:layout_marginTop="37dp"
android:text="TextView" />
Help me to give the solution.
I am trying to fetch the EditText value on click of a button.
String ETValue = ((EditText)findViewById(R.id.ETID)).getText().toString().trim();
Every things works fine on other android versions, but on 1.6 I am getting ""(Empty) String.
Whats going wrong on Android 1.6, how this is happening?
Thanks
Screen Shots:
Code Reference :
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:text="Type you text here"
android:id="#+id/TextView02"
android:layout_height="wrap_content"
android:textColor="#333333"
android:layout_width="fill_parent"
android:textSize="16dip">
</TextView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID1">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="250dip"/>
<Button android:id="#+id/btnID"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#drawable/icon"
android:gravity="bottom"
android:paddingBottom="9dip"
android:layout_marginLeft="3dip"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID2"
android:visibility="gone">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="5dip" />
</LinearLayout>
<Button android:text="Click" android:id="#+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
Etext.Java
public class EText extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = ((EditText) findViewById(R.id.keyword)).getText().toString().trim();
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
Even this way doesn't works
public class EText extends Activity {
/** Called when the activity is first created. */
EditText ETextt1 = null;
EditText ETextt2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Integer.parseInt(Build.VERSION.SDK) < 7){
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
ETextt1 = ((EditText) findViewById(R.id.keyword1));
}else{
ETextt2 = ((EditText) findViewById(R.id.keyword2));
}
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = null;
if(null == ETextt1){
ETValue = ETextt2.getText().toString().trim();
}else if(null == ETextt2){
ETValue = ETextt1.getText().toString().trim();
}
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
This Works Perfectly Fine :-)
package com.test.et;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class EText extends Activity {
/** Called when the activity is first created. */
EditText ETextt1 = null;
EditText ETextt2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Integer.parseInt(Build.VERSION.SDK) < 7){
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
}
//ETextt = ((EditText) findViewById(R.id.keyword1));
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = null;
if(Integer.parseInt(Build.VERSION.SDK) < 7){
ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
}else{
ETValue = ((EditText) findViewById(R.id.keyword1)).getText().toString().trim();
}
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
I have found the problems source. You declareted two EditTexts with the same id. To resolve problem just rename your EditText as keyword1 and keyword2. Then, get text from second EditText.
public class EdText extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
Toast.makeText(EdText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
layout:
`
<TextView
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:text="Type you text here"
android:id="#+id/TextView02"
android:layout_height="wrap_content"
android:textColor="#333333"
android:layout_width="fill_parent"
android:textSize="16dip">
</TextView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID1">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword1"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="250dip"/>
<Button android:id="#+id/btnID"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#drawable/icon"
android:gravity="bottom"
android:paddingBottom="9dip"
android:layout_marginLeft="3dip"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID2"
android:visibility="gone">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword2"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="5dip" />
</LinearLayout>
`
I have just finished struggling with EditText returning an empty string ("").
The reason was that setContentView(R.layout.main); was invoked twice:
private EditText editText1;
private EditText editText2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /// !!!
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
// some really long and untrivial initialization stuff
setContentView(R.layout.main); /// !!!
}
It looks like editText1 and editText2 pointed into the parts of the previous incarnation of R.layout.main...
Here is my test example for your case:
public class EdText extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// string initialised here will always have initial value and never changes
final String ETValue = ((EditText) findViewById(R.id.EditText01)).getText().toString().trim();
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(EdText.this, ((EditText) findViewById(R.id.EditText01)).getText().toString().trim(), Toast.LENGTH_SHORT).show();
}
});
} }
Everything works fine for API 4. Maybe your probles connected with the moment when you read the string value. see my comment.
public class EditTextDemo extends Activity {
/** Called when the activity is first created. */
Button btnClick;
String ETValue;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ETValue = ((EditText)findViewById(R.id.editText1)).getText().toString().trim();
btnClick.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
//assign a default value
ETValue.equals("Welcome!!");
Toast.makeText(getBaseContext(), "Value is:"+ETValue, Toast.LENGTH_SHORT).show();
Log.i("EditTextDemo","-----Value of EditText is:----------"+ETValue);
}
});
}}
You can also give value at runtime.. Also that value can be used for further coding.
Hope this will help!!