I am trying to embed a Fragment in either a Dialog or DialogFragment
public class addAccountDialog extends DialogFragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.add_account_dialog, container);
accountType.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
passwordFragment newFragment = new passwordFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_base, newFragment);
transaction.commit();
}
}
}
}
<LinearLayout
android:id="#+id/fragment_base"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="3" >
</LinearLayout>
I have tried this using with both a Dialog and DialogFragmentto no avail. Has anyone been able to implement a fragment inside a dialog.
thank you in advance.
Roger
You aren't able to put Fragments within other Fragments.
My suggestion would be to put your Fragments within an Activity like normal and then give the Activity a Dialog theme by doing this in the XML:
<activity android:theme="#android:style/Theme.Dialog" />
edit: just a note, as of API-17 (4.2) you can now nest Fragments: Nested Fragments
Related
this is the code of dashboard page where when i click the list item ..it will overlap one fragment with other ..iam using navigation drawer activity
In this listview is overlap with recycler view i am sharing one image file also
this is problem in my app of overlapping
android
public class DashboardFragment extends Fragment {
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
ListView simpleList;
TextView textView;
String List[] = {"RELIGION", "CASTE", "SUBCASTE"};
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
simpleList=root.findViewById(R.id.simplelist);
textView=root.findViewById(R.id.item);
mFragmentManager = getActivity().getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.list_item_lisview,R.id.item, List);
simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.getItem(position);
if(position==0)
{
if (savedInstanceState == null) {
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(android.R.id.content, new ReligionFragment()).commit();
}
}
else
if(position==1) {
loadFragment(new CasteFragment());
}
else if(position==2) {
loadFragment(new SubCasteFragment());
}
}
});
simpleList.setAdapter(adapter);
return root;
}
public void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, fragment);
transaction.commit();
}
}
I have used Navigation Component for navigating among fragments which is pretty common in Android now a days. You would have seen navigation folder is created when you create a new Navigation Drawer Activity or Bottom Navigation Activity in updated android studio versions. Let's look one by one. First things first:
DashboardFragment.java
public class DashboardFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dashboard, container, false);
}
}
fragment_dashboard.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=".ui.dashboard.DashboardFragment">
<fragment
android:id="#+id/nav_host_dashboard_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/dashboard_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now create a file having name dashboard_navigation inside your navigation folder.
dashboard_navigation.xml
<navigation 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:id="#+id/dashboard_navigation"
app:startDestination="#id/listFragment">
<fragment
android:id="#+id/listFragment"
android:name="com.pagingdemo.ui.dashboard.ListFragment"
android:label="fragment_list"
tools:layout="#layout/fragment_list" >
<action
android:id="#+id/action_listFragment_to_religionFragment"
app:destination="#id/religionFragment" />
<action
android:id="#+id/action_listFragment_to_casteFragment"
app:destination="#id/casteFragment" />
<action
android:id="#+id/action_listFragment_to_subCasteFragment"
app:destination="#id/subCasteFragment" />
</fragment>
<fragment
android:id="#+id/religionFragment"
android:name="com.pagingdemo.ui.dashboard.ReligionFragment"
android:label="fragment_religion"
tools:layout="#layout/fragment_religion" />
<fragment
android:id="#+id/casteFragment"
android:name="com.pagingdemo.ui.dashboard.CasteFragment"
android:label="fragment_caste"
tools:layout="#layout/fragment_caste" />
<fragment
android:id="#+id/subCasteFragment"
android:name="com.pagingdemo.ui.dashboard.SubCasteFragment"
android:label="fragment_sub_caste"
tools:layout="#layout/fragment_sub_caste" />
I have just created 4 fragments. 1st, ListFragment to show Religion, Caste and Sub Caste List. 2nd, ReligionFragment for religion recycler view. 3rd, CasteFragment for caste recycler view. 4th, SubCasteFragment for sub caste recycler view.
See my package name is different from your package name. So if you are copying code then please make sure you have created 4 fragments with correct name then replace in this navigation xml file.
I am just posting onCreateView() code of each of the above mentioned 4 fragments.
ListFragment's onCreateView looks like :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
String[] list = {"RELIGION", "CASTE", "SUBCASTE"};
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
final ListView simpleList = rootView.findViewById(R.id.list_dashboard);
ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.list_item_view,R.id.list_item_text_view, list);
simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0 : {
Navigation.findNavController(rootView).navigate(R.id.religionFragment);
break;
}
case 1 : {
Navigation.findNavController(rootView).navigate(R.id.casteFragment);
break;
}
case 2 : {
Navigation.findNavController(rootView).navigate(R.id.subCasteFragment);
break;
}
}
}
});
simpleList.setAdapter(adapter);
return rootView;
}
ListFragment's layout i.e fragment_list.xml looks like :
<androidx.constraintlayout.widget.ConstraintLayout 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=".ui.dashboard.ListFragment">
<ListView
android:id="#+id/list_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ReligionFragment's onCreateView looks like :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<String> religionList = new ArrayList();
religionList.add("Hindu");
religionList.add("Islam");
religionList.add("Christianity");
religionList.add("Budhisism");
religionList.add("Jainisism");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_religion, container, false);
RecyclerView recyclerView = view.findViewById(R.id.religion_list_view);
recyclerView.setAdapter(new MyRecyclerAdapter(religionList));
return view;
}
Like wise you could use other two fragments such as CasteFragment and SubCasteFragment along with RecyclerView.
Please set android:background="#color/white" in your XML's Parent widget. it works for me because always fragment set default color transparent.
I am now practicing on ViewPager and Fragments but I am facing an issue with replacing fragment to one another.
It is working well with swiping screen to move to another fragment, but I also wanted to try out if I can do this with a button too.
So I added a button, and tried to use FragmentManager's replace() method.
But what it actually does is not replacing the fragment but adding the fragment on the current fragment.
In a simple word, it overlaps.
Could not really find anything helpful for hours now, can anyone please help me with it?
Here is my code of the fragment that has the button:
public class MainActivity extends Fragment {
Fragment fragment;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Button buildButton;
private ViewPager viewPager;
public static Fragment newInstance(Context context) {
MainActivity mainActivity = new MainActivity();
return mainActivity;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.main, null);
return root;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
buildButton = (Button) getView().findViewById(R.id.buildButton);
buildButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fragment = new SecondActivity();
fragmentManager = getActivity().getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.calendarActivity, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
And this is the xml file of the fragment:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<fragment class="com.example.auclo.calendarpractice5.MainActivity"
android:id="#+id/calendarActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="#+id/buildButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:layout_marginEnd="148dp"
android:layout_marginStart="148dp"
android:layout_marginTop="16dp"
android:text="Track"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/calendarView" />
</android.support.constraint.ConstraintLayout>
And this is the fragment that I want to replace to:
public class SecondActivity extends Fragment {
TextView textView;
public static Fragment newInstance(Context context) {
SecondActivity secondActivity = new SecondActivity();
return secondActivity;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_second, null);
return root;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textView = getView().findViewById(R.id.textView);
}
}
And xml of the second 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=".SecondActivity">
<fragment class="com.example.auclo.calendarpractice5.SecondActivity"
android:id="#+id/secondActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pikachu"
android:textSize="18sp" />
</RelativeLayout>
I will really appreciate to any kind of help.
Thank you!
The issue is that you're replacing the element with ID calendarActivity with your new fragment. This element is inside the XML for your first fragment (I think, based on your post). So it's putting this fragment in your old fragment.
You'll need to instead use the id in R.layout.main that represents your first fragment. I usually use a framelayout for this. Then I have the activity replace that framelayout with a fragment in its onCreate().
To add the same visual effects, you have to add a animation to your fragmentTransaction
Exemple:
fragmentTransaction.setCuston(
R.anim.enter_from_right,
R.anim.exit_to_left);
See this answer for explanation on fragments animations
Hello I'm trying to replace view in FrameLayout with fragment. I want to make this like in Android guides, when I create fragment and it does't have enough space then it create new Activity. example from android side
I have problem with it. My fragment container is FrameLayout
<FrameLayout
android:id="#+id/fragment"
android:layout_width="344dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
fragment transaction suite my fragment to FrameLayout size. It makes that my fragment is not all visible (it's layout is bigger than FrameLayout size) I don't know why I have this problem. Here is my fragment transaction code (it makes when I click on Text View):
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment, new FragmentPlan());
transaction.commit();
my Fragment
public class FragmentPlan extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_plan, container, false);
ButterKnife.bind(this, view);
return view;
}
}
I believe your code is running ok.
When you add/replace a viewGroup with a fragment, the fragment inherits the size from the viewGroup, for that reason, it is very small.
As I see, you can either increase the size of the frameLayout. Or like it was mentioned above int he comments, use an Intent to start a new Activity.
I wanna open fragment from my activity. The problem is that the fragment never appears.
This is the part of my activity:
btn_opciones.setOnValueChangedListener(new ToggleButton.OnValueChangedListener() {
#Override
public void onValueChanged(int position) {
if(position == 1){
OrdenarComplejosFragment ordenarComplejosFragment = new OrdenarComplejosFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
ft.add(ordenarComplejosFragment, null);
ft.commit();
}
}
});
And this is my fragment that I wanna open and show:
public class OrdenarComplejosFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ordenar_complejos, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//do your stuff for your fragment here
}
}
The xml of the fragment above:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="HOLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
android:id="#+id/textView35"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I don't know why the fragment never show. The button works fine.
Try using a different "add" method. Using the one which takes in a view id indicates where the fragment should show up within the existing Activity's layout.
ft.add(R.id.fragment_frame, ordenarComplejosFragment);
Fragments are always housed within an Activity. If your activity doesn't provide a container/location for the fragment to show up, you'll need to re-think the layout or consider opening this new view with a separate Activity.
I have 3 buttons in a fragment and I want to disable them from the main activity. I have tried to use the bundle option before replace() newfragment but it created other issues. Can a button in a fragment be casted to a main activity ? Something like this. I get the error cannot cast but my entries are incorrect.
RedUp = (ButtonControls) getFragmentManager().findFragmentById(R.id.btnRedUP);
Here is my main.xml this holds the fragment called rgb_controls
<FrameLayout
android:id="#+id/rgb_controls"
android:layout_width="390dp"
android:layout_height="match_parent"
android:layout_marginLeft="550dp"
android:layout_marginTop="10dp" >
</FrameLayout>
Here is the button in the fragment xml called button_controls.xml
<Button
android:id="#+id/btnRedUP"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="80dp"
android:text="UP"
android:textSize="30dp" />
Here is the fragment actiivty
public class ButtonControls extends Fragment {
public Button RedUp, RedDn, GreenUp, GreenDn, BlueUp, BlueDn;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.button_controls, container, false);
RedUp = (Button) view.findViewById(R.id.btnRedUP);
return view;
}
}
Fragment is added using the replace()
Fragment ButtonFragment = new ButtonControls();
ButtonFragment.setArguments(bundle);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.rgb_controls, ButtonFragment).addToBackStack(null).commit();
Something like this should do the thing:
ButtonControls fragment = (ButtonControls)
getFragmentManager().findFragmentById(R.id.rgb_controls);
fragment.setButtonsGone();
You have to actually implement this setButtonsGone method in ButtonControls. Something as:
public void setButtonsGone() {
RedUp.setVisibility(View.GONE);
RedDn.setVisibility(View.GONE);
GreenUp.setVisibility(View.GONE);
GreenDn.setVisibility(View.GONE);
BlueUp.setVisibility(View.GONE);
BlueDn.setVisibility(View.GONE);
}