Saving edit to listview android studio - java

I have a list view of restaurants with 4 different strings: Name, Address, description and tags.
When I click a restaurant it leads me to my detailsActivity.java where I let user edit the name, address, description and tags, and then user can save the edited info to be stored in the list view.
So far when I click save it will only save the data for Name... Address, description and tag strings are not saved with new information.
Here is my code :
public class DetailsActivity extends AppCompatActivity {
private int pos;
//RATE RESTAURANT BUTTON
RatingBar ratingbar1;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent i = getIntent();
String name = i.getStringExtra("name");
final String address = i.getStringExtra("address");
String description = i.getStringExtra("description");
String tags = i.getStringExtra("tags");
pos = i.getIntExtra("position", -1); //-1 means not set
EditText ename = findViewById(R.id.editName);
EditText eaddress = findViewById(R.id.editAddress);
EditText edescription = findViewById(R.id.editDescription);
EditText etags = findViewById(R.id.editTags);
ename.setText(name);
eaddress.setText(address);
edescription.setText(description);
etags.setText(tags);
findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = getIntent();
EditText ename = findViewById(R.id.editName);
EditText eaddress = findViewById(R.id.editAddress);
EditText edescription = findViewById(R.id.editDescription);
EditText etags = findViewById(R.id.editTags);
String name = ename.getText().toString();
String address = eaddress.getText().toString();
String description = edescription.getText().toString();
String tags = etags.getText().toString();
i.putExtra("name", name);
i.putExtra("address", address);
i.putExtra("description", description);
i.putExtra("tags", tags);
i.putExtra("position", pos);
setResult(RESULT_OK, i);
finish();
}
});
Here are my xml files in case they are needed:
activity_details.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailsActivity"
tools:layout_editor_absoluteY="25dp">
<EditText
android:id="#+id/editName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="Restaurant Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editAddress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Restaurant Address (Street #, City) (***)-***-****"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editName" />
<EditText
android:id="#+id/editDescription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Restaurant Description"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editAddress" />
<EditText
android:id="#+id/editTags"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:ems="10"
android:hint="tags"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editDescription" />
<Button
android:id="#+id/btnSave"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTags" />
<Button
android:id="#+id/btnMap"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnSave" />
<Button
android:id="#+id/btnBack"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnMap" />
<RatingBar
android:id="#+id/rating1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnBack"/>
<Button
android:id="#+id/btnRate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Rate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rating1" />
Here is activity_main.xml where the listview is located:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/btnabout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="512dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="about us"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="New Item"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="Add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/editItem" />
<ListView
android:id="#+id/itemList"
android:layout_width="match_parent"
android:layout_height="433dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toTopOf="#+id/editItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" >
</ListView>
</android.support.constraint.ConstraintLayout>
and finally mainactivity.java which includes my onActivityResult function:
public class MainActivity extends AppCompatActivity {
private ArrayList<Item> items;
private ItemsAdapter itemsAdapter;
private ListView lvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvItems = findViewById(R.id.itemList);
items = new ArrayList<>();
items.add(new Item ("Mcdonalds", "108 queen st", "come get a junior chicken", "fast food"));
items.add(new Item("Pizza Pizza", "9 moms st", "wonderful pizza made by mom", "pizza"));
itemsAdapter = new ItemsAdapter(this, R.layout.row_layout, items);
lvItems.setAdapter(itemsAdapter);
Button btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//ADD NEW ITEM TO LIST
EditText et = findViewById(R.id.editItem);
String text = et.getText().toString();
if(!text.isEmpty())
{
itemsAdapter.add(new Item(text, "default address", "default desc", "default tag"));
et.setText("");
}
}
});
//ABOUT PAGE REDIRECT
Button btn1 = findViewById(R.id.btnabout);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,aboutmembers.class);
startActivity(i);
}
});
//INTERACTION WITH LIST ITEMS (LONG CLICK TO DELETE)
lvItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final int pos = position;
new AlertDialog.Builder(view.getContext()).setTitle("Warning!")
.setMessage("Do you want to remove this item?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
items.remove(pos);
itemsAdapter.notifyDataSetChanged();
}
}).show();
return true;
}
});
//ADD NEW LISTENER TO LIST
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(view.getContext(), DetailsActivity.class);
i.putExtra("position", position);
i.putExtra("name", items.get(position).getName());
i.putExtra("address", items.get(position).getAddress());
i.putExtra("description", items.get(position).getDescription());
i.putExtra("tags", items.get(position).getTags());
startActivityForResult(i, EDIT_ITEM);
}
});
}
//DEFINE OUR CALL OF EDIT ITEM
public static final int EDIT_ITEM = 1;
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(requestCode==EDIT_ITEM)
{
if(resultCode==RESULT_OK)
{
int pos = data.getIntExtra("position", -1);
if(pos!=-1)
{
String name = data.getStringExtra("name");
String address = data.getStringExtra("address");
String description = data.getStringExtra("description");
String tags = data.getStringExtra("tags");
Item item = items.get(pos);
item.setName(name);
items.set(pos, item);
itemsAdapter.notifyDataSetChanged();
}
}
}
}

