Unable to see anything when changing the fragment from another fragment ,but works fine with selecting the Tab.
Here is my code which I am calling on button click:
With Main Activtyxml and Fragment Xml
private Button btnskip ;
btnskip = (Button)rootView.findViewById(R.id.skipbutton);
btnskip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.viewpager_main, new CategoryListingFragment());
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(null);
transaction.commit();
}
});
> **this Main Activity xml file which changes the fragment on Tab click**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root_view_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scu.smartgrocerytracker.welcome.WelcomeClassesTabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/tab_background"
app:tabIndicatorColor="#color/tab_indicator" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tab_layout" />
</RelativeLayout>
Related
I want to switch from activity to fragment after clicking the button.application crashes when button is clicked and gives this error
For transition I wrote it in acitvity
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction= getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.notesFragment,new NotesFragment());
fragmentTransaction.commit();
}
});
fragment_notes.xml
i gave id as "noteFragment"
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.NotesFragment"
android:background="#color/mainDarkBlue"
android:id="#+id/notesFragment">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:fabSize="auto"
android:layout_margin="#dimen/fab_margin"
android:backgroundTint="#color/rossyBrown"
android:src="#drawable/ic_fab_add_note"
android:id="#+id/button_fab"/>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</FrameLayout>
You need to pass your parent class id to inflate your fragment not your fragment name itself
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction= getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.parentLayoutId, new NotesFragment());
//Change R.id.notesFragment to your parent class main layout id
fragmentTransaction.commit();
}
});
Something Like
<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">
<Button
android:id="#+id/BtnRedirectFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Default Fragment" />
<!-- Frame Layout for placing Fragments -->
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
I have a activity_settings.xml file like below in this file I have 3 text views if I click one it will take me to a fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.crowderia.chat.SettingsActivity"
android:background="#color/black">
<TextView
android:id="#+id/tv_added_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Added Me" />
<TextView
android:id="#+id/tv_add_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Friends" />
<TextView
android:id="#+id/tv_my_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Friends" />
<FrameLayout
android:id="#+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
My SettingsActivity.class like below
onclick event for each text view
tv_added_me.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setFragment(new AddedMeFragment());
}
});
set fragment method
public void setFragment(Fragment f) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragmentTransaction.replace(R.id.settings_fragment, f);
fragmentTransaction.commit();
}
If I click each text view it will take me to each fragment but after I click back its not going to go to Settings Activity how can I make this work
Please help me Im new to these stuff
You should remove all the fragments from FragmentManager on the click of back button.
FragmentManager fm = getSupportFragmentManager();
for (Fragment f : fm.getFragments()) {
fm.beginTransaction().remove(f).commit();
}
I am working in a Fragment. it contains 2 Buttons and a FrameLayout:
<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="fragments.Fragment_Book_A_Cam">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/bt_Public"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#drawable/red"
android:text="Public Jobs "
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bt_Active"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#drawable/black"
android:text="Active Jobs"
android:textColor="#FFFFFF" />
</LinearLayout>
<FrameLayout
android:id="#+id/Fragment_Jobs_BookACam"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
I want to load another Fragment in the FrameLayout on a Button click and don't want to replace the main FrameLayout.
All the methods I have used either crash or replace the main Fragment.
bt_Public.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// fragment = new Fragment_MyCloudVideo();
// FragmentManager fragmentManager = ;
// FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.replace(R.id.Fragment_MyVideos, fragment);
// fragmentTransaction.commit();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_Public_Jobs fragment = new Fragment_Public_Jobs();
fragmentTransaction.addToBackStack("xyz");
fragmentTransaction.hide(Fragment_Book_A_Cam.this);
fragmentTransaction.add(android.R.id.content, fragment);
fragmentTransaction.commit();
}
});
You never seem to use the Fragment_Jobs_BookACam FrameLayout id
If you want to show Fragments inside Fragments, you need to use the method to get the child FragmentManager
First of all correct the errors present in your xml file and try replace() method of fragmentTransaction instead add().
I've been trying to solve this problem for a few days now and still cant figure it out. I've checked many similiar topics about this but didnt get me what I need.
As you can see I'm adding the fragments dynamically:
public void displayFragment1() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Fragment_1(), "frag1");
fragmentTransaction.commit();
}
public void displayFragment2() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2");
fragmentTransaction.commit();
}
Here is the part that I cant figure out. I'm searching the fragments by tag and it seems that its not working since I'm always getting nullpointer exception or classexception. Toast just shows the value of the text that I entered so its sending the data from a fragment to the activity. so that part is fine. Any ideas guys???
#Override
public void inputValue(String text) {
if (text != null) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2");
fragmentTransaction.commit();
FragmentManager fm = getSupportFragmentManager();
Fragment_2 fragment = (Fragment_2) fm.findFragmentByTag("frag2");
fragment.setChangeTo(text);
} else {
Toast.makeText(MainActivity.this, "didnt recieve the value", Toast.LENGTH_SHORT).show();
}
}
activity main xml
<RelativeLayout
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.neven.question_for_stackoverflow.MainActivity"
android:background="#drawable/my_background">
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
MainActivity:
public class MainActivity extends AppCompatActivity implements Fragment_1.sendData {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayFragment1();
}
fragment 1 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="com.example.neven.question_for_stackoverflow.Fragment_1"
android:background="#drawable/my_background">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="fragment 1"
android:id="#+id/textView" android:layout_gravity="center"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/etInputID" android:layout_gravity="center" android:layout_marginTop="50dp"
android:hint="enter text here" android:textColorHint="#e3ca0a"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/bSendID" android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="130dp"/>
fragment 2 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="com.example.neven.question_for_stackoverflow.Fragment_2"
android:background="#drawable/my_background">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="fragment 2 - change me"
android:id="#+id/tvChangeMeID" android:layout_gravity="center"/>
The issue seems to be that you are calling displayFragment1(); in your Activity's onCreate and yet you are expecting to find Fragment_2 which you are only setting/initializing in displayFragment2(); - you never call this method.
So, please change your code in the onCreate method to call displayFragment2() like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayFragment2();
}
UPDATE:
Please add the call getSupportFragmentManager().executePendingTransactions() after the commit statement and then try looking up the Fragment by TAG and see if this at least returns a Fragment object and not null. Your code will now look like:
...
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2");
fragmentTransaction.commit();
getSupportFragmentManager().executePendingTransactions()
Fragment_2 fragment = (Fragment_2) getSupportFragmentManager().findFragmentByTag("frag2");
fragment.setChangeTo(text);
I hope this helps.
You should declare a FrameLayout in your Activity's layout xml where the fragments you want will be inflated. Then use that FrameLayout's id when making a transaction instead of android.R.id.content.
The way to handle this is:
Fragment fragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);
if (fragment instanceof YourFragmentClass) {
fragment.setChangeTo(value);
}
I can't replace the fragments using the codes below. I mean that the second fragment appeared under the first fragment. The first fragmnent didn't hide as I expected. What is wrong here?
Thanks a lot!
My activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentManager fm = this.getFragmentManager();
final Fragment fragmenttwo = new FragmentTwo();
Button fmchangebutton = (Button) this.findViewById(R.id.fmchangebutton);
fmchangebutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1, fragmenttwo);
ft.commit();
}
});
};
};
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/fmchangebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Fragment" />
<fragment
android:id="#+id/fragment1"
android:name="com.example.fragementex.FragmentOne"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
fragmnetone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="clip_horizontal"
android:ems="10"
android:gravity="center_vertical|center_horizontal|top|fill_vertical"
android:text="This is Fragment One" >
</EditText>
</LinearLayout>
fragmnettwo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="clip_horizontal"
android:ems="10"
android:gravity="center_vertical|center_horizontal|top|fill_vertical"
android:text="This is Fragment Two" >
</EditText>
</LinearLayout>
You cannot replace a fragment attached to the layout, you need to create a host container as FrameLayout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/fmchangebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Fragment" />
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then, you will able to - first - add and display the first fragment and replace it with the second as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the layout which contains the framelayout
setContentView(R.layout.activity_main);
final FragmentManager fm = this.getFragmentManager();
final Fragment fragmentone = new FragmentOne();
final Fragment fragmenttwo = new FragmentTwo();
// if first initialization
if(savedInstanceState == null) {
// display with "add" the first fragment
this.getFragmentManager().beginTransaction()
.add(R.id.frame_layout, fragmentone).commit();
}
Button fmchangebutton = (Button) this.findViewById(R.id.fmchangebutton);
fmchangebutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
FragmentTransaction ft = fm.beginTransaction();
// "replace" the fragment inside the framelayout
ft.replace(R.id.frame_layout, fragmenttwo);
ft.commit();
}
});
}