why is emulator killed whenever i tried running the device? - android

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

Related

how to solve at com.example.dbdemo.MainActivity$2.onClick(MainActivity.java:61)?

Please help me out. I have tried everything but am unable to solve this. The app keeps crashing Because of activity main:61 what is wrong with the code?
MainActivity.java
package com.example.dbdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button btnLogin;
TextView tvRegister;
EditText edtEmail,edtPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin = findViewById(R.id.btnLogin);
tvRegister = findViewById(R.id.tvRegister);
edtEmail = findViewById(R.id.edtEmail);
edtPassword= findViewById(R.id.edtPassword);
SharedPreferenceConfig sp = new SharedPreferenceConfig(getApplicationContext());
if(sp.readLoginStatus()){
startActivity(new Intent(MainActivity.this,HomeActivity.class));
}
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String uname = edtEmail.getText().toString();
String upass = edtPassword.getText().toString();
if(uname.equals(getResources().getString(R.string.uname))
&& upass.equals(getResources().getString(R.string.upass))){
startActivity(new Intent(MainActivity.this,HomeActivity.class));
}
else {
Toast.makeText(MainActivity.this, "Incorrect Username or Password", Toast.LENGTH_SHORT).show();
edtEmail.setText("");
edtPassword.setText("");
}
}
});
tvRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent int1 = new Intent( MainActivity.this,RegistrationActivity.class);
startActivity(int1);
}
});
}
}
activity_main.xml for ActivityMain.java
activity_main.xml
<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="#3C3C3C"
tools:context=".MainActivity">
<EditText
android:id="#+id/edtEmail"
android:layout_width="265dp"
android:layout_height="48dp"
android:ems="10"
android:hint="#string/enter_email_id"
android:inputType="textEmailAddress"
android:textColor="#FFFFFF"
android:textColorHighlight="#D6B5B5"
android:textColorHint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.417"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.243"
android:autofillHints="" />
<EditText
android:id="#+id/edtPassword"
android:layout_width="263dp"
android:layout_height="50dp"
android:ems="10"
android:hint="#string/enter_password"
android:inputType="textPassword"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.425"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.393"
android:autofillHints="" />
<TextView
android:id="#+id/tvRegister"
android:layout_width="229dp"
android:layout_height="47dp"
android:text="#string/register_yourself"
android:textAlignment="center"
android:textColor="#FFEB3B"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.912"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.603" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is RegistrationActivity that is intended to show when I click on on Registr yourself
RegistrationActivity.java
package com.example.dbdemo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class RegistrationActivity extends AppCompatActivity {
Button btnRegister;
EditText edtEmail2 , edtPassword2 , edtPassword3;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
btnRegister = findViewById(R.id.btnRegister);
edtEmail2 = findViewById(R.id.edtEmail2);
edtPassword2= findViewById(R.id.edtpassword2);
edtPassword3= findViewById(R.id.edtPassword3);
SharedPreferenceConfig sp = new SharedPreferenceConfig(RegistrationActivity.this);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String uname2 = edtEmail2.getText().toString();
String upass2 = edtPassword2.getText().toString();
String upass3 = edtPassword3.getText().toString();
}
});
}
}
activity_register.xml
<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="#373737">
<EditText
android:id="#+id/edtEmail2"
android:layout_width="218dp"
android:layout_height="58dp"
android:ems="10"
android:hint="#string/enter_emailn_id"
android:inputType="textEmailAddress"
android:textColorHint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.471"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.142"
android:autofillHints="" />
<EditText
android:id="#+id/edtpassword2"
android:layout_width="222dp"
android:layout_height="54dp"
android:ems="10"
android:hint="#string/enter_password"
android:inputType="numberPassword"
android:textColorHint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3"
android:autofillHints="" />
<Button
android:id="#+id/btnRegister"
android:layout_width="142dp"
android:layout_height="56dp"
android:text="#string/register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.663" />
<EditText
android:id="#+id/edtPassword3"
android:layout_width="223dp"
android:layout_height="53dp"
android:ems="10"
android:hint="#string/reenter_password"
android:inputType="numberPassword"
android:textColorHint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.457"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.467"
android:importantForAutofill="no" />
</androidx.constraintlayout.widget.ConstraintLayout>
It shows that errors in MainActivity:61 but I can't find it
Error
at com.example.dbdemo.MainActivity$2.onClick(MainActivity.java:61)
if you are getting this error
Unable to find explicit activity class {.RegistrationActivity}; have you declared this activity in your AndroidManifest.xml?
then add this in your AndroidManifest.xml file
<activity android:name=".RegistrationActivity"/>
under the application tag <Application>

One of the Android Button work while the other does not

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

Implementing formulas into each value in the spinner which will change the result everytime the user change selected value

