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);
}
}
});
Related
I have a few cardviews in a recyclerview. When I click on one I want it to show a different layout.
This is the 'template' that each card should have when I click on it.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollview_sam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/reading_bg"
android:fillViewport="true"
tools:context=".ScrollviewSam"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="188dp"
android:layout_marginStart="15dp"
android:layout_marginTop="56dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/sam" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="56dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/name"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="32dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/about"
android:textColor="#color/white"
android:textSize="35sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/background"
android:textColor="#color/white"
android:textSize="35sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="17dp"
android:fontFamily="#font/sackers_gothic_light"
android:lineHeight="20dp"
android:paddingLeft="17sp"
android:paddingRight="17sp"
android:text="#string/about_sam"
android:textAlignment="textStart"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="17dp"
android:fontFamily="#font/sackers_gothic_light"
android:lineHeight="20sp"
android:paddingLeft="17sp"
android:paddingRight="17sp"
android:text="#string/background_sam1"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView19" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="17dp"
android:fontFamily="#font/sackers_gothic_light"
android:lineHeight="20sp"
android:paddingLeft="17sp"
android:paddingRight="17sp"
android:text="#string/background_sam2"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/name_sam"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="88dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/gender_male"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView10"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="88dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/gender"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="116dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/hair_color"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView12"
android:layout_width="56dp"
android:layout_height="21dp"
android:layout_marginStart="8dp"
android:layout_marginTop="115dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/color_brown"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView11"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="148dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/eye_color"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="176dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/cast"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView16"
android:layout_width="101dp"
android:layout_height="39dp"
android:layout_marginStart="8dp"
android:layout_marginTop="176dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:lineHeight="20dp"
android:text="#string/cast_norman"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="148dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/color_blue"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="224dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/status_alive"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="224dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/status"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="#+id/like"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginStart="30dp"
android:layout_marginTop="56dp"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_baseline_favorite_border_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
This is my RecyclerViewAdapter.
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import com.example.myapplication.ScrollviewSam;
import java.util.List;
public class CharacterAdapter extends RecyclerView.Adapter<CharacterAdapter.ViewHolder> {
List<String> name;
List<Integer> images;
LayoutInflater layoutInflater;
public CharacterAdapter( Context context, List<String> name, List<Integer> images) {
this.name = name;
this.images = images;
this.layoutInflater = LayoutInflater.from(context);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.character_grid_layout,parent,false);
return new ViewHolder(view);
}
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(name.get(position));
holder.image.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return name.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView name;
ImageView image;
public ViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
image = itemView.findViewById(R.id.image);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getAdapterPosition() == 0 ){
Intent intent = new Intent(v.getContext(), ScrollviewSam.class);
v.getContext().startActivity(intent);
}
}
});
}
}
}
As you can see I set a clickListener for the item. This is where it gets confusing for me since I created a new activity for the view but that will get super repetitive with creating the activity and the view over and over again.
You are almost there. Instead of checking the adapterPosition, and starting an Activity, you want to pass your data to the ViewHolder, and then to the Activity.
You can use onBindViewHo\lder() for the first, and [Intents][1] for the second part.
// In your adapter's onBindViewHolder
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bindDataToYourViewHolder(name.get(position), images.get(position))
}
The above, will set the name and image in your ViewHolder. You need to adjust your ViewHolder slightly:
public ViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
image = itemView.findViewById(R.id.image);
}
public void bindDataToYourViewHolder(String yourName, int yourImage){
name.setText(yourName);
image.setImageResource(yourImage);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ScrollviewSam.class);
intent.putExtra("name", yourName)
// You can use yourImage here as well
v.getContext().startActivity(intent);
}
});
}
This way, you can pass name.getText() to your ScrollviewSam Activity.
Inside ScrollviewSam.onCreate() you can grab the extra by doing:
// In ScrollviewSam Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String name = getIntent().getStringExtra("name");
}
Besides Strings, you can pass Booleans, Integer and more through Bundles. You can even pass around whole objects if they implement the Parcelable interface. But mostly, basic primitives should get you going.
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...
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
I am trying to develop a simple app to find the day for a given date
xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.dayfinder.MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/bg" />
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
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:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Date:"
android:textColor="#android:color/black"
android:textSize="35dp" />
<TextView
android:id="#+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Month:"
android:textColor="#android:color/black"
android:textSize="35dp" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Year:"
android:textColor="#android:color/black"
android:textSize="35dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/editText1"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="38dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Date"
android:inputType="date"
android:maxLength="2"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="40dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Month!"
android:inputType="date"
android:maxLength="2"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="41dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Year!"
android:inputType="date"
android:maxLength="4"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_bel="#android:color/white"
android:onClick="chow="#id/l1"
android:layout_centerHorizontal="true"
android:text="Find"
android:layout_margin="24dp"
android:backgroundeck"/>
<TextView
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_margin="48dp"
android:fontFamily="sans-serif-condensed"
android:text="Answer"
android:textColor="#android:color/black"
android:textSize="40dp" />
</RelativeLayout>
MainActivity.java
package com.example.android.dayfinder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import source.dayfinder;
public class MainActivity extends AppCompatActivity {
int e1;
String s1,s2,s3;
private EditText medit1;
private EditText medit2;
private EditText medit3;
private TextView mText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button mbutton=(Button)findViewById(R.id.button);
public void check(View view) {
medit1=(EditText)findViewById(R.id.editText1);
medit2=(EditText)findViewById(R.id.editText2);
medit3=(EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.answer);
s1=medit1.getText().toString();
s2=medit2.getText().toString();
s3=medit3.getText().toString();
dayfinder f=new dayfinder();
f.finder(s1,s2,s3);
mText.setText(f.finder(s1,s2,s3));
}
}
package dayfinder.java (which i use in mainactivity.java)
package source;
public class dayfinder {
public String finder(String a1, String a2, String a3) {
int q, w, x;
String e1;
q = Integer.parseInt(a1);
if ((q < 1000) || (q > 4000)) {
e1 = "error";
return e1;
}
else
{
e1="null";
return e1;
}
}
}
Reformat your MainActivity.java like below and if you have any your error after this again please update your question
Follow naming conventions
Took dayfinder object creation to onCreate to avoid creating new objects every time your check method gets called
package com.example.android.dayfinder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import source.dayfinder;
public class MainActivity extends AppCompatActivity {
int e1;
String s1,s2,s3;
private EditText medit1;
private EditText medit2;
private EditText medit3;
private TextView mText ;
private Button mbutton;
private dayfinder f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
f=new dayfinder();
}
public void initializeViews(){
mbutton=(Button)findViewById(R.id.button);
medit1=(EditText)findViewById(R.id.editText1);
medit2=(EditText)findViewById(R.id.editText2);
medit3=(EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.answer);
}
public void check(View view) {
s1=medit1.getText().toString();
s2=medit2.getText().toString();
s3=medit3.getText().toString();
f.finder(s1,s2,s3);
mText.setText(f.finder(s1,s2,s3));
}
}