1 year ago
#284234
Chief Simz
Button onClick in fragment to open new fragment not working
I'm working on a Bible app divided by Books -> Chapter -> Verse. When I attempted to create an onClick listener for the Book of Genesis, the button is unresponsive. I'm attempting to create a onclick inside one fragment to go to another fragment , book -> chapters.
Clicking on Genesis should take me to a different scroll view fragment that contains all 50 chapters.
For context, fragment1 is the the first tab , fragment2 is second tab , fragment3 is 3rd tab
I believe the issue is with handling the onButtonSelected method in MainActivity,
public class MainActivity extends AppCompatActivity implements fragment1.onGenesisSelected{
private TabLayout tabLayout;
private ViewPager viewPager;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tablayout);
viewPager= findViewById(R.id.viewpager);
tabLayout.setupWithViewPager(viewPager);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),
FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
viewPagerAdapter.addFragment(new fragment1(),"Old Covenant");
viewPagerAdapter.addFragment(new fragment2(),"Apocrypha");
viewPagerAdapter.addFragment(new fragment3(),"New Covenant");
viewPager.setAdapter(viewPagerAdapter);
}
@Override
public void onButtonSelected() {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.viewpager,new GenesisChapters());
fragmentTransaction.commit();
}
activity_main.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"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="409dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OLD COVENANT" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="APOCRAPHYA" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEW COVENANT" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tablayout"
app:layout_constraintVertical_bias="0.0"
app:flow_horizontalBias="0.5"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I have included fragment1.java where I am creating the onClicklistener for the book of Genesis
public class fragment1 extends Fragment {
private onGenesisSelected listener1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
Button GenesisBtn = view.findViewById(R.id.Genesis);
GenesisBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener1.onButtonSelected();
}
});
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof onGenesisSelected){
listener1 = (onGenesisSelected) context;
}else{
throw new ClassCastException(context.toString() + " must implement listener");
}
}
public interface onGenesisSelected{
public void onButtonSelected();
}
}
fragment1.xml
<FrameLayout 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"
tools:context=".fragment1"
android:id="@+id/frag1">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/OldCovScroll">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/Genesis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Genesis" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Lastly, GenesisChapters.java i.e. where the on click should send you to
public class GenesisChapters extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_genesis_chapters, container, false);
}
}
And fragment_genesis_chapters.xml
<FrameLayout 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"
tools:context=".GenesisChapters"
android:id="@+id/GenChapters">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/GenCh1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chapter 1" />
</LinearLayout>
</ScrollView>
</FrameLayout>
android
android-fragments
scrollview
onclicklistener
android-tablayout
0 Answers
Your Answer