One listener - Multiple buttons - android

I'm trying to create a calculator on Eclipse as an Android application.
I'm having some trouble with the listener for multiple buttons.
I know many solutions are available on Internet for that project but I'm learning so I really would like to understand where is my mistake.
Here is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.rayana.calculator.MainActivity" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:layout_marginTop="39dp"
android:text="Result ="
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/button1"
android:layout_marginBottom="31dp"
android:text="B =" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/textView2"
android:text="A =" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:layout_marginBottom="45dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/button2"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:text="-" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView3"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text="+" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/button2"
android:text="x" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/button3"
android:text="รท" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView3"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
And here is my Main.java:
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button plus = (Button)findViewById(R.id.button1);
Button minus = (Button)findViewById(R.id.button2);
Button multiply = (Button)findViewById(R.id.button3);
Button devise = (Button)findViewById(R.id.button4);
final EditText editText1 = (EditText)findViewById(R.id.editText1);
final double A = Double.valueOf(editText1.getText().toString());
final EditText editText2 = (EditText)findViewById(R.id.editText2);
final double B = Double.valueOf(editText2.getText().toString());
final EditText editText3 = (EditText)findViewById(R.id.editText3);
plus.setOnClickListener(Operation1);
minus.setOnClickListener(Operation1);
multiply.setOnClickListener(Operation1);
devise.setOnClickListener(Operation1);
OnClickListener Operation1 = new OnClickListener() {
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
double result = A+B;
editText3.setText(String.valueOf(result));
break;
case R.id.button2:
double result2 = A-B;
editText3.setText(String.valueOf(result2));
break;
case R.id.button3:
double result3 = A*B;
editText3.setText(String.valueOf(result3));
break;
case R.id.button4:
double result4 = A/B;
editText3.setText(String.valueOf(result4));
break;
}
}
};
}

Write your blow code outside your onCreate() method.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button plus = (Button)findViewById(R.id.button1);
Button minus = (Button)findViewById(R.id.button2);
Button multiply = (Button)findViewById(R.id.button3);
Button devise = (Button)findViewById(R.id.button4);
final EditText editText1 = (EditText)findViewById(R.id.editText1);
final double A = Double.valueOf(editText1.getText().toString());
final EditText editText2 = (EditText)findViewById(R.id.editText2);
final double B = Double.valueOf(editText2.getText().toString());
final EditText editText3 = (EditText)findViewById(R.id.editText3);
plus.setOnClickListener(Operation1);
minus.setOnClickListener(Operation1);
multiply.setOnClickListener(Operation1);
devise.setOnClickListener(Operation1);
}
///Your click listener method:
OnClickListener Operation1 = new OnClickListener() {
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
double result = A+B;
editText3.setText(String.valueOf(result));
break;
case R.id.button2:
double result2 = A-B;
editText3.setText(String.valueOf(result2));
break;
case R.id.button3:
double result3 = A*B;
editText3.setText(String.valueOf(result3));
break;
case R.id.button4:
double result4 = A/B;
editText3.setText(String.valueOf(result4));
break;
}
}
};
}

Related

I applied validation to my code still I guess its not working

