I have generated following code:
package com.example.bmi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void myButtonListenerMethod() {
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText heightText = (EditText)
findViewById(R.id.heightInput);
String heightStr = heightText.getText().toString();
double height = Double.parseDouble(heightStr);
final EditText weightText = (EditText)
findViewById(R.id.weightInput);
String weightStr = weightText.getText().toString();
double weight = Double.parseDouble(weightStr);
double BMI = (weight)/(height*height);
{
final EditText BMIResult = (EditText)
findViewById(R.id.BMIResult);
BMIResult.setText(Double.toString(BMI));}
String BMI_Cat;
{
if (BMI < 15) {
BMI_Cat = "Very severely underweight";
} else if (BMI < 16) {
BMI_Cat = "Severely underweight";
} else if (BMI < 18.5) {
BMI_Cat = "Underweight";
} else if (BMI < 25) {
BMI_Cat = "Normal";
} else if (BMI < 30) {
BMI_Cat = "Overweight";
} else if (BMI < 35) {
BMI_Cat = "Obese Class 1 - Moderately Obese";
} else if (BMI < 40) {
BMI_Cat = "Obese Class 2 - Severely Obese";
} else {
BMI_Cat = "Obese Class 3 - Very Severely Obese";
}
}
final TextView BMICategory = (TextView)
findViewById(R.id.BMICategory);
BMICategory.setText(BMI_Cat);
}
});
}
}
Now I am getting following error:
Error message shown in Android Studio
My xml file code is here:
<?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:minHeight="129dp"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="BMI Calculator"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginLeft="34dp"
android:layout_marginEnd="38dp"
android:layout_marginRight="38dp"
android:text="Enter your weight (kg):"
android:textSize="18sp"
app:layout_constraintBaseline_toBaselineOf="#+id/weightInput"
app:layout_constraintEnd_toStartOf="#+id/weightInput"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginLeft="34dp"
android:layout_marginEnd="45dp"
android:layout_marginRight="45dp"
android:text="Enter your height (m):"
android:textSize="18sp"
app:layout_constraintBaseline_toBaselineOf="#+id/heightInput"
app:layout_constraintEnd_toStartOf="#+id/heightInput"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginLeft="34dp"
android:layout_marginEnd="114dp"
android:layout_marginRight="114dp"
android:layout_marginBottom="56dp"
android:text="Your BMI:"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toStartOf="#+id/BMIResult"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/BMICategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="77dp"
android:text="BMI category"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:text="Calculate My BMI"
app:layout_constraintBottom_toTopOf="#+id/BMICategory"
app:layout_constraintStart_toStartOf="#+id/BMICategory" />
<EditText
android:id="#+id/weightInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="154dp"
android:layout_marginEnd="41dp"
android:layout_marginRight="41dp"
android:layout_marginBottom="155dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="80"
app:layout_constraintBottom_toBottomOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/heightInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginEnd="41dp"
android:layout_marginRight="41dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="1.80"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/weightInput" />
<EditText
android:id="#+id/BMIResult"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="<><><><>"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toBottomOf="#+id/heightInput" />
</android.support.constraint.ConstraintLayout>
I need help to resolve this issue. I have tried other source codes also but I was not able to resolve the issues to handle those one. As I am newbee in the field of programming therefore, it will be more helpful for me to resolve the above issue through relevant possible examples.
Thanking in advance.
The issue is error: not well-formed (invalid token).
Reason is android:text="<><><><>"
Solution is: <string name="name"><![CDATA[<><><><>]]></string>
pass this in your text view
Related
I had created a Tic Tac Toe game,which created a black and red image user have to create three pair of sets,two user can play,any one whoever made it first will win .I created a Play again button for restarting the game again and again ,but after clicking that button, i am getting out of the app. Can you guys help me where i am going wrong .Below i am giving my code.
MainActivity.java
package com.example.tictac;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//0:cross,1:red,2:empty
int[] gameState = {2,2,2,2,2,2,2,2,2};
int[][] winningPositions = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
int activePlayer =0;
boolean gameActive = true;
public void dropIn(View view){
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if(gameState[tappedCounter] == 2 && gameActive) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1500);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.cross);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.redd);
activePlayer = 0;
}
counter.animate().translationYBy(1500).rotation(3600).setDuration(300);
for (int[] winningPositions : winningPositions) {
if (gameState[winningPositions[0]] == gameState[winningPositions[1]] && gameState[winningPositions[1]] == gameState[winningPositions[2]] && gameState[winningPositions[0]] != 2) {
//someone win
gameActive = false;
String winner = "";
if (activePlayer == 1) {
winner = "Black";
} else {
winner = "Red";
}
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
winnerTextView.setText(winner + " has won");
playAgainButton.setVisibility(View.VISIBLE);
winnerTextView.setVisibility(View.VISIBLE);
}
}
}
}
public void okayPlay(View view){
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
playAgainButton.setVisibility(View.INVISIBLE);
winnerTextView.setVisibility(View.INVISIBLE);
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i=0; i<gridLayout.getChildCount() ;i++){
ImageView counter =(ImageView) gridLayout.getChildAt(i);
counter.setImageDrawable(null);
}
for(int i=0;i<gameState.length;i++){
gameState[i]=2;
}
activePlayer =0;
gameActive = true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Content.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=".MainActivity" >
<androidx.gridlayout.widget.GridLayout
android:id="#+id/gridLayout"
android:layout_width="368dp"
android:layout_height="368dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/bd"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.296"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498"
app:rowCount="3">
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="17dp"
android:contentDescription="TODO"
android:onClick="dropIn"
android:tag="0"
app:layout_column="0"
app:layout_row="0" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="1"
app:layout_column="1"
app:layout_row="0"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="2"
app:layout_column="2"
app:layout_row="0"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="17dp"
android:onClick="dropIn"
android:tag="3"
app:layout_column="0"
app:layout_row="1"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="4"
app:layout_column="1"
app:layout_row="1"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="5"
app:layout_column="2"
app:layout_row="1"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="17dp"
android:onClick="dropIn"
android:tag="6"
app:layout_column="0"
app:layout_row="2"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="7"
app:layout_column="1"
app:layout_row="2"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="8"
app:layout_column="2"
app:layout_row="2"
tools:ignore="SpeakableTextPresentCheck" />
</androidx.gridlayout.widget.GridLayout>
<TextView
android:id="#+id/winnerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="177dp"
android:text="TextView"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/gridLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.349" />
<Button
android:id="#+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="164dp"
android:layout_marginEnd="153dp"
android:onClick="okayPlay"
android:text="#string/play_again"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/winnerTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your app is crashing because in your okayPlay(View view) method you're trying to cast
androidx.gridlayout.widget.GridLayout
to
android.widget.GridLayout
In your Activity, change your android.widget.GridLayout import to
androidx.gridlayout.widget.GridLayout
I know it have been asked many times but this time i tried many things such as reinstall android studio, update android studio, change the graphics and so on,but it occured everytime.
Here I will also provide my coding if any mistakes were made that may have any relation with my emulator got killed.
activity_main.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=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="162dp"
android:layout_marginTop="16dp"
android:text="Login"
android:textSize="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textview"
android:layout_centerHorizontal="true"
android:layout_marginStart="168dp"
android:layout_marginTop="20dp"
android:text="PSIS"
android:textColor="#000000"
android:textSize="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview" />
<EditText
android:id="#+id/editText"
android:layout_width="212dp"
android:layout_height="46dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="100dp"
android:layout_marginTop="76dp"
android:focusable="true"
android:hint="Enter Name"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:layout_alignRight="#+id/editText"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="100dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#ffff299f"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="41dp"
android:layout_marginTop="11dp"
android:text="Attempts Left:"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:layout_toEndOf="#+id/textview"
android:layout_toRightOf="#+id/textview"
android:text="New Text"
android:textSize="25dp"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/button2" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="113dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="30dp"
android:layout_toStartOf="#+id/textview"
android:layout_toLeftOf="#+id/textview"
android:text="login"
app:layout_constraintEnd_toStartOf="#+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="30dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="88dp"
android:layout_toEndOf="#+id/textview"
android:layout_toRightOf="#+id/textview"
android:text="Cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.task;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static String EXTRA_TEXT;
Button b1, b2;
EditText ed1, ed2;
TextView tx1;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
ed1 = (EditText) findViewById(R.id.editText);
ed2 = (EditText) findViewById(R.id.editText2);
b2 = (Button) findViewById(R.id.button2);
tx1 = (TextView) findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ed1.getText().toString().equals("mohdnoor") &&
ed2.getText().toString().equals("123456")) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
EditText editText1 = (EditText) findViewById(R.id.editText);
String text = editText1.getText().toString();
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra(EXTRA_TEXT, text);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
activity_second.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">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="272dp"
android:text="!!WELCOME!!"
android:textSize="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="109dp"
android:layout_height="34dp"
android:layout_marginTop="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview" />
</androidx.constraintlayout.widget.ConstraintLayout>
secondActivity.java
package com.example.task;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class secondActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b1 = (Button) findViewById(R.id.button2);
Toast.makeText(getApplicationContext(),
"Successful!", Toast.LENGTH_SHORT).show();
Intent intent = getIntent();
String text = intent.getStringExtra(MainActivity.EXTRA_TEXT);
TextView textView1 = (TextView) findViewById(R.id.textview);
textView1.setText(text);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(secondActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
I'm pretty much a beginner still and I've tried to make an App to ease my work in the Lab. It's just some basic calculation. I want to make an App that is compatible to the most devices, in case its useful information. Even though the App seems to Build successfully, as soon as it starts on my Device it just crashes immediatelly. Files used are listed below. I tried all the hints Android Studio gave me with no result. App still crashed.
Would be very thankful for some hints and constructive feeback!
Main Activity.java
package com.e.concalc;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText protein_sp;
private EditText salt_sp;
private EditText protein_st;
private EditText salt_st;
private EditText volume_sp;
private TextView tv_resultH2O;
private TextView tv_resultSalt;
private TextView tv_resultProtein;
private Button button1;
public MainActivity(TextView tv_resultH2O, TextView tv_resultSalt, TextView tv_resultProtein, Button button1) {
this.tv_resultH2O = tv_resultH2O;
this.tv_resultSalt = tv_resultSalt;
this.tv_resultProtein = tv_resultProtein;
this.button1 = button1;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protein_sp = findViewById(R.id.edit1);
protein_st = findViewById(R.id.edit2);
salt_sp = findViewById(R.id.edit3);
salt_st = findViewById(R.id.edit4);
volume_sp = findViewById(R.id.edit5);
button1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
makeCalculations();
}
});
}
private void makeCalculations() {
double p_sp = Double.valueOf(protein_sp.getText().toString());
double p_st = Double.valueOf(protein_st.getText().toString());
double s_sp = Double.valueOf(salt_sp.getText().toString());
double s_st = Double.valueOf(salt_st.getText().toString());
double v_sp = Double.valueOf(volume_sp.getText().toString());
double resultH2O;
double resultSalt;
double resultProtein;
resultProtein = p_sp * v_sp / p_st;
resultSalt = s_sp * v_sp / s_st;
resultH2O = v_sp - resultProtein - resultSalt;
tv_resultH2O.setText(Double.toString(resultH2O));
tv_resultSalt.setText(Double.toString(resultSalt));
tv_resultProtein.setText(Double.toString(resultProtein));
}
}
activity_main.xml - Layout
<?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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Protein1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.133"
app:layout_constraintVertical_bias="0.070" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Protein2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.77"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.070" />
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Salt1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Salt2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/SampleVolume"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.35" />
<EditText
android:id="#+id/edit1"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text1"
app:layout_constraintTop_toBottomOf="#+id/text1"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit2"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text2"
app:layout_constraintTop_toBottomOf="#+id/text2"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit4"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text4"
app:layout_constraintTop_toBottomOf="#+id/text4"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit3"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text3"
app:layout_constraintTop_toBottomOf="#+id/text3"
android:importantForAutofill="no" />
<EditText
android:id="#+id/edit5"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text5"
app:layout_constraintTop_toBottomOf="#+id/text5"
android:importantForAutofill="no" />
<Button
android:id="#+id/button1"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginBottom="128dp"
android:text="#string/button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:text="#string/button2"
app:layout_constraintBottom_toBottomOf="#+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.745"
app:layout_constraintStart_toEndOf="#+id/button1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/button1" />
<TextView
android:id="#+id/tv_resultH2O"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintVertical_bias="0.6"/>
<TextView
android:id="#+id/tv_resultSalt"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.6"/>
<TextView
android:id="#+id/tv_resultProtein"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.9"
app:layout_constraintVertical_bias="0.6"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You forgot to findViewById the button1.So in onCreate method, before button1.setOnClickListener.... add this line:
button1 = findViewById(R.id.button1);
Remove your custom constructor.
Activities must have a no-argument constructor that Android uses to create your class and when you define any override then the no-argument constructor is no longer automatically created for you in Java...
So i've make some change's in my app (it was about nesting layouts - My root layout, was linear layout,i wanted to change it to relative layout,i remember,it was something wrong with lines like xlmns:tools etc. I've copy those lines from my previous app,but then i've got an error that something is missing,so i google it,and somebody wrote about delete .idea and .gradle folders so i did,and now,my avd showing me "old content" just app before some changes. How i can solve it? The application that i've trying to make,it's just an typical clicker at case of my course. It's about to counting score of two teams. Screen with an issue https://imgur.com/a/U9gianT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button
android:layout_gravity="center"
android:id="#+id/reset_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/resetBTN"
android:layout_marginTop="15dp"
android:onClick="reset_score"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_a"
android:textSize="14sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"
android:layout_marginTop="16dp"/>
<TextView
android:layout_gravity="center"
android:id="#+id/team_a_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_a_points"
android:layout_marginTop="16dp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"
android:textSize="56sp"/>
<Button
android:layout_gravity="center"
android:id="#+id/three_points_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_3_points_btn"
android:layout_marginTop="24dp"
android:onClick="addThreePointsA"/>
<Button
android:layout_gravity="center"
android:id="#+id/two_points_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_2_points_btn"
android:layout_marginTop="24dp"
android:onClick="addTwoPointsA"/>
<Button
android:layout_gravity="center"
android:id="#+id/free_throw_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/free_throw_btn"
android:layout_marginTop="24dp"
android:onClick="addOnePointA"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="340dp"
android:background="#android:color/darker_gray"
android:layout_marginTop="15dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_marginTop="16dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_b"
android:textSize="14sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"/>
<TextView
android:layout_gravity="center"
android:id="#+id/team_b_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_b_score"
android:layout_marginTop="16dp"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"/>
<Button
android:layout_gravity="center"
android:id="#+id/three_points_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_3_points_btn"
android:layout_marginTop="24dp"
android:onClick="addThreePointsB"/>
<Button
android:layout_gravity="center"
android:id="#+id/two_points_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_2_points_btn"
android:layout_marginTop="24dp"
android:onClick="addTwoPointsB"/>
<Button
android:layout_gravity="center"
android:id="#+id/free_throw_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/free_throw_btn"
android:layout_marginTop="24dp"
android:onClick="addOnePointB"/>
</LinearLayout>
package com.example.kacper.courtcounter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int team_a_score = 0;
int team_b_score = 0;
private void display_a_score(int score){
TextView scoreView = (TextView) findViewById(R.id.team_a_points);
scoreView.setText(String.valueOf(score));
}
private void display_b_score(int score){
TextView scoreView = (TextView) findViewById(R.id.team_b_points);
scoreView.setText(String.valueOf(score));
}
public void addThreePointsA(View view) {
team_a_score = team_a_score+3;
display_a_score(team_a_score);
}
public void addTwoPointsA(View view) {
team_a_score = team_a_score+2;
display_a_score(team_a_score);
}
public void addOnePointA(View view) {
team_a_score = team_a_score+1;
display_a_score(team_a_score);
}
public void addThreePointsB(View view) {
team_b_score = team_b_score+3;
display_b_score(team_b_score);
}
public void addTwoPointsB(View view) {
team_b_score = team_b_score+2;
display_b_score(team_b_score);
}
public void addOnePointB(View view) {
team_b_score = team_b_score+1;
display_b_score(team_b_score);
}
public void reset_score(View view) {
team_a_score = 0;
team_b_score = 0;
display_a_score(team_a_score);
display_b_score(team_b_score);
}
}
Just Clean and rebuild your project and then uninstall app from device and then run again in device it will works fine (If you really make changes in code)
I did my best to solve the problem and I hope someone would be able to help me finding a solution.
I delcared the following layout for my activity:
<?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:layout_gravity="center"
android:background="#android:color/holo_orange_light"
android:visibility="visible"
tools:context="com.example.youssef.mylocation.Main2Activity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="#android:color/holo_orange_dark"
android:text="#string/menumessage"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText1"
android:layout_width="217dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="128dp"
android:ems="10"
android:hint="#string/phone_number"
android:inputType="phone"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="196dp"
android:ems="10"
android:hint="#string/address"
android:inputType="textPersonName"
android:textColor="#android:color/holo_orange_dark"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="#string/place_name"
android:inputType="textPersonName"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="340dp"
android:textColor="#color/Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="24dp"
android:textColor="#color/Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<Button
android:id="#+id/CancelBtn"
android:layout_width="88dp"
android:layout_height="48dp"
android:layout_marginBottom="40dp"
android:layout_marginStart="32dp"
android:background="#android:color/holo_red_dark"
android:text="#string/cancel"
android:onClick="Cancelonbuttonclickfunc"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="#string/city"
android:inputType="textPersonName"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<TextView
android:id="#+id/textView6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="144dp"
android:text="Required !"
android:textColor="#android:color/holo_red_dark"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/editText1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/AddBtn"
android:layout_width="124dp"
android:layout_height="60dp"
android:layout_marginStart="96dp"
android:layout_marginTop="464dp"
android:text="Add"
android:background="#color/Color"
android:onClick="buttonClickFunction"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/CancelBtn"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The related java code is the following:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
TextView textView;
TextView laltitude;
TextView longitude;
Bundle bundle;
Double lal;
Double longt;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView=(TextView)findViewById(R.id.textView6);
editText = (EditText) findViewById(R.id.editText1);
laltitude =(TextView)findViewById(R.id.textView2);
longitude =(TextView)findViewById(R.id.textView3);
bundle = getIntent().getExtras();
lal = bundle.getDouble("laltitude");
longt = bundle.getDouble("longitude");
laltitude.setText("laltitude : " + lal.toString());
longitude.setText("longitude : " + longt.toString());
//hhhhhhhhhhhhhhhhh going back to main activity
// CancelButton.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
//
// }
// });
//hhhhhhhhhhhhhhh
// AddButton.setOnClickListener({});
// if (editText.getText() == null)
// textView.setVisibility(View.VISIBLE);
// else
// textView.setVisibility(View.INVISIBLE);
// });
}
public void buttonClickFunction(View view) {
Toast.makeText(getApplicationContext(),editText.getText().toString(),Toast.LENGTH_LONG);
if (editText.getText().toString() == "")
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
public void Cancelonbuttonclickfunc(View view) {
startActivity(new Intent(Main2Activity.this, MainActivity.class));
}
}
The problem is that only one button seems to work (cancelbutton) but the
but the add button does not seem work.
I did declare the button and use the findViewById method but no luck so far.
In your button click method you're comparing strings using the == operator which tests for reference equality and not value equality. This is why your textView's visibility never get set to VISIBLE. To check for value equality you should use equals method instead, like this:
public void buttonClickFunction(View view) {
Toast.makeText(getApplicationContext(),editText.getText().toString(),Toast.LENGTH_LONG).show();
if (editText.getText().toString().equals(""))
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
Note that you also forgot to put .show() at the end of your Toast.
Try this
mBtnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String text= edittext.getText().toString().trim();
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
if (text.length() <= 0) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.INVISIBLE);
}
}
});