in mainactivity.java in the onActivityResult function set the address/description/tags:
item.setAddress((address));
item.setDescription(description);
item.setTags(tags);

Related

Sending Data from Alert Dialog to New Activity

I am new to Android development and I am trying to send data via EditText to TextView from an alert dialog inflated in the MainActivity to a different activity (NewGame.java). I have tried intent.putExtra (see below) however I am getting null pointer exceptions as it seems I cannot access the views from the dialog layout xml file.
I know there are a few other ways to create an alert dialog so perhaps this way is not best for what i am trying to achieve?
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button newGame, prevGames, newSquad, savedSquads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newGame = findViewById(R.id.buttonNewGame);
newGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater= getLayoutInflater();
builder.setView(R.layout.new_game_dialog);
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
EditText enterTeam1, enterTeam2, enterDate, enterTime, enterLocation;
enterTeam1 = myView.findViewById(R.id.etTeam1);
enterTeam2 = myView.findViewById(R.id.etTeam2);
enterDate = myView.findViewById(R.id.etDate);
enterTime = myView.findViewById(R.id.etTime);
enterLocation = myView.findViewById(R.id.etLocation);
builder.setTitle("New Game")
.setCancelable(true)
.setPositiveButton("Start", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(MainActivity.this, NewGame.class);
intent.putExtra("TEAM1", enterTeam1.getText().toString());
intent.putExtra("TEAM2", enterTeam2.getText().toString());
intent.putExtra("DATE", enterDate.getText().toString());
intent.putExtra("TIME", enterTime.getText().toString());
intent.putExtra("LOCATION", enterLocation.getText().toString());
startActivity(intent);
}
})
.setNegativeButton("Cancel", null);
builder.create();
builder.show();
}
});
NewGame.java
public class NewGame extends AppCompatActivity {
TextView team1, team2, date, time, location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
team1 = findViewById(R.id.tvTeam1);
team2 = findViewById(R.id.tvTeam2);
date = findViewById(R.id.tvDate);
time = findViewById(R.id.tvTime);
location = findViewById(R.id.tvLocation);
team1.setText(getIntent().getStringExtra("TEAM1"));
team2.setText(getIntent().getStringExtra("TEAM2"));
date.setText(getIntent().getStringExtra("DATE"));
time.setText(getIntent().getStringExtra("TIME"));
location.setText(getIntent().getStringExtra("LOCATION"));
}
}
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvNewGame"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:textColor="#color/black"
android:textSize="30dp" />
<EditText
android:id="#+id/etTeam1"
android:layout_width="211dp"
android:layout_height="53dp"
android:ems="10"
android:hint="Team 1"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etTeam2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Team 2"
android:inputType="textPersonName"/>
<EditText
android:id="#+id/etDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Date"
android:inputType="date" />
<EditText
android:id="#+id/etTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Time"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Location"
android:inputType="textPersonName"/>
</LinearLayout>
activity_new_game.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=".NewGame">
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="time"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvNewGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:textColor="#color/black"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.085" />
<TextView
android:id="#+id/tvTeam1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="team1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.192" />
</androidx.constraintlayout.widget.ConstraintLayout>
I believe you are having this problem because you are actually inflating 2 views. And referencing the wrong one when you use findById.
LayoutInflater inflater= getLayoutInflater();
builder.setView(R.layout.new_game_dialog);
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
should look like this
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
builder.setView(myView);

