How to add a history to calculator? - java

How do I add a history for a calculator in android studio/java? I need it to be only 3 lines of history before it disapears. How do I also show the entire calculation, and not just print out the result?
first one is where i store all my functions, and the 2nd one is primarily just to customize all the calculator buttons, etc.
import static com.example.calculator.R.string.display;
import androidx.appcompat.app.AppCompatActivity;
import org.mariuszgromada.math.mxparser.*;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.input);
display.setShowSoftInputOnFocus(false);
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (getString(R.string.display).equals(display.getText().toString())){
display.setText("");
}
}
});
}
private void updateText(String strToAdd){
String oldStr = display.getText().toString();
int cursorPos = display.getSelectionStart();
String leftStr = oldStr.substring(0, cursorPos); //code that prints the numbers on the screen
String rightStr = oldStr.substring(cursorPos);
if (getString(R.string.display).equals(display.getText().toString())){
display.setText(strToAdd);
display.setSelection(cursorPos + 1);
}
else{
display.setText(String.format("%s%s%s", leftStr, strToAdd, rightStr));
display.setSelection(cursorPos + 1); //moves cursor position to one right
}
}
public void zeroBTN(View view){
updateText("0");
}
public void oneBTN(View view){
updateText("1");
}
public void twoBTN(View view){
updateText("2");
}
public void threeBTN(View view){
updateText("3");
}
public void fourBTN(View view){
updateText("4");
}
public void fiveBTN(View view){
updateText("5");
}
public void sixBTN(View view){
updateText("6");
}
public void sevenBTN(View view){
updateText("7");
}
public void eightBTN(View view){
updateText("8");
}
public void nineBTN(View view){
updateText("9");
}
public void multiplyBTN(View view){
updateText("×");
}
public void divideBTN(View view){
updateText("÷");
}
public void subtractBTN(View view){
updateText("-");
}
public void addBTN(View view){
updateText("+");
}
public void clearBTN(View view){
display.setText("");
}
public void expBTN(View view){
updateText("^");
}
public void parenthesesBTN(View view){
int cursorPos = display.getSelectionStart();
int openPar = 0;
int closedPar = 0;
int textLength = display.getText().length();
for (int i = 0; i < cursorPos; i++){
if (display.getText().toString().substring(i, i+1).equals("(")){
openPar += 1;
}
if (display.getText().toString().substring(i, i+1).equals(")")){
closedPar += 1;
}
}
if (openPar == closedPar || display.getText().toString().substring(textLength - 1, textLength).equals("(")){
updateText("(");
display.setSelection(cursorPos + 1);
}
else if (closedPar < openPar && !display.getText().toString().substring(textLength - 1, textLength).equals("(")){
updateText(")");
}
display.setSelection(cursorPos + 1);
}
public void plusMinusBTN(View view){
updateText("thx for using my calc, you found an easter egg :)");
}
public void decimalBTN(View view){
updateText(".");
}
public void equalBTN(View view){
String userExp = display.getText().toString();
userExp = userExp.replaceAll("÷", "/");
userExp = userExp.replaceAll("×", "*");
Expression exp = new Expression(userExp);
String result = String.valueOf(exp.calculate());
display.setText(result);
display.setSelection(result.length());
}
public void backspaceBTN(View view){
int cursorPos = display.getSelectionStart();
int textLen = display.getText().length();
if (cursorPos != 0 && textLen !=0){ //code to delete stuff
SpannableStringBuilder selection = (SpannableStringBuilder) display.getText();
selection.replace(cursorPos - 1, cursorPos, "");
display.setSelection(cursorPos - 1);
}
}
}
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TableLayout
android:id="#+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/input"
app:layout_constraintVertical_bias="0.95"
tools:layout_editor_absoluteX="1dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/clearBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="clearBTN"
android:text="#string/clear"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/parenthesesBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="parenthesesBTN"
android:text="#string/parentheses"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/exponentBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="expBTN"
android:text="#string/exponent"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/divideBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="divideBTN"
android:text="#string/divide"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/sevenBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="sevenBTN"
android:text="#string/seven"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/eightBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="eightBTN"
android:text="#string/eight"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/nineBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="nineBTN"
android:text="#string/nine"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/multiplyBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="multiplyBTN"
android:text="#string/multiply"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/fourBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="fourBTN"
android:text="#string/four"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/fiveBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="fiveBTN"
android:text="#string/five"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/sixBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="sixBTN"
android:text="#string/six"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/subtractBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="subtractBTN"
android:text="#string/subtract"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/one"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="oneBTN"
android:text="#string/one"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/two"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="twoBTN"
android:text="#string/two"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/three"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="threeBTN"
android:text="#string/three"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/add"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="addBTN"
android:text="#string/add"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/plusMinusBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="plusMinusBTN"
android:text="#string/plusMinus"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/zeroBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="zeroBTN"
android:text="#string/zero"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/pointBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="decimalBTN"
android:text="#string/point"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/equalsBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="equalBTN"
android:text="#string/equals"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
</TableLayout>
<EditText
android:id="#+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="#string/display"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="false"
android:inputType="none"
android:textSize="36sp"
android:textAlignment="textEnd"
tools:ignore="LabelFor" />
<ImageButton
android:id="#+id/backspace"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/circle"
android:contentDescription="#string/backspace"
android:onClick="backspaceBTN"
app:layout_constraintBottom_toTopOf="#+id/tableLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/backspace"
tools:ignore="UsingOnClickInXml" />
</androidx.constraintlayout.widget.ConstraintLayout>

