How do I use the bottomNavigationView variable outside of the MainActivity?
In MainActivity, I would use this to set and reove a badge
// To Add
BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(menuItemId);
badge.setVisible(true);
// To remove
bottomNavigationView.removeBadge(menuItem.getItemId());
However it will return null in a fragment if I try the same method
public BottomNavigationView bottomNavigationView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MainActivity main = new MainActivity();
main.bottomNavigationView.removeBadge(2131231026);
I have also tried this in the fragment, but nothing occurs
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_chat, container, false);
final View nav = inflater.inflate(R.layout.activity_main, container, false);
bottomNavigationView = nav.findViewById(R.id.bottomNav);
BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(2131231026);
badge.setVisible(false);
I don't know if you have this in Java (requireActivity) and if you have only one Activity in your project, but It can be the solution :
(requireActivity() as MainActivity).findViewById(R.id.bottomNav)
Also you can add function in your main Activity like addBadge / removeBadge.
But I think you can't get bottomNavigationView outside the main activity because it's part of UI mainActivity and not others fragments
PS : Your line " MainActivity main = new MainActivity();" isn't good because you instanciate an activity and not get actual instance so bottomNavigationView is null. May be use this.getActivity() ( this = fragment instance)
If you want to use BottmNavigationView outside of MainActivity first you should create a separate activity like MenuActivity and suppose you want to use 5 items in BottmNavigationView, so:
public class MenuActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
final Fragment fragment1 = new DiscoverFragment();
final Fragment fragment2 = new CreateFragment();
final Fragment fragment3 = new AnalyticsFragment();
final Fragment fragment4 = new MoreFragment();
final FragmentManager fm = getSupportFragmentManager();
final Fragment[] active = {fragment1};
fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1, "1").commit();
bottomNavigationView=findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
fm.beginTransaction().hide(active[0]).show(fragment1).commit();
active[0] = fragment1;
return true;
case R.id.add:
fm.beginTransaction().hide(active[0]).show(fragment2).commit();
active[0] = fragment2;
return true;
case R.id.chart:
fm.beginTransaction().hide(active[0]).show(fragment3).commit();
active[0] = fragment3;
return true;
case R.id.more:
fm.beginTransaction().hide(active[0]).show(fragment4).commit();
active[0] = fragment4;
break;
}
return true;
}
});
}
}
XML view:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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">
<FrameLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="MainActivity"
tools:showIn="#layout/activity_main"
android:padding="1dp"
android:id="#+id/main_container"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
app:itemIconSize="26dp"
app:itemTextAppearanceActive="#style/BottomNavigationView.Active"
app:itemTextAppearanceInactive="#style/BottomNavigationView"
app:itemIconTint="#drawable/navigation_view_colored"
app:itemTextColor="#drawable/navigation_view_colored"
app:labelVisibilityMode="labeled"
android:layout_gravity="bottom"
android:background="#android:color/white"
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/bottom_navigation_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
be sure you created 5 fragments like DiscoverFragment, CreateFragment, and so on ...
and XML view of them.
Related
I have a problem I don't know how to navigate from one fragment to another, practically the first fragment has a listview and by clicking on a value in the list I would like to open another fragment, I paste the various codes, Help me please
Main Activity
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView btnNav=findViewById(R.id.navigationBottom);
btnNav.setOnNavigationItemSelectedListener(navListner);
//Salve lo stato del fragment
if(savedInstanceState==null){
//Setting Home Fragment as main fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new PlaceFragment()).commit();
}
}
//Listner Nav Bar
private BottomNavigationView.OnNavigationItemSelectedListener navListner = new
BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment selectedFragment=null;
switch (item.getItemId()){
case R.id.place:
selectedFragment=new PlaceFragment();
break;
case R.id.commercial:
selectedFragment=new CommercialFragment();
break;
case R.id.emergency:
selectedFragment=new EmergencyFragment();
break;
}
//Begin Transaction
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,selectedFragment).commit();
return true;
}
};
}
Main Layout
<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=".MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/navigationBottom"/>
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/navigationBottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/menu"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:foregroundGravity="center"/>
Fragment Activity
public class PlaceFragment extends Fragment {
public PlaceFragment() {
// 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.fragment_place, container, false);
}
}
Fragment layout
<FrameLayout 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"
tools:context=".PlaceFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listplace"
android:entries="#array/place_visit"/>
I was looking here some ways to do what i want, i tried some codes but nothen works. I have a app with bottom bar (3 itens), and i have one fragment for each one. The 3 fragments load a layout with webview (3 differents links, of course). What i want is switch between fragment and keep it in the background, beause when I leave fragment 1 to fragment 2, and i back to fragment 1, the fragment 1 load again, and i just want to keep it load, and the others fragments too. How can i do that ?
Thanks for the help !!
Activity Main XML
<LinearLayout
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bandeira.thalisson.appExample.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation"/>
Main Activity Java
public class MainActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_item1:
selectedFragment = Fragment1.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
return true;
case R.id.navigation_item2:
selectedFragment = Fragment2.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
return true;
case R.id.navigation_item3:
selectedFragment = Fragment3.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
//*Iniciar um fragmento junto com o aplicativo
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content, Fragment1.newInstance());
transaction.commit();
}
Fragment Example Java
public class Google extends Fragment {
public WebView myWebView;
public static Google newInstance() {
Googlefragment = new Google();
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.webview, container, false);
myWebView = (WebView) rootView.findViewById(R.id.WebViewLayout);
myWebView.loadUrl("http://google.com/");
//*Ativar JavaScript
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//*Forçar links para abrir no WebView ao invés do navegador
myWebView.setWebViewClient(new WebViewClient());
return rootView;
}
Fragment XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/WebViewLayout"/>
Instead of using replace everytime in your onNavigationItemSelected, use show and hide.
Programmatically create all the fragments probably in your onCreate and in your item selected method
instead of this
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
you do
getSupportFragmentManager().beginTransaction().show(R.id.content, selectedFragment).commit();
and similarly for hiding
getSupportFragmentManager().beginTransaction().hide(R.id.content, selectedFragment).commit();
In this case, it will created only once(the first time), and subsequently will be just shown and hidden (think in terms of VISIBLE and GONE, not saying its the same, but similar)
The Problem:
In my onCreate() on my main activity, I am trying to set the text value of a TextView in my fragment which is displayed by a viewpager. When I try to get my fragment instance, It always returns null.
Solutions I Tried:
Almost every solution related to this problem on SO gives me the same result(NPE).
My Code is below as well as some explanation...
Home.java Activity (My Main Activity):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the home layout
setContentView(R.layout.home);
// Setup the toolbar
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Now setup our tab bar
TabLayout tabLayout = (TabLayout)findViewById(R.id.homeTabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
// Setup the view pager
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
// Start our service controller
Intent startServiceController = new Intent(this, ServiceController.class);
startService(startServiceController);
Fragment viewFragment = pagerAdapter.getRegisteredFragment(viewPager.getCurrentItem());
if(viewFragment == null){
Log.v("Fragment", "NULL");
}
}
After setting up the pageAdapter and viewpager, I try to access the fragment thats being shown. The code for my PagerAdapter is below...
PagerAdapter:
public class PagerAdapter extends SmartFragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ParkListFragment();
case 1:
return new MapFragment();
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
CharSequence title;
switch (position){
case 0:
title = "Spaces Near You";
break;
case 1:
title = "Map";
break;
default:
title = "N/A";
break;
}
return title;
}
}
SmartFragmentStatePagerAdapter:
public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
// Sparse array to keep track of registered fragments in memory
private SparseArray<Fragment> registeredFragments = new SparseArray<>();
public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Register the fragment when the item is instantiated
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
// Unregister when the item is inactive
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
I really don't understand what the issue is. I have tried many things. What I don't want to use is this:
String tag="android:switcher:" + viewId + ":" + index;
ParkListFragment.java:
public class ParkListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.park_list_fragment, container, false);
}
}
ParkList Fragment Layout:
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/confidenceText"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="#android:color/primary_text_light"
android:padding="10dp"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/activityText"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="#android:color/primary_text_light"
android:padding="10dp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Stack Trace: Pastebin
So after hours of trial and error, the only thing that worked for me was this SO post:
ViewPager Fragments are not initiated in onCreate
At the moment I have a activity with a viewpager which inflates a listfragment.
When a user click on a item in the listview I would like to inflate a new fragment.
Test.java
public class Test extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
final Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
ViewPager vp = (ViewPager) findViewById(R.id.n_Viewpager);
this.addPages(vp);
}
//ADD ALL PAGES
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
adapter.addPage(new TestFragment());
//SET ADAPTER TO PAGER
pager.setAdapter(adapter);
}
}
layout.activity_test
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="tutorial.com.ScratchGolfer.Test">
<RelativeLayout
android:id="#+id/nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/appbarr"
>
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/n_Viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appbarr"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
/>
</RelativeLayout>
</RelativeLayout>
TestFragment.java
public class TestFragment extends ListFragment {
//Setting the name of each event
String[] options = {"1", "2", "3", "4", "5", "6", "7"};
ArrayAdapter<String> adapter;
//Inflating the view with the fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
adapter = new ArrayAdapter<>(getActivity(), R.layout.listitem, R.id.textview, options);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
//Click events
#Override
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View v, int pos, long l) {
switch (pos) {
case 0:
Intent intent0 = new Intent(v.getContext(), NewActivity.class);
v.getContext().startActivity(intent0);
break;
case 1:
break;
}
}
});
}
}
I have managed to open a activity when I click a item in the listview, but I would like to inflate the current activity with a new fragment depending on what item is clicked in the listview
It's not the best and shortest solution, but I found it more understandable.
You can create a Fragment as container and add it to your ViewPager. Then replace fragments inside it.
fragment_container.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container_root_view">
</LinearLayout>
ContainerFragment.Java
public class ContainerFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_container, container, false);
getChildFragmentManager().beginTransaction().replace(R.id.fragment_container_root_view, new TestFragment()).commit();
return view;
}
}
In you activity:
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
adapter.addPage(new ContainerFragment());
//SET ADAPTER TO PAGER
pager.setAdapter(adapter);
}
In your TestFragment:
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View v, int pos, long l) {
switch (pos) {
case 0:
getParentFragment().getChildFragmentManager().beginTransaction().replace(R.id.fragment_container_root_view, new SecondFragment()).addToBackStack("").commit();
break;
case 1:
break;
}
}
});
I didn't test this code to see if it has error or not, but with some changes, You can achieve to what you want.
I have a ViewPager in which I am able to view between 4 different tabs (which are fragments) I created. In one of my fragments, I want to have a button that will, after I click a button on that page, replace that view with another fragment. Now I don't want to swipe to another tab, I just merely want to replace the view of the current fragment when I press that button. Here is my attempt, but I'm not sure why when I click my button, nothing happens. If anyone could help me that would be great. Thanks!
This is the Fragment I call to create the view and the button I want to press.
ManualSelection.java
public class ManualSelection extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_manual_selection, container, false);
intent = new Intent(getActivity(), ManualSelectionListView.class);
return rootView;
}
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button listViewGenerated = (Button) getActivity().findViewById(R.id.manual_generate);
listViewGenerated.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
});
}
}
And here is the Fragment I want to create
ManualSelectionListView.java
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ManualSelectionListView extends Fragment {
static final String[] MOBILE_OS =
new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.listview, container, false);
ListView lv = (ListView)rootView.findViewById(R.id.listview);
lv.setAdapter(new MobileArrayAdapter(getActivity(),MOBILE_OS));
return rootView;
}
}
And here are my XML files:
activity_manual_selection.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.ecs160.homestay.ManualSelection"
android:id="#+id/fragmentTest">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"
android:id="#+id/manual_generate"
android:layout_centerHorizontal="true"
android:onClick="generateListView"/>
And here is the XML file called listview.xml that contains R.id.listView_manual
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView_manual">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I get nothing displayed when I hit "Generate." Any ideas? Thanks!
In your Activity you should create this:
public void generateListView(View v){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
The problem is that you are trying to get the Button id from your main activity which will return null
Button listViewGenerated = (Button) getActivity().findViewById(R.id.manual_generate);
Button manual_generate is located in your activity_manual_selection layout not in your Main activity's layout you cant get view from different layout or else it will return null
solution:
Get the Button's id from your fragment's layout
public class ManualSelection extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_manual_selection, container, false);
intent = new Intent(getActivity(), ManualSelectionListView.class);
Button listViewGenerated = (Button) rootView.findViewById(R.id.manual_generate);
listViewGenerated.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
});
return rootView;
}
remove onActivityCreated