I have writtn the code for validation...but while executing the app the validations are not working .like when i clicked the button without entering the details it should show the validations...but instead it is directed on the next page.
this is my activity_main.xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_background"
tools:context=".MainActivity">
<TextView
android:id="#+id/plain_text_input"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="142dp"
android:layout_marginRight="142dp"
android:layout_marginBottom="439dp"
android:drawableLeft="#drawable/ic_action_username"
android:fontFamily="sans-serif"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="USERNAME"
android:textColor="#color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:id="#+id/txtusername"
android:layout_width="293dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="41dp"
android:layout_marginBottom="383dp"
android:contextClickable="true"
android:ems="10"
android:hint="ENTER USERNAME"
android:textSize="15dp" />
<TextView
android:id="#+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="23dp"
android:layout_marginLeft="23dp"
android:layout_marginEnd="78dp"
android:layout_marginRight="78dp"
android:layout_marginBottom="325dp"
android:drawableLeft="#drawable/ic_action_password"
android:fontFamily="sans-serif"
android:text="PASSWORD"
android:textColor="#color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:id="#+id/txtpassword"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="56dp"
android:layout_marginBottom="263dp"
android:hint="ENTER PASSWORD"
android:inputType="textPassword"
android:textSize="15dp" />
<androidx.cardview.widget.CardView
android:layout_width="348dp"
android:layout_height="56dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="28dp"
android:layout_marginBottom="170dp"
app:cardBackgroundColor="#color/DeepPink"
app:cardCornerRadius="30dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="373dp"
android:layout_height="73dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="100dp">
<Button
android:id="#+id/textView2"
android:layout_width="124dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="147dp"
android:layout_marginBottom="18dp"
android:background="#android:color/transparent"
android:freezesText="true"
android:onClick="LOGIN"
android:text="LOGIN "
android:textColor="#color/Black"
android:textSize="25dp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="118dp"
android:layout_marginRight="118dp"
android:layout_marginBottom="66dp"
android:background="#android:color/transparent"
android:fontFamily="sans-serif"
android:text="SIGN IN NOW"
android:textAllCaps="true"
android:textColor="#color/Black"
android:textSize="20dp" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="106dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="73dp"
android:layout_marginBottom="522dp"
app:srcCompat="#drawable/ticket" />
</RelativeLayout>
and this is my MainActivity.java code
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText txtusername;
EditText txtpassword;
Button textView2;
ToastManager toastManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtusername = (EditText) findViewById(R.id.txtusername);
txtpassword = (EditText) findViewById(R.id.txtpassword);
textView2 = (Button) findViewById(R.id.textView2);
Button textView3 = (Button) findViewById(R.id.textView3);
textView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, NewUserActivity.class);
startActivity(i);
}
});
Button textView2 = (Button) findViewById(R.id.textView2);
textView2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent u = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(u);
}
});
}
public void LOGIN(final View view) {
textView2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String username = txtusername.getText().toString().trim();
String password = txtpassword.getText().toString().trim();
ToastManager toastManager = new ToastManager(MainActivity.this);
boolean isAtLeastOneEditTextNotEmpty = !username.isEmpty()
|| !password.isEmpty();
if (isAtLeastOneEditTextNotEmpty) {
if (username.isEmpty()) {
toastManager.addToast("ENTER USERNAME", ToastManager.Duration.LENGTH_SHORT);
} else if (!((username.length() > 6) && (username.length() < 15))) {
toastManager.addToast("USERNAME IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
}
if (password.isEmpty()) {
toastManager.addToast("ENTER PASSWORD", ToastManager.Duration.LENGTH_SHORT);
} else if (!((password.length() > 6) && (password.length() < 15))) {
toastManager.addToast("PASSWORD IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
}
} else {
toastManager.addToast("ALL FIELDS ARE COMPULSORY", ToastManager.Duration.LENGTH_SHORT);
}
}
});
}
}
There are two types to call the actions
one: is setOnClickListenr
two: (xml) onClick
you are using both in your code
but when you use them both, (setOnClickListenr) will be execute
so the solution is to remove (setOnClickListenr) or remove the (android:onClick="LOGIN")
EDIT:- if you choice to take (setOnClickListenr) don't forget to copy the validation code and paste it in there body like this
public class MainActivity extends AppCompatActivity {
EditText txtusername;
EditText txtpassword;
Button textView2;
ToastManager toastManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtusername = (EditText) findViewById(R.id.txtusername);
txtpassword = (EditText) findViewById(R.id.txtpassword);
textView2 = (Button) findViewById(R.id.textView2);
Button textView3 = (Button) findViewById(R.id.textView3);
textView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, NewUserActivity.class);
startActivity(i);
}
});
textView2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String username = txtusername.getText().toString().trim();
String password = txtpassword.getText().toString().trim();
ToastManager toastManager = new ToastManager(MainActivity.this);
boolean isAtLeastOneEditTextNotEmpty = !username.isEmpty()
|| !password.isEmpty();
if (isAtLeastOneEditTextNotEmpty) {
if (username.isEmpty()) {
toastManager.addToast("ENTER USERNAME", ToastManager.Duration.LENGTH_SHORT);
} else if (!((username.length() > 6) && (username.length() < 15))) {
toastManager.addToast("USERNAME IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
}
if (password.isEmpty()) {
toastManager.addToast("ENTER PASSWORD", ToastManager.Duration.LENGTH_SHORT);
} else if (!((password.length() > 6) && (password.length() < 15))) {
toastManager.addToast("PASSWORD IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
}
} else {
toastManager.addToast("ALL FIELDS ARE COMPULSORY", ToastManager.Duration.LENGTH_SHORT);
}
}
});
}
have a nice day

