I have a bottomappbar like this with some rounded button. I use the first answer to add contents to my bottomappbar(with linearlayout and imageviews). I have one file LandingActivity which inflates this bottomappbar at the bottom of my screen.
- is there any itemreselectlistener for bottomappbar?
even on setting backstack(null) on fragment transaction the fragment keep appearing in stack and on back space they keep comming back. How should I make it so on back click it should exit the app.
whenever I click on any of the button in my bottomappbar I have to write a bunch of boiler plate code for selecting and deselecting the icons. i.e setting the selected icon with my color iamgeview.colorfilter="Color.BLACK" of my color. and unselected icon with iamgeview.colorfilter="null" So, here is my LandingActivity.java file
public class LandingActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = LandingActivity.class.getSimpleName();
@BindView(R.id.iv_icon_profile)
ImageView ivIconProfile;
@BindView(R.id.iv_home_icon)
ImageView ivHomeIcon;
@BindView(R.id.iv_icon_membership)
ImageView ivMemberShip;
@BindView(R.id.ic_icon_list)
ImageView ivListIcon;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_home_icon:
updateUI(selectedBottomScreen.HOME);
break;
case R.id.iv_icon_membership:
updateUI(selectedBottomScreen.MEMBERSHIP);
break;
case R.id.iv_icon_profile:
updateUI(selectedBottomScreen.PROFILE);
break;
case R.id.ic_icon_list:
updateUI(selectedBottomScreen.LISTICON);
break;
}
}
enum selectedBottomScreen {
PROFILE,
HOME,
MEMBERSHIP,
LISTICON
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing);
ButterKnife.bind(this);
ivIconProfile.setOnClickListener(this);
ivMemberShip.setOnClickListener(this);
ivListIcon.setOnClickListener(this);
ivHomeIcon.setOnClickListener(this);
updateUI(selectedBottomScreen.HOME);
}
void startFragment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
Log.d(TAG, "HomeFragment is called");
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment).addToBackStack(null).commit();
}
public void updateUI(selectedBottomScreen selectedBottomScreen) {
switch (selectedBottomScreen) {
case HOME:
startFragment(new HomeFragment());
ivHomeIcon.setColorFilter(R.color.black_overlay);
ivListIcon.setColorFilter(null);
ivIconProfile.setColorFilter(null);
ivMemberShip.setColorFilter(null);
break;
case PROFILE:
startFragment(new ProfileFragment());
ivHomeIcon.setColorFilter(null);
ivListIcon.setColorFilter(null);
ivIconProfile.setColorFilter(R.color.black_overlay);
ivMemberShip.setColorFilter(null);
break;
case MEMBERSHIP:
startFragment(new MembershipFragment());
ivHomeIcon.setColorFilter(null);
ivListIcon.setColorFilter(null);
ivIconProfile.setColorFilter(null);
ivMemberShip.setColorFilter(R.color.black_overlay);
break;
case LISTICON:
ivHomeIcon.setColorFilter(null);
ivListIcon.setColorFilter(R.color.black_overlay);
ivIconProfile.setColorFilter(null);
ivMemberShip.setColorFilter(null);
break;
default:
ivHomeIcon.setColorFilter(null);
ivListIcon.setColorFilter(null);
ivIconProfile.setColorFilter(null);
ivMemberShip.setColorFilter(null);
}
}
}
activitylanding.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/coordinator"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:id="@+id/container"
android:layout_height="match_parent" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:gravity="center"
app:fabCradleVerticalOffset="8dp"
app:fabCradleMargin="10dp"
android:elevation="24dp"
app:fabCradleRoundedCornerRadius="16dp"
app:fabAlignmentMode="center">
<LinearLayout
android:layout_width="match_parent"
android:elevation="24dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:id="@+id/iv_home_icon"
android:src="@drawable/ic_icon_menu_home_normal"
android:layout_height="24dp" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:paddingEnd="20dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="auto"
android:id="@+id/iv_icon_membership"
android:layout_gravity="center"
android:src="@drawable/ic_icon_menu_membership_normal"
android:layout_height="24dp">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:paddingStart="20dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:id="@+id/ic_icon_list"
android:src="@drawable/ic_icon_list_normal"
android:layout_gravity="center"
android:layout_height="24dp">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:layout_gravity="center"
android:id="@+id/iv_icon_profile"
android:src="@drawable/ic_icon_account_normal"
android:layout_height="24dp">
</ImageView>
</LinearLayout>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:maxImageSize="50dp"
android:backgroundTint="#212121"
android:src="@drawable/ic_icon_menu_offer"
app:layout_anchor="@id/bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Ok, SO once when I start my HomeFragment. everything seems to start fine when it launches for the first time. But when I go to my profile fragment or membership fragment and come back to my HomeFragment. Only the contents in my AppBarLayout and bottomappbar shows and recyclerview just fades off. The recyclerview appears with all the contents when I click the search tab at he top. I don't know how but it does.
here is my HomeFragment.java file
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
@BindView(R.id.rv_spa_cards)
RecyclerView rvSpaCards;
@BindView(R.id.rv_featured)
RecyclerView rvFeatured;
@BindView(R.id.sv_to_card_items)
SearchView svCardItems;
FeaturedAdapter featuredAdapter;
private CardAdapter cardListAdapter;
private CardImageAdapter cardGridAdapter;
private ArrayList<classmodel> rocketModelArrayList;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void addSearchButton() {
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
svCardItems.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
svCardItems.setMaxWidth(Integer.MAX_VALUE);
svCardItems.setQueryHint("Search Here");
svCardItems.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
cardListAdapter.getFilter().filter(s);
return false;
}
@Override
public boolean onQueryTextChange(String s) {
cardListAdapter.getFilter().filter(s);
return false;
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, view);
addFakeData();
addFakeDataForFeatured();
addSearchButton();
featuredAdapter = new FeaturedAdapter();
rvSpaCards.setLayoutManager(new LinearLayoutManager(getContext()));
rvSpaCards.setHasFixedSize(true);
rvSpaCards.setAdapter(cardListAdapter);
return view;
}
private void addFakeDataForFeatured() {
rvFeatured.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
rvFeatured.setHasFixedSize(true);
rvFeatured.setAdapter(new FeaturedAdapter());
}
private void addFakeData() {
rocketModelArrayList = new ArrayList<>();
rocketModelArrayList.add(new classmodel("someSpa", "24 K.M.", "Anna", "2400 for 30 mins", "AVAilable"));
rocketModelArrayList.add(new classmodel("Ospa", "24 K.M.", "Anna", "2400 for 30 mins", "AVAilable"));
rocketModelArrayList.add(new classmodel("Green", "24 K.M.", "Anna", "2400 for 30 mins", "AVAilable"));
rocketModelArrayList.add(new classmodel("Somespaspa", "24 K.M.", "Anna", "2400 ", "AVAilable"));
rocketModelArrayList.add(new classmodel("yolospa", "24 K.M.", "Anna", "2400", "AVAilable"));
rocketModelArrayList.add(new classmodel("oncespa", "24 K.M.", "Anna", "2400", "AVAilable"));
rocketModelArrayList.add(new classmodel("ppkspa ", "24 K.M.", "Anna", "2400", "AVAilable"));
rocketModelArrayList.add(new classmodel("ornspa", "24 K.M.", "Anna", "2400", "AVAilable"));
cardListAdapter = new CardAdapter(getContext(), rocketModelArrayList);
cardGridAdapter = new CardImageAdapter(rocketModelArrayList);
cardGridAdapter.notifyDataSetChanged();
cardListAdapter.notifyDataSetChanged();
Log.d(TAG, "CardGridAdapter notify is called");
}
}