How to handle multiple intents on same activity?

Okay, I simply want to have multiple intents passing data into a single activity which will summarise all the user inputs into a nice easy to read form.
This is for a mock up school application form.
The following code works fine but will produce two activities: One showing the student's gender and one showing the students name. How to get it to display both in the same activity? Pulling my hair out over this!!
XML of studentInfoActivity:
<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:background="#android:color/background_light"
tools:context=".studentInfoActivity">
<TextView
android:id="#+id/textView4"
android:layout_width="143dp"
android:layout_height="42dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center"
android:text="Student Full Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="143dp"
android:layout_height="42dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center"
android:text="Gender"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout" />
XML of reviewScreenActivity:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".reviewScreenActivity">
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="SUMMARY:"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/summaryStudentFamilyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="150dp"
android:layout_marginRight="150dp"
android:text="The student's name is: "
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView15" />
<TextView
android:id="#+id/summaryStudentGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="150dp"
android:layout_marginRight="150dp"
android:text="The student's gender is: "
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView15" />
<TextView
android:id="#+id/studentFullNameReviewScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/summaryStudentFamilyName"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/studentGenderReviewScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/summaryStudentGender"
app:layout_constraintTop_toBottomOf="#+id/studentFullNameReviewScreen" />
</android.support.constraint.ConstraintLayout>
Java code of studentInfoActivity:
public class studentInfoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_info);
//TAKEN FROM https://stackoverflow.com/questions/13377361/how-to-create-a-drop-down-list.
//AUTHOR: NICOLAS TYLER
//get the spinner from the xml.
Spinner dropdown = findViewById(R.id.genderSpinner);
//create a list of items for the spinner.
String[] items = new String[]{"Male", "Female", "Other"};
//create an adapter to describe how the items are displayed, adapters are used in several places in android.
//There are multiple variations of this, but this is the basic variant.
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
//set the spinners adapter to the previously created one.
dropdown.setAdapter(adapter);
//TAKEN FROM https://stackoverflow.com/questions/13377361/how-to-create-a-drop-down-list.
//AUTHOR: NICOLAS TYLER
//get the spinner from the xml.
Spinner atsiDropdown = findViewById(R.id.atisSpinner);
//create a list of items for the spinner.
String[] atsiItems = new String[]{"Yes", "No"};
//create an adapter to describe how the items are displayed, adapters are used in several places in android.
//There are multiple variations of this, but this is the basic variant.
ArrayAdapter<String> atsiAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, atsiItems);
//set the spinners adapter to the previously created one.
atsiDropdown.setAdapter(atsiAdapter);
}
public void createCaregiverInfoSplash(View v){
//Send Student Full Name
EditText studentFullNameInput = (EditText) findViewById(R.id.studentFullNameInput);
String studentFullName = studentFullNameInput.getText().toString();
Intent studentIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
studentIntent.putExtra("studentFullName", studentFullName);
startActivity(studentIntent);
//Send Student Gender
Spinner genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
String studentGender = genderSpinner.getSelectedItem().toString();
Intent genderIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
genderIntent.putExtra("studentGender", studentGender);
startActivity(genderIntent);
}
}
Java code of reviewScreenActivity:
public class reviewScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_review_screen);
//Student Full Name reciever
Bundle sfnBundle = getIntent().getExtras();
String studentFullName = sfnBundle.getString("studentFullName");
if (studentFullName != null) {
TextView sfn = (TextView) findViewById(R.id.studentFullNameReviewScreen);
sfn.setText(studentFullName);
}
else {
TextView sfn = (TextView) findViewById(R.id.studentFullNameReviewScreen);
sfn.setText("Not showing right!");
}
//Student Gender Reciever
Bundle sgBundle = getIntent().getExtras();
String studentGender = sgBundle.getString("studentGender");
TextView sg = (TextView)findViewById(R.id.studentGenderReviewScreen);
sg.setText(studentGender);
}
}
Well you have a lot of mistakes such as incorrect class and method names.
You can pass as many as you want extra in one intent.
public void createCaregiverInfoSplash(View v){
//Send Student Full Name
EditText studentFullNameInput = (EditText) findViewById(R.id.studentFullNameInput);
String studentFullName = studentFullNameInput.getText().toString();
Spinner genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
String studentGender = genderSpinner.getSelectedItem().toString();
Intent reviewIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
reviewIntent.putExtra("studentFullName", studentFullName);
reviewIntent.putExtra("studentGender", studentGender);
startActivity(studentIntent);
}

