So I have this code :
public void openSearch() {
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
if(mSearchFragment == null) {
mSearchFragment = SearchFragment.newInstance("Search");
t.add(android.R.id.content,mSearchFragment);
} else {t.show(mSearchFragment); }
t.commit();
}
I call openSearch from here:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
boolean returnValue = false;
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
}
}
after openSearch() runs.. onTabSelected is called immediately after. Why is this?
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction ft) {
If you need mroe information please let me know. I included only the pieces I thought were relevant to lessen the chatter. What's even crazier is that the first time I run this onTabSelected gets selected, but if i navigate my app and run openSearch again the 2nd and consecutive times the onOptionsItemSelected won't be called. it's only the initial time
Edit:
This is the main content view and I'm just supplying fragments for the viewPager. The Layouts for each fragment is the same, a FrameLayout with one LinearLayout child.
<LinearLayout
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/holo_orange_dark"
android:visibility="gone" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the search view" />
</LinearLayout>
<android.support.v4.view.ViewPager
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Related
I have made an app with bottom navigation view with Promo, Store, Reward, Coupon and Account tabs when I am changing from Coupon fragment to any other fragment, the bottom navigation view gets shrunk down as shown in the image, I tried changing layout width, height and converting coordinator layout to linear layout but it didn't help. The problem is occurring when I'm switching from home to any other tab only.
Layout File activity_main.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="56dp"
android:text="#string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemTextColor="#color/selector_bottom_navigation"
app:itemIconTint="#color/selector_bottom_navigation"
app:menu="#menu/navigation" />
Java File MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//loading the default fragment
loadFragment(new PromoFragment());
//getting bottom navigation view and attaching the listener
BottomNavigationView navigation = findViewById(R.id.navigation);
BottomNavigationViewUtils.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
getMenuInflater().inflate(R.menu.menu_wallet, menu);
return super.onCreatePanelMenu(featureId, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_wallet1:
return true;
case R.id.menu_qrcode:
Intent intent = new Intent(this, ScannerActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_promo:
fragment = new PromoFragment();
break;
case R.id.navigation_store:
fragment = new StoreFragment();
break;
case R.id.navigation_reward:
fragment = new RewardFragment();
break;
case R.id.navigation_coupon:
fragment = new CouponFragment();
break;
case R.id.navigation_account:
fragment = new AccountFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
This is the picture I meant:
I previously added SearchView to the coupon fragment and fragment store
It turns out that if you are using a coordinator layout and viewpager inside a fragment, you will notice that the viewpager extends the screen a bit. Just disable the scroll features of the coordinator layout inside the fragment and you will notice the bottom bar doesn't get shrunk. Weird I know, but it works.
I add android:fitsSystemWindows="false" in CoordinatorLayout and it worked
Find android:fitsSystemWindows="true" in your fragment xml and remove this line
or change it to android:fitsSystemWindows="false". Your problem will be solved.
I have developed an app, in that I have a fragment transaction,
MainActivity contains Fragment View.
MainActivity have three Button on top of the screen, that remain same when we go this Activity to another Fragment, only MainActivity Fragment part change when we click out of three.
But, my problem is that when I move from the this MainActivity to Fragment when click on First-Button that is OK, but when I click on Second-Button, result is overwrite the screen with first Fragment to another Fragment.
it is in same Activity so , I can not remove Fragment through remove(fr).commit();
Because if I do that then it become non-clickable may be fragment remove so not response on click of next button.
overall result, It display both FirstFragment and NewFragment Screen , when i move to NewFragment how i remove the FirstFragment screen ?
In MainActivity three button have following code to change fragment :
Main Activity :
public class MasterActivity extends Activity {
ImageView imgOne, imgTwo, imgThree;
Fragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
imgOne = (ImageView) findViewById(R.id.imgOne);
imgTwo = (ImageView) findViewById(R.id.imgTwo);
imgThree = (ImageView) findViewById(R.id.imgThree);
imgOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
fr = new FirstFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
//fragmentTransaction.remove(fr).commit();
//getFragmentManager().beginTransaction().remove(fr).commit();
fragmentTransaction.commit();
}
});
imgTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
fr = new SecondFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
//fragmentTransaction.remove(fr).commit();
// getFragmentManager().beginTransaction().remove(fr).commit();
fragmentTransaction.commit();
}
});
its xml file like following :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000000"
android:gravity="bottom"
android:weightSum="3" >
<ImageView
android:id="#+id/imgOne"
android:layout_width="0dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/img1" />
<ImageView
android:id="#+id/imgTwo"
android:layout_width="0dp"
android:layout_gravity="center"
android:layout_height="27dp"
android:layout_weight="1"
android:src="#drawable/img2"/>
<ImageView
android:id="#+id/imgThree"
android:layout_width="0dp"
android:layout_gravity="center"
android:layout_height="27dp"
android:layout_weight="1"
android:src="#drawable/img3" />
</LinearLayout>
<fragment
android:id="#+id/fragment_place"
android:name="packagename.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.87"/>
<LinearLayout/>
<LinearLayout/>
First Fragment :
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View r = inflater.inflate(R.layout.first_fratgment, container, false);
return r;
}
}
Second Fragment :
public class SecondFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View r = inflater.inflate(R.layout.second_fratgment, container, false);
return r;
}
}
But when click on one button Fragment button to another it display both first and second Fragment screens.
So how to resolve it and how to remove first view when second click?
I used this
fragmentTransaction.remove(fr).commit();
and this
getFragmentManager().beginTransaction().remove(fr).commit();
but it's not working.
Fragment declared in the layout are treated differently by Android. Replace
<fragment
android:id="#+id/fragment_place"
android:name="packagename.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.87"/>
with a FrameLayout, or a subclass of ViewGroup of your choice, and handle all the transactions programmatically. The rest looks good to me
Its better to use a viewGroup or a layout/FrameLayout instead of fragments (fragment navigations on button clicks in an Activity). Because fragments have their own lifecycle and fragment hold thier views or are not killed when you do fragment transition within an activity.
Although if you still want to go with fragment, first load a fragment of first button click and handle the click events of other two buttons by removing the fragment previously attached (Fragment attached to first button click).
This can be done by:
getSupportFragmentManager().beginTransaction().remove(YOUR_FIRST_FRAGMENT).commit();
and after this you can write a code to add fragment in this place,
getFragmentManager().beginTransaction().replace(YOUR_FIRST_FRAGMENT, new YourSecondButtonFragment()).commit(); and hence this can be done for your third button click too, just need to change the fragment attached to second button.
I hope this will help you.
Fragments that are hard coded in XML, cannot be replaced. Use the FramLayout as a fragment container instead.
in your XML replace
<fragment
android:id="#+id/fragment_place"
android:name="packagename.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.87"/>
<LinearLayout/>
with
<FrameLayout
android:id="#+id/contentFragment"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="2" />
Now, Do follow the following code style to achieve your goal:
First set the onClickListeners to all the imageviews:
like: imgOne.setOnClickListener(this);
#Override
public void onClick(View v) {
Fragment fragment = null;
switch (v.getId()) {
case R.id.imgOne:
fragment = new Fragment1();
break;
case R.id.imgTwo:
fragment = new Fragment2();
break;
}
if (fragment != null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.contentFragment, fragment).commit();
} else {
Log.e(LOG_TAG, "Error in fragment transaction.");
}
}
Hope this helps.
you can use: getSupportFragmentManager().beginTransaction().remove(fragment).commit();
Hope this will help.
Make necessary changes. Replace your activity and xml with this.
It should run solid if you do.
public class MasterActivity extends Activity {
ImageView imgOne, imgTwo, imgThree;
Fragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
imgOne = (ImageView) findViewById(R.id.imgOne);
imgTwo = (ImageView) findViewById(R.id.imgTwo);
imgThree = (ImageView) findViewById(R.id.imgThree);
getFragmentManager().beginTransaction().add(R.id.fragment_place, new FirstFragment()).commit();
imgOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction().replace(R.id.fragment_place, new FirstFragment()).commit();
}
});
imgTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction().replace(R.id.fragment_place, new SecondFragment()).commit();
}
});
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000000"
android:gravity="bottom"
android:weightSum="3" >
<ImageView
android:id="#+id/imgOne"
android:layout_width="0dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/img1" />
<ImageView
android:id="#+id/imgTwo"
android:layout_width="0dp"
android:layout_gravity="center"
android:layout_height="27dp"
android:layout_weight="1"
android:src="#drawable/img2"/>
<ImageView
android:id="#+id/imgThree"
android:layout_width="0dp"
android:layout_gravity="center"
android:layout_height="27dp"
android:layout_weight="1"
android:src="#drawable/img3" />
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.87"/>
<LinearLayout/>
<LinearLayout/>
when i move to NewFragment how i remove the first frament screen ?
well you can do that by hiding the old(first) Fragment source
getSupportFragmentManager().beginTransaction().hide(getSupportFragmentManager()
.findFragmentById(R.id.the_id_of_my_first_fragment));
you can also get him(the first fragment) back by the same approach source
getSupportFragmentManager().beginTransaction().show(getSupportFragmentManager()
.findFragmentById(R.id.the_id_of_my_first_fragment));
This approach buys flexibility and not re-changing your app's architecture, and logic, and also one line of work
im always late to a party (:
copy and pest code in your button click event.
fr = new FirstFragment();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
this is completed work.
I have a layout that holds 3 fragments.
<?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="horizontal" >
<fragment android:name="com.carefreegroup.rr3.carefreeoncall.CarerAwayCarerListFragment"
android:id="#+id/carerlist"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/carerawaydatetimefragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/carerawayreasonlistfragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
.
The first fragment on the left will load automatically when this layout loads. I have a Fragment called dateTime Fragment that loads in the middle fragment based on a listview choice in the 1st fragment. The dateTime fragment has an optionsmenu in which i want to hide one of the options based on whether the far right fragment(reasonList Fragment) is showing.
My question is how can i check if the reasonList fragment on the far right is showing from the onPrepareOptionsMenu of the middle DateTime fragment.
I have tried the following but it returns true as the fragment container in the layout is there.
#Override
public void onPrepareOptionsMenu(Menu menu) {
View reasonListView = getActivity().findViewById(R.id.carerawayreasonlistfragment_container);
if(reasonListView != null && reasonListView.getVisibility() == View.VISIBLE){
menu.getItem(5).setVisible(false);
}else{
menu.getItem(5).setVisible(true);
}
super.onPrepareOptionsMenu(menu);
}
.
I can't use findFragmentByTag as there is only the reasonList container in the view at that time.
thanks
you can override setUserVisibleHint in the fragment
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
}
http://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint%28boolean%29
[solved]
#Override
public void onPrepareOptionsMenu(Menu menu) {
CarerAwayReasonUpdatefragment reasonListView = (CarerAwayReasonUpdatefragment) getFragmentManager().findFragmentById(R.id.carerawayreasonlistfragment_container);
if(reasonListView == null ) {
menu.getItem(5).setVisible(true);
} else {
menu.getItem(5).setVisible(false);
}
super.onPrepareOptionsMenu(menu);
}
Im building dynamic UI for tablets with two fragments side by side.
I recognize which layout is displayed with getSupportFragmentManager().findFragmentById(R.id.my_fragment), where my_fragments is 2nd fragment which is shown next to primary listfragment. But after some rotations and clicks getSupportFragmentManager().findFragmentById() returns fragment even if it shouldn't (another layout is displayed, only listfragment).
#Override
public void onRSSMessageSelected(int position) {
AppEngine.getInstance().setRSSMessagePosition(position);
// The user selected the RSSMessge from the RSSMessagesList
// Capture the detail fragment from the activity layout
RSSMessageDetail fragment = (RSSMessageDetail)
getSupportFragmentManager().findFragmentById(R.id.rssmessage_detail);
// BUG: supportManager doesn't return null even if it should
if (fragment!=null) {
fragment.updateRSSMessageDetail();
} else {
fragment = new RSSMessageDetail();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
layout-land:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="cz.cvut.sabattom.fragment.RSSMessagesList"
android:id="#+id/feed_messages_list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="cz.cvut.sabattom.fragment.RSSMessageDetail"
android:id="#+id/rssmessage_detail"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
I solved it with method isInLayout()
if (fragment!=null&&fragment.isInLayout()) {
fragment.updateRSSMessageDetail();
}
I want to call an fragment method from my activity, describe like in this SO-question: FindByID
TestFragment testFrag = (TestFragment) getFragmentManager().getFragmentById(R.id.testfragment);
if(testFrag != null && testFrag.isAdded())
testFrag.testMethod("Test");
As seen they identify the fragment by ID which is set in the xml. As I only have a fragment container none of my fragment layouts have a ID.
So my seconds thought was using the function getFragmentByTag `like in Tags
fragmentTransaction.replace(R.id.frameTitle, casinodetailFragment, "fragmentTag");
but as I use an action bar with tabs I don't have any tags:
My activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragmentcontainer);
// ActionBar gets initiated
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Define Tabs
ActionBar.Tab BasicInfoTab = actionbar.newTab().setText(
R.string.tab_BaseInfo);
..
// Define Fragments
PlantBasicInfoFragment BasicInfoFragment = new PlantBasicInfoFragment();
..
// Set TabListeners
BasicInfoTab.setTabListener(new TabListener(BasicInfoFragment));
..
// Add tabs to Actionbar
actionbar.addTab(BasicInfoTab);
..
// Icon clickable
actionbar.setDisplayHomeAsUpEnabled(true);
}
and its layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<LinearLayout
android:orientation="horizontal"
android:id="#+id/LabeledObjectHeaderInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- ... -->
</LinearLayout >
<!-- fragment container -->
<LinearLayout
android:orientation="horizontal"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</LinearLayout>
One of my fragments:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="#+id/table">
<TableRow>
<!-- ... -->
</TableRow>
<!-- ... -->
</TableLayout>
and finally my tab listener
public class TabListener implements ActionBar.TabListener {
public Fragment fragment;
public TabListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
So how can I identify my current fragment?
Thanks in advance
P.S. The whole tab switching works perfectly, but now I want to build an addional feature inside the activity!
Add a tag while creating tabs.
ActionBar.Tab BasicInfoTab = actionbar.newTab().setText(R.string.tab_BaseInfo);
BasicInfoTab.setTag("tab1");
And in the listener class, retrieve the tag upon event.
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
String tabId = tab.getTag().toString();
}
This way you get the current tag. To identify the current fragment, you can keep a stack of fragments and pick the right one on tab change.
Edit
The tabId, aforementioned, identifies which tab is which. In order to set a fragment id/tag, there are many ways depending on your needs. One method is to replace your actual fragment layout with an intermediate layout.
For instance, A be the fragment you want to add to fragment container. Create another fragment class called IntermediateFragment with the following layout.
<?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"
android:baselineAligned="false">
<fragment
android:id="#+id/fragmentId"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="classA" />
</LinearLayout>
Now assign the layout you mentioned in the post (containing table) as a new one and add it to fragment class A. Now add Intermediate fragment to container which in turn pulls fragment A. Identify fragment A by id/tag added in the layout.
Hope it makes some sense!
Finally I got my solution by changing the TabListener, so I can add the fragment with an tag and then use the findFragmentByTag() function.
So my TabListener now looks like this (only constructor and onTabSelected changed)
public TabListener(Fragment fragment, String tag) {
this.fragment = fragment;
this.fragmentTag = tag;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment, fragmentTag);
}
With this I only need to add a string (as tag) when adding the TabListener to my fragments and don't have to do further changes.