How to Open a Fragment on Button Click from a Fragment in Android

How to open a Fragment on button click from a fragment in Android

You can replace the fragment using FragmentTransaction on button click. Something like this:

  Fragment someFragment = new SomeFragment(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();

Learn about performing fragment transactions here.

code:

  package com.rupomkhondaker.sonalibank;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HomeFragment extends Fragment implements View.OnClickListener {

public HomeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_home, container, false);

Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);

aboutBtn.setOnClickListener(this);
phonebookBtn.setOnClickListener(this);

return rootView;
}

@Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.aboutusButton:
fragment = new AboutFragment();
replaceFragment(fragment);
break;

case R.id.phbookButton:
fragment = new PhoneBookFragment();
replaceFragment(fragment);
break;
}
}

public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}

}

How to open a fragment from a button click which is included in another layout

you have to set OnClickListener for this Button... use editProfileButton.setOnClickListener(this); - this is used as your Fragment implements View.OnClickListener

edit: well, your stacktrace is saying everything...

IllegalArgumentException: adjustViewBounds not supported.
at de.hdodenhof.circleimageview.CircleImageView.setAdjustViewBounds(CircleImageView.java:141)

just remove android:adjustViewBounds="true" line from XML-declared CircleImageView. and also remove android:scaleType="centerCrop". read THIS article, in which you can find:

The ScaleType of CircleImageView is always CENTER_CROP and if we try to change it we get an exception. So it is perfect for the profile pictures.

Enabling adjustViewBounds is not supported as this requires an unsupported ScaleType

How to open a fragment on click of a button?

In your activity_layout.xml you should add a linearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

</LinearLayout>

Then the rest is up to your code

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Fragment newCase=new NewCase();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container,newCase); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();

}
});

Notice the Layout id in the xml and containe id same

Button Click inside Fragment to open up new fragment

Try this:

From below code change id show_fragment to match with your layout XML id..

 @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearch:
//what to put here
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.show_fragment, new TestFragment(), "fragment_screen");
ft.commit();
break;
}
}

<FrameLayout
android:id="@+id/show_fragment"
android:layout_width="match_parent"
android:layout_height="0dp">

</FrameLayout>

How can I move from one fragment to another fragment on button click? (kotlin)

Moving from one fragment to another can be easily achieved using Navigation component.
I would suggest taking a look at this link as a starting point:

https://developer.android.com/guide/navigation/navigation-getting-started

Open fragment from Fragment on button click

There some problems here.
First, you have to add a container with the id R.id.fragment_container inside your fragment like FrameLayout
which will store your new fragment.

If your want to open a fragment as a new screen, you have to put it inside a new activity. Fragments are piece of screens and should not be used without activities or view pagers.

Have a look at the Android deverlopers page: http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

Basically, you define an interface in your Fragment A, and let your Activity implement that Interface. Now you can call the interface method in your Fragment, and your Activity will receive the event. Now in your activity, you can call your second Fragment to update the textview with the received value

// You Activity implements your interface
public class YourActivity implements FragmentA.TextClicked{
@Override
public void sendText(String text){
// Get Fragment B
FraB frag = (FragB)
getSupportFragmentManager().findFragmentById(R.id.fragment_b);
frag.updateText(text);
}
}

// Fragment A defines an Interface, and calls the method when needed
public class FragA extends Fragment{

TextClicked mCallback;

public interface TextClicked{
public void sendText(String text);
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (TextClicked) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement TextClicked");
}
}

public void someMethod(){
mCallback.sendText("YOUR TEXT");
}

@Override
public void onDetach() {
mCallback = null; // => avoid leaking, thanks @Deepscorn
super.onDetach();
}
}

// Fragment B has a public method to do something with the text
public class FragB extends Fragment{

public void updateText(String text){
// Here you have it
}
}


Related Topics



Leave a reply



Submit