com.rengwuxian.materialedittext.MaterialEditText cannot be cast to android.view.ViewGroup

My app is always crashing because of this error. It says:
com.rengwuxian.materialedittext.MaterialEditText cannot be cast to android.view.ViewGroup.
I'm a beginner who's looking for some code on YouTube, but I can't figure it out.
<ImageView
android:id="#+id/imageView2"
android:layout_width="243dp"
android:layout_height="166dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/textView"
android:layout_width="102dp"
android:layout_height="24dp"
android:layout_marginStart="8dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="8dp"
android:text="USERNAME"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.259"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/usernameTIL"
android:layout_width="251dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.527"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="268dp"
android:layout_height="36dp"
android:hint="username" />
</com.rengwuxian.materialedittext.MaterialEditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="PASSWORD"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.255"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/usernameTIL"
app:layout_constraintVertical_bias="0.018" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/passwordTIL2"
android:layout_width="253dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.535"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<CheckBox
android:id="#+id/rememberCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Remember Me?"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/passwordTIL2" />
<Button
android:id="#+id/signInBTN"
android:layout_width="145dp"
android:layout_height="49dp"
android:layout_marginTop="28dp"
android:background="#color/signInBTN"
android:text="SIGN IN"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rememberCB" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Dont't have an Account?"
android:textColor="#android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.323"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signInBTN" />
My Java:
public class sign_in extends AppCompatActivity {
EditText usernameTIL, passwordTIL;
Button signInBTN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
usernameTIL = (MaterialEditText) findViewById(R.id.usernameTIL);
passwordTIL = (MaterialEditText) findViewById(R.id.passwordTIL2);
signInBTN = (Button) findViewById(R.id.signInBTN);
//Init FireBase
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference table_user = database.getReference("User");
signInBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final ProgressDialog mDialog = new Progress codeDialog(sign_in.this);
mDialog.setMessage("Please wait...");
mDialog.show();
table_user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//check if user doesn't have an account
if (dataSnapshot.child(usernameTIL.getText().toString()).exists()) {
//get user information
mDialog.dismiss();
User user = dataSnapshot.child(usernameTIL.getText().toString()).getValue(User.class);
if (user.getPassword().equals(passwordTIL.getText().toString())) {
Intent homeIntent = new Intent(sign_in.this, HomeScreen.class);
Common.curentUser = user;
startActivity(homeIntent);
finish();
} else {
Toast.makeText(sign_in.this, "Sign In Failed !!", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(sign_in.this, "User not found !!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
}
}
MaterialEditText is an EditText
public class MaterialEditText extends AppCompatEditText {
it can not contain any widgets. In your layout you have
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/usernameTIL"
android:layout_width="251dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.527"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="268dp"
android:layout_height="36dp"
android:hint="username" />
</com.rengwuxian.materialedittext.MaterialEditText>
a MaterialEditText containing another MaterialEditText, which is causing your app to crash for ClassCastException
According to this library's sample code, you must cast your MaterialEditText to EditTextlike this:
usernameTIL = (EditText) findViewById(R.id.usernameTIL);
passwordTIL = (EditText) findViewById(R.id.passwordTIL2);

How to open a new activity on click of textview in android?

I am new to android programming world, I have designed as a layout with username, password AS edittext control and "forgot password" as textview control, on click/tap of "forgot password", I want to open a new activity.
This is what I am trying, looking forward for your help.
My Application's xml and java code is mentioned below.
<?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:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light">
<TextView
android:id="#+id/txtVwAccountLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Account Login"
android:textColor="#android:color/background_light"
android:textSize="24sp"
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_constraintVertical_bias="0.057" />
<EditText
android:id="#+id/eTxtUserName"
android:layout_width="288dp"
android:layout_height="48dp"
android:background="#android:color/background_light"
android:ems="10"
android:hint=" Username"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.593"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />
<EditText
android:id="#+id/eTxtPassword"
android:layout_width="290dp"
android:layout_height="45dp"
android:background="#android:color/background_light"
android:ems="10"
android:hint=" Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.606"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.535" />
<TextView
android:id="#+id/txtVwForgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password ?"
android:textColor="#android:color/background_light"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.854"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.632" />
<Button
android:id="#+id/btnLogin"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.779" />
<Button
android:id="#+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:background="#android:color/holo_blue_light"
android:text="Register"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.662" />
</android.support.constraint.ConstraintLayout>
Login.java
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final TextView tvForgotPwd = (TextView) findViewById(R.id.txtVwForgetPassword);
tvForgotPwd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Login.this, "you clicked me", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(),PatientSearch.class);
startActivityForResult(myIntent, 0);
}
});
final Button btnRegister = (Button) findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),MainActivity.class);
startActivityForResult(myIntent,0);
}
});
Even though this question has been asked so many times, I decided to answer anyway. This will work:
tvForgotPwd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Login.this, PatientSearch.class);
startActivity(myIntent);
}
});
In your onCreate() method, set an OnClickListener callback on your TextView. In the callback, use an Intent to start the desired activity.
Here is an exemple of code:
yourTextView= (TextView) findViewById(R.id.txtVwForgetPassword);
yourTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
}
});
Probably it is necessary to try a method
startActivity (new Intent (Activity.this, Open Activity.class));

