Two recyclerview not scrollable in one layout - android-studio

I created a View that has two recyclerviews inside.
Everything works correctly but I get that the two recyclerviews are scrollable.
I would like the two recyclerview to be shown in its maximum length to show all lines and for the scrolling of the view to work directly
if I set android: nestedScrollingEnabled = "false" the recyclerview is not shown totally long
what do i have to change?
Thanks
<?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="wrap_content"
android:fillViewport="true"
android:background="#color/fond"
tools:context=".dossier.FicheDossier">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_gravity="center|top"
android:gravity="center|top">
<TextView
android:id="#+id/txtNomUsageDossierFragment"
android:layout_width="match_parent"
android:layout_height="86dp"
android:background="#drawable/fond_jaune_top"
android:fontFamily="#font/montserrat_bold"
android:gravity="center_horizontal"
android:onClick="backList"
android:paddingTop="21dp"
android:text="Dossiers"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="normal" />
<ImageView
android:id="#+id/btn_Back"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="23dp"
app:srcCompat="#drawable/icone_arrow_left_blanc" />
<EditText
android:id="#+id/txtFindDocument"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="24dp"
android:layout_marginTop="62dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="4dp"
android:height="48dp"
android:background="#drawable/textedit_blanc_rounded_corners"
android:drawableLeft="#drawable/icone_recherche_jaune"
android:drawablePadding="6dp"
android:elevation="4dp"
android:ems="10"
android:hint="#string/txt_input_recherche"
android:inputType="text"
android:maxLines="1"
android:outlineSpotShadowColor="#color/jauneSombre"
android:paddingLeft="10dp"
android:scrollHorizontally="true"
android:textStyle="italic" />
<ProgressBar
android:id="#+id/progressBarTopListDocument"
style="?android:attr/progressBarStyle"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_marginTop="23dp"
android:layout_marginRight="23dp"
android:theme="#style/colorProgressBarTop"
/>
</RelativeLayout>
<TextView
android:id="#+id/msgListDocument"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="2dp"
android:fontFamily="#font/montserrat"
android:text="#string/txt_msg_dossiers"
android:textColor="#color/neutre"
android:textStyle="bold"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_classeurs_dossier"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="12dp"
android:layout_marginRight="24dp"
android:divider="#drawable/rectangle_border_bottom"
android:dividerHeight="1dp"
android:isScrollContainer="false"
android:nestedScrollingEnabled="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_docs_dossier"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:divider="#drawable/rectangle_border_bottom"
android:dividerHeight="1dp"
android:isScrollContainer="false"
android:nestedScrollingEnabled="false" />
</LinearLayout>

You have to use only one RecyclerView and use the type to manage which card you want to recall
public class RecyclerViewAdapterClasseursDocs extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<Document> documents;
private final List<Classeur> classeurs;
private final Context context;
public RecyclerViewAdapterClasseursDocs(List<Classeur> classeurs, List<Document> documents, Context context) {
this.classeurs = classeurs;
this.context = context;
this.documents = documents;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == 0) {
return new ViewHolderClasseur(LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.classeur, parent, false));
}
return new ViewHolderDoc(LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.docs, parent, false));
}
#Override
public int getItemViewType(int position) {
if (position < this.classeurs.size()){
return 0;
} else {
return 1;
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//Doc
if (holder.getItemViewType() == 0) {
//Classeur
ViewHolderClasseur c = (ViewHolderClasseur) holder;
final Classeur classeur = classeurs.get(position);
c.titleClasseur.setText(classeur.getNom());
} else {
ViewHolderDoc d = (ViewHolderDoc) holder;
final Document document = documents.get(position - classeurs.size());
d.titleDocument.setText(document.getIntitule());
}
}
#Override
public int getItemCount() {
return classeurs.size()+documents.size();
}
public static class ViewHolderClasseur extends RecyclerView.ViewHolder {
private final TextView titleClasseur;
private final View linearLayoutClasseur;
public ViewHolderClasseur(#NonNull View itemView) {
super(itemView);
linearLayoutClasseur = itemView.findViewById(R.id.rowClasseur);
titleClasseur = itemView.findViewById(R.id.txtClasseurTitle);
}
}
public static class ViewHolderDoc extends RecyclerView.ViewHolder {
private final TextView titleDocument;
public ViewHolderDoc(#NonNull View itemView) {
super(itemView);
itemView.findViewById(R.id.rowDocument);
titleDocument = itemView.findViewById(R.id.txtDocTitle);
itemView.findViewById(R.id.txtDocData);
}
}
}

