BottomNavigation Menu with Jetpack Navigation does not navigate - android

I have a strange problem regarding a BottomNavigationBar which I could not solve altough having spent a huge amount of time into it. When I use it in the 'recommended' way (from many tutorials) it just does not navigate.
So what do I mean by 'recommended' way: I have a single acticity with a navHostFragment called 'MainActivity'. This main activity has a XML layout file in which I put the BottomNavigationBar. The BottomNavigationBar also has a XML layout file. Now I have a Fragment called 'FR_Menu' with a Java file and a XML layout file. I also have a NavGraph. In the XML layout of the Fragment 'FR_Menu' I do not use the BottomNavigationBar and in the Java class I do not instantiate the BottomNavigationBar as I have already done this in the main Activity. But using this approach the Navigation does not work. Altough the BottomNavigationBar is correctly displayd in the Fragment 'FR_Menu', when clicking on the Bottom just nothing happens.
Now here come the strange thing. When I use the code as posted, but not put the BottomNavigationBar in the XML layout file of the main activity and instead put it separately in every XML-layout file of all Fragments (in this example the fragment 'FR_Menu') and when I also instantiate the BottomNavigationBar in every Java file of each Fragment (in this example the fragment 'FR_Menu'), then the Navigation works perfectly. So with this approach I have to put the BottomNavigationBar in every XML layout file of the fragments and also I have to instantiate the BottomNavigationBar in every Java file of the Fragments. I know that normally using the Jetpack Navigation components, this should not be the case and instead I should only have to instantiate once in the MainActivity as the BottomNavigationBar should only be added to the XML layout file of the MainActivity.
Does anyone have an idea what goes wrong when I am trying to implement the 'recommended' approach? The names are all correct (maybe there is a small error in my shown example because I had to simplify and adjust it a little bit), because when using the second approach (putting the BottomNavigationBar in every XML-layout file and in every Java file of the fragments) the navigation works perfectly (I also tried it with multiple items and destinations).
I have really spent quite much time on that and I could not figure out what my mistake it. Because of this I would highly appreciate every comment and would be quite thankful for your help.
Does anyone have an idea what the reason for this strange behaviour might be?
Java code of the 'MainActivity':
package com.example.td.bapp;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.td.bapp.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = ActivityMainBinding.inflate(inflater, container, false);
NavController navController = Navigation.findNavController(this, R.id.navHostfragment);
NavigationUI.setupWithNavController(binding.bottomNavigation,navController );
setContentView(binding.getRoot());
return binding.getRoot();
}
}
XML layout file of the 'MainActivity':
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity"
tools:ignore="ExtraText">
<fragment
android:id="#+id/navHostfragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottom_navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
app:labelVisibilityMode="labeled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorGreen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_navigation"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#color/colorAccent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
XML-layout file of BottomNavigationBar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/FR_LanguageSelection"
android:icon = "#drawable/ic_add_circle_full"
android:title = "Language" />
</menu>
Java file of the Fragment 'FR_Menu'
package com.example.td.bapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import com.example.td.bapp.databinding.ActivityMainBinding;
import com.example.td.bapp.databinding.FragmentMenuBinding;
public class FR_Menu extends Fragment implements View.OnClickListener {
// TODO: Rename and change types of parameters
public FR_Menu() {
}
public static FR_Menu newInstance(String param1, String param2) {
FR_Menu fragment = new FR_Menu();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private FragmentMenuBinding binding;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// return inflater.inflate(R.layout.fragment_menu, container, false);
binding = FragmentMenuBinding.inflate(inflater, container, false);
/*
// IMPORTANT REMARK: When I use the following code in the second option this in the second option,
with the BottomNavigationBar in the XML document, the navigation works well
NavController navController = Navigation.findNavController(getActivity(), R.id.navHostfragment);
NavigationUI.setupWithNavController(binding.bottomNavigation,navController );
*/
return binding.getRoot();
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.imageButton_A.setOnClickListener(this);
binding.imageButton_B.setOnClickListener(this);
}
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
#Override
public void onClick(View view) {
if(view.getId() == R.id.imageButton_A) {
String argument = DataBaseEntries.A;
FR_MenuDirections.ActionFRMenuToFRGenericD action =
FR_MenuDirections.actionFRMenuToFRGenericD(argument);
Navigation.findNavController(view).navigate(action);
}
if(view.getId() == R.id.imageButton_B) {
String argument = DataBaseEntries.B;
FR_MenuDirections.ActionFRMenuToFRGenericD action =
FR_MenuDirections.actionFRMenuToFRGenericD(argument);
Navigation.findNavController(view).navigate(action);
}
}
}
Here is the XML layout file of the fragment 'FR_Menu'
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_mainActivity"
android:layout_width="match_parent"
android:layout_height="135dp"
android:background="#435cb53f"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#android:color/holo_green_light">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="24sp"
android:text="Menu" />
</androidx.appcompat.widget.Toolbar>
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar_mainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
tools:ignore="ExtraText">
<ImageButton
android:id="#+id/imageButton_A"
android:layout_width="0dp"
android:layout_height="128dp"
android:background="#00000000"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/imageButton_B"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/menu_A" />
<ImageButton
android:id="#+id/imageButton_B"
android:layout_width="0dp"
android:layout_height="128dp"
android:layout_marginTop="12dp"
android:background="#00000000"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="#id/imageButton_A"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/menu_B" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
XML code of the NavGraph
<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
app:startDestination="#id/FR_LanguageSelection">
<fragment
android:id="#+id/FR_Menu"
android:name="com.example.td.bapp.FR_Menu"
android:label="FR_Menu"
tools:layout="#layout/fragment_menu" >
<action
android:id="#+id/action_FR_Menu_to_FR_LanguageSelection"
app:destination="#id/FR_LanguageSelection" />
</fragment>
<fragment
android:id="#+id/FR_LanguageSelection"
android:name="com.example.td.bapp.FR_LanguageSelection"
android:label="FR_LanguageSelection" >
<action
android:id="#+id/action_FR_LanguageSelection_to_FR_Menu"
app:destination="#id/FR_Menu" />
</fragment>
</navigation>

