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());
Related
In the below code, I've set the new Intent in the autocomplete_next TextView, but in my Mobile, when I run the app, and When I try to move to the next activity, It does not go the next Activity, Instead of that, it comes back again to the same previous Activity. In the Android Studio LOGCAT, I've seen the RenderScript: SETAFFINITY ret = -1 . I got no trace about this error on Google? Please Help me to fix my issue. Thank You in Advance...
activity_calculator.xml
<?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"
tools:context=".CalculatorActivity"
android:layout_centerHorizontal="true">
<TableLayout
android:id="#+id/tableinput"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Operand 1 : "
android:textSize="20dp"
android:textAlignment="textStart"
/>
<EditText
android:id="#+id/oper1_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="#+id/autocomplete_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Operand 2 : "
android:textSize="20dp"
android:textAlignment="textStart"
android:layout_weight="0"/>
<EditText
android:id="#+id/oper2_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
<GridLayout
android:id="#+id/calculator"
android:layout_below="#id/tableinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/add_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="0"
android:text="Add"/>
<Button
android:id="#+id/sub_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="1"
android:text="Sub"/>
<Button
android:id="#+id/min_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="2"
android:text="Min"/>
<Button
android:id="#+id/mul_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="0"
android:text="Multiply"/>
<Button
android:id="#+id/div_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="1"
android:text="Divide"/>
<Button
android:id="#+id/pow_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="1"
android:text="Power"/>
<Button
android:id="#+id/max_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="2"
android:text="Max"/>
<Button
android:id="#+id/rem_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="0"
android:text="Remainder"/>
<Button
android:id="#+id/clear_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="2"
android:text="clear"/>
</GridLayout>
<TableRow
android:layout_below="#id/calculator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="#+id/video_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ans: "
android:textSize="20dp"
android:textAlignment="textStart"
android:layout_weight="0"/>
<EditText
android:id="#+id/ans_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
</RelativeLayout>
CalculatorActivity.java (Activity which calls another Activity)
package com.example.lab_excercise_2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.Math;
public class CalculatorActivity extends AppCompatActivity {
private EditText oper1, oper2, ans;
private Button add_btn, sub_btn, div_btn, mul_btn, rem_btn, max_btn, min_btn, pow_btn, clear_btn;
private Integer oper1_text, oper2_text;
private TextView video_next,autocomplete_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
add_btn = findViewById(R.id.add_btn);
sub_btn = findViewById(R.id.sub_btn);
mul_btn = findViewById(R.id.mul_btn);
div_btn = findViewById(R.id.div_btn);
rem_btn = findViewById(R.id.rem_btn);
min_btn = findViewById(R.id.min_btn);
max_btn = findViewById(R.id.max_btn);
pow_btn = findViewById(R.id.pow_btn);
clear_btn = findViewById(R.id.clear_btn);
oper1 = findViewById(R.id.oper1_edit_text);
oper2 = findViewById(R.id.oper2_edit_text);
ans = findViewById(R.id.ans_edit_text);
video_next = findViewById(R.id.video_textView);
autocomplete_next = findViewById(R.id.autocomplete_textView);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text+oper2_text));
}
});
sub_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text-oper2_text));
}
});
mul_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text*oper2_text));
}
});
div_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text/oper2_text));
}
});
rem_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text%oper2_text));
}
});
max_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText((oper1_text>oper2_text ? oper1_text: oper2_text).toString());
}
});
min_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText((oper1_text<oper2_text ? oper1_text: oper2_text).toString());
}
});
pow_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
Double pow = Math.pow(oper1_text,oper2_text);
ans.setText(pow.toString());
}
});
clear_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1.setText("");
oper2.setText("");
ans.setText("");
}
});
video_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent videoIntent = new Intent(CalculatorActivity.this,VideoActivity.class);
startActivity(videoIntent);
}
});
autocomplete_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent autocompleteIntent = new Intent(CalculatorActivity.this,AutocompleteActivity.class);
startActivity(autocompleteIntent);
}
});
}
}
activity_autocomplete.xml
<?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"
tools:context=".AutocompleteActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="30sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:text="Date-Time Picker Autocomplete"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_margin="20dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/setTime_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:text="Set Time"
android:layout_weight="1"
android:layout_margin="20dp"/>
<TextView
android:id="#+id/time_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/setDate_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:text="Set Date"
android:layout_margin="20dp"
android:layout_weight="1" />
<TextView
android:id="#+id/date_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/label_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:text="Label "
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_margin="20dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<MultiAutoCompleteTextView
android:id="#+id/label_edit"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3" />
</TableRow>
<Button
android:id="#+id/setalarm_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="20dp"
android:text="Set Time" />
</TableLayout>
</RelativeLayout>
AutocompleteActivity.java (Activity which was called by the above Activity)
package com.example.lab_excercise_2;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class AutocompleteActivity extends AppCompatActivity {
private Button setTime, setDate, setalarm;
private TextView time_text, date_text, label_text;
private Calendar calendar = Calendar.getInstance();
private String date_string, time_string, label_string;
private String[] labels = {"Wake Up","Time to School","Time to College","Time to Work","Complete Activity","Finish Project"};
private MultiAutoCompleteTextView label_autocomplete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autocomplete);
setTime = findViewById(R.id.setTime_btn);
setDate = findViewById(R.id.setDate_btn);
setalarm = findViewById(R.id.setalarm_btn);
time_text = findViewById(R.id.time_text);
date_text = findViewById(R.id.date_text);
label_autocomplete = findViewById(R.id.label_edit);
label_text = findViewById(R.id.label_textView);
setDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDateDialog(setDate);
}
});
setTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showTimeDialog(setTime);
}
});
setalarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextpage(v);
}
});
label_autocomplete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent alarmIntent = new Intent(AutocompleteActivity.this,AlarmActivity.class);
startActivity(alarmIntent);
}
});
label_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent alarmIntent = new Intent(AutocompleteActivity.this,AlarmActivity.class);
startActivity(alarmIntent);
}
});
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,labels);
label_autocomplete.setAdapter(adapter);
label_autocomplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
public void showTimeDialog(final Button time){
TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
time_text.setText(simpleDateFormat.format(calendar.getTime()));
}
};
new TimePickerDialog(AutocompleteActivity.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();
}
public void showDateDialog(final Button date){
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd");
date_text.setText(simpleDateFormat.format(calendar.getTime()));
}
};
new DatePickerDialog(AutocompleteActivity.this, dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
public void nextpage(View view){
date_string = date_text.getText().toString();
time_string = time_text.getText().toString();
label_string = label_autocomplete.getText().toString();
Intent nextIntent = new Intent(this,Autocomplete2Activity.class);
nextIntent.putExtra("date",date_string);
nextIntent.putExtra("time",time_string);
nextIntent.putExtra("label",label_string);
startActivity(nextIntent);
}
}
LOGCAT
2020-03-02 19:55:10.865 4078-4136/com.example.lab_excercise_2E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4137/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4139/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4138/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4140/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4142/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4141/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1`
[Solved. I solved it myself]
Tips: if you want to access a child in the firebase realtime database for collecting data and then by using that data u store or upload something at that child on the firebase realtime database again, then you can't access it. Either u have to create another child or u must not use that child. (I don't no the actual solution but It works for me)]
I want to create a child under the reference of the user phone number. That child will store one or more childs. These child will be called "serialNo" of the item. And under serialNo child, the item name and amount of piece will store. I want to create like this:
[N: B: I've created this on firebase directly, not by my application.]
But when I put my data object as .setValue(dtObject) the previous one will always be replaced.
"orderNo: 1" child was replaced by "orderNo: 2" child
I also use .updateChildren(dtMap) but everytime previous orderNo child was replaced.
My Code of confirm Fragment(from where I upload):
package com.binarysoftwareltd.airaid;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
import java.util.Map;
public class AddressFragment extends Fragment {
private int orderNo = 1;
private int orderSerial = 0;
private DatabaseReference dbReference, dbr;
private static final String STATE_USER = "user";
private String mUser;
private View oldView;
private TextView numWarning;
private EditText nameField, phoneField, areaField, addressField,
detailsField;
private CardView confirmCV;
private int len;
private String nameOfPerson, phoneNumber, areaName, addressOfOrder,
detailsOfOrder;
private int[] serialNos = new int[100];
private String[] names = new String[100];
private int[] pieces = new int[100];
private String imageUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mUser = savedInstanceState.getString(STATE_USER);
} else {
// Probably initialize members with default values for a new
instance
mUser = "NewUser";
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(STATE_USER, mUser);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
if (oldView != null) {
return oldView;
}
View v = inflater.inflate(R.layout.fragment_address, container,
false);
initializeAll(v);
Bundle bundle = getArguments();
if (bundle != null) {
len = bundle.getInt("cValue");
imageUri = bundle.getString("imgUri");
serialNos = bundle.getIntArray("mSerialNos");
names = bundle.getStringArray("mNames");
pieces = bundle.getIntArray("mPieces");
}
confirmCV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
collectAllData();
if (!phoneNumber.equals("")) {
checkOrderSerial();
} else {
phoneField.requestFocus();
Toast.makeText(getContext(), orderSerial,
Toast.LENGTH_SHORT).show();
}
}
});
oldView = v;
return v;
}
private void checkOrderSerial() {
dbr=FirebaseDatabase.getInstance().getReference(phoneNumber).
child("currentOrderSerial");
dbr.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Integer value = dataSnapshot.getValue(Integer.class);
if (value != null)
orderSerial = value;
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
showConfirmDialog();
}
private void showConfirmDialog() {
AlertDialog.Builder alb = new AlertDialog.Builder(getContext());
alb.setIcon(R.drawable.question);
alb.setTitle("Confirm");
alb.setMessage("Are you sure want to order?");
alb.setPositiveButton(R.string.exit_no, new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alb.setNegativeButton(R.string.exit_yes, new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setOrderSerial();
}
});
AlertDialog ald = alb.create();
ald.show();
}
private void setOrderSerial() {
dbr = FirebaseDatabase.getInstance().getReference(phoneNumber);
if (orderNo <= orderSerial) {
orderNo = orderSerial;
orderNo += 1;
}
OrderSerial osObject = new OrderSerial(orderNo);
dbr.setValue(osObject);
uploadAllData();
}
private void uploadAllData() {
dbReference =
FirebaseDatabase.getInstance().getReference(phoneNumber).child("orderNo:
"+orderNo);
DataTemplate dtObject;
int i;
for (i = 0; i < len; i++) {
if (pieces[i] != 0) {
dtObject = new DataTemplate(names[i],pieces[i]);
dbReference.child("serialNo:
"+serialNos[i]).setValue(dtObject);
}
}
}
private void collectAllData() {
nameOfPerson = nameField.getText().toString();
phoneNumber = phoneField.getText().toString();
areaName = areaField.getText().toString();
addressOfOrder = addressField.getText().toString();
detailsOfOrder = detailsField.getText().toString();
}
private void initializeAll(View v) {
numWarning = v.findViewById(R.id.numWarning);
nameField = v.findViewById(R.id.nameField);
phoneField = v.findViewById(R.id.phoneField);
areaField = v.findViewById(R.id.areaField);
addressField = v.findViewById(R.id.addressField);
detailsField = v.findViewById(R.id.detailsField);
confirmCV = v.findViewById(R.id.confirmCV);
}
}
My OrderSerial Class::
package com.binarysoftwareltd.airaid;
public class OrderSerial {
private int currentOrderSerial;
public OrderSerial() {
}
public OrderSerial(int currentOrderSerial) {
this.currentOrderSerial = currentOrderSerial;
}
public int getCurrentOrderSerial() {
return currentOrderSerial;
}
public void setCurrentOrderSerial(int currentOrderSerial) {
this.currentOrderSerial = currentOrderSerial;
}
}
The XML code of the AddressFragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/app_main_bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="120dp"
android:text="#string/address_fragment_title"
android:textColor="#color/pureBlack"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:id="#+id/numWarning"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="15dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="#string/number_warning"
android:textColor="#color/img_warning"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical"
android:weightSum="8"
android:orientation="horizontal">
<ImageView
android:contentDescription="#string/name_field"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:src="#drawable/ic_person"/>
<EditText
android:id="#+id/nameField"
android:layout_marginStart="5dp"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="70dp"
android:hint="#string/name_field" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical"
android:weightSum="8"
android:orientation="horizontal">
<ImageView
android:contentDescription="#string/phone_field"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:src="#drawable/ic_phone"/>
<EditText
android:id="#+id/phoneField"
android:layout_marginStart="5dp"
android:inputType="phone"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="4"
android:hint="#string/phone_field" />
<EditText
android:id="#+id/areaField"
android:layout_marginStart="10dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3"
android:hint="#string/area_field" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical"
android:weightSum="8"
android:orientation="horizontal">
<ImageView
android:contentDescription="#string/address_fragment_title"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:src="#drawable/ic_location"/>
<EditText
android:id="#+id/addressField"
android:layout_marginStart="5dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="7"
android:hint="#string/address_fragment_title" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical"
android:weightSum="8"
android:orientation="horizontal">
<ImageView
android:contentDescription="#string/details_field"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:src="#drawable/ic_details"/>
<EditText
android:id="#+id/detailsField"
android:layout_marginStart="5dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="7"
android:hint="#string/details_field" />
</LinearLayout>
<androidx.cardview.widget.CardView
android:id="#+id/confirmCV"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:focusable="true"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
app:cardCornerRadius="18dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/card_view_bg"
android:gravity="center">
<ImageView
android:contentDescription="#string/order_now"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_confirm" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text="#string/order_now"
android:textColor="#color/pureWhite"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
here my JSON::
{
"23" : {
"currentOrderSerial" : 2,
"orderNo: 2" : {
"serialNo: 1" : {
"name" : "napa Extra",
"piece" : 200
}
}
}
}
[I use phone number as primary Identifier for every user. That's why phone numbers must be required.]
Please help to fix this...
Thanks in Advance...
So, content of second activity does not appear when app is running, although content is showing in xml design. Programming in Java on Android Studio. In similar article answer didn't help. I also tried just to put one element in second activity, same result. Thanks in advance!
This is code from MainActivity.java:
package todo.beginner.com.carchooser2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import static todo.beginner.com.carchooser2.R.id.checkBoxPrice;
import static todo.beginner.com.carchooser2.R.id.checkBoxGas;
import static todo.beginner.com.carchooser2.R.id.checkBoxYear;
import static todo.beginner.com.carchooser2.R.id.checkBoxMileage;
import static todo.beginner.com.carchooser2.R.id.checkBoxCapacity;
public class MainActivity extends AppCompatActivity {
private CheckBox check1, check2, check3, check4, check5;
private static Button button_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerToCeckBox();
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_next = (Button)findViewById(R.id.button);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("todo.beginner.com.SecondActivity");
startActivity(intent);
}
}
);
}
public void addListenerToCeckBox() {
check1 = (CheckBox)findViewById(checkBoxCena);
check1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Price is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check2 = (CheckBox)findViewById(checkBoxGads);
check2.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Year is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check3 = (CheckBox)findViewById(checkBoxTilpums);
check3.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Engine capacity is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check4 = (CheckBox)findViewById(checkBoxDegviela);
check4.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Gas consumption is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check5 = (CheckBox)findViewById(checkBoxNobraukums);
check5.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Mileage is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
This is from 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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="todo.raitis.com.carchooser.MainActivity">
<CheckBox
android:text="Price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBoxPrice"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="67dp" />
<CheckBox
android:text="Year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxPrice"
android:layout_alignRight="#+id/checkBoxPrice"
android:layout_alignEnd="#+id/checkBoxPrice"
android:layout_marginTop="33dp"
android:id="#+id/checkBoxYear" />
<CheckBox
android:text="Engine Capacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:id="#+id/checkBoxCapacity"
android:layout_below="#+id/checkBoxYear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:text="Gas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxCapacity"
android:layout_alignRight="#+id/checkBoxCapacity"
android:layout_alignEnd="#+id/checkBoxCapacity"
android:layout_marginTop="30dp"
android:id="#+id/checkBoxGas" />
<CheckBox
android:text="Mileage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxGas"
android:layout_alignRight="#+id/checkBoxGas"
android:layout_alignEnd="#+id/checkBoxGas"
android:layout_marginTop="33dp"
android:id="#+id/checkBoxMileage" />
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:id="#+id/button" />
<TextView
android:text="Choose criteria!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
This is from SecondActivity.java:
package todo.beginner.com.carchooser2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
And this is from activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TableRow
android:background="#607D8B"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Car Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Price" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Year" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gas" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mileage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Capacity" />
</TableRow>
<TableRow
android:background="#ECEFF1"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Audi" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2001" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="280000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2.5" />
</TableRow>
</TableLayout>
You are using the intent incorrectly. It should be:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Don't think you're starting the activity correctly. Try changing Intent intent = new Intent("todo.beginner.com.SecondActivity"); to Intent intent = new Intent(MainActivity.this, SecondActivity.class); in your MainActivity.
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 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);
}
});
}
}