replace view pager fragment with another fragment - android

So I am building an Android application which utilize view pager code. The first view of the view pager is a list where user can choose one and view more detail regarding items of which the user selected. I have managed to successfully do this using fragment inside fragment method.
My current issue is with the third view of the view pager. the third view show a summary of action that has been done in the first view and then user can click a button which will replace the current third view with another fragment. The code I used is identical with the one i used in the first view, which is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.cart_fragment_layout, container, false);
btn_1 = (Button) rootView.findViewById(R.id.btn_1);
btn_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fm = getChildFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
CF = new CustomFragment();
ft.addToBackStack("fragment_a");
ft.add(R.id.fragment_custom_container, CF, "custom_fragment1");
ft.commit();
fm.executePendingTransactions();
}
});
return rootView;
}
The onCreateView code for the container of the fragment is as follow:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
try{
rootView = inflater.inflate(R.layout.cart_container, container, false);
fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
CF = new CustomFragment();
Bundle arguments = new Bundle();
ft.addToBackStack("somewords");
ft.add(R.id.fragment_custom_container, CF, "custom_fragment");
CF.setRetainInstance(true);
ft.commit();
fm.executePendingTransactions();
}
catch(Exception E)
{
E.printStackTrace();
System.out.println("exception:"+E.getMessage());
}
return rootView;
}
Whereas the xml layout file of the container is as follow:
<?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"
android:id="#+id/ll_fragment_custom_layout"
android:background="#fff">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_custom_container"
/>
</LinearLayout>
However the code did not work and I received exception:
05-07 12:29:54.570: E/AndroidRuntime(7365): java.lang.IllegalArgumentException: No view found for id 0x7f060045 (com.example.fragmentname:id/fragment_cart_container) for fragment fragmenta{42bae2a0 #0 id=0x7f060045 fragment b}
Is there any error that I made?

Related

Android - set button text from Fragment in Activity

I created new project with Navigation Drawer Activity template in Android Studio 2.2.2. I decided to use FrameLayout, so as i click on navigation menu item, new fragment loads.
In my content_main.xml i have FrameLayout element like this:
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
That is Frame where the content will be shown.
Then i have layout fragment_one.xml and its class FragmentOne and they look like this:
fragment_one.xml
<Button
android:text="Default Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/mybutton" />
FragmentOne class
public class FragmentOne extends Fragment {
Button btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
btn = (Button) view.findViewById(R.id.mybutton);
return view;
}
public void changeBtnText(String txt)
{
btn.setText(txt);
}
}
In MainActivity's onCreate() method i have code:
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new FragmentOne()).commit();
FragmentOne fragment_obj = (FragmentOne)getSupportFragmentManager().
findFragmentById(R.id.frOne);
fragment_obj.changeBtnText("Changed text");
Here i get error:
Attempt to invoke virtual method 'void com.example.user.myapplication.FragmentOne.changeBtnText(java.lang.String)' on a null object reference
How I can change fragment's Button text from Main Activity by using changeBtnText(String txt) method?
Call changeBtnText(String txt) inside fragment not in the activity.
EDIT:
inside FragmentOne.class
public FragmentOne newInstance(String text) {
FragmentOne fragmentOne = new FragmentOne ();
Bundle args = new Bundle();
args.putInt("text", text);
fragmentOne .setArguments(args);
return fragmentOne ;
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
btn = (Button) view.findViewById(R.id.mybutton);
String text = getArguments().getString("text", null);
changeBtnText(text);
return view;
}
in the Activity
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, FragmentOne.newInstance("text")).commit();
You haven't created the Fragment, which is why it's null. You create a new one in line but don't save it.
Try this;
FragmentOne fragment_obj = new FragmentOne();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, fragmentOne).commit();
fragment_obj.changeBtnText("Changed text");

Load view from fragment when the fragment loaded

I have button to go to (load) this new fragment
buttonToFragment1.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View arg0) {
// return inflater.inflate( R.layout.fragment_one, container, false);
Fragment fr = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_awal, fr);
fragmentTransaction.commit();
}
}
);
the current fragment (R.id.fragment_awal) now replaced with loaded new fragment (R.id.fragment_one) which has layout (fragment_one.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"
android:background="#00c4ff">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Ini fragment 1"
android:textStyle="bold" />
</LinearLayout>
and the class is :
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate( R.layout.fragment_one, container, false);
}
}
My question is how to load this TextView1 so that I can do such :
TextView textFragment = (TextView)findViewById(R.id.textView1);
textFragment.setText(" new text");
which basically set text to a view inside the new loaded fragment.
EDIT : May I know who answered this question earlier??? basically he did answered this correct, I just somehow so confused. He just deleted the answer. I want to accept his answer.
Fragments are essentially view containers that contain a hierarchy of views. When a fragment is inserted into the view hierarchy, it must have an activity as its root. There can be more 0 or more fragments active at any given time.
In loose words, you're replacing the current view with the view of another fragment (FragmentOne).
To access the TextView with id textView1, you need to qualify the findViewById method with the current view of the fragment.
Change your fragment code to:
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
View view = inflater.inflate( R.layout.fragment_one, container, false);
// The findViewById method returns child views from either a Context,
// an Activity or another View itself.
TextView textFragment = (TextView) view.findViewById(R.id.textView1);
textFragment.setText(" new text");
return view;
}
}
You need to inflate the layout and after that you can reference the TextView:
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
View view = inflater.inflate( R.layout.fragment_one, container, false);
TextView textFragment = (TextView) view.findViewById(R.id.textView1);
textFragment.setText(" new text");
return view;
}