onCreateView is a method of a Fragment, not a method on Activity, so your method in your MainActivity isn't ever called by the framework.
In fact, that code isn't what you'd use in an Activity at all as per the View Binding documentation.
Therefore your MainActivity should instead look like:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate, and then call setContentView() on the returned view root
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
NavController navController = Navigation.findNavController(this,
R.id.navHostfragment);
NavigationUI.setupWithNavController(binding.bottomNavigation, navController);
}
}

Related

Nested Fragments (multi-pane in tab)

i have problems to use a fragment in a fragment. The first fragment is a tab view an in one tab i want to have a multi-pane Layout like this:
The error is:
android.view.InflateException: Binary XML file line #8: Error
inflating class fragment
The tab fragment (SettingsFragmentBox):
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SettingsFragmentBox extends Fragment implements SettingsFragmentBoxList.OnItemSelectedListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_settings_box, container, false);
return rootView;
}
public void onBoxSelect(int id) {
SettingsFragmentBoxDetail fragment = (SettingsFragmentBoxDetail) getFragmentManager().findFragmentById(R.id.box_detail);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(id);
} else {
Intent intent = new Intent(getActivity(),SettingsActivityBoxDetail.class);
intent.putExtra(SettingsActivityBoxDetail.EXTRA_ID, id);
startActivity(intent);
}
}
}
The xml (fragment_settings_box.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/box_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
class="de.resper.e2cast.SettingsFragmentBoxList"
tools:layout="#layout/fragment_settings_box_list">
</fragment>
<fragment
android:id="#+id/box_detail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
class="de.resper.e2cast.SettingsFragmentBoxDetail"
tools:layout="#layout/fragment_settings_box_detail">
</fragment>
</LinearLayout>
Usually this is done using fragments in ACTIVITIES, not in other fragments. Are you sure you really want fragments in other fragments ?
The picture you are referring to is by the way showing this scenario (ACTIVITIES contain fragment A or B).
You should read http://developer.android.com/training/multiscreen/screensizes.html to handle different screensizes. There is an example with a layout containt two fragments in it. Also learn how to adapt to it by reading this http://developer.android.com/training/multiscreen/adaptui.html

Expandable List + Other Fragments in a single Activity

I would like to add multiple fragments in a single layout. However, I'm getting errors when I include additional fragments. Kindly assist.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#F0F8FF"
android:id="#+id/my_layout">
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="250dp"
android:layout_height="match_parent" >
</ExpandableListView>
<fragment
android:id="#+id/fragw3"
android:name="com.example.engineercalclist2.arrayHistory"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
I hope i understood right, but here it goes:
Instead of adding Fragments directly from xml, you can use a FrameLayout. Then on your code ,lets say when you click on your ExpandableListView item, you can change the fragment. Here is a simple example.
Add this to your xml.
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
Now move on to your Activity, declara a Fragment
Fragment fragment = null;
Then on your OnItemClickListener for your ExpandableListView set the fragment to the fragment you want to change like this.
fragment = new Fragment_ToGo();
Then use this
FragmentTransaction ft = getFragmentManager().beginTransaction();
getFragmentManager().beginTransaction().commit();
ft.replace(R.id.content_frame, fragment);
ft.commit();
EDIT
Now, I'm going to use the tutorial from here. I suggest you to read there first, down below is my explanation.
First of all, if your API is older than 11 your MainActivity ,which contains Fragments, should be extended to FragmentActivity. If you are using API 11 or newer, you are good to go.
Now, create your Fragment(s) like this.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class ArticleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
}
This is going to be your Activity's XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="#+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="#+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
And finally, your MainActivity:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
}
}
Now since i don't know what error do you get, I'm only able to help you to this point. I will edit my answer again when you post your Logcat.

