PreferenceFragment not shown on clicking settings menu option - android

I am trying to display custom preferences on clicking Settings options in the app. I have a listview in my MainActivity. On clicking settings, there is some INCOMPLETE VIEW displayed on top of MainActivity. And on clicking this view, the PreferenceFragment is visible with the settings layout. The listview scrolling gets disabled as a result of this view added on top of MainActivity.java. I need to display the PreferenceFragment as soon as Settings menu is clicked, instead of this incomplete view.
Please find my code below
MainActivity.java
MainActivity extends FragmentActivity{
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
}
}
PreferenceFragment.java
public class SettingsFragment extends PreferenceFragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Any help is welcome !!

You can look that post to know how to create Preference Activity :
https://stackoverflow.com/a/23545532/5652770

Related

One bottom menu for all activities in app

I'm looking for your help!
I want to create an app with bottom navigation bar. Of coarse it is possible to add menu bar to every activity in app but i want to find solution where i can make one activity with menu and extend it in other activity.
So i made MenuActivity :
public class MenuActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
// mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
// mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
// mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigationBar);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}}
And after that i create some other activity :
public class testActivity extends MenuActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}}
And here is the problem, when i start my testActivity its create MenuActivity when calling super.onCreate() which contains setContentView(R.layout.activity_menu);
But i want to add only bottom menu to other activity. Can you provide me to answer how can i do this?
Thank you all in advance for any help!
It can be achieved easily with a single Activity and multiple Fragments.
Make an activity which contains only the bottom navigation and a FrameLayout container.
Implement your pages with Fragments and after that, you can load the required fragment in the activity's container with fragment manager
Try using Fragments it will help you get desired results.

Android AppCompatPreferenceActivity - flat settings list without subcategories/headers

I have added SettingsActivity to my project. By default, Android Studio has created 3 categories with 3 headers. When I tap to category, new settings screen appears.
I don't want that. My app has very limited settigns options, so I want to get only flat settings list, without any headers and categories.
What I found, not working or deprecated api. Can you point me right, valid direction how to implement flat settings screen without using any deprecated api?
In onCreate() method of SettingActivity add general fragment and do the changes whatever you like in GeneralPreferenceFragment
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
this.getFragmentManager().beginTransaction().replace(android.R.id.content, new GeneralPreferenceFragment()).commit();
}
And override onOptionItemSelected in GeneralPreferenceFragment like this:
public static class GeneralPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("your_key"));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}
}

click home button in fragment page to disappear action bar activity title

I am facing weird issue.I am using PageActivity inside three
fragments swipes one after another and so on.
My problem is,when I am in FragmentA page,on click the home
button ,again come back to the application, my action bar title name is
Chapter,which I was added the action bar title name in PageActivity and
after 2 seconds the fragment title was loading with the help of
query.
My only problem is,on home button click in fragment page instead of
activity title name,Fragment page title name have to be shown
directly after enter the application.
Below I am posted the code related to this:
PageActivity.java:
public class PageActivity extends FragmentActivity {
static TextView mTitleTextView;
.......
.......
#Override
protected void onResume() {
super.onResume();
ActionBar(Constants.Titlebarcolor);
........
........
}
public void ActionBar(String color) {
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mTitleTextView = (TextView) mCustomView.findViewById(R.id.textviewHeading);
mTitleTextView.setText("Chapter");
}
FragmentPageA.java:
#Override
public void onResume() {
super.onResume();
....
....
}
#Override
public void onPause() {
super.onPause();
....
....
}
#Override
public void setMenuVisibility(final boolean visible) {
//get action bar title for every fragment page
PageActivity.mTitleTextView.setText(DatabaseQueryHelper.getInstance().getPagename(getArguments().getInt(Constants.PAGEID)));
......
......
}
Anyone can give me any suggestion regarding to this.Thank you.
I just moved this ActionBar(Constants.Titlebarcolor); line inside onResume() method to onCrate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar(Constants.Titlebarcolor);
}
So that When I entering into the activity,the action bar activity title will not resume.

Why is the up button present without setDisplayHomeAsUpEnabled()?

According to Google's document, getActionBar().setDisplayHomeAsUpEnabled(true) is needed to show the up button. I created a bare-bone activity using the wizard in Eclipse and specified its parent activity. I could not find any getActionBar().setDisplayHomeAsUpEnabled(true) in the automatically generated code, but the up button is present when this activity is started and it works as expected. Could anyone shed some light on this?
public class FooActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foo);
//more code...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//more code...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//more code...
return rootView;
}
}
}
When you specify a parentActivityName in your AndroidManifest, Acitivty will check for that and automatically enable the "up" affordance if it's present.
I described all possible combinations below and their outcomes:
You have both android:parentActivityName=".MyActivityand this getActionBar().setDisplayHomeAsUpEnabled(true); - back button appears and it works;
You only have this android:parentActivityName=".MyActivity - back button appears and it works, the same as above;
You only have this getActionBar().setDisplayHomeAsUpEnabled(true);, - back button appears but clicking on it doesn't go anywhere;
You have set the parameter to false in this getActionBar().setDisplayHomeAsUpEnabled(false);, even though you have this android:parentActivityName=".MyActivity in the manifest, the back button doesn't show up.
That's how this works my friend.

Call an Activity by using Click on SideBar Navigation Menu Item

How to Call an Activity while using Side Navigation in Android.
I am using this sample :Navigation menu for Android (based off Google+ app)
https://github.com/darvds/RibbonMenu
Here i want, whenever user will click on Home then need to call CategoryActivity and when do click on Home2 then need to call OptionsActivity and so on.....
RibbonsampleActivity.Java:
public class RibbonsampleActivity extends Activity implements iRibbonMenuCallback {
/** Called when the activity is first created. */
private RibbonMenuView rbmView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rbmView = (RibbonMenuView) findViewById(R.id.ribbonMenuView1);
rbmView.setMenuClickCallback(this);
rbmView.setMenuItems(R.menu.ribbon_menu);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
rbmView.toggleMenu();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
#Override
public void RibbonMenuItemClick(int itemId) {
// Handle item selection
}
}
ribbon_menu.xml:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/ribbon_menu_home" android:title="Home" android:icon="#drawable/ic_launcher"></item>
<item android:id="#+id/ribbon_menu_home2" android:title="Home2" android:icon="#drawable/ic_launcher"></item>
<item android:id="#+id/ribbon_menu_home3" android:title="Home3" android:icon="#drawable/ic_launcher"></item>
I want to call activities by using particular Side Bar Navigation Item:
Home > CategroyActivity
Home2 > OptionsActivity
Home3 > ArrowActivity
you have got a null pointer in getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar() returns null. there could be couple of reasons for this, most probably this could be an issue of your project setup. check your AndroidManifest.xml and remove if you have set a "fullscreen" or "no title bar" theme.
I was facing the same issue. Solution for this problem is you need to use Fragment instead of Activity.
Best Example here: http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

Categories

Resources