Related

Android studio checkbox gets unchecked when moved from one screen to next screen

I am new to android studio, I tried all possible options, but unable to understand why checkbox gets unchecked when moved from one screen to other when I come back to previous screen.
I am missing out something in onsavedInstanceState or onRestoreInstanceState
Below is my Java Code
package com.example.qualitycheckstation;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Qc2_1_1 extends AppCompatActivity {
CheckBox qc2_1cb1a,qc2_1cb1b,qc2_1cb1c, qc2_1cb2a,qc2_1cb2b,qc2_1cb2c ,qc2_1cb3a,qc2_1cb3b, qc2_1cb3c;
TextView date2_1_1, time2_1_1;
Button next2_1_1,back2_1_1;
Boolean myBoolean1,myBoolean2,myBoolean3,myBoolean4,myBoolean5,myBoolean6,myBoolean7,myBoolean8,myBoolean9;
private static String pr2_1_1,pr2_1_2,pr2_1_3;
private boolean myBoolean = false;
public static String getPr2_1_1 (){
return pr2_1_1;
}
public static String getPr2_1_2 (){
return pr2_1_2;
}
public static String getPr2_1_3(){
return pr2_1_3;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qc2_1_1);
date2_1_1= findViewById(R.id.date2_1_1);
time2_1_1 = findViewById(R.id.time2_1_1);
Calendar calendar= Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = simpleDateFormat.format(calendar.getTime());
date2_1_1.setText(date);
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("hh:mm:ss a");
String time0 = simpleDateFormat1.format(calendar.getTime());
time2_1_1.setText(time0);
qc2_1cb1a = findViewById(R.id.qc2_1cb1a);
qc2_1cb1b = findViewById(R.id.qc2_1cb1b);
qc2_1cb1c = findViewById(R.id.qc2_1cb1c);
qc2_1cb2a = findViewById(R.id.qc2_1cb2a);
qc2_1cb2b = findViewById(R.id.qc2_1cb2b);
qc2_1cb2c = findViewById(R.id.qc2_1cb2c);
qc2_1cb3a = findViewById(R.id.qc2_1cb3a);
qc2_1cb3b = findViewById(R.id.qc2_1cb3b);
qc2_1cb3c = findViewById(R.id.qc2_1cb3c);
next2_1_1 = findViewById(R.id.next2_1_1);
back2_1_1 = findViewById(R.id.back2_1_1);
next2_1_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if((qc2_1cb1a.isChecked())){
pr2_1_1 = "O";
}
else if((qc2_1cb1b.isChecked())){
pr2_1_1 = "X";
}
else if((qc2_1cb1c.isChecked())){
pr2_1_1 = "N/A";
}
if((qc2_1cb2a.isChecked())){
pr2_1_2 = "O";
}
else if((qc2_1cb2b.isChecked())){
pr2_1_2 = "X";
}
else if((qc2_1cb2c.isChecked())){
pr2_1_2 = "N/A";
}
if((qc2_1cb3a.isChecked())){
pr2_1_3 = "O";
}
else if((qc2_1cb3b.isChecked())){
pr2_1_3 = "X";
}
else if((qc2_1cb3c.isChecked())){
pr2_1_3 = "N/A";
}
if (
((qc2_1cb1a.isChecked()) && (qc2_1cb1b.isChecked())) || ((qc2_1cb1b.isChecked()) && (qc2_1cb1c.isChecked())) || ((qc2_1cb1a.isChecked()) && (qc2_1cb1c.isChecked())) ||
((qc2_1cb2a.isChecked()) && (qc2_1cb2b.isChecked())) || ((qc2_1cb2b.isChecked()) && (qc2_1cb2c.isChecked())) || ((qc2_1cb2a.isChecked()) && (qc2_1cb2c.isChecked())) ||
((qc2_1cb3a.isChecked()) && (qc2_1cb3b.isChecked())) || ((qc2_1cb3b.isChecked()) && (qc2_1cb3c.isChecked())) || ((qc2_1cb3a.isChecked()) && (qc2_1cb3c.isChecked()))
)
{ { Toast.makeText(getApplicationContext(),"Select Either OK, NG or N/A", Toast.LENGTH_SHORT).show(); }}
else if (
((qc2_1cb1a.isChecked()) || (qc2_1cb1b.isChecked()) || (qc2_1cb1c.isChecked())) &&
((qc2_1cb2a.isChecked()) || (qc2_1cb2b.isChecked()) || (qc2_1cb2c.isChecked()))&&
((qc2_1cb3a.isChecked()) || (qc2_1cb3b.isChecked()) || (qc2_1cb3c.isChecked()))
)
{ Intent intent = new Intent(Qc2_1_1.this, Qc2_1_2.class);startActivity(intent); }
else { Toast.makeText(getApplicationContext(),"Select all parameters", Toast.LENGTH_SHORT).show(); }
}
});
back2_1_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Qc2_1_1.this, Qc2.class);
startActivity(intent);
}
});
}
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("qc2_1cb1a", qc2_1cb1a.isChecked());
outState.putBoolean("qc2_1cb1b", qc2_1cb1b.isChecked());
outState.putBoolean("qc2_1cb1c", qc2_1cb1c.isChecked());
outState.putBoolean("qc2_1cb2a", qc2_1cb2a.isChecked());
outState.putBoolean("qc2_1cb2b", qc2_1cb2b.isChecked());
outState.putBoolean("qc2_1cb2c", qc2_1cb2c.isChecked());
outState.putBoolean("qc2_1cb3a", qc2_1cb3a.isChecked());
outState.putBoolean("qc2_1cb3b", qc2_1cb3b.isChecked());
outState.putBoolean("qc2_1cb3c", qc2_1cb3c.isChecked());
}
#Override
protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myBoolean1 = savedInstanceState.getBoolean("qc2_1cb1a");
myBoolean2 = savedInstanceState.getBoolean("qc2_1cb1b");
myBoolean3 = savedInstanceState.getBoolean("qc2_1cb1c");
myBoolean4 = savedInstanceState.getBoolean("qc2_1cb2a");
myBoolean5 = savedInstanceState.getBoolean("qc2_1cb2b");
myBoolean6 = savedInstanceState.getBoolean("qc2_1cb2c");
myBoolean7 = savedInstanceState.getBoolean("qc2_1cb3a");
myBoolean8 = savedInstanceState.getBoolean("qc2_1cb3b");
myBoolean9 = savedInstanceState.getBoolean("qc2_1cb3c");
qc2_1cb1a.setChecked(myBoolean1);
qc2_1cb1b.setChecked(myBoolean2);
qc2_1cb1c.setChecked(myBoolean3);
qc2_1cb2a.setChecked(myBoolean4);
qc2_1cb2b.setChecked(myBoolean5);
qc2_1cb2c.setChecked(myBoolean6);
qc2_1cb3a.setChecked(myBoolean7);
qc2_1cb3b.setChecked(myBoolean8);
qc2_1cb3c.setChecked(myBoolean9);
}
}
xml Code
<TableLayout
android:layout_width="match_parent"
android:layout_height="462dp"
android:layout_marginStart="15dp"
android:layout_marginTop="125dp"
android:layout_marginEnd="15dp"
android:background="#color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="Sl No"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Measured Item"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="199dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="OP No"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text="Gauge Used"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Parameter"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="91dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="OK"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="92dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="NG"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="N/A"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="1."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Groove Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="25"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="131dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Exist"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb1a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb1b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb1c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="2."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Serial Engraving Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="30"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="41dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Numbers should be readable"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb2a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb2b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb2c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="3."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="251dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Machining Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="251dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="25"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="31dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="328dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="No rough surface, unwash surface,scratch, burr"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb3a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb3b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb3c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
</TableLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="360dp"
android:layout_marginTop="32dp"
android:text="QC Station 2.1"
android:textColor="#color/black"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="211dp"
android:layout_height="68dp"
android:layout_marginStart="25dp"
android:layout_marginTop="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/date2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginEnd="50dp"
android:ems="6"
android:hint="DD/MM/YYYY"
android:inputType="date"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/time2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginEnd="50dp"
android:ems="6"
android:hint="HH.MM.SS"
android:inputType="time"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/next2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginBottom="30dp"
android:background="#drawable/button"
android:text="next"
android:textColor="#color/white"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/back2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginBottom="30dp"
android:background="#drawable/button"
android:text="Back"
android:textColor="#color/white"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Any support to solve this would be of great help
You should get values from savedInstanceState in onCreate() method. Someting like:
if (savedInstanceState!=null){
myBoolean1 = savedInstanceState.getBoolean("qc2_1cb1a");
myBoolean2 = savedInstanceState.getBoolean("qc2_1cb1b");
myBoolean3 = savedInstanceState.getBoolean("qc2_1cb1c");
myBoolean4 = savedInstanceState.getBoolean("qc2_1cb2a");
myBoolean5 = savedInstanceState.getBoolean("qc2_1cb2b");
myBoolean6 = savedInstanceState.getBoolean("qc2_1cb2c");
myBoolean7 = savedInstanceState.getBoolean("qc2_1cb3a");
myBoolean8 = savedInstanceState.getBoolean("qc2_1cb3b");
myBoolean9 = savedInstanceState.getBoolean("qc2_1cb3c");
}
and then assign it to your check boxes