Fragment is not covering entire screen

please help
When I run my ListFragment actitivy and click on a fragment (either Buick or Mazda), the layout of the fragment I clicked on is supposed to replace and cover the entire screen but the
container containing the list of cars stays on the screen and my fragment layout is shown below it. How can I make my fragment's layout replace the entire screen?
I couldn't post my AVD image showing the screen because I don't have enough reputatons to do
but the screen was showing as below when I clicked on the first item on the list.
Buick
Mazda
Hello! It's a Buick
my Mainactivity
package in.cars.demo;
import android.app.Activity;
import android.os.Bundle;
public class CarsMainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
My ListFragment activity
package in.cars.demo;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class CarsList extends ListFragment {
String[] cars = new String[] { "Buick", "Mazda" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListAdapter myListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cars);
setListAdapter(myListAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cars_container, container, false);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
switch (position) {
case 0:
Fragment fragment1 = new Fragment1();
FragmentTransaction transaction1 = getFragmentManager().beginTransaction();
transaction1.replace(R.id.car_fragment, fragment1);
transaction1.addToBackStack(null);
transaction1.commit();
break;
case 1:
Fragment fragment2 = new Fragment2();
FragmentTransaction transaction2 = getFragmentManager().beginTransaction();
transaction2.replace(R.id.car_fragment, fragment2);
transaction2.addToBackStack(null);
transaction2.commit();
break;
}
}
}
Fragment1
package in.cars.demo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
Fragment2
package in.cars.demo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
main.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" >
<fragment
android:id="#+id/car_fragment"
android:name="in.cars.demo.CarsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
cars_container.xml
<?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"
android:paddingLeft="4dp"
android:paddingRight="4dp">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
</LinearLayout>
fragment1.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" >
<TextView
android:id="#+id/frag1TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello! It's a Buick" />
</LinearLayout>
fragment2.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" >
<TextView
android:id="#+id/frag2TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello! It's a Mazda" />
</LinearLayout>
The problem is that you set the layout_height of your fragment to wrap_content in your main.xml. The visible size will be reduced to the actual size of the fragment.
Change it to match_parent to fill the whole screen:
...
<fragment
android:id="#+id/car_fragment"
android:name="in.cars.demo.CarsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
...
Furthermore use match_parent instead of fill_parent in apps with API Level 8+. See this question for more information.
Edit:
You have to change the container for the Fragments. The Fragment set with the tag will be static. Every fragment will be set in addition to this container. To have a dynamic approach use a FrameLayout. Now you can replace the fragment which was in there before.
...
<FrameLayout
android:id="#+id/car_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
...
To show your CarsList you have to do a Fragment transaction in your Activity.
getFragmentManager() / getSupportFragmentManager()
.beginTransaction()
.replace(R.id.car_fragment, new CarsList())
.addToBackstack(null)
.commit();
<?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:background="#color/white"
android:clickable="true"
android:orientation="vertical">
Try to setUp a color background to your fragment. In this way the background covers the entire space.
It is a little bit late, but maybe I will help someone else. The problem is with 'fragment' in your activity xml layout file (main.xml). You have to change:
<fragment
android:id="#+id/car_fragment"
android:name="in.cars.demo.CarsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
to
<fragment
android:id="#+id/car_fragment"
android:name="in.cars.demo.CarsList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Android fragment not created after runtime

I'm trying to add a fragment to an activity after the content view in the activity has been set. I'm not using the compatibility package and have my target API set to 16. I'm expecting the activity to be set to the fragments layout after the setContentView method in the main activity but on runtime, the fragment is never added and the screen in blank. The callbacks for the life cycle events in the fragment are never called thus the System.outs don't output. Logcat doesn't output any errors relating to fragments either. If a fragment element is made in the activity_main.xml, the fragment works fine but adding it to the RelativeLayout in the class doesn't work. Can someone please help me? Thanks!
Edit: Tried using a FrameLayout as the fragment_container but no difference.
This is my main activity:
package com.example.derp;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment frag = new Fragment();
FragmentManager fManager = getFragmentManager();
fManager.beginTransaction().add(R.id.fragment_container, frag).commit();
}
}
This is my fragment:
package com.example.derp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag extends Fragment{
public Frag(){
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
System.out.println("onAttach");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate");
}
#Override
public void onStart() {
super.onStart();
System.out.println("onStart");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("onCreateView");
return inflater.inflate(R.layout.fragment_frag, container, false);//
}
}
This is the fragment_frag.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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="64dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</RelativeLayout>
The 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>
You are not creating the instance of your fragment class. Rather you are creating the base fragment class's object.
Change...
Fragment frag = new Fragment();
To...
Fragment frag = new Frag();