passing edittext values to another activity getting null values

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());

Implementing Passcode screen in android

I need to build a Passcode screen which should get 4 numeric characters and I have 4 input image view inside the grid layout above the keypad while i am giving input the circle should fill color to indicate that the input is taken.
Like this it should fill the color:
Passcode_Screen.Java
public class Passcode_Screen extends Activity implements View.OnClickListener{
String userEntered;
String userPin = "1234";
final int PIN_LENGTH = 4;
boolean keyPadLockedFlag = false;
Context appContext;
TextView titleView;
TextView pinBox0;
TextView pinBox1;
TextView pinBox2;
TextView pinBox3;
TextView pinBox4;
TextView statusView;
Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button button10;
Button buttonExit;
Button buttonDelete;
EditText passwordInput;
ImageView backSpace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appContext = this;
userEntered = "";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set the view content
setContentView(R.layout.activity_passcode__screen);
//Typeface xpressive=Typeface.createFromAsset(getAssets(), "fonts/XpressiveBold.ttf");
statusView = (TextView) findViewById(R.id.statusview);
passwordInput = (EditText) findViewById(R.id.Result);
backSpace = (ImageView) findViewById(R.id.imageView);
buttonExit = (Button) findViewById(R.id.buttonexit);
backSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
passwordInput.setText(passwordInput.getText().toString().substring(0, passwordInput.getText().toString().length() - 2));
}
});
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// //Exit app
// Intent i = new Intent();
// i.setAction(Intent.ACTION_MAIN);
// i.addCategory(Intent.CATEGORY_HOME);
// appContext.startActivity(i);
// finish();
Intent camera = new Intent(Passcode_Screen.this, MainActivity.class);
startActivity(camera);
}
}
);
//buttonExit.setTypeface(xpressive);
buttonDelete = (Button) findViewById(R.id.buttondelete);
buttonDelete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (keyPadLockedFlag == true) {
return;
}
if (userEntered.length() > 0) {
userEntered = userEntered.substring(0, userEntered.length() - 1);
passwordInput.setText("");
}
}
}
);
titleView = (TextView) findViewById(R.id.time);
//titleView.setTypeface(xpressive);
View.OnClickListener pinButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
if (keyPadLockedFlag == true) {
return;
}
Button pressedButton = (Button) v;
if (userEntered.length() < PIN_LENGTH) {
userEntered = userEntered + pressedButton.getText();
Log.v("PinView", "User entered=" + userEntered);
//Update pin boxes
passwordInput.setText(passwordInput.getText().toString() + "*");
passwordInput.setSelection(passwordInput.getText().toString().length());
if (userEntered.length() == PIN_LENGTH) {
//Check if entered PIN is correct
if (userEntered.equals(userPin))
{
Intent login = new Intent(Passcode_Screen.this,Site_screen.class);
startActivity(login);
}
else
{
Toast.makeText(getApplicationContext(), "PIN Invalid",Toast.LENGTH_SHORT).show();
}
}
} else {
//Roll over
passwordInput.setText("");
userEntered = "";
// statusView.setText("");
userEntered = userEntered + pressedButton.getText();
Log.v("PinView", "User entered=" + userEntered);
//Update pin boxes
passwordInput.setText("8");
}
}
};
button0 = (Button) findViewById(R.id.button0);
//button0.setTypeface(xpressive);
button0.setOnClickListener(pinButtonHandler);
button1 = (Button) findViewById(R.id.button1);
//button1.setTypeface(xpressive);
button1.setOnClickListener(pinButtonHandler);
button2 = (Button) findViewById(R.id.button2);
//button2.setTypeface(xpressive);
button2.setOnClickListener(pinButtonHandler);
button3 = (Button) findViewById(R.id.button3);
//button3.setTypeface(xpressive);
button3.setOnClickListener(pinButtonHandler);
button4 = (Button) findViewById(R.id.button4);
//button4.setTypeface(xpressive);
button4.setOnClickListener(pinButtonHandler);
button5 = (Button) findViewById(R.id.button5);
//button5.setTypeface(xpressive);
button5.setOnClickListener(pinButtonHandler);
button6 = (Button) findViewById(R.id.button6);
//button6.setTypeface(xpressive);
button6.setOnClickListener(pinButtonHandler);
button7 = (Button) findViewById(R.id.button7);
//button7.setTypeface(xpressive);
button7.setOnClickListener(pinButtonHandler);
button8 = (Button) findViewById(R.id.button8);
//button8.setTypeface(xpressive);
button8.setOnClickListener(pinButtonHandler);
button9 = (Button) findViewById(R.id.button9);
//button9.setTypeface(xpressive);
button9.setOnClickListener(pinButtonHandler);
buttonDelete = (Button) findViewById(R.id.buttondelete);
//buttonDelete.setTypeface(xpressive);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
//App not allowed to go back to Parent activity until correct pin entered.
return;
//super.onBackPressed();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_pin_entry_view, menu);
return true;
}
#Override
public void onClick(View view) {
}
private class LockKeyPadOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 2; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
statusView.setText("");
//Roll over
passwordInput.setText("");
;
userEntered = "";
keyPadLockedFlag = false;
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
}
activity_passcode_screen:
<?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:layout_centerInParent="true"
>
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:background="#FFF"
android:layout_above="#+id/numericPad"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:id="#+id/view" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_above="#+id/view"
android:layout_marginBottom="10dp"
android:id="#+id/imageView" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="1"
android:rowCount="4"
android:id="#+id/glResult"
android:layout_below="#+id/statusview"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:id="#+id/imvPin1"
android:background="#drawable/round"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:id="#+id/imvPin2"
android:background="#drawable/round"
android:orientation="horizontal"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:id="#+id/imvPin3"
android:background="#drawable/round"
android:orientation="horizontal"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:id="#+id/imvPin4"
android:background="#drawable/round"
android:orientation="horizontal"
android:layout_marginRight="10dp"
/>
</LinearLayout>
<GridLayout
android:id="#id/numericPad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="3"
android:rowCount="4"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_below="#+id/glResult"
android:layout_marginTop="20dp"
android:layout_centerInParent="true">
<Button android:text="1"
android:layout_row="0"
android:layout_column="0"
android:id="#+id/button1"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
/>
<Button android:text="2"
android:layout_row="0"
android:layout_column="1"
android:id="#+id/button2"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"/>
<Button android:text="3"
android:layout_row="0"
android:layout_column="2"
android:id="#+id/button3"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
/>
<Button android:text="4"
android:layout_row="1"
android:layout_column="0"
android:id="#+id/button4"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"/>
<Button android:text="5"
android:layout_row="1"
android:layout_column="1"
android:id="#+id/button5"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp" android:layout_marginRight="15dp"
android:layout_marginBottom="15dp" />
<Button android:text="6"
android:layout_row="1"
android:layout_column="2"
android:id="#+id/button6"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
/>
<Button android:text="7"
android:layout_row="2"
android:layout_column="0"
android:id="#+id/button7"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"/>
<Button android:text="8"
android:layout_row="2"
android:layout_column="1"
android:id="#+id/button8"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"/>
<Button android:text="9"
android:layout_row="2"
android:layout_column="2"
android:id="#+id/button9"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"/>
<Button
android:text="0"
android:layout_row="3"
android:layout_column="1"
android:id="#+id/button0"
android:background="#drawable/round"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:layout_width="48dp"
/>
</GridLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter Password"
android:layout_centerInParent="true"
android:textSize="20dp"
android:textStyle="bold"
android:id="#+id/statusview"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/buttonexit"
android:background="#00000000"
android:layout_alignBottom="#+id/buttondelete" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="#+id/buttondelete"
android:background="#00000000"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Here instead of Edit text (passcodeInput) where i am storing the input value i have removed that and i need to add image view field and to fill the circles with color when a button is pressed, should fill from circle 1 to 4 linearly and when the 4th input is filling it should validate the entire input to the default given input if it matches it should move to the next activity else should show error message.
I need like this image :
https://play.google.com/store/apps/details?id=com.smart.mobile.lin.pin.locker
I don't have much knowledge in Android so please help me to figure out this. Please share any tutorial links or sample code for the above question. Thanks in advance.

Create multiplication table layout

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.

copy text field data to other text field Android

i need to copy the text field data to another onclick
below is my activitymain,xml of my app
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="63dp"
android:text="Button" />
<Chronometer
android:id="#+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="108dp"
android:text="Chronometer" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="textPersonName" />
</RelativeLayout>
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String data = editText1.getText().toString();
editText2.setText(data);
});
Hope it helps
package com.example.rohitsapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText) findViewById(R.id.editText1);
final EditText editText2 = (EditText) findViewById(R.id.editText2);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String data = editText1.getText().toString();
editText2.setText(data);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Categories

Resources