Fix Overlapping Collapsing/Expanding Cards Java

I am using Android Studio to create 2 CardViews that expand and collapse on a mouseclick. However, when I expand a card, the card underneath does not get pushed down and still visible even if the card above is expanded and is an overlap problem.
How do i fix this so either the card below the one being expanded gets pushed down or that the card that is being expanded information is at the front?
Screenshot of how cards look before clicked:Before Clicked
screenshot of when when clicked: when clicked (as you can see the 'Steven Smith card is still being shown)
Note: I did look into using a Recycler View + adapter etc. but in each card I would want different layouts/images (vs all being consistent info like a contact in someones phone)
Here is my java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_engineering_page);
expandableView = findViewById(R.id.expandableView);
arrowBtn = findViewById(R.id.arrowBtn);
cardView = findViewById(R.id.cardView);
arrowBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandableView.getVisibility()==View.GONE){
TransitionManager.beginDelayedTransition(cardView, new AutoTransition());
expandableView.setVisibility(View.VISIBLE);
arrowBtn.setBackgroundResource(R.drawable.down_arrow);
} else {
TransitionManager.beginDelayedTransition(cardView, new AutoTransition());
expandableView.setVisibility(View.GONE);
arrowBtn.setBackgroundResource(R.drawable.android_icon);
}
}
});
expandableView2 = findViewById(R.id.expandableView2);
arrowBtn2 = findViewById(R.id.arrowBtn2);
cardView2 = findViewById(R.id.cardView2);
arrowBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandableView2.getVisibility()==View.GONE){
TransitionManager.beginDelayedTransition(cardView2, new AutoTransition());
expandableView2.setVisibility(View.VISIBLE);
arrowBtn2.setBackgroundResource(R.drawable.down_arrow);
} else {
TransitionManager.beginDelayedTransition(cardView2, new AutoTransition());
expandableView2.setVisibility(View.GONE);
arrowBtn2.setBackgroundResource(R.drawable.android_icon);
}
}
});
}
}
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".EngineeringPage"
android:background="#22325A">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:background="#C4C4C4"
app:logo="#drawable/image1"
app:title="">
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/engineeringTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="409dp"
android:layout_marginEnd="111dp"
android:layout_marginLeft="111dp"
android:layout_marginRight="111dp"
android:layout_marginStart="111dp"
android:layout_marginTop="61dp"
android:text="Engineering"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/CardView.Light"
android:layout_marginTop="98dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Section 1"
style="#style/TextAppearance.AppCompat.Title"
android:layout_marginStart="12dp" />
<Button
android:id="#+id/arrowBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="12dp"
android:background="#drawable/down_arrow"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/expandableView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:visibility="gone"
android:layout_marginTop="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<ImageView
android:id="#+id/phoneIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="52dp"
android:layout_marginStart="12dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/phoneNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="(999) 345 32 45"
android:layout_marginStart="32dp"
android:textColor="#000"
style="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintTop_toTopOf="#+id/phoneIcon"
app:layout_constraintStart_toEndOf="#id/phoneIcon"
app:layout_constraintBottom_toTopOf="#+id/phoneDesc"/>
<TextView
android:id="#+id/phoneDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Mobile"
android:layout_marginStart="32dp"
android:textColor="#8A000000"
style="#style/TextAppearance.AppCompat.Body1"
app:layout_constraintTop_toBottomOf="#+id/phoneNumber"
app:layout_constraintStart_toEndOf="#id/phoneIcon"
app:layout_constraintBottom_toBottomOf="#+id/phoneIcon"/>
<ImageView
android:id="#+id/mailIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginStart="12dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/phoneIcon"/>
<TextView
android:id="#+id/mailNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="workemail#gmail.com"
android:layout_marginStart="32dp"
android:textColor="#000"
style="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintTop_toTopOf="#+id/mailIcon"
app:layout_constraintStart_toEndOf="#id/mailIcon"
app:layout_constraintBottom_toTopOf="#+id/mailDesc"/>
<TextView
android:id="#+id/mailDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Work"
android:layout_marginStart="32dp"
android:textColor="#8A000000"
style="#style/TextAppearance.AppCompat.Body1"
app:layout_constraintTop_toBottomOf="#+id/mailNumber"
app:layout_constraintStart_toEndOf="#id/mailIcon"
app:layout_constraintBottom_toBottomOf="#+id/mailIcon"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
style="#style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="184dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<TextView
android:id="#+id/name2"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="Steven Smith" />
<Button
android:id="#+id/arrowBtn2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="12dp"
android:background="#drawable/down_arrow"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/expandableView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:paddingBottom="12dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/phoneIcon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:layout_marginTop="52dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/phoneNumber2"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="(999) 345 32 45"
android:textColor="#000"
app:layout_constraintBottom_toTopOf="#+id/phoneDesc2"
app:layout_constraintStart_toEndOf="#id/phoneIcon2"
app:layout_constraintTop_toTopOf="#+id/phoneIcon2" />
<TextView
android:id="#+id/phoneDesc2"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="Mobile"
android:textColor="#8A000000"
app:layout_constraintBottom_toBottomOf="#+id/phoneIcon2"
app:layout_constraintStart_toEndOf="#id/phoneIcon2"
app:layout_constraintTop_toBottomOf="#+id/phoneNumber2" />
<ImageView
android:id="#+id/mailIcon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:layout_marginTop="22dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/phoneIcon2" />
<TextView
android:id="#+id/mailNumber2"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="workemail#gmail.com"
android:textColor="#000"
app:layout_constraintBottom_toTopOf="#+id/mailDesc2"
app:layout_constraintStart_toEndOf="#id/mailIcon2"
app:layout_constraintTop_toTopOf="#+id/mailIcon2" />
<TextView
android:id="#+id/mailDesc2"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="Work"
android:textColor="#8A000000"
app:layout_constraintBottom_toBottomOf="#+id/mailIcon2"
app:layout_constraintStart_toEndOf="#id/mailIcon2"
app:layout_constraintTop_toBottomOf="#+id/mailNumber2" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
I have modified your sample to make it work correctly, you need to animated the parent layout for the two cardViews to make the animation smoth
java
final ConstraintLayout content = findViewById(R.id.content_layout);
final ConstraintLayout expandableView = findViewById(R.id.expandableView);
final Button arrowBtn = findViewById(R.id.arrowBtn);
arrowBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandableView.getVisibility() == View.GONE) {
TransitionManager.beginDelayedTransition(content, new AutoTransition());
expandableView.setVisibility(View.VISIBLE);
arrowBtn.setBackgroundResource(R.drawable.down_arrow);
} else {
TransitionManager.beginDelayedTransition(content, new AutoTransition());
expandableView.setVisibility(View.GONE);
arrowBtn.setBackgroundResource(R.drawable.android_icon);
}
}
});
final ConstraintLayout expandableView2 = findViewById(R.id.expandableView2);
final Button arrowBtn2 = findViewById(R.id.arrowBtn2);
arrowBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandableView2.getVisibility() == View.GONE) {
TransitionManager.beginDelayedTransition(content, new AutoTransition());
expandableView2.setVisibility(View.VISIBLE);
arrowBtn2.setBackgroundResource(R.drawable.down_arrow);
} else {
TransitionManager.beginDelayedTransition(content, new AutoTransition());
expandableView2.setVisibility(View.GONE);
arrowBtn2.setBackgroundResource(R.drawable.android_icon);
}
}
});
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:id="#+id/content_layout"
android:background="#22325A">
<android.support.v7.widget.Toolbar
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
android:background="#C4C4C4"
app:logo="#drawable/logo"
app:title="">
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="#style/CardView.Light"
android:layout_marginTop="98dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Section 1"
style="#style/TextAppearance.AppCompat.Title"
android:layout_marginStart="12dp" />
<Button
android:id="#+id/arrowBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="12dp"
android:background="#drawable/down_arrow"
app:layout_constraintTop_toTopOf="#id/name"
app:layout_constraintBottom_toBottomOf="#id/name"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/expandableView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="#id/arrowBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<ImageView
android:id="#+id/phoneIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:src="#drawable/logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/phoneNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="(999) 345 32 45"
android:layout_marginStart="32dp"
android:textColor="#000"
style="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintTop_toTopOf="#+id/phoneIcon"
app:layout_constraintStart_toEndOf="#id/phoneIcon"
app:layout_constraintBottom_toTopOf="#+id/phoneDesc"/>
<TextView
android:id="#+id/phoneDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Mobile"
app:layout_constraintStart_toStartOf="#id/phoneNumber"
android:textColor="#8A000000"
style="#style/TextAppearance.AppCompat.Body1"
app:layout_constraintTop_toBottomOf="#+id/phoneNumber"
app:layout_constraintStart_toEndOf="#id/phoneIcon"
app:layout_constraintBottom_toBottomOf="#+id/phoneIcon"/>
<ImageView
android:id="#+id/mailIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginStart="12dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/phoneIcon"/>
<TextView
android:id="#+id/mailNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="workemail#gmail.com"
app:layout_constraintStart_toStartOf="#id/phoneNumber"
android:textColor="#000"
style="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintTop_toTopOf="#+id/mailIcon"
app:layout_constraintStart_toEndOf="#id/mailIcon"
app:layout_constraintBottom_toTopOf="#+id/mailDesc"/>
<TextView
android:id="#+id/mailDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work"
android:textColor="#8A000000"
style="#style/TextAppearance.AppCompat.Body1"
app:layout_constraintStart_toStartOf="#id/phoneNumber"
app:layout_constraintTop_toBottomOf="#+id/mailNumber"
app:layout_constraintBottom_toBottomOf="#+id/mailIcon"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
style="#style/CardView.Light"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cardView">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/name2"
style="#style/TextAppearance.AppCompat.Title"
android:layout_marginStart="12dp"
android:text="Steven Smith" />
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="12dp"
android:background="#drawable/down_arrow"
app:layout_constraintTop_toTopOf="#id/name2"
app:layout_constraintBottom_toBottomOf="#id/name2"
app:layout_constraintEnd_toEndOf="parent"
android:id="#+id/arrowBtn2"
/>
<android.support.constraint.ConstraintLayout
android:id="#+id/expandableView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
app:layout_constraintTop_toBottomOf="#id/arrowBtn2"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/phoneIcon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:src="#drawable/logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/phoneNumber2"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="(999) 345 32 45"
android:textColor="#000"
app:layout_constraintBottom_toTopOf="#+id/phoneDesc2"
app:layout_constraintStart_toEndOf="#id/phoneIcon2"
app:layout_constraintTop_toTopOf="#+id/phoneIcon2" />
<TextView
android:id="#+id/phoneDesc2"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#id/phoneNumber2"
android:text="Mobile"
android:textColor="#8A000000"
app:layout_constraintBottom_toBottomOf="#+id/phoneIcon2"
app:layout_constraintStart_toEndOf="#id/phoneIcon2"
app:layout_constraintTop_toBottomOf="#+id/phoneNumber2" />
<ImageView
android:id="#+id/mailIcon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:layout_marginTop="22dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/phoneIcon2" />
<TextView
android:id="#+id/mailNumber2"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#id/phoneNumber2"
android:text="workemail#gmail.com"
android:textColor="#000"
app:layout_constraintBottom_toTopOf="#+id/mailDesc2"
app:layout_constraintStart_toEndOf="#id/mailIcon2"
app:layout_constraintTop_toTopOf="#+id/mailIcon2" />
<TextView
android:id="#+id/mailDesc2"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work"
android:textColor="#8A000000"
app:layout_constraintStart_toStartOf="#id/phoneNumber2"
app:layout_constraintBottom_toBottomOf="#+id/mailIcon2"
app:layout_constraintStart_toEndOf="#id/mailIcon2"
app:layout_constraintTop_toBottomOf="#+id/mailNumber2" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Just put everything inside a Linear Layout orientation vertical or Inside other layouts align your bottom and tops correctly with android:layout_below="#+id/YOUR ID"