Fragment doesn't work

I have main layout like this
<?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="horizontal" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_showdetails"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginLeft="110dip"
android:layout_marginTop="15dip"
android:text="Show Details" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/my_parent_layout"
>
</LinearLayout>
</LinearLayout>
am trying to call another activity in the my_parent_layout using this class
package com.appnetics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
public class FragmenttestActivity extends FragmentActivity {
Button LogIn = null ;
final FragmentManager fragmentManager = getSupportFragmentManager() ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LogIn = (Button) findViewById(R.id.btn_showdetails);
LogInEvent() ;
}
public void LogInEvent(){
LogIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
details fragment = new details();
fragmentTransaction.add(R.id.my_parent_layout, fragment);
fragmentTransaction.commit();
}
});
}
}
the details layout is :
<?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" >
<TextView
android:id="#+id/date_time_label"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32dp"
android:textStyle="bold"
android:text="time_label"
/>
<TextView
android:id="#+id/last_view_time"
android:layout_toRightOf="#+id/date_time_label"
android:layout_alignBaseline="#+id/date_time_label"
android:layout_marginLeft="8dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32dp"
/>
</LinearLayout>
and the layout code is
/* $Id: $
*/
package com.appnetics;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* DateTime
*/
public class details extends Fragment {
private static String getDateTimeString(Date time) {
return new SimpleDateFormat("d MMM yyyy HH:mm:ss")
.format(time);
}
private final String time = getDateTimeString(new Date());
/** #see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle b)
{
View view = inflater.inflate(
R.layout.details,
container,
false); //!!! this is important
((TextView) view.findViewById(R.id.last_view_time))
.setText(time);
return view;
}
}
the problem is that when click the button, nothing appear , any idea what's wrong in my code
Lets start!
On your details XML:
android:layout_toRightOf="#+id/date_time_label"
android:layout_alignBaseline="#+id/date_time_label"
this two tags are for relative layouts, the parent of the layouts must be of relative type in order to work.
android:layout_centerVertical="true"
This tag is also incorrect, if you want to center the text in the textview you can use two ways or wrap content in textView and set center in its parent or use:
android:gravity="right"
android:width="fill_parent"
This will be the detail_fragment.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" >
<TextView
android:id="#+id/date_time_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32dp"
android:textStyle="bold"
android:text="time_label"
/>
<TextView
android:id="#+id/last_view_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="view time"
android:textSize="32dp" />
</LinearLayout>
On your main.xml you declare the schema lots of times! should be declared once on your root layout.
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
So the main.xml will be...
<?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:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/add_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
The fragment is an element that is declared in its class and apply its xml.
public class DetailsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
}
And in the main activity add it to the holder layout:
public class FragmentSimpleTestActivity extends FragmentActivity {
private Button addFragment;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final FragmentManager fragmentManager = getSupportFragmentManager();
addFragment = (Button) findViewById(R.id.add_fragment);
addFragment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
DetailsFragment fragment = new DetailsFragment();
fragmentTransaction.add(R.id.fragment_holder,fragment, "MY_FRAG");
fragmentTransaction.commit();
}
});
}
}
Its important that fragmentTransaction.add has parameters
1: the id of the view to hold the fragment.
2: the fragment object.
3: a tag that is used to find the fragments in the stack.
and thats all :)
Enjoy your fragments! you can also set animations when adding or removing it.
onCreateView()
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you
must return a View from this method that is the root of your
fragment's layout. You can return null if the fragment does not
provide a UI.
Call OnCreateView in your fragment class
return inflater.inflate(R.layout.fragmentlayout, container, false);
I had a similar problem, when fragment wasn't showing up. The problem was that I removed AndroidAnnotations, but forgot to move the code from onViewCreated and onCreate methods to the onCreateView. I found this mistake by comparing the code of two fragments - one, that was showing up and the other that wasn't. So, if you have similar problems, try to check the code of fragments. It may not show any errors at first glance, but will not still work or show up because of those errors.
Particularly, check that you have this method and that you inflate a view:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, container, false);
return v;
}

Categories

Resources