I am creating a diet app for myself and I do not know how to transfer result from one calculation which was done in a different activity and use it in another one.
To make things more complicated, I want my spinner in MoreLooseWeightDetails.class to have a list of different diet types which have different fat, proteins and carb ratio. Every time the user choose different type it should automatically change the ratio.
This is where the calculation is being done and has to be trnasfered to MoreLooseWeightDetails.class
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class LooseWeight extends AppCompatActivity {
TextView TotalCal;
EditText numb2;
Spinner ActLvl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loose_weight);
numb2 = (EditText) findViewById(R.id.value1);
ActLvl = (Spinner) findViewById(R.id.value2);
TotalCal = (TextView)findViewById(R.id.resultDisplay);
final Spinner ActLvl = (Spinner)findViewById(R.id.value2);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(LooseWeight.this, android.R.layout.simple_expandable_list_item_1,getResources().getStringArray(R.array.lvl));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ActLvl.setAdapter(myAdapter);
Button calcBtn = (Button)findViewById(R.id.result);
calcBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
float numb3 = Float.parseFloat(numb2.getText().toString());
float a = numb3 * 24;
float b = a * Float.parseFloat(ActLvl.getSelectedItem().toString());
float c = b - 500;
TotalCal.setText(Float.toString(c));
}
});
Button extra = (Button)findViewById(R.id.advance);
extra.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),MoreLooseWeightDetails.class);
startActivity(intent);
}
});
}
}
Here is the MoreLooseWeightDetails.class
package com.fitup.fit_up;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MoreLooseWeightDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_loose_weight_details);
}
}
Here is the MoreLooseWeightDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_more_loose_weight_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.fitup.fit_up.MoreLooseWeightDetails">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/Title3"
tools:text="More"
android:textSize="36sp" />
<TextView
android:text="Select Type of Diet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Title3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp"
android:id="#+id/Info3"
android:textSize="30sp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Info3"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:id="#+id/spinner" />
<TextView
android:text="You Should Eat :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/Info4"
android:textSize="30sp"
android:layout_below="#+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Protein (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit1"
android:textSize="24sp"
android:layout_below="#+id/Info4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="78dp" />
<TextView
android:text="Fat (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit3"
android:textSize="24sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/result1"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignLeft="#+id/result2"
android:layout_alignStart="#+id/result2" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/unit2"
android:id="#+id/result2"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/result3"
android:layout_alignStart="#+id/result3" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit2"
android:id="#+id/result3"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/unit2"
android:layout_toEndOf="#+id/unit2" />
<TextView
android:text="Carbohydrates (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/unit2"
android:textSize="24sp"
android:layout_below="#+id/unit1"
android:layout_alignRight="#+id/unit1"
android:layout_alignEnd="#+id/unit1" />
</RelativeLayout>

How to switch from simple activity to drawer activity?

I am new to android. I want to know how to switch from a simple activity to drawer activity. I read that there are 2 ways to do it.
The first is Intent i =new Intent("com.example.signin");
the second is Intent i =new Intent(MainActivity.this,signin.class);
I tried both. But in the 1st case, my layout which contains toolbar, drawer and the button inside is appearing perfect. But buttons functionality is not working. in 2nd case, buttons functionality is working but layout is disturbed.
Can anyone tell me solution for this problem?I'm using Android Studio.
here is my code:
MainActivity
package com.example.asus1.tlogin;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button signInBtn;
private Button signUpBtn;
private Button submit;
private EditText mUsername, mPassword;
private Layout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signInBtn = (Button) findViewById(R.id.signin);
signUpBtn = (Button) findViewById(R.id.signup);
submit = (Button) findViewById(R.id.submit);
mUsername = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.password);
//String userName = mUsername.getText().toString();
// String password = mPassword.getText().toString();
signUpBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.asus1.tlogin.SignUpActivity");
startActivity(intent);
}
});
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mUsername.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter username..", Toast.LENGTH_SHORT).show();
mPassword.setText("");
mUsername.setText("");
} else if (mPassword.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter password..", Toast.LENGTH_SHORT).show();
mPassword.setText("");
mUsername.setText("");
} else if (mUsername.getText().toString().equals("gaurav") && mPassword.getText().toString().equals("gaurav")) {
Toast.makeText(getApplicationContext(), "Login Successful..", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,ContentDrawer.class);
startActivity(intent);
setContentView(R.layout.content_drawer);
} else {
Toast.makeText(getApplicationContext(), "Wrong username or password..", Toast.LENGTH_LONG).show();
mPassword.setText("");
mUsername.setText("");
}
}
});
}
}
ContentDrawer.java:
package com.example.asus1.tlogin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
public class ContentDrawer extends AppCompatActivity {
private Button createBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_drawer);
//Toolbar toolbar = (Toolbar)findViewById(R.id.draw);
createBtn = (Button)findViewById(R.id.createBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(getApplicationContext(),CreateGroup.class);
startActivity(intent1);
}
});
}
}
ContentDrawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.asus1.tlogin.DrawerActivity"
tools:showIn="#layout/app_bar_drawer"
android:orientation="vertical"
android:weightSum="1"
android:background="#ffffff">
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Create your personal profile"
android:background="#d31313"
android:textColor="#ffffff"
android:textAllCaps="false"
android:id="#+id/createBtn"
android:clickable="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:orientation="horizontal"
android:background="#f29494"
android:clickable="true"
android:id="#+id/linear">
<Button
android:layout_width="180dp"
android:layout_height="match_parent"
android:text="Join group"
android:id="#+id/joinGroup"
android:background="#984848"
android:textAllCaps="false"
android:clickable="true" />
<Button
android:layout_width="195dp"
android:layout_height="match_parent"
android:text="Setup a group"
android:id="#+id/button2"
android:background="#4e7db2"
android:textAllCaps="false"
android:clickable="true" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Setup an eventk"
android:id="#+id/eventBtn"
android:background="#e7ed21"
android:textAllCaps="false"
android:clickable="true" />
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Notifications"
android:id="#+id/button4"
android:textAllCaps="false"
android:background="#8b9ddb" />
</LinearLayout>
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:com.facebook.widget.LoginButtonandroid="http://schemas.android.com/apk/res-auto"
tools:context="com.example.asus1.tlogin.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="#drawable/roomates1"
android:id="#+id/imageView" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:id="#+id/linearLayout">
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/signup"
android:background="#0000"
android:text="SIGN UP"
android:textAlignment="gravity" />
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:id="#+id/signin"
android:layout_alignBottom="#+id/imageView"
android:layout_toRightOf="#+id/signup"
android:layout_toEndOf="#+id/signup"
android:background="#0000"
android:text=" SIGN IN"
android:textAlignment="textStart"
android:textColor="#fefefe"
android:textStyle="bold" />
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<EditText
android:layout_width="272dp"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/username"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:hint="Username"
android:textAlignment="center" />
<EditText
android:layout_width="274dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/password"
android:layout_gravity="center_horizontal"
android:hint="Password"
android:textAlignment="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIGN IN"
android:id="#+id/submit"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#drawable/roundbutton"/>
</LinearLayout>
</RelativeLayout>
layout is disturbed
I'm assuming what you mean by that is that you are completely replacing the layout of the MainActivity?
That is fixable by just not doing so.
Intent intent = new Intent(MainActivity.this,ContentDrawer.class);
startActivity(intent);
// Remove this line
/* setContentView(R.layout.content_drawer); */
Other than that, it seems you should be able to start the other Activity just fine.

