How to handle multiple intents on same activity? - java

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

Related

set.Visibility(View.GONE) not making the Button and EditText gone, and it's showing up like I didn't even add the code

Trying to make two login, one for admin, one for user. They share the same layout, but admin has additional TextFields and Buttons to add data to the menu. So when logging in with user credentials, the TextFields and Buttons will be setVisibility(View.GONE), but it's not working at all, in fact it's showing up like setVisibility(View.GONE) was added at all. Any advice would be appreciated.
loginpage.java
DatabaseHelper myDB;
EditText LoginEMail;
EditText LoginPassword;
Button LoginBtn;
LinearLayout linearLayout;
View add_image;
View add_name;
View add_desc;
View add_data;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
LoginEMail = findViewById(R.id.LoginEMail);
LoginPassword = findViewById(R.id.LoginPassword);
LoginBtn = findViewById(R.id.LoginBtn);
myDB = new DatabaseHelper(this);
linearLayout = findViewById(R.id.linearLayout);
linearLayout = new LinearLayout(this);
add_image = new View(this);
add_name = new View(this);
add_desc = new View(this);
add_data = new View(this);
}
public void Login(View view) {
Intent intent = new Intent(loginpage.this, MenuSelection.class);
if (LoginEMail.getText().toString().equals("admin") && (LoginPassword.getText().toString().equals("admin"))) {
startActivity(intent);
linearLayout.setVisibility(View.VISIBLE);
}
else if(LoginEMail.getText().toString().equals("user") && (LoginPassword.getText().toString().equals("user"))) {
startActivity(intent);
linearLayout.setVisibility(View.GONE);
add_image.setVisibility(View.GONE);
add_name.setVisibility(View.GONE);
add_desc.setVisibility(View.GONE);
add_data.setVisibility(View.GONE);
}
else
Toast.makeText(loginpage.this, "Incorrect E-mail or Password.", Toast.LENGTH_SHORT).show();
}
loginpage.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">
<EditText
android:id="#+id/LoginEMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="E-Mail"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.204" />
<EditText
android:id="#+id/LoginPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.338" />
<Button
android:id="#+id/LoginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
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.602"
android:onClick="Login"/>
</androidx.constraintlayout.widget.ConstraintLayout>
menu_selection.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ListView
android:id="#+id/menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ImageButton
android:id="#+id/add_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#color/black" />
<EditText
android:id="#+id/add_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="#+id/add_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Description" />
<Button
android:id="#+id/add_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Instead of add_image = new View(this); you need to associate the variable add_image to the view in your XML layout using the findViewById(R.id.add_image) method. Similarly for the other 3 views that you have initialized this way.
You also need to delete the line linearLayout = new LinearLayout(this);. The previous line setting this variable is correct.

how do you fix a java.langIllegal state exception on fragments?

I've made a checklist app and when the user add's the check to the app I get the below error and a crash. Can someone provide some advice to how to fix this? As i'm using fragments, i've created a second Java file and refernced that to the XML as you cannot have more then one class in a single Java, i've tried merging them but get lots of errors and cannot use the public class 'extends' more then once.
Here's the error
Heres the XML
<TextView
android:id="#+id/text_checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Checklist"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="normal"
app:fontFamily="#font/basic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/text_checklist"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/inputText"
android:layout_width="369dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="#string/placeholder"
android:inputType="textPersonName" />
<Button
android:id="#+id/check_list_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:onClick="addButton2"
android:text="#string/buttonText" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="107dp" />
Heres the 1st Java (Fragment)
checklistViewModel =
ViewModelProviders.of(this).get(com.example.csd301_project_trackday_timing_dh.ui.checklist.ChecklistViewModel.class);
View root = inflater.inflate(R.layout.fragment_checklist, container, false);
final TextView textView = root.findViewById(R.id.text_checklist);
checklistViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
Heres the 2nd Java (CheckList)
Button btn_checklist;
EditText inputText;
ListView listView;
ArrayList<String> list;
public void addButton2(View view){
String text = inputText.getText().toString();
list.add(text);
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_checklist);
btn_checklist= findViewById(R.id.check_list_button);
inputText=findViewById(R.id.inputText);
listView=findViewById(R.id.listView);
list = new ArrayList<>();
}
}

Saving edit to listview android studio

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

how to pass button intent to make drawable change color in another activity?

