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) { }});
Related
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>
If i click on login button my username and password is same as condition but get login failed in Toast Message. Insert correct username and password compiler going to else part. I don't know what is the problem. I am stuck from 4 hours. If you know please solve i am a newbie.
Here down is my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Username=(TextView) findViewById(R.id.Username);
TextView password=(TextView) findViewById(R.id.password);
Button loginbtn=(Button) findViewById(R.id.Button);
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Username.getText().toString().equals("ADMIN") && password.getText().toString().equals("ADMIN")) {
Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(getApplicationContext(), "Login Faild!!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity.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"
android:background="#drawable/backs"
tools:context=".MainActivity">
<TextView
android:id="#+id/Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Sign in"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:background="#FFFFFF"
android:drawableLeft="#drawable/ic_baseline_person_outline_24"
android:drawablePadding="8dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:minHeight="48dp"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Username" />
<EditText
android:id="#+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:background="#FFFFFF"
android:drawableLeft="#drawable/ic_baseline_lock_24"
android:drawablePadding="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edittext" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Forgot password"
android:textColor="#000000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.837"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/password" />
<Button
android:id="#+id/Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="28dp"
android:layout_marginRight="30dp"
android:background="#drawable/bg"
android:text="#string/login"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="288dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="or sign in with"
android:textColor="#BDB9B9"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/other"
app:layout_constraintVertical_bias="1.0">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="20dp"
android:src="#drawable/google" />
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:src="#drawable/facebook" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="20dp"
android:src="#drawable/twitter" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT
You are referring to the R.id.Username component which is the SIGN UP text and you are casting the fields as TextViews instead of EditTexts.
Change your variables to:
EditText Username=(EditText) findViewById(R.id.edittext);
EditText password=(EditText) findViewById(R.id.password);
You are getting the text from the wrong reference, Change the below code to later.
TextView Username=(TextView) findViewById(R.id.Username);
to
TextView Username=(TextView) findViewById(R.id.edittext);
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.
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>
This is kind of beginner/noob question, but I want to use InputActivity.java and activity_input.xml as the one that the user use to input data. The code for the input are as shown below:
InputActivity.java
package com.lukmanyahoo.denny.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class InputActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
}
//Send data to HasilActivity.java
}
activity_input.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"
tools:context="com.lukmanyahoo.denny.myapplication.InputActivity">
<EditText
android:id="#+id/editText"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="636dp"
android:layout_marginStart="16dp"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/umur"
android:inputType="number"
app:layout_constraintBottom_toTopOf="#+id/editText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="DuplicateIds,NotSibling,RtlHardcoded"
app:layout_constraintVertical_bias="0.251" />
<EditText
android:id="#+id/editText2"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/daerah"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:ignore="MissingConstraints,RtlHardcoded" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="#string/jurusan"
android:textSize="30sp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="MissingConstraints"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="#+id/editText2"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:ignore="RtlHardcoded">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/informatika" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/sistem_informasi"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="178dp" />
</RadioGroup>
<Button
android:id="#+id/button3"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/kirim"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioGroup"
tools:ignore="RtlHardcoded" />
</android.support.constraint.ConstraintLayout>
The code for the output part of my application are shown below:
HasilActivity.java
package com.lukmanyahoo.denny.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class HasilActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hasil);
}
}
activity_hasil.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"
tools:context="com.lukmanyahoo.denny.myapplication.HasilActivity">
<TableLayout
android:layout_width="568dp"
android:layout_height="791dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/umur"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
tools:layout_editor_absoluteX="58dp"
tools:layout_editor_absoluteY="169dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/daerah"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/jurusan"
android:textStyle="bold" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>
Thanks for taking time answering this. I am new in giving question here.
In your InputActivity
public class InputActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
}
//Send data to HasilActivity.java
Intent intent = new Intent(this, HasilActivity.class);
// put your data here
intent.putExtra("value_one", editText.getText().toString().trim());
intent.putExtra("value_two", editText2.getText().toString().trim());
startActivity(intent)
}
and in your HasilActivity
public class HasilActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hasil);
String value = getIntent().getStringExtra("value_one");
String value2 = getIntent().getStringExtra("value_two");
}
}
that is :)