Why is my Text Change Listener not working?

Why is my "addTextChangedListener" on android not working? Here is my code:
activity_ticketing.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".TicketingMain.Ticketing">
<!-- Main Content -->
<!-- HEADING -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/button_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:layout_marginStart="28dp"
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/icon_exit" />
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:minHeight="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView_route"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:text="Tacloban - Tanawan"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.285" />
<TextView
android:id="#+id/textView_vehicle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:text="ZZZ999"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/textView_route"
app:layout_constraintStart_toStartOf="#+id/textView_route"
app:layout_constraintTop_toBottomOf="#+id/textView_route" />
<TextView
android:id="#+id/textView_driver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="Driver Name"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/textView_route"
app:layout_constraintTop_toTopOf="#+id/textView_route" />
<TextView
android:id="#+id/textView_conductor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:text="Conductor Name"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/textView_vehicle"
app:layout_constraintTop_toBottomOf="#+id/textView_driver" />
<TextView
android:id="#+id/textView_numberOfPassengers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="0"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/textView3"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="Passengers"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<!-- HEADING END -->
<!-- ROUTEPOST ORIGIN -->
<!-- ROUTEPOST ORIGIN END -->
<!-- MAIN -->
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/materialCardView">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- spinner_direction -->
<Spinner
android:id="#+id/spinner_direction"
android:layout_width="200dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<!-- textView_currentPost -->
<TextView
android:id="#+id/textView_currentPost"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#drawable/border_box"
android:text="Current"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/button_prev"
app:layout_constraintEnd_toStartOf="#+id/button_next"
app:layout_constraintStart_toEndOf="#+id/button_prev"
app:layout_constraintTop_toTopOf="#+id/button_prev" />
<!-- button_next -->
<com.google.android.material.button.MaterialButton
android:id="#+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:textAlignment="center"
app:icon="#drawable/ic_right_arrow"
app:iconGravity="textStart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView_currentPost"
app:layout_constraintTop_toBottomOf="#+id/spinner_direction" />
<!-- button_prev -->
<com.google.android.material.button.MaterialButton
android:id="#+id/button_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textStyle="bold"
app:icon="#drawable/ic_left"
app:iconGravity="textStart"
app:layout_constraintEnd_toStartOf="#+id/textView_currentPost"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner_direction" />
<!-- button_postCurrentRoutePost -->
<!-- button_logout -->
<!-- textView = "Direction" -->
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Direction"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- textView_currentLocation -->
<TextView
android:id="#+id/textView_currentLocation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="No current location set"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="#+id/button_next"
app:layout_constraintStart_toStartOf="#+id/button_prev"
app:layout_constraintTop_toBottomOf="#+id/textView_currentPost" />
<!-- button_nextDestination -->
<com.google.android.material.button.MaterialButton
android:id="#+id/button_nextDestination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAlignment="center"
app:icon="#drawable/ic_right_arrow"
app:iconGravity="textStart"
app:layout_constraintEnd_toEndOf="#+id/textView_currentLocation"
app:layout_constraintStart_toEndOf="#+id/textView_currentPost"
app:layout_constraintTop_toBottomOf="#+id/textView_currentLocation" />
<!-- button_prevDestination -->
<com.google.android.material.button.MaterialButton
android:id="#+id/button_prevDestination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:textAlignment="center"
android:textStyle="bold"
app:icon="#drawable/ic_left"
app:iconGravity="textStart"
app:layout_constraintEnd_toStartOf="#+id/textView_currentPost"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView_currentLocation" />
<!-- textView_currentPostDestination -->
<TextView
android:id="#+id/textView_currentPostDestination"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#drawable/border_box"
android:text="Current"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"
android:clickable="true"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="#+id/button_prevDestination"
app:layout_constraintEnd_toStartOf="#+id/button_nextDestination"
app:layout_constraintStart_toEndOf="#+id/button_prevDestination"
app:layout_constraintTop_toTopOf="#+id/button_nextDestination" />
<com.google.android.material.button.MaterialButton
android:id="#+id/button_postDestination"
style="#style/Widget.MaterialComponents.Button"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_marginTop="8dp"
android:text="Submit Destination"
android:textAlignment="center"
app:iconGravity="textStart"
app:layout_constraintEnd_toEndOf="#+id/button_nextDestination"
app:layout_constraintStart_toStartOf="#+id/textView_currentPostDestination"
app:layout_constraintTop_toBottomOf="#+id/textView_currentPostDestination" />
<!-- RECEIPT -->
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView3"
android:layout_width="347dp"
android:layout_height="332dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_postDestination">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Destination"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView_receiptDestination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Tacloban - Tanawan"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/textView4"
app:layout_constraintTop_toTopOf="#+id/textView4" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Passengers"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="#+id/textView4"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textView_receiptPassengers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="0"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/textView6"
app:layout_constraintTop_toTopOf="#+id/textView6" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Fare"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="#+id/textView6"
app:layout_constraintTop_toBottomOf="#+id/textView6" />
<TextView
android:id="#+id/textView_receiptFare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="#+id/textView_receiptPassengers"
app:layout_constraintTop_toTopOf="#+id/textView8" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Total Due"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="#+id/textView8"
app:layout_constraintTop_toBottomOf="#+id/textView8" />
<TextView
android:id="#+id/textView_receiptTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="#+id/textView_receiptFare"
app:layout_constraintTop_toTopOf="#+id/textView10" />
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView4"
android:layout_width="108dp"
android:layout_height="97dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="#+id/textView_receiptTotal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView_receiptDestination">
<ImageView
android:id="#+id/imageView_qrCode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
</com.google.android.material.card.MaterialCardView>
<RadioGroup
android:id="#+id/radioGroup_discount"
android:layout_width="205dp"
android:layout_height="106dp"
android:layout_marginEnd="140dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView10"
app:layout_constraintVertical_bias="0.615">
<RadioButton
android:id="#+id/radioButton_noDiscount"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:checked="true"
android:text="No Discount"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView10" />
<RadioButton
android:id="#+id/radioButton_ssDiscount"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:text="Senior/Student"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView10" />
<RadioButton
android:id="#+id/radioButton_spDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:text="Special Discount"
android:textSize="12sp"
android:textStyle="bold" />
</RadioGroup>
<Button
android:id="#+id/button_accept"
android:layout_width="108dp"
android:layout_height="74dp"
android:text="Accept"
app:backgroundTint="#009688"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/materialCardView4"
app:layout_constraintStart_toStartOf="#+id/materialCardView4"
app:layout_constraintTop_toBottomOf="#+id/materialCardView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<!-- <SearchView-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="0dp"-->
<!-- android:id="#+id/search"-->
<!-- app:layout_constraintBottom_toBottomOf="#+id/button_postDestination"-->
<!-- app:layout_constraintEnd_toEndOf="#+id/button_prevDestination"-->
<!-- app:layout_constraintStart_toStartOf="#+id/button_prevDestination"-->
<!-- app:layout_constraintTop_toTopOf="#+id/button_postDestination" />-->
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/editText"
app:layout_constraintBottom_toBottomOf="#+id/button_postDestination"
app:layout_constraintEnd_toEndOf="#+id/button_prevDestination"
app:layout_constraintStart_toStartOf="#+id/button_prevDestination"
app:layout_constraintTop_toBottomOf="#+id/button_prevDestination">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textKm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Km" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<!-- MAIN END -->
<!-- MAIN -->
<!-- HEADING -->
<!-- LOGOUT -->
<!-- LOGOUT END -->
</androidx.constraintlayout.widget.ConstraintLayout>
Ticketing.java
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ticketing);
editText = (EditText) findViewById(R.id.textKm);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.e("TEXT", "A");
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("TEXT", "A");
}
#Override
public void afterTextChanged(Editable s) {
Log.e("TEXT", "A");
}
});
}
I don't get any error. On debugging, it doesn't stop on any of the overrides.
You can set in tow way
First Way:
EditText et_auto_complete_edit_text;
et_auto_complete_edit_text = findViewById(R.id.et_auto_complete_edit_text);
et_auto_complete_edit_text.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
s.toString()
}
});
XML:
<EditText
android:id="#+id/et_auto_complete_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="8"
android:background="#color/gray_light_EA"
android:drawableRight="#mipmap/ic_search_new"
android:drawablePadding="#dimen/margin5dp"
android:hint="Search here"
android:inputType="text"
android:maxLines="1"
android:padding="10dp"
android:textColor="#color/black"
android:textColorHint="#color/grey"
android:visibility="visible" />
Second Way :
SearchView search;
search = findViewById(R.id.search);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
fetchData(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//collectionProductsListAdapter.getFilter().filter(newText);
return false;
}
});
search.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
search.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
return false;
}
});
XML:
<androidx.appcompat.widget.SearchView
xmlns:n2="http://schemas.android.com/tools"
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
n2:searchIcon="#mipmap/ic_et_search" />
Hope this may help you first way is more easy.
Try changing it to EditText
<android.support.design.widget.TextInputLayout
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Km"
app:layout_constraintEnd_toEndOf="#+id/button_prevDestination"
app:layout_constraintStart_toStartOf="#+id/button_prevDestination"
app:layout_constraintTop_toBottomOf="#+id/textView_currentPostDestination">
<EditText
android:id="#+id/textKm"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
Working fine in my code.

