I need to stop the app icon in the upper left corner on the actionBar from acting as a button. I tried using actionBar.setHomeEnabled(false), but it isn't working and i'm not sure why. Any help is really appreciated. This is my onCreate method:
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_board);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if(fragment == null){
fragment = new SoundBoardFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
}
}
You will also need to call setDisplayHomeAsUpEnabled(false)
Related
I have added three menus in BottomNavigationLayout
How can I open Center menu by default on startup?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
loadFragment(new ProfileFragment());
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
I used this to load the fragment and it also loads fragment associated with the center menu but on bottom navigation the first menu is selected.
I hope you understand my problem. if not then check the Clash Royale. in this game, the battle layout is the first pop-up at startup as well as battle menu selected in the bottom navigation.
If anyone knows how to do this please help me.
thanks in advance
Just Go for This mate ! Just Copy and paste in your editor and there you go!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
// loadFragment(new ProfileFragment()); Removed this line
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
// Added this line
navigation.setSelectedItemId(bottomNavigation.getMenu().getItem(1).getItemId());
}
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Inside onCreate() method call that fragment which you want to open first at time of app launching.
When you tap on bottom menus you are performing some action for change the color of menu with fragment, Put that fragment on onCreate of the Activity with actions, So when by default when app launches it will show you your desired fragment.
Here is reference :
HomeFragment homeFragment = new HomeFragment().newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, homeFragment)
.commit();
Full Code Here :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
loadFragment(new ProfileFragment());
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
HomeFragment homeFragment = new HomeFragment().newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, homeFragment)
.commit();
}
add this in your Mainactivity
navigation.setSelectedItemId(R.id.navigation_notifications); // Pass your menu id which you want to selected first
Set your mid tab as selected item
bottomNavigationView.setSelectedItemId(R.id.tab2);
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.tab1:
loadFragment(new ProfileFragment1());
return true;
case R.id.tab2:
loadFragment(new ProfileFragment2());
return true;
case R.id.tab3:
loadFragment(new ProfileFragment3());
return true;
}
return false;
}
};
From a while i had been using viewpager to display webview pages in activity, but now i have change from viewpager to navigation drawer with RecyclerView, to display webview, i have multiple webview loading in my activity.
Suppose activity starts on webview 1 and then i click on webview 2 if i go back to webview 1 it will be reloading(refresh) and i want to prevent that from happening.
Please help.
Here is my main activity.
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar toolbar;
private FragmentDrawer drawerFragment;
private static String TAG = MainActivity.class.getSimpleName();
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
if (toolbar != null) {
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
}
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
displayView(0);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new TopRatedFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new GamesFragment();
title = getString(R.string.title_friends);
break;
case 2:
fragment = new MoviesFragment();
title = getString(R.string.title_messages);
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
getSupportActionBar().setTitle(title);
}
}
}
Are you saying that you want to restore the same fragment? This is from developer.android.com
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
This enables the back navigation button to restore the same fragment that already loaded the webview. You are creating a new one each time in the switch statement.
Once you get the back button working how you want it you can easily learn how to manually pull the specific fragment off of the stack.
Edit:
I do not think you are refreshing the webview. You are creating a new fragment each time you select an item by calling the empty constructor. You need to only create each fragment one time and then use the same instance of that fragment when you go back to that webview. Using the back stack will enable it to stay loaded while you leave the application and come back to it later.
Also try using the static method YourFragment.newInstance(params) which returns a new instance of YourFragment. Do this instead of the empty constructor, as that is considered incorrect practice.
I have 1 Activity with 3 Fragments inside (Home-Login-RestorePass) Initially, HomeFragment shows and the other two are hidden. I want that the ActionBar Title change depending on which Fragment is showing.
I'm trying in my Activity with:
public void setActionBarTitle(String title){
getSupportActionBar().setTitle(title);
}
#Override
public void onResume() {
super.onResume();
// Set title
setActionBarTitle(getString(R.string.app_name));
}
and the fragments has the same:
#Override
public void onResume() {
super.onResume();
// Set title
((LoginActivity) getActivity()).setActionBarTitle(getString(R.string.fragment_login));
}
But it doesn't work. It always show R.string.fragment_login on the title.
I'm using FragmentTransaction for the fragment transition:
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
HomeFragment homeFragment = (HomeFragment) getFragmentManager().findFragmentById(R.id.fragmentHome);
LoginFragment loginFragment = (LoginFragment) getFragmentManager().findFragmentById(R.id.fragmentLogin);
ft.hide(homeFragment).addToBackStack(null);
ft.show(loginFragment).addToBackStack(null).commit();
}
});
Additionally if i could make appear an Arrow Button (Back) on ActionBar depending of the fragment would be great.
Thanks for your time! Regards.
Keep in mind that if you are using the support Library, you have to specifically cast your Activity when you get it through getActivity(). And then you'll want to make sure you are retrieving a support ActionBar by using getSupportActionBar(). I was able to set the ActionBar title in my app by using the following code in my Fragment's onResume()...
#Override
public void onResume() {
super.onResume();
AppCompatActivity activity = (AppCompatActivity) getActivity();
ActionBar actionBar = activity.getSupportActionBar();
actionBar.setTitle(R.string.my_fragment_title);
}
Use this method in activity to change the Fragment and set the title programmatically:
private void displayFragment(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
String title = "";
switch (position) {
case 0:
fragment = new Home();
title = "Home";
break;
case 1:
fragment = new Login();
title = "Login";
break;
case 2:
fragment = new RestorePass();
title = "Restore Password";
break;
default:
break;
}
// update selected fragment and title
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, fragment).commit();
getSupportActionBar().setTitle(title);
// change icon to arrow drawable
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow);
}
}
For example, you want Fragment Home to be displayed:
displayFragment(0);
If you set the activity attribute android:label="" in the AndroidManifest you will be able to set the title post initialisation. I discovered this tracing through the Toolbar source code.
I've got an issue with dealing with ActionBarDrawerToggle(v7) and icons.
Basically, i've got a classical Activity with a DrawerFragment and Fragments to be displayed and it works.
My issue occurs when i try to replace a Fragment with another for detail view (List/Detail Fragments).
I push my new DetailFragment with:
private void putDetailFragment(Fragment fragment, String fragmentName) {
getSupportFragmentManager()
.beginTransaction()
.addToBackStack(fragmentName)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(R.id.container, fragment)
.commit();
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(getString(mTitleId).toUpperCase());
mNavigationDrawerFragment.setHomeAsUp(getSupportActionBar(), true);
}
}
public void setHomeAsUp(ActionBar actionBar, boolean show) {
actionBar.setDisplayHomeAsUpEnabled(show);
setDrawerIndicatorEnabled(!show);
}
public void setDrawerIndicatorEnabled(Boolean enabled) {
if (mDrawerToggle != null) {
mDrawerToggle.setDrawerIndicatorEnabled(enabled);
mDrawerToggle.syncState();
}
if(mDrawerLayout != null) {
mDrawerLayout.setDrawerLockMode(enabled ? DrawerLayout.LOCK_MODE_UNLOCKED : DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
Unfortunately, the arrow is not animated as it is with opening the drawer. And when i go back from my DetailFragment, the hamburger icon's gone.
Does anyone has a clue what's wrong here? It was working nice with ActionBarDrawerToggle(v4), but not with the new animated one.
Thanks !
I have a layout that has an EditText and a Button. I <include> it in my main layout.
I'm having a weird issue with the layout and rotation. It seems to duplicate itself when the device (physical) is rotated, messing up the text and layout.
Here it is on first open, after I add some extra garble:
DSC_0013 is in the EditText on launch of the fragment.
Then, I rotate the phone and add some different garble:
And you can see the issue pretty clearly. At first, I thought it was just the EditText messing up. But if I add enough text to make a new line:
I can see that the button gets messed up too.
I do override onSaveInstanceState, but in it I don't touch the EditText or its value, it's strictly used for something else.
What's happening and how do I fix it?
Fixed it!
Turns out it wasn't the view duplicating itself, or the EditText, or the Button. It was the entire fragment.
In my Activity's onCreate, I add the fragment to an xml layout:
private FileDetails fileDetailsFragment;
public void onCreate(Bundle savedInstanceState) {
...
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fileDetailsFragment = new FileDetails(fileData);
fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
fragmentTransaction.commit();
And onCreate was being called every time I rotated the phone (as it's meant to). So I put in a check to see if the activity is being run for the first time, and it works great.
private FileDetails fileDetailsFragment;
public void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fileDetailsFragment = new FileDetails(fileData);
fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
fragmentTransaction.commit();
} else {
fileDetailsFragment = (FileDetails) getSupportFragmentManager().findFragmentById(R.id.DetailsHolder);
}
You can also setRetainedInstance(true) on your fragment, then try to get the Fragment form de FragmentManager.findFragmentById(int) or FragmentManager.findFragmentByTag(String), and if it returns null it meant you had to create a new instance of your Fragment.
private FileDetails fileDetailsFragment;
public void onCreate(Bundle savedInstanceState) {
...
FragmentManager fragmentManager = getSupportFragmentManager();
fileDetailsFragment = (FileDetails) getSupportFragmentManager().findFragmentById(R.id.DetailsHolder);
if (fileDetailsFragment == null) {
fileDetailsFragment = new FileDetails(FileData);
}
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
fragmentTransaction.commit();
}
In some cases, the value of savedInstanceState may be null after rotation, so it is better to add another condition:
FragmentManager fragmentManager = getSupportFragmentManager();
if (savedInstanceState == null &&
fragmentManager.getFragments().size() == 0) {
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fileDetailsFragment = new FileDetails(fileData);
fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
fragmentTransaction.commit();
} else {
fileDetailsFragment = (FileDetails)
getSupportFragmentManager().findFragmentById(R.id.DetailsHolder);
}