Android relationships between fragments

I'm using some fragments programmatically in activity. There is one button in my first fragment and when i click to this button, it replaces to second fragment.My second fragment's background is 90% transparent, and when it starts, i can see button which is situated in first fragment, and it also works. I want to stop or do something, because i dont want to see first fragment features and use it.
First Fragment
public class RegistrationFirstFragment extends Fragment {
RegistrationSecondFragment rf;
ImageButton btnNewUser,btnNewAgent;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View v =inflater.inflate(R.layout.fragment_registration_first,container,false);
rf = new RegistrationSecondFragment();
btnNewUser = (ImageButton)v.findViewById(R.id.btnNewUserRegistrationFirstFragment);
btnNewAgent = (ImageButton)v.findViewById(R.id.btnNewAgentRegistrationFirstFragment);
btnNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Transaction completed succesfully", Toast.LENGTH_LONG).show();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.flRegistrationFirst, rf);
ft.commit();
}
});
return v;
}
}
Second Fragment
public class RegistrationSecondFragment extends Fragment {
RegistrationFirstFragment rtl;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rtl = new RegistrationFirstFragment();
//return super.onCreateView(inflater, container, savedInstanceState);
View v =inflater.inflate(R.layout.fragment_registration_second,container,false);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// ft.replace(R.id.flRegistrationFirst, rf);
ft.remove(rtl);
ft.commit();
return v;
}
}
Main Activity
public class RegistrationActivity extends AppCompatActivity{
RegistrationFirstFragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
fr = new RegistrationFirstFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.flRegistrationFragment,fr);
ft.commit();
}
}
You can put
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
(the parameter fragment would be an instance of your second fragment)
into your onClick(View view){...} method to change the fragment instead of adding it.
Next time code for understanding your problem btw ;)
Add to fragment layout android:clickable="true". Int his way fragment will catch event so the click will not be caught by "main fragment".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
Give android:clickable="true" for Second Fragment root layout parent, when ever fragment opens It catches the click event of root and ignored previous click events.
Second One: If u use replace Fragment it's better than add fragment.
getActivity().getSupportFragmentManager().beginTransaction().replace
(R.id.YOUR_CONTAINER, 'FragmentObject').addToBackStack("TAG").commitAllowingStateLoss();

Dynamically add elements to fragment

Hi I'm new to android and was trying to dynamically add content to a Fragment.
I've two Fragments inside an Activity. The first point to the second like this:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_welcome,
container, false);
Button searchActivityBtn = (Button) rootView.findViewById(R.id.SearchActivityBtn);
searchActivityBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("##SecondActivity", "search btn click");
getFragmentManager().beginTransaction()
.replace(R.id.welcome_fragment, new searchAnnouncementsFragment()).addToBackStack(null).commit();
}
});
return rootView;
}
}
the second Fragment class is
public static class searchAnnouncementsFragment extends Fragment {
public searchAnnouncementsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search,
container, false);
return rootView;
}
//public void onStart(){
// super.onStart();
// RelativeLayout root = (RelativeLayout) getActivity().findViewById(R.id.annunciContainer);
// Log.d("##onstart", "root:" + (root == null));
//}
}
As you can see i tried to do something in onStart method but I've always a bunch of errors, the R.id.annunciContainer is the id of the fragment_search.xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id ="#+id/annunciContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
What I'm trying to achieve is to dynamically add elements like other FrameLayout to my Fragment ( represented by the above RelativeLayout ).
Any thought?
You didn't mention the name of the activity, lets say the name as Activity1,
First create a linear layout or frame layout in the Activity1
Second add the following lines of code to your Activity1 for inflating PlaceholderFragment into your layout in Activity1
PlaceholderFragment pf = new PlaceholderFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.annunciContainer, pf,"place_holder");
ft.commit();
For inflating searchAnnouncementsFragment replace PlaceholderFragment with searchAnnouncementsFragment.
What I was searching for are ListView

findViewById of TextSwitcher returns null after replacing fragment

I've tried almost everything I found on internet.
I cleaned the project, tried to call in view's context, many other things but to no avail.
Here's my code in activity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.container, new displayFragment() );
ft.addToBackStack(null);
// Start the animated transition.
ft.commit();
TextSwitcher textSwitcher = (TextSwitcher) findViewById(R.id.tswitch);
if(textSwitcher == null)
{
Toast.makeText(getApplicationContext(), "Pointer is null",
Toast.LENGTH_LONG).show();
}
displayFragment class
public class displayFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_display, container, false);
}
fragment_display.xml
<TextSwitcher
android:layout_marginTop="50dp"
android:id="#+id/tswitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
Your fragment is not guaranteed to be available as soon as you call ft.commit().
Try accessing your textSwitcher from the onCreateView() method of your fragment, rather than directly from the activity.
Example:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_display, container, false);
TextSwitcher textSwitcher = (TextSwitcher) rootView.findViewById(R.id.tswitch);
//Do stuff with textSwitcher
return rootView;
}

Categories

Resources