I have a FragmentActivity which has a button, on clicking the button I am showing a dailog
public class FragMainActivity extends FragmentActivity implements android.view.View.OnClickListener {
Button shoBut;
public String DailogTag="dialog";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shoBut= (Button) findViewById(R.id.button1);
shoBut.setOnClickListener(this);
}
#Override
public void onClick(View v) {
MyDialogFragment newFragment = MyDialogFragment.newInstance();
FragmentManager fm ;
fm=getSupportFragmentManager();
newFragment.show(fm, DailogTag);
}
}
My dialog Class
public class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inView= inflater.inflate(R.layout.dialog_layout, container, false);
return inView;
}
}
this is my dialog_layout xml file
<?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="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingBottom="36dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="36dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Hi, i am the dailog BOX...Boom"
android:textAppearance="?android:textAppearanceMedium" />
</LinearLayout>
I am getting the dailog properly
But I have a Fragment class, ExampleFragment like this
public class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.frag_content, container, false);
}
}
Whose layout is
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Fragment Conttent"
android:textAppearance="?android:textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
When i try to put this fragment in my dailog layout , my application is crashing.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="You really want to read this text?"
android:textAppearance="?android:textAppearanceMedium" />
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.addfragment.ExampleFragment"
android:id="#+id/example_fragment"
android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
When I put it in my activity_main layout it shows the fragment, but the same does not work in dailogFragment.
I am getting
08-28 15:44:09.969: E/AndroidRuntime(438): android.view.InflateException: Binary XML file line #22: Error inflating class fragment
Anyone have any suggestion for this ? Why cant a fragment be included in dailogFragment layout ?
Why can't we add a fragment in a daiologFragment ?
, why cant a fragment be included in
dailogFragment layout ?
Because nested fragment can't be added to a parent fragment through inflation of a layout file containing them. Below is a note from the docs:
Note: You cannot inflate a layout into a fragment when that layout includes a < fragment >. Nested fragments are only supported when added to a fragment dynamically.
Any have any suggestion for this ?
Add the ExampleFragment in code: in the onCreateView() method of the dialog fragment create a instance of ExampleFragment and using getChildFragmentManager() add it to a container from dialog_layout.
Related
This is a known problem, when I change fragments they overlap.
I followed the examples on the internet with a brand new project to test it out, but no success.
What am I missing here?
I started a simple project with the Navigation Drawer Activity
I've given the fragment_home and fragment_gallery an as id
<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:id="#+id/fragment_home"
android:background="#android:color/background_light"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="333dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I setup fragment manager with the transaction:
public void replaceFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_home, GalleryFragment.class, null);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Created a button and set the method as the onClick activity:
public class HomeFragment extends Fragment {
private View root;
private FragmentHomeBinding binding;
private Button button;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
root = binding.getRoot();
button = root.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Change Fragment
((MainActivity)getActivity()).replaceFragment();
}
});
return root;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
I run the app and press the button, the fragments overlap. What is going on? I've already added a background to the fragments,
did not work. It has something to do with the BackStack, but how? Why is this so difficult? Why can't I simply add and remove a fragment?
I've implemented onClickListener but inside onCreateView() it's unable to find the button's id and i.e "myButton"
My Code is:
public class Category extends Fragment implements View.OnClickListener {
View view;Button b;
public Category() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_category, container,false);
b=(Button)view.findViewById(R.id.myButton);
b.setOnClickListener(this);
return view;
}
My fragment_category.xml is as below it's containing one button and a textView:
<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="layout.Category"
android:id="#+id/constraintLayout">
<TextView
android:layout_width="368dp"
android:layout_height="495dp"
android:text="#string/hello_blank_fragment"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="Button"
tools:layout_editor_absoluteX="135dp"
tools:layout_editor_absoluteY="208dp" />
You are inflating "fragment_category", but you said you posted "fragment" xml code.
You're most likely not inflating the correct layout file.
Actually problem was there in IDE after some rectifications the error is sorted out.
Hello i want to add a popup window to a fragment i tried a lot of things still nothing i have. If someone helps me it will be great.
Here is my Fragment code;
public class Info extends Fragment {
public Info() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.info, container, false);
}
Here is my fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<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/buttonShowCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Dialog" />
</LinearLayout>
Here is my popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:src="#drawable/pozitif"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:text="dfdfdsfs"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
You can use a DialogFragment.
Instead of extending your Activity with Fragment, use DialogFragment
In on createView of the DailogFragment Activity inflate your layout
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
Find your View elements
TextView Message = (TextView) v.findViewById(R.id.text_view1);
To call your DailogFragment use this
MyDialogFragment dialog = MyDialogFragment.newInstance();
dialog.show(getActivity().getFragmentManager(), "MyDialogFragment");
For more info on this you can check out this link.
https://developer.android.com/reference/android/app/DialogFragment.html
I'm trying to transit between 2 fragment from games.xml to levels.xml
its like angry bird game menu:
games: http://i.stack.imgur.com/9S7Ar.png
levels:http://i.stack.imgur.com/uB8rP.jpg
games.java:
public class Games extends Fragment implements OnClickListener{
public Games(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.games, container, false);
return rootView;
}
#Override
public void onClick(View v) {
Fragment f = null;
switch (v.getId()) {
case R.id.s4:{
f = new Levels();
break;
default:
break;
}
if (f != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.lvl_container, f);
transaction.addToBackStack(null);
transaction.commit();
}
}
level.java:
public class Levels extends Fragment {
public Levels(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.levels, container, false);
return rootView;
}
}
levels.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lvl_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="#string/s4"/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtLabel"
android:src="#drawable/lvlgray_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
games.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/lastgame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="#string/games"/>
<ImageButton
android:id="#+id/s4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lastgame"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="#drawable/s4" />
the problem is when I click on imagebotton nothing happens (should go to levels), I think there is problem with these line
transaction.replace(R.id.lvl_container, f);
but I don't know what is it. Any ideas?
I've already seen this (developer.android.com/guide/components/fragments) but no help! What did I miss?
Is there any other way to transition between 2 fragments?
Thank you
Add this line in onCreateView at Games.java:
ImageButton im = (ImageButton) rootView.findViewById(R.id.s4);
im.setOnClickListener(this);
This make refrence to your imagebutton and set onClickListener on it to call your onClick method.
I have a FragmentActivity that should hold 2 Fragments. Each fragment has it's own layout with a table in it. I need to access the tables from the fragments' layouts from within the FragmentActivity to put some data that I read from an XML file. My problem is that when I use
tableOrders = (TableLayout)findViewById(R.id.tabel1);
it returns null. Now, I know that items from layouts can only be accessed before they have been inflated, and I've done that in the FragmentActivity before trying to call findViewById(). Here is the code for what I've tried:
The onCreate of the FragmentHolder.java that is derived from FragmentActivity:
private TableLayout tableOrders = null;
private TableLayout tableExecutions = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_holder);
initialisePaging();
//View orderFragment = (View)findViewById(R.layout.fragment_orders);
tableOrders = (TableLayout)/*orderFragment.*/findViewById(R.id.tabel1);
//View execFragment = (View)findViewById(R.layout.executions_fragment);
tableExecutions = (TableLayout)/*execFragment.*/findViewById(R.id.tabel2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null)
logedUser = (User) bundle.getSerializable("user");
AssetManager am = getApplicationContext().getAssets();
String xmlContentOrders = XMLfunctions.getXml(am, "ordersData.xml");
populateOrdersTable(xmlContentOrders);
String xmlContentExecutions = XMLfunctions.getXml(am, "executionsData.xml");
populateExecutionsTable(xmlContentExecutions);
}
The fragment class (don't know if it helps):
public class OrdersFragment extends Fragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orders, container, false);
return view;
}
}
The fragment class (don't know if it helps):
public class OrdersFragment extends Fragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orders, container, false);
return view;
}
}
And the .xml file for the fragment:
<?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" >
<TableLayout
android:id="#+id/tabel1header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/tablebgcolor" >
<TableRow >
<TextView
android:id="#+id/ordersHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="#string/orders"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/ordersTable"
style="#style/HeaderRow" >
<TextView
android:id="#+id/textSymbol"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/symbol"
style="#style/HeaderText" />
<TextView
android:id="#+id/textSide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/side"
style="#style/HeaderText" />
<TextView
android:id="#+id/textPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/price"
style="#style/HeaderText" />
<TextView
android:id="#+id/textQuantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/quantity"
style="#style/HeaderText"/>
<TextView
android:id="#+id/textRemaining"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/remanent"
style="#style/HeaderText" />
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableLayout
android:id="#+id/tabel1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/tablebgcolor">
</TableLayout>
</ScrollView>
</LinearLayout>
With that said hope someone has a solution for this (probably it's something easy that I missed).
Edit: The code I use to put the Fragments on the FragmentActivity:
private void initialisePaging() {
OrdersFragment fragmentOne = new OrdersFragment();
ExecutionsFragment fragmentTwo= new ExecutionsFragment();
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(fragmentOne);
pagerAdapter.addFragment(fragmentTwo);
ViewPager viewPager = (ViewPager) super.findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(2);
viewPager.setCurrentItem(0);
}