'Sending a notification through a button by accepting data from both tabs in a tabbed activity in Android Studio
I am currently creating my very first application, it's a very dummy coffee shop application that includes multiple activities like a basic activity and a log in activity. However, I have currently created my first tabbed activity where the user can "order" coffee (first tab) and food (second tab) by selecting each CheckBox respectively as you can see in the images below.
As you can see, there are two buttons on the bottom of the screen, the "GO BACK" button that sends the user back to the main activity and another button next to it called "ORDER NOW" which is where I am stuck. Both buttons are visible in both tabs and I have an onClick method for the first button "sendNotification". When I press that button it checks which check boxes are clicked and stores them accordingly inside the String order and then insert that value inside the notification. Everything works fine when I "order" coffees but no matter what selection I make in the second tab called "FOOD" it always sends an empty message like this "Here is your oder: , it will arrive in about 1 hour, thank you!".
Implementation for sendNotification:
package com.example.myapp;
public class OrderActivity extends AppCompatActivity {
private ActivityOrderBinding binding;
private PageViewModel pageViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityOrderBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = binding.viewPager;
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = binding.tabs;
tabs.setupWithViewPager(viewPager);
}
public void goBack(View v)
{
finish();
}
public void sendNotification(View v)
{
CheckBox ch1 = findViewById(R.id.cb1);
CheckBox ch2 = findViewById(R.id.cb2);
CheckBox ch3 = findViewById(R.id.cb3);
CheckBox ch4 = findViewById(R.id.cb4);
CheckBox ch5 = findViewById(R.id.cb5);
CheckBox ch6 = findViewById(R.id.cb6);
CheckBox ch7 = findViewById(R.id.cb7);
CheckBox ch8 = findViewById(R.id.cb8);
CheckBox ch9 = findViewById(R.id.cb9);
CheckBox ch10 = findViewById(R.id.cb10);
String order = "";
if (ch1.isChecked()) {
order += ch1.getText().toString() + ", ";
}
if (ch2.isChecked()) {
order += ch2.getText().toString() + ", ";
}
if (ch3.isChecked()) {
order += ch3.getText().toString() + ", ";
}
if (ch4.isChecked()) {
order += ch4.getText().toString() + ", ";
}
if (ch5.isChecked()) {
order += ch5.getText().toString() + ", ";
}
if (ch6.isChecked()) {
order += ch6.getText().toString() + ", ";
}
if (ch7.isChecked()) {
order += ch7.getText().toString() + ", ";
}
if (ch8.isChecked()) {
order += ch8.getText().toString() + ", ";
}
if (ch9.isChecked()) {
order += ch9.getText().toString() + ", ";
}
if (ch10.isChecked()) {
order += ch10.getText().toString() + ", ";
}
NotificationChannel channel;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel("1", "channel1", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "1")
.setSmallIcon(R.drawable.coffeeicon)
.setContentTitle("Order confirmation")
.setContentText("Here is your order: " + order + " it will arrive in about 1 hour, thank you!")
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManagerCompat notifyAdmin = NotificationManagerCompat.from(this);
notifyAdmin.notify(1, notification.build());
}
finish();
Toast.makeText(getApplicationContext(), "Thank you for ordering, you will now receive a confirmation for your order!", Toast.LENGTH_LONG).show();
}
}
My PageViewModel.java
package com.example.myapp.ui.main;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
public class PageViewModel extends ViewModel {
private MutableLiveData<Integer> mIndex = new MutableLiveData<>();
private LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
@Override
public String apply(Integer input) {
return "Hello world from section: " + input;
}
});
public void setIndex(int index) {
mIndex.setValue(index);
}
public LiveData<String> getText() {
return mText;
}
}
My PlaceHolderFragment.java
package com.example.myapp_csc392.ui.main;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.AsyncListUtil;
import com.example.myapp_csc392.R;
import com.example.myapp_csc392.databinding.FragmentOrderBinding;
import org.w3c.dom.Text;
/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private PageViewModel pageViewModel;
private FragmentOrderBinding binding;
public static PlaceholderFragment newInstance(int index) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
}
pageViewModel.setIndex(index);
}
@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentOrderBinding.inflate(inflater, container, false);
View root = binding.getRoot();
ImageView img1 = root.findViewById(R.id.ivEspresso);
ImageView img2 = root.findViewById(R.id.ivCapuccino);
ImageView img3 = root.findViewById(R.id.ivLatte);
ImageView img4 = root.findViewById(R.id.ivMilkshake);
ImageView img5 = root.findViewById(R.id.ivWater);
ImageView img6 = root.findViewById(R.id.ivHamburger);
ImageView img7 = root.findViewById(R.id.ivSandwich);
ImageView img8 = root.findViewById(R.id.ivSalad);
ImageView img9 = root.findViewById(R.id.ivCake);
ImageView img10 = root.findViewById(R.id.ivChips);
CheckBox c1 = root.findViewById(R.id.cb1);
CheckBox c2 = root.findViewById(R.id.cb2);
CheckBox c3 = root.findViewById(R.id.cb3);
CheckBox c4 = root.findViewById(R.id.cb4);
CheckBox c5 = root.findViewById(R.id.cb5);
CheckBox c6 = root.findViewById(R.id.cb6);
CheckBox c7 = root.findViewById(R.id.cb7);
CheckBox c8 = root.findViewById(R.id.cb8);
CheckBox c9 = root.findViewById(R.id.cb9);
CheckBox c10 = root.findViewById(R.id.cb10);
TextView e1 = root.findViewById(R.id.tv1);
TextView e2 = root.findViewById(R.id.tv2);
TextView e3 = root.findViewById(R.id.tv3);
TextView e4 = root.findViewById(R.id.tv4);
TextView e5 = root.findViewById(R.id.tv5);
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1)
{
img6.setVisibility(View.INVISIBLE);
img7.setVisibility(View.INVISIBLE);
img8.setVisibility(View.INVISIBLE);
img9.setVisibility(View.INVISIBLE);
img10.setVisibility(View.INVISIBLE);
c6.setVisibility(View.INVISIBLE);
c7.setVisibility(View.INVISIBLE);
c8.setVisibility(View.INVISIBLE);
c9.setVisibility(View.INVISIBLE);
c10.setVisibility(View.INVISIBLE);
c1.setText("Espresso");
c2.setText("Capuccino");
c3.setText("Latte");
c4.setText("Milkshake");
c5.setText("Water");
e1.setText("2.50€");
e2.setText("3.50€");
e3.setText("4.50€");
e4.setText("4.00€");
e5.setText("1.00€");
}
else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2)
{
img1.setVisibility(View.INVISIBLE);
img2.setVisibility(View.INVISIBLE);
img3.setVisibility(View.INVISIBLE);
img4.setVisibility(View.INVISIBLE);
img5.setVisibility(View.INVISIBLE);
c1.setVisibility(View.INVISIBLE);
c2.setVisibility(View.INVISIBLE);
c3.setVisibility(View.INVISIBLE);
c4.setVisibility(View.INVISIBLE);
c5.setVisibility(View.INVISIBLE);
c6.setText("Hamburger");
c7.setText("Sandwich");
c8.setText("Salad");
c9.setText("Red Velvet Cake");
c10.setText("Salty Chips");
e1.setText("7.00€");
e2.setText("5.00€");
e3.setText("5.00€");
e4.setText("4.50€");
e5.setText("1.50€");
}
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
and the fragment_order.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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<ImageView
android:id="@+id/ivEspresso"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023"
app:srcCompat="@drawable/espresso" />
<ImageView
android:id="@+id/ivHamburger"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023"
app:srcCompat="@drawable/hamburger" />
<ImageView
android:id="@+id/ivCapuccino"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.214"
app:srcCompat="@drawable/capuccino" />
<ImageView
android:id="@+id/ivSandwich"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.214"
app:srcCompat="@drawable/sandwich" />
<ImageView
android:id="@+id/ivLatte"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.394"
app:srcCompat="@drawable/latte" />
<ImageView
android:id="@+id/ivSalad"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.394"
app:srcCompat="@drawable/salad" />
<ImageView
android:id="@+id/ivMilkshake"
android:layout_width="51dp"
android:layout_height="61dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.588"
app:srcCompat="@drawable/milkshake" />
<ImageView
android:id="@+id/ivCake"
android:layout_width="50dp"
android:layout_height="62dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.585"
app:srcCompat="@drawable/cake" />
<ImageView
android:id="@+id/ivWater"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.775"
app:srcCompat="@drawable/water" />
<ImageView
android:id="@+id/ivChips"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.775"
app:srcCompat="@drawable/chips" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:onClick="goBack"
android:text="GO BACK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.791"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.959" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:onClick="sendNotification"
android:text="ORDER NOW"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.239"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.959" />
<CheckBox
android:id="@+id/cb6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.409" />
<CheckBox
android:id="@+id/cb8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.409"
tools:ignore="DuplicateClickableBoundsCheck" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />
<CheckBox
android:id="@+id/cb10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
<CheckBox
android:id="@+id/cb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
<CheckBox
android:id="@+id/cb7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
<CheckBox
android:id="@+id/cb9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.824" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.144" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.48" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.66" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.313" />
</androidx.constraintlayout.widget.ConstraintLayout>
If anyone could help out I would really appreciate it and please let me know if you need me to show more code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