Query on a Text view

I want to display the following output:
Name : Sangeetha
Usn : 4AL09IS025
Address : Anderi West, Mubai.
Name and usn are displaying properly. But my Address is not displaying. What's the problem. Here's my code. Can anyone tell me what's the problem?MainActivity.java is the my main activity class.
package com.example.assignment3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button btn;
EditText etext1;
EditText etext2;
EditText etext3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext1 = (EditText) findViewById(R.id.edit_name);
etext2 = (EditText) findViewById(R.id.edit_usn);
etext3 = (EditText) findViewById(R.id.edit_add);
btn = (Button) findViewById(R.id.submit);
/** Called when the user clicks the Submit button */
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this, Details.class);
intent.putExtra("name", etext1.getText().toString());
intent.putExtra("usn", etext2.getText().toString());
intent.putExtra("address", etext3.getText().toString());
startActivity(intent);
}
}
Display.java is my Display activity to receive the intent sent by Main Activity.java
package com.example.assignment3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class Details extends ActionBarActivity {
TextView text1;
TextView text2;
TextView text3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
text1=(TextView)findViewById(R.id.new_name);
text2=(TextView)findViewById(R.id.new_usn);
text3=(TextView)findViewById(R.id.new_add);
Intent intent=getIntent();
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("add");
text1.setText(Name);
text2.setText(Usn);
text3.setText(Address);
}
}
Two layout files. First one is for the first activity(input) and 2nd one is for 2nd activity(output).main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="20.3dp"
android:text="#string/name" />
<EditText
android:id="#+id/edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="85dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_name" />
<TextView
android:id="#+id/usn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="30dp"
android:paddingLeft="32dp"
android:text="#string/usn" />
<EditText
android:id="#+id/edit_usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_name"
android:layout_marginLeft="85dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_usn" />
<TextView
android:id="#+id/add"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="40dp"
android:text="#string/add" />
<EditText
android:id="#+id/edit_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_usn"
android:layout_marginLeft="85dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_add" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_centerHorizontal="true"
android:text="#string/submit"/>
</RelativeLayout>
activity_details.java's layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="20.3dp"
android:text="#string/name"
android:textStyle="bold" />
<TextView
android:id="#+id/new_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="85dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/usn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="20dp"
android:paddingLeft="32dp"
android:text="#string/usn"
android:textStyle="bold" />
<TextView
android:id="#+id/new_usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/new_name"
android:layout_marginLeft="85dp"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/add"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="30dp"
android:text="#string/add"
android:textStyle="bold" />
<TextView
android:id="#+id/new_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/new_usn"
android:layout_marginLeft="85dp"
android:layout_marginTop="30dp"
android:singleLine="false" />
</RelativeLayout>
try that ;
Define same string for address
Intent intent=getIntent();
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("address");
text1.setText(Name);
text2.setText(Usn);
text3.setText(Address);
See lines intent.putExtra("address", etext3.getText().toString()); and String Address=intent.getStringExtra("add"); You need to change the add to address to make it work

Categories

Resources