How to get value from **edit text** and display it in a textview in another activity

I have been pulling my hair out for the last 4 hours trying to find out how to do this.
I have checked SO for other questions but found nothing good.
I am an intermediate programmer and would be very appreciative if your comments and answers were written in a way that I can understand.
I want to be able to take the text that the user enters into the #+id/location Edittext box on the InputPage activity and display it in the #+id/ListName TextView on the InputPage2 activity when the button on the InputPage activity is pressed.
InputPage java code
public class InputPage extends Activity {
public String name;
public InputPage() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_page );
Button button = (Button)findViewById(R.id.addButton);
}
public InputPage(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String name(String name){
this.name = name;
EditText editText = (EditText)findViewById(R.id.location);
String value = editText.getText().toString();
return value;
}
public void toInputScreen2(View view) {
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2 = new Intent(InputPage.this, InputPage2.class);
startActivity(intent2);
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InputPage"
android:background="#color/BackgroundGrey">
<TextView
android:id="#+id/mapNo"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="#string/mapNoTextBox"
android:textColor="#color/textColour"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.04"
app:layout_constraintVertical_bias="0.13" />
<TextView
android:id="#+id/inputPageTitle"
android:layout_width="178dp"
android:layout_height="21dp"
android:text="#string/inputPageTitle"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/textColour"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.01999998" />
<EditText
android:id="#+id/noInput"
android:layout_width="48dp"
android:layout_height="39dp"
android:ems="10"
android:inputType="number"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.13"
app:layout_constraintHorizontal_bias="0.51" />
<TextView
android:layout_width="150dp"
android:layout_height="38dp"
android:text="#string/location"
android:textColor="#color/textColour"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintVertical_bias="0.24" />
<EditText
android:id="#+id/location"
android:layout_width="177dp"
android:layout_height="44dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/hintText"
android:textColorHint="#color/hintTextColour"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintVertical_bias="0.243" />
<EditText
android:id="#+id/dateBox"
android:layout_width="133dp"
android:layout_height="44dp"
android:ems="10"
android:inputType="date"
android:hint="#string/dateBoxHint"
android:textColorHint="#color/hintTextColour"
android:layout_marginLeft="61dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.335"
app:layout_constraintVertical_bias="0.374" />
<TextView
android:layout_width="101dp"
android:layout_height="34dp"
android:text="#string/dateBox"
android:textColor="#color/textColour"
android:textStyle="bold"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.032"
app:layout_constraintVertical_bias="0.369" />
<Button
android:id="#+id/button2"
android:layout_width="165dp"
android:layout_height="58dp"
android:text="#string/continueButton"
android:background="#drawable/buttons"
android:layout_marginRight="8dp"
android:onClick="toInputScreen2"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintVertical_bias="0.778" />
</android.support.constraint.ConstraintLayout>
**InputPage2 java code **
public class InputPage2 extends Activity {
Button saveButton1;
EditText message;
String Message;
TextView[] pairs;
int num_match;
public TextView[] getPairs() {
return pairs;
}
public void setPairs(TextView[] pairs) {
this.pairs = pairs;
}
public int getNum_match() {
return num_match;
}
public void setNum_match(int num_match) {
this.num_match = num_match;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_page2);
message = (EditText) findViewById(R.id.textNotAtHomes);
}
public void toSaveField(View v) {
saveButton1 = (Button) findViewById(R.id.saveButton1);
saveButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Message = message.getText().toString();
try {
FileOutputStream fos = openFileOutput("Text.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
try {
osw.write(Message);
osw.flush();
osw.close();
Toast.makeText(getBaseContext(), "Saved successfully", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Intent intent2 = new Intent(InputPage2.this, MainActivity.class);
startActivity(intent2);
}
public void linearLayout(TextView[] pairs, int num_match) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
pairs = new TextView[num_match + 1];
for (int l = 1; l <= num_match; l++) {
pairs[1].setTextSize(15);
pairs[1].setLayoutParams(layoutParams);
pairs[1].setId(l);
linearLayout.addView(pairs[1]);
}
}
}
associated XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InputPage2"
android:background="#ffffff"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp"
android:backgroundTint="#color/BackgroundGrey">
<Button
android:onClick="toSaveField"
android:id="#+id/saveButton1"
android:layout_width="341dp"
android:layout_height="70dp"
android:background="#drawable/buttons"
android:text="#string/saveButton"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintVertical_bias="1.0"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:id="#+id/textNotAtHomes"
android:layout_width="346dp"
android:layout_height="477dp"
android:ems="10"
android:background="#color/hintTextColour"
android:inputType="textMultiLine"
android:hint="#string/hintText2"
android:textColorHint="#color/hintTextColour2"
android:layout_marginLeft="8dp"
android:gravity="fill_horizontal"
android:padding="6dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.303" />
<TextView
android:id="#+id/ListName"
android:layout_width="344dp"
android:layout_height="37dp"
android:textColor="#color/blackText"
android:background="#color/whiteBackground"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
Any help is appreciated .
You can pass data between activities like this
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
and get it with the other activity
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

Categories