Android TextView OnClick-Event doesn't work

I have to program a TicTacToe app:
This is the code of the activity_main.xml:
(It's a simple 3x3 field of TextViews on a black background)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="#000000"
android:screenOrientation="portrait"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:layout_width="294dp"
android:layout_height="286dp"
android:background="#000000"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<TextView
android:id="#+id/tictactoe_center_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_left"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_center_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_center_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_top_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:enabled="false"
android:focusable="true"
android:onClick="click_top_left"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="0"
tools:text="#string/game_playersymbol_x" />
<TextView
android:id="#+id/tictactoe_top_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tictactoe_top_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tictactoe_bottom_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_left"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="2" />
<TextView
android:id="#+id/tictactoe_bottom_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="2" />
<TextView
android:id="#+id/tictactoe_bottom_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="2" />
</androidx.gridlayout.widget.GridLayout>
<Button
android:id="#+id/button"
android:layout_width="213dp"
android:layout_height="61dp"
android:layout_marginBottom="56dp"
android:background="#drawable/roundbutton"
android:text="#string/menu_play_singleplayer"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#979797"
android:textSize="50sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.066" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the code of the MainActivity.java:
package com.example.tictactoe;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void click_top_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_left)); }
public void click_top_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_center)); }
public void click_top_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_right)); }
public void click_center_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_left)); }
public void click_center_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_center)); }
public void click_center_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_right)); }
public void click_bottom_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_left)); }
public void click_bottom_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_center)); }
public void click_bottom_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_right)); }
public void click_action(TextView tv) {
if ((counter%2)==0)
tv.setText("O");
else
tv.setText("X");
counter++;
}
}
I've set the android:clickable to true.
I've set a onclick listener.
I simply cannot find my mistake...
I would appreciate any kind of help!
You have set to all the TextViews:
android:enabled="false"
Why?
Change it to:
android:enabled="true"
first give an id to your textview then Define a Sample like Textview textview find it in java and use setOnClickListener() in onCreate method just like below.It will definitely works.
textveiw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { }});