Related

the fragments do not replace each other but are superimposed android studio

I'm trying to replace a fragment in a tabbed activity with another, when I search online I find people using FragmentLayout or View but I can't find a solution that works. This is my first application, sorry if the code is not optimized.
MainActivity.java :
`
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public ViewPager viewPager;
public FragmentAbstract fragmentActuel;
public FragmentAbstract fragmentAncien;
public VPAdapter vpAdapter;
public String message;
public ViewModelFactory viewModelFactory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//on récupere le container des tableau par son id
TabLayout tabLayout = findViewById(R.id.tabs);
//page de base
viewPager = findViewById(R.id.view_pager);
//on definit la page de base
tabLayout.setupWithViewPager(viewPager);
viewModelFactory = new ViewModelFactory(this);
//on ajouter les fragments dans la page de base
this.vpAdapter = new VPAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.fragmentActuel = new Fragment_repetition_home(this);
this.fragmentAncien = fragmentActuel;
vpAdapter.addfragment(fragmentActuel,"REPETITION");
vpAdapter.addfragment(new Fragment_technique_home(this),"TECHNIQUE");
vpAdapter.addfragment(new Fragment_figure_home(this),"FIGURE");
viewPager.setAdapter(vpAdapter);
//pour dire que l'on veut 3 pages en arrière plan
viewPager.setOffscreenPageLimit(3);
}
}
VPAdapter.java :
public class VPAdapter extends FragmentPagerAdapter {
private ArrayList<FragmentAbstract> fragmentArrayList = new ArrayList<>();
private final ArrayList<String> fragmentTitle = new ArrayList<>();
public VPAdapter(#NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
#NonNull
#Override
public Fragment getItem(int position) {
return fragmentArrayList.get(position);
}
#Override
public int getCount() {
return fragmentArrayList.size();
}
public void addfragment(FragmentAbstract fragment, String title){
fragmentArrayList.add(fragment);
fragmentTitle.add(title);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitle.get(position);
}
//pour changer les fragments dans la liste
public void changeFragment(MainActivity mainActivity) {
if (!mainActivity.fragmentAncien.equals(mainActivity.fragmentActuel)) {
int index = fragmentArrayList.indexOf(((FragmentAbstract) mainActivity.vpAdapter.getItem(mainActivity.viewPager.getCurrentItem())));
fragmentArrayList.set(index,mainActivity.fragmentActuel);
}
}
}
activity_main.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" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.MonProgramme.AppBarOverlay">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/app_name"
android:textAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REPETITION"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIGURE"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TECHNIQUE"/>
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
fragment_figure_home.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/figure_home"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/selection_figure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ListView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/button_plus_figure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="#dimen/fab_margin"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_plus"
android:backgroundTint="#color/grey_2"
android:onClick="onClick"/>
</FrameLayout>
Do you have a idea for replace the fragment not superimposed ?

android studio navigtion not woring when movieng with fragments in java

so i wanted to creat a footer navigtion on the main activity and a fragment view above it
but now when i try to use the normal navigtion with the navigtion grafh it just crashes.
mainActivity java code:
package com.example.mywatchlist;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button homeBtn = findViewById(R.id.homeBtn);
Button profileBtn = findViewById(R.id.profileBtn);
Button socialBtn = findViewById(R.id.theSocialSearchBtn);
Button contentBtn = findViewById(R.id.contentContentSearchBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
homeFragment home = new homeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,home).commit();
}
});
profileBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
userProfileFragment profile = new userProfileFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,profile).commit();
}
});
socialBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
socialSearchFragment social = new socialSearchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,social).commit();
}
});
contentBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
contentSearchFragment contentSearch = new contentSearchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,contentSearch).commit();
}
});
}
}
main activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="632dp"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation" />
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Go to\nHome"
android:textSize="12dp" />
<Button
android:id="#+id/profileBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="My\nProfile"
android:textSize="12dp" />
<Button
android:id="#+id/theSocialSearchBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Search for\nPeople"
android:textSize="12dp" />
<Button
android:id="#+id/contentContentSearchBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Search for\nContent"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
and now it crashes when i try to move in this fragment:
fragment to move from java code:
package com.example.mywatchlist;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {#link Fragment} subclass.
* Use the {#link contentSearchFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class contentSearchFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public contentSearchFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment contentSearchFragment.
*/
// TODO: Rename and change types and number of parameters
public static contentSearchFragment newInstance(String param1, String param2) {
contentSearchFragment fragment = new contentSearchFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content_search,container, false);
// Inflate the layout for this fragment
Button searchBtn = view.findViewById(R.id.contentContentSearchBtn);
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_contentSearchFragment_to_contentFragment);
}
});
return view;
}
}
and this is the 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".contentSearchFragment" >
<TextView
android:id="#+id/contentSearchTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C4FF80"
android:gravity="center"
android:text="Content search"
android:textSize="30dp" />
<Button
android:id="#+id/contentSearchAddNewContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add new content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFEB3B"
android:orientation="horizontal">
<EditText
android:id="#+id/contentSearchBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#C8C8C8"
android:ems="10"
android:hint="Search for content"
android:inputType="textPersonName"
android:minHeight="48dp" />
<Button
android:id="#+id/contentContentSearchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="502dp">
<TextView
android:id="#+id/contentSearchData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntest" />
</ScrollView>
</LinearLayout>
and this is the navigtion xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation"
app:startDestination="#id/startFragment">
<fragment
android:id="#+id/startFragment"
android:name="com.example.mywatchlist.startFragment"
android:label="fragment_start"
tools:layout="#layout/fragment_start" >
<action
android:id="#+id/action_startFragment_to_logInFragment"
app:destination="#id/logInFragment" />
<action
android:id="#+id/action_startFragment_to_registerFragment"
app:destination="#id/registerFragment" />
</fragment>
<fragment
android:id="#+id/logInFragment"
android:name="com.example.mywatchlist.logInFragment"
android:label="fragment_log_in"
tools:layout="#layout/fragment_log_in" >
<action
android:id="#+id/action_logInFragment_to_homeFragment"
app:destination="#id/homeFragment" />
</fragment>
<fragment
android:id="#+id/registerFragment"
android:name="com.example.mywatchlist.registerFragment"
android:label="fragment_register"
tools:layout="#layout/fragment_register" >
<action
android:id="#+id/action_registerFragment_to_homeFragment"
app:destination="#id/homeFragment" />
</fragment>
<fragment
android:id="#+id/homeFragment"
android:name="com.example.mywatchlist.homeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/userProfileFragment"
android:name="com.example.mywatchlist.userProfileFragment"
android:label="fragment_user_profile"
tools:layout="#layout/fragment_user_profile" />
<fragment
android:id="#+id/contentFragment"
android:name="com.example.mywatchlist.contentFragment"
android:label="fragment_content"
tools:layout="#layout/fragment_content" />
<fragment
android:id="#+id/contentSearchFragment"
android:name="com.example.mywatchlist.contentSearchFragment"
android:label="fragment_content_search"
tools:layout="#layout/fragment_content_search" >
<action
android:id="#+id/action_contentSearchFragment_to_contentFragment"
app:destination="#id/contentFragment" />
</fragment>
<fragment
android:id="#+id/socialSearchFragment"
android:name="com.example.mywatchlist.socialSearchFragment"
android:label="fragment_social_search"
tools:layout="#layout/fragment_social_search" >
<action
android:id="#+id/action_socialSearchFragment_to_userProfileFragment"
app:destination="#id/userProfileFragment" />
</fragment>
</navigation>
crashing:
--------- beginning of crash
2022-07-01 14:32:49.986 25363-25363/com.example.mywatchlist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mywatchlist, PID: 25363
java.lang.IllegalStateException: View com.google.android.material.button.MaterialButton{3bedc11 VFED..C.. ...P.... 921,5-1440,173 #7f080237 app:id/contentContentSearchBtn} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.kt:71)
at com.example.mywatchlist.contentSearchFragment$1.onClick(contentSearchFragment.java:73)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2022-07-01 14:32:50.027 25363-25363/com.example.mywatchlist I/Process: Sending signal. PID: 25363 SIG: 9
thank you to all the helpers
can you please share the crashing error? It's hard to figure out the error. If you can also add how to reproduce the crash it can be very helpful
You forgot to initialize NavHostFragent
so kindly Initiliaze In your OnCreate
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id. fragmentContainerView);
NavController navController = navHostFragment.getNavController();
and for the navigate one fragment to another fragment use navigate
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {navController.navigate(R.id. action_startFragment_to_registerFragment
}
});)

Recyclerview animation not working in activity

i have an activity that contains 2 fragments
StepsFragment contains a Recyclerview but the Recyclerview's animation is not working
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:background="#B3D1DB"
tools:context=".StepsFragment">
<TextView
android:id="#+id/inputEntred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="invisible" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/RV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:layoutAnimation="#anim/layou_animation_fall_down"
android:paddingTop="10dp"
android:paddingBottom="30dp" />
</RelativeLayout>
layou-animation-fall-down
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/fall_down"
android:animationOrder="normal"
android:delay="15%">
fall-down
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="6000">
<translate
android:fromYDelta="-20%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toYDelta="0"
android:duration="1000" />
<alpha
android:fromAlpha="0"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toAlpha="3"
android:duration="1000"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="100%"
android:toYScale="100%" android:duration="1000"
/>
</set>
activity layout
<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=".Sorting_activity">
<RelativeLayout
android:id="#+id/triLayout"
android:layout_width="0dp"
android:layout_height="300dp"
app:layout_constraintBottom_toTopOf="#+id/stepsLayout"
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="0.0">
</RelativeLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/stepsLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/triLayout"></androidx.coordinatorlayout.widget.CoordinatorLayout>
StepsFragment
public class StepsFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private static TextView inputEntred ;
private static RecyclerView RV ;
private Handler mhandler = new Handler();
public StepsFragment() {
// Required empty public constructor
}
public void setTextView(String textentered){
inputEntred.setText(textentered);
String[] numberList = inputEntred.getText().toString().split(",");
final Integer[] numbers = new Integer[numberList.length];
SelectionSort m1 = new SelectionSort();
ArrayList<String> mystepsList = m1.steps(numbers);// returns the steps numbers
final StepListAdapter adapter = new StepListAdapter() ;
RV.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
RV.setLayoutManager(llm);
adapter.setList(mystepsList);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_steps, container, false);
inputEntred =view.findViewById(R.id.inputEntred);
RV =view.findViewById(R.id.RV);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
// mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}}
Could you try this? I have to remove unnecessary duration in the XML. Thanks
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250">
<translate
android:fromYDelta="-20%"
android:toYDelta="0"
android:interpolator="#android:anim/decelerate_interpolator"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="#android:anim/decelerate_interpolator"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="#android:anim/decelerate_interpolator"
/>
</set>

Android - Recycler View Focuses Wrong Holder on EditText click

I have a Recycler View to show a list of data and have edit text inside of Recycler View to allow user edit some number.
My app also resizing activity when the keyboard is shown/hidden.
The app works great unless I want to edit items which are placed at the bottom. When I click to edit text, it resizes the screen, shows keyboard, focuses holder but wrong one: usually one before and sometimes 2.
I tried to track issue, and understand that LinearLayoutMnager's onFocusSearchFailed function returns the wrong view.
Any idea why could it be?
Adapter
private final LinearLayoutManager linearLayoutManager;
public Observable<double[][]> observable;
private ObservableEmitter<double[][]> observe;
List<Invoice> invoices = Collections.emptyList();
double[][] amounts;
Context context;
public AdjustInvoiceAdapter(List<Invoice> list, Context context, LinearLayoutManager linearLayoutManager) {
this.invoices = list;
this.context = context;
this.linearLayoutManager = linearLayoutManager;
amounts = new double[invoices.size()][1];
for (int i = 0; i < list.size(); i++) {
amounts[i][0] = invoices.get(i).getAmountDue();
}
observable = Observable.create(new ObservableOnSubscribe<double[][]> (){
#Override
public void subscribe(ObservableEmitter<double[][]> emitter) throws Exception {
observe = emitter;
}
});
}
#Override
public InvoiceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_adjust_invoice_payment_amount, parent, false);
InvoiceHolder holder = new InvoiceHolder(view);
return holder;
}
#Override
public void onBindViewHolder(InvoiceHolder holder, final int position) {
holder.update(invoices.get(position), amounts[position][0]);
holder.amount.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0 || (s.length() == 1 && (s.toString().charAt(0)) == '-'))
amounts[position][0] = 0.0;
else
amounts[position][0] = Double.parseDouble(s.toString());
observe.onNext(amounts);
}
});
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return invoices.size();
}
Holder
public class InvoiceHolder extends RecyclerView.ViewHolder {
EditText amount;
TextView invoiceAmountDue;
TextView invoiceId;
TextView invoiceDueDate;
TextInputLayout textInputLayout;
private NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.US);
public InvoiceHolder(View itemView) {
super(itemView);
invoiceId = (TextView) itemView.findViewById(R.id.invoice_number_text_view);
invoiceDueDate = (TextView) itemView.findViewById(R.id.invoice_due_text_view);
invoiceAmountDue = (TextView) itemView.findViewById(R.id.invoice_amount_due_text_view);
amount = (EditText) itemView.findViewById(R.id.editText);
textInputLayout = (TextInputLayout) itemView.findViewById(R.id.text_input_layout);
}
void update(Invoice invoice, double currentAmount) {
textInputLayout.setHint("(" + numberFormat.format(invoice.getAmountDue())+")");
invoiceId.setText(invoice.getinvoiceId());
invoiceDueDate.setText(invoice.getInvoiceDueDate());
amount.setText(currentAmount);
}
}
Layout (RecyclerView doesn't have any attribute)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:focusableInTouchMode="true"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:gravity="center_vertical">
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_main_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5">
<TextView
android:id="#+id/invoice_number_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/red_warning_image_view"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginRight="5dp"
android:backgroundTint="#color/ebiz_red"
android:tint="#color/ebiz_red"
android:visibility="gone"
app:srcCompat="#drawable/ic_error_black_24dp" />
<TextView
android:id="#+id/invoice_due_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:text="TextView"
android:textColor="#color/ebiz_gray_medium"
android:textSize="12sp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/linearLayout4"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.511">
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout"
android:layout_width="100dp"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="143dp"
tools:layout_editor_absoluteY="0dp">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="numberDecimal"
android:text="$100"
android:focusableInTouchMode="true"
android:textColor="#android:color/black"
android:textSize="18sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

getBaseline of TextView returns -1

I am trying to align two TextView one is inside HorizontalScrollView, and both are children of LinerLayout .
When I am trying to call getBaseLine of TextView inside onWindowAttached of RecyclerView Adapter it always returns -1.
Yes, the parent view of TextView, LinerLayout has android:baselineAligned="true"
Edit : Adding Code
Layout
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dp"
android:paddingEnd="10dp"
android:baselineAligned="true"
android:paddingStart="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/outerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/inlineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
/>
</HorizontalScrollView>
</LinearLayout>
Code in RecyclerView Adapter
#Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder)
{
super.onViewAttachedToWindow(holder);
if(holder instanceof MyHolder)
{
dostuff((MyHolder)holder);
}
}
private void dostuff(QACommentViewHolder holder)
{
int baseline = holder.outerTextView.getBaseline();
}
View is not measured when onViewAttachedToWindow is called, hence it is returning -1.
Better approach would be.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
.
.
.
viewHolder.itemView.post(new Runnable()
{
//put your code here

Resources