I have a Frangent class to which I am trying to put ImageButton buttons so that when choosing one of them I show another Fragment class
This is the code with which I'm testing now, which does not do anything, well, when you click on the icons the application closes.
public class Menu_6 extends Fragment implements View.OnClickListener{
Button botonRetraso1;
Button botonRetraso2;
private Context context;
public Menu_6() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate( R.layout.menu_6, container, false );
botonRetraso1 = rootView.findViewById(R.id.btRetraso1);
botonRetraso2 = rootView.findViewById(R.id.btRetraso2);
botonRetraso1.setOnClickListener(this);
botonRetraso2.setOnClickListener(this);
return rootView;
}
public void onClick(View v) {
switch (v.getId()){
case R.id.btRetraso1:
cargarFragment(new Retrasos1());
break;
case R.id.btRetraso2:
cargarFragment(new Retrasos2());
break;
}
}
private void cargarFragment(Fragment fragment) {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_container, fragment).addToBackStack(null).commit();
}
}
Notes:
- I removed the error message, because now it shows none
- I've changed ImageButton to Button , from the code buttonRelay1 = (Button) rootView.findViewById (R.id.btRelay1); I've deleted (Button) it seems to be unnecessary.
Code of Delays1.java
public class Retrasos1 extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
WebView mWebView;
private OnFragmentInteractionListener mListener;
public Retrasos1() {
}
public static Retrasos newInstance(String param1, String param2) {
Retrasos1 fragment = new Retrasos1();
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.retrasos1, container, false );
mWebView = view.findViewById(R.id.webView1 );
mWebView.getSettings().setJavaScriptEnabled( true );
mWebView.getSettings().setAppCacheEnabled( true );
mWebView.getSettings().setBuiltInZoomControls( true );
mWebView.getSettings().setDisplayZoomControls( false );
mWebView.getSettings().setSupportZoom( true );
mWebView.getSettings().setDefaultZoom( WebSettings.ZoomDensity.FAR );
mWebView.getSettings().setLoadWithOverviewMode( true );
mWebView.getSettings().setUseWideViewPort( true );
mWebView.setInitialScale(1);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.setScrollbarFadingEnabled(false);
mWebView.loadUrl( "https://www.google.es/" );
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
delays1.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.SoCu.CirDocu.Retrasos1">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
I would appreciate it if you can help me with the code to open a Fragment from another Fragment using buttons
Greetings.