Android ... My application doesn't launch?

So, I am trying to do an application which gives a user the ability to keep track of the score and number of fouls of two different teams playing football
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="Team A"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_a_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<TextView
android:id="#+id/team_a_foul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addGoalForTeamA"
android:text="GOAL" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addFoulForTeamA"
android:text="FOUL" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="Team B"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_b_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<TextView
android:id="#+id/team_b_foul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addGoalForTeamB"
android:text="GOAL" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addFoulForTeamB"
android:text="FOUL" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="Reset"
android:text="Reset" />
and java code
package com.example.android.scorekeeper;
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int goalTeamA = 0;
int foulTeamA = 0;
int goalTeamB = 0;
int foulTeamB = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addGoalForTeamA(View view) {
goalTeamA = +1;
displayGoalTeamA(goalTeamA);
}
public void addFoulForTeamA(View view) {
foulTeamA = +1;
displayFoulTeamA(foulTeamA);
}
public void addGoalForTeamB(View view) {
goalTeamB = +1;
displayGoalTeamB(goalTeamB);
}
public void addFoulForTeamB(View view) {
foulTeamB = +1;
displayFoulTeamB(foulTeamB);
}
public void Reset(View view) {
goalTeamA = 0;
foulTeamA = 0;
goalTeamB = 0;
foulTeamB = 0;
displayGoalTeamA(goalTeamA);
displayFoulTeamA(foulTeamA);
displayGoalTeamB(goalTeamB);
displayFoulTeamB(foulTeamB);
}
public void displayGoalTeamA(int score) {
TextView scoreView = findViewById(R.id.team_a_goal);
scoreView.setText(String.valueOf(score));
}
public void displayFoulTeamA(int score) {
TextView scoreView = findViewById(R.id.team_a_foul);
scoreView.setText(String.valueOf(score));
}
public void displayGoalTeamB(int score) {
TextView scoreView = findViewById(R.id.team_b_goal);
scoreView.setText(String.valueOf(score));
}
public void displayFoulTeamB(int score) {
TextView scoreView = findViewById(R.id.team_b_foul);
scoreView.setText(String.valueOf(score));
} }
The code seems fine to me and Android Studio doesn't report any errors but it did't launch on my android
make sure you have added your activity to your manifest file
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Categories