I am new to Android Studio and Java and have been working on a project for some time now and would appreciate help with an issue I cannot seem to find any tutorials or information on.
I am using android Studio version 3.1.4 on a computer running Windows 7 pro.
I cannot fit all my buttons and all my drawables onto the MainActivity, so I want all my buttons on the MainActivity and all my drawables on Activity 2.
My Problem is that I cannot work out how to make my drawables change color in the second Activity when I click any buttons in the MainActivity.
My drawables in the form of circles are in svg format with a diameter of 10mm
I have pasted all relevant code for 3 buttons to work with 3 svg drawable on the MainActivity so that you can see how it works on a virtual device or your mobile connected via a usb cable.
I have also added a button in the main activity to take you to Activity 2 where I have put 3 drawable that I need to work in the same way as the main activity but with the buttons from the first activity changing them.
In my project I have many buttons. Each button when clicked changes the color of its related SVG drawable as per my program. The drawable are black to start with. Each click will make them change to the next colors in this sequential order. White, Yellow, Orange, Red, then back to black
So what I need to do is have my buttons in the main activity and the drawables in activity 2.
If Button 1 (attribute is btn1) is clicked in activity_main.xml I want the button information passed to Activity2 to make the drawable with attribute iv1 change color.
So if you were to click Button1, Button2, Button3, Button4 and use the go to activity2 button (attribute btn) the drawable in Activity2 would be white, yellow, white from left to right.
Thanks in advance for any help.
<package com.example.chucky.svg;
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.ImageView;
public class MainActivity extends AppCompatActivity {
private Button button;
int[] colors = {0xffffffff, 0xffffff00, 0xffff6600, 0xffff0000, 0xff000000};
int counter1 = -1;
int counter2 = -1;
int counter3 = -1;
ImageView iv1;
ImageView iv2;
ImageView iv3;
Button btn;
Button btn1;
Button btn2;
Button btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter3++;
if (counter3> colors.length -1){
counter3 = 0;
}
iv3.setColorFilter(colors[counter3]);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter2++;
if (counter2> colors.length - 1){
counter2 = 0;
}
iv2.setColorFilter(colors[counter2]);
}
});
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter1++;
if (counter1> colors.length - 1){
counter1 = 0;
}
iv1.setColorFilter(colors[counter1]);
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
Activity2 class
package com.example.chucky.svg;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
Activity_main xml
<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/btn1"
android:layout_width="wrap_content"
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:layout_marginTop="8dp"
android:text=" Button 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.091"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
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:layout_marginTop="8dp"
android:text="Button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
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:layout_marginTop="8dp"
android:text="Button 3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.895"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
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:layout_marginTop="8dp"
android:text="Go to Activity 2"
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.087" />
<ImageView
android:id="#+id/iv1"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
<ImageView
android:id="#+id/iv2"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
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.296"
app:srcCompat="#drawable/ic_circle_e" />
<ImageView
android:id="#+id/iv3"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.852"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
</android.support.constraint.ConstraintLayout>
activity_2 xml
<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=".Activity2">
<ImageView
android:id="#+id/iv3"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.852"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
<ImageView
android:id="#+id/iv2"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
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.296"
app:srcCompat="#drawable/ic_circle_e" />
<ImageView
android:id="#+id/iv1"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
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:layout_marginTop="8dp"
android:text="Activity2"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
You have colour codes in integer array int[] colors and selected position in counter variable. By the way set color to imageview | pass it via intent.
Try below code:
Intent i = new Intent(MainActivity.this,Activity2.class);
i.putExtra("COLOR_1",colors[counter1]);
i.putExtra("COLOR_2",colors[counter2]);
i.putExtra("COLOR_3",colors[counter3]);
startActivity(i);
Then in Activity2 get intent value & set to corresponding ImageView.
Add this code in onCreate()
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
iv1.setColorFilter(bundle.getInt("COLOR_1",0));
iv2.setColorFilter(bundle.getInt("COLOR_2",0));
iv3.setColorFilter(bundle.getInt("COLOR_3",0));
}
What you need to do is to pass an information to the second activity, extract, and make use of the information.
With an Intent Extra your goal can be achievable just like this:
First of all, declare a unique constant variable in your second activity that will hold information across the communication.
public static final String EXTRA_USER_ID = "userId";
Now, FirstActivity sending info:
String changeColorToBlack = " black"
Intent intent = new Intent(firstActivty.this, SecondActivity.class);
intent.putExtra(SecondActivity.EXTRA_COLOR_KEY, changeColorToBlack);
startActivity(intent);
SecondActivity can retrieve the information like this
String colorIdKey = getIntent().getStringExtra(EXTRA_COLOR_ID);
if (colorIdKey == null) {
throw new IllegalArgumentException("Must pass EXTRA_COLOR_KEY");
}
//colorIdKey is your color
That is all. All you need is an Intent Extra to pass information between 2 activities
The easiest way to active what you required is pass the color codes to second activity as arguments
in Activity1.class do this
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
// pass the color value as bundle
intent.putExtra("color1", <color_code1>);
intent.putExtra("color2", <color_code2>);
intent.putExtra("color3", <color_code3>);
startActivity(intent); }
in Activity2.class
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Bundle extras = getIntent().getExtras();
if(extras != null){
String color1 = extras.getString("color1");
String color2 = extras.getString("color2");
String color3 = extras.getString("color3");
// set values to image views here
}
}
}
Official documentation here : https://developer.android.com/guide/components/activities/parcelables-and-bundles#kotlin

I'm not getting EditText Value in another class

