I have a activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="fragments"
android:id="#+id/lm_fragment"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"/>
<fragment
android:name="fragments"
android:id="#+id/pm_fragment"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="2"/>
</LinearLayout>
So I have 2 Fragment in my Activity
And when I add a Fragment in android.R.id.content, which Fragment will be added?
My Mainactivity.java:
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
LM_Fragment lm_Fragment = new LM_Fragment();
fragmentTransaction.replace(android.R.id.content, lm_Fragment);
}else{
PM_Fragment pm_Fragment = new PM_Fragment();
fragmentTransaction.replace(android.R.id.content, pm_Fragment);
}
fragmentTransaction.commit();
}
LM_Fragment and PM_Fragment is used for Landscape mode and Portrait mode. And when I use fragmentTransaction.replace(android.R.id.content, lm_Fragment);
It will be added in 1 in 2 fragment which I specified in xml, but where lm_fragment or pm_fragment? Thank you!
Here is replace method signature
public abstract FragmentTransaction replace (int containerViewId, Fragment fragment)
You don't have such id android.R.id.content in your layout.
Thing is that you're using static fragments. But you need dynamic. Replace both fragment tag with single FrameLayout with id=android.R.id.content. Or better use your own id. E.g. "fragment_container", and then call
fragmentTransaction.replace(R.id.fragment_container, lm_Fragment);
and
fragmentTransaction.replace(R.id.fragment_container, pm_Fragment);
Related
I am trying to run a class extending a fragment from a class extending AppCompactActivity, I have tried everything I have saw in Stackover flow and I cant get any to fix my problem. LineDetails is extending the Fragment
Progress1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fr = new LineDetails();
android.app.FragmentManager fm = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
});
XML
<fragment android:name="com.almac.tracker.LineDetails"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
PART OF XML FOR ACTIVITY CLASS
<?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"
android:overScrollMode="never"
android:scrollbars="none"
tools:context=".Dashboard">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fragment_place">
</FrameLayout>
</RelativeLayout >
If your activity extends AppCompatActivity, then you cannot use getFragmentManager().
In fact you should get rid of classes in the package android.app such as android.app.FragmentManager. You should use the support classes from the package android.support.v4.app such as android.support.v4.app.FragmentManager
Use getSupportFragmentManager() instead of getFragmentManager()
The stack trace reports that you don't have any Layout with id R.id.fragment_place inside your activity. Check the xml of your activity and correct the id of fragment holder.
You're replacing instead of adding without a refresh, just do an add. I also simplified one of your useless variables and just went straight to ft instead of having an intermediate fm.
Progress1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fr = new LineDetails();
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragment_place, fr);
ft.commit();
}
});
getSupportFragmentManager() should be used if activity extends AppcompatActivity
EDIT :
As per the Errors ! There is no View named fragment_place in the activity's layout that is #+id/fragment_place must exist in activity_main (If this is the layout attached to activity)
Double check your layout xml, It should have a FrameLayout with attribute #+id/fragment_place act as a placeholder for your LineDetails fragment.
If you already place <fragment /> in your layout, you don't need to replace() it programmatically.
activity_main.xml
Hi all, here i got Linear and Frame layout, I intend to display my linear at the beginning, and then i Created toolbar that consists of list of items that will display in fragment, problem is i cannot overwrite content of linear layout after I select fragment
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/toolbar"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/logo"
android:src="#drawable/splash_img1"
android:layout_gravity="center"/>
<GridView
android:id="#+id/gridview"
android:numColumns="2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/logo" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_container">
</FrameLayout>
</LinearLayout>
MainActivity.java
error from android monitor said that null pointer at line getsupportactionbar.settittle
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.main_container, new HomeFragment());
ft.commit();
getSupportActionBar().setTitle("Home Fragment...");
nv = (NavigationView) findViewById(R.id.navigation_view);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.home_id:
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_container, new HomeFragment());
ft.commit();
getSupportActionBar().setTitle("Home Fragment");
item.setChecked(true);
dl.closeDrawers();
break;
case R.id.setting_id:
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_container, new Setting_Fragment());
ft.commit();
getSupportActionBar().setTitle("Setting Fragment");
item.setChecked(true);
dl.closeDrawers();
break;
........// I dont display all since this rest is working fine
i am very new to Android Developing, have been developing mainly for ios for several years now but having problems in my first android app.
I am developing an app where at the beginning you have log in, so i created the UI and the login button and so far that works great, the app is able to connect to the database a download the necessary information for it to work via xml.
The downloaded Xml gives the items in the navigation drawer their names,
Basically all of the navigation drawer items should lead you to the same fragment, the content changes when loading the contents of the downloaded xml to it, but the window remains the same.
So in order to do this i created the navigation drawer items programmatically, added the ui for the fragment in a new xml.
When running the app everything works great, once you are logged in you are able to open the NavigationDrawer by swiping right, the right Items and names appear, but when clicking an item... nothing happens.
I used LOGE to know if the code was working and witch menu item was trying to open and it does return always the right menu item etc.
If there is a code in the fragment it executes the code and returns what its expected, but its simply not visible.
Here is my onNavigationItemSelected in MainActivity.
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
String title = item.getTitle().toString();
int order = (int) getMenuItemTag(item);
if (title != "Modos Personales" && title != "Home") {
Log.e("onNavigationItem: ", Integer.toString(order));
Fragment fragment = null;
Class fragmentClass;
fragmentClass = MainFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.flContent, fragment);
fragmentTransaction.commit();
} catch (Exception e) {
e.printStackTrace();
}
//FragmentManager fragmentManager = getFragmentManager();
//android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
//fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
}
Here is the XML of my main activity "activity_main.xml"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.paranoidinteractive.heimer.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:foreground="#mipmap/logo_menu"
android:foregroundGravity="center">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#style/Base.Theme.AppCompat.Light"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
Here is the XML of my Fragment "room_view.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/room_view"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorScreen"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_gravity="center_vertical" />
</LinearLayout>
and here is my fragmet "MainFragment.java"
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
int number = 0;
if (inflater != null){
Log.e("INFLATER NO NULL: ", Integer.toString(number));
} else {
Log.e("INFLATER NULL: ", Integer.toString(number));
}
if (container != null){
Log.e("CONTAINTER NO NULL: ", Integer.toString(number));
} else {
Log.e("CONTAINER NULL: ", Integer.toString(number));
}
return inflater.inflate(R.layout.room_view, container, false);
}
}
And if you view the LOGCAT it allways returns:
01-26 16:27:36.771 9259-9259/com.paranoidinteractive.heimer E/INFLATER NO NULL:: 0
01-26 16:27:36.771 9259-9259/com.paranoidinteractive.heimer E/CONTAINER NO NULL:: 0
So i know its working and getting there but its simply not showing it.
I've followed all the tutorials available and read tons of questions and answers here but none of the answers have helped me so far so i Decided to post this question.
Thank you in advance for your help, hope you can assist me with this.
Sorry for the log post!
I want to add a fragment to an existing fragment on a button press (button sits on parent fragment) but I get an error: java.lang.ClassCastException: must implement OnFragmentInteractionListener.
What does that mean and why doesn't any of the example have it?
Parent fragment button press code:
Button interestedButton = (Button) myFragmentView.findViewById(R.id.interestedButton);
interestedButton.setOnClickListener(new View.OnClickListener() {
InterestedFormFragment interestedFormFragment = new InterestedFormFragment();
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction().add(R.id.interestedFrag_container, interestedFormFragment, "INTERESTED_FORM");
fragmentTransaction.commit();
}
});
Parent fragment 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"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_gravity="bottom"
android:id="#+id/buildingPageId">
<LinearLayout
android:orientation="vertical"
android:id="#+id/interestedFrag_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
...
The "interestedFrag_container" should hold the newly added child fragment.
My parent fragment isinflated and hold the button that should add the child.
What am I missing here?
Thank you!
Nested fragment are only added since API 17.
You can use getChildFragmentManager() to manage your nested fragments.
I have made a tab container with TabHost, the layout xml file is like below:
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dip" >
<fragment
android:id="#+id/tab1"
android:name="com.test.Tab1Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/tab5"
android:name="com.test.Tab5Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</TabHost>
In MainActivity, I init and add tab into activity in onCreate callback like this,
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(mRes.getString(R.string.tab1)).setContent(R.id.tab1));
tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator(mRes.getString(R.string.tab5)).setContent(R.id.tab5));
On the tab changed, I want to find the current fragment, and do some init while current fragment is showing.
private OnTabChangeListener tabChangedListener = new OnTabChangeListener() {
public void onTabChanged(String tabid) {
Log.d(TAG, "tab changed " + tabid);
currentTab = tabid;
TabChangeCallback currentFragment = (TabChangeCallback) getFragmentManager().findFragmentByTag(currentTab);
if(currentFragment != null) {
currentFragment.onTabChanged(currentTabIndex);
} else {
Log.d(TAG, "get fragment null ");
}
}
};
Question is why getFragmentManager().findFragmentByTag(currentTab) is return null, and I couldn't call the fragment init code. thanks for your help.
I think if you are dealing with only fragment in the Tabhost then you better use FragmentTabHost. It will be more optimized and easy.
Example
And getting child fragment by Tag - link
This issue is because the fragemnt has not created at the time OnTabChnaged gets called.
Please check my answer here