MainActivity.java (First Class)
package com.example.we.reportcardjavaclass;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public EditText one, two, three, four, five, six;
public String bang1, bang2, bang3, bang4, bang5, bang6;
public Button results;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the numbers category
}
public void getResult(View view){
results = (Button) findViewById(R.id.result);
Intent numbersIntent = new Intent(MainActivity.this, result.class);
startActivity(numbersIntent);
one = (EditText) findViewById(R.id.name);
bang1 = one.getText().toString();
two = (EditText) findViewById(R.id.Class);
bang2 = two.getText().toString();
three = (EditText) findViewById(R.id.English);
bang3 = three.getText().toString();
four = (EditText) findViewById(R.id.Hindi);
bang4 = four.getText().toString();
five = (EditText) findViewById(R.id.Maths);
bang5 = five.getText().toString();
six = (EditText) findViewById(R.id.Science);
bang6 = six.getText().toString();
}
}
activity_main.xml (First Layout File)
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.we.reportcardjavaclass.MainActivity"
android:padding="16dp">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Student's Name"
android:id="#+id/name"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Class"
android:id="#+id/Class"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="64dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in English"
android:id="#+id/English"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="128dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Hindi"
android:id="#+id/Hindi"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="192dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Maths"
android:id="#+id/Maths"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="256dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Science"
android:id="#+id/Science"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="320dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:text="Get result"
android:id="#+id/result"
android:onClick="getResult"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="384dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
## result.xml (Second Layout File) ##
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="32dp">
<android.support.v7.widget.CardView
android:id="#+id/resultCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
android:layout_gravity="center_vertical"
card_view:cardUseCompatPadding="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Student's Name"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Class"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:background="#color/cardview_dark_background"
android:padding="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="English"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="40dp"
android:text="Hindi"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="40dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="80dp"
android:text="Maths"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="80dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="120dp"
android:text="Science"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="120dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:text="BACK TO HOME"
/>
</FrameLayout>
</android.support.v7.widget.CardView>
</LinearLayout></ScrollView>
Second Class
package com.example.we.reportcardjavaclass;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by we on 08-07-2017.
*/
public class result extends MainActivity{
public TextView one, two, three, four, five, six;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setTitle(bang1+"s Result");
one = (TextView) findViewById(R.id.one);
one.setText(bang1);
two = (TextView) findViewById(R.id.two);
two.setText(bang2);
three = (TextView) findViewById(R.id.three);
three.setText(bang3);
four = (TextView) findViewById(R.id.four);
four.setText(bang4);
five = (TextView) findViewById(R.id.five);
five.setText(bang5);
six = (TextView) findViewById(R.id.six);
six.setText(bang6);
}
}
EditText values are getting stored MainActivity.java in variables bang - 123456. I added public modifier while declaring them so I can access these in second class too. In second class I want to use these variable values to set in into Textviews but while running it is not showing the value.
I have also attached the screenshots of both layouts after running in my emulator.
Screenshot 1
Screenshot 2
In second layout it is supposed to show name and other values.
I would suggest that you take a few steps back and get a better understanding of Android's fundamentals, including Activities in general. Perhaps a good place to start, would be understanding the Activity Lifecycle.
An answer to your question isn't very simple, because the way in which Android works is likely very different from your understanding.
In short, your second activity is a completely different instance of a class than the first. Extending the second from the first does not mean that the assigned variables are available to the second, it merely means that the class inherits the structure of its parent in the form of methods and fields.
You should instead be designing your activities in such a way that they relay those strings. When launching your second activity, include those strings in the intent when starting it.
For instance:
public class MainActivity extends AppCompatActivity {
// ...
/** Your method where the second activity is launched */
public void onButtonClicked() {
Intent intent = new Intent(this, SecondActivity.class);
intent.putString(SecondActivity.EXTRA_BANG1, bang1);
// ...
startActivity(intent);
}
}
public class SecondActivity extends AppCompatActivity {
public static final String EXTRA_BANG1 = "bang1";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
// ...
TextView tvOne = (TextView) findViewById(R.id.one);
// ...
String bang1 = getIntent().getString(EXTRA_BANG1);
tvOne.setText(bang1);
}
}
I thoroughly recommend reading up in the Android documentation (it's excellently written!) to better understand how (and why) it works through this mechanism. In particular, this article on Sending data between activities is very relevant to you.
If you want to use these values in another activity then simply pass these values along with the intent that is used to start a new activity.
MainActivity.java
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("Bang1",bang1);
i.putExtra("Bang2",bang2);
i.putExtra("Bang3",bang3);
...
And you can get them in the SecondActivity.java file as:
Intent i =getIntent();
int bang1 = i.getIntExtra("Bang1");
...
But instead so choosing the string names as shown i would suggest to create a new class to store the the string names as final string constants in order to avoid any kind of mistake.
Or you can directly declare these variables as static and access them in the SecondActivity by referring to their names as :
int bang1 = MainActivity.bang1;

Categories