PreferenceFragment not shown when using ActionBarActivity - android

I have a PreferenceFragment and it can be shown inside an Activity. However, when I switch the Activity to an ActionBarActivity, the fragment is not shown. (I can only see an action bar and a blank white screen below.) The theme I am using is Theme.AppCompat.Light, therefore I need to use ActionBarActivity in order to display the ActionBar.
Here is my original code:
public class SettingsActivity extends Activity { // later changed to extend ActionBarActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content
getFragmentManager().beginTransaction().
replace(android.R.id.content, new SettingsFragment()).commit();
}
}

I'm extending ActionBarActivity and my PreferenceFragment works.
I think you need to call setContentView() on your Activity, to have an activity layout in which the fragment will be loaded.
activity_preference_layout.xml
<LinearLayout 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:orientation="vertical"
tools:context="your.package.SettingsActivity">
<include layout="#layout/toolbar"/>
<FrameLayout
android:id="#+id/preference_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Then your Activity should be something like:
public class SettingsActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preference_layout);
getFragmentManager().beginTransaction().
replace(R.id.preference_container, new SettingsFragment()).commit();
}
}
Note that I've replaced android.R.id.content with R.id.preference_container, which is the frame defined in the layout above.

Related

How to add width and height of fragment programatically?

I want to add a fragment to MainActivity but when I am adding a fragmnet to it, it is covering the full width and height of main activity. So how to set the width and height of fragment to wrap content programatically, not using XML.
Here is the code of what I have do so far:
MainActivity.xml
<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:id="#+id/rekl"
android:layout_height="match_parent"
tools:context="com.example.ankit.fragment2.MainActivity">
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment3 f=new Fragment3();
FragmentManager m=getFragmentManager();
FragmentTransaction t=m.beginTransaction();
t.add(R.id.rekl,f,"Frag");
t.commit();
}
}

Navigation drawer in multiple activities

I know this question has been asked multiple times. But my concern is different.
Using Mr. Tamada Tutorial I've created a NavigaionActivity and multiple fragments to replace in FrameLayout. It's working fine.
Now, after selecting any option in a fragment, another activity gets open.
I want the same Navigation menu in that Activity.
Navigation view -- fragments -- Activity (display navigation view here)
What I tried:
use the xml code of displaying Navigation view in that activity.
(DrawerLayout, CoordinatorLayout, AppBarLayout etc)
Then in Activity.java, on click of menu item diverting to the Navigation Activity.
Code:
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
....>
<android.support.design.widget.CoordinatorLayout
....>
<android.support.design.widget.AppBarLayout
....>
<android.support.v7.widget.Toolbar
.... />
</android.support.design.widget.AppBarLayout>
<LinearLayout
.... /> <!-- main content of this Acitivity-->
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
.... />
</android.support.v4.widget.DrawerLayout>
Activity.java:
public void dashboard(MenuItem item) {
Bundle bundle = new Bundle();
bundle.putString("fragment", Constant.DASHBOARD_FRAGMENT);
UtilityClass.newActivity(this, NavigationActivity.class, bundle);
}
And handling the call on Navigation Activity. It is doing the task but code isn't re-usable
Create a separate layout file for Navigation and include it in the Activity. But, this is replacing the main content. Here only included Navigation is visible.
Is there any other way to do it?
Downvoters - here the solution is..
Create an xml file which will have DrawerLayout and NavigationView (one can use the xml given in Question, without the main content) - navigation.xml
As suggested in many answers "create a BaseActivity which extends AppCompatActivity. And inflate navigation.xml.
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = View.inflate(this, R.layout.navigation, null);
// view declarations
DrawerLayout drawer = (DrawerLayout) view.findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) view.findViewById(R.id.nav_view);
...... }}
In whichever Activity you wanna use this NavigationMenu, extend BaseActivity for that class.
GraphActivity extends BaseActivity { .... }
In GraphActivity.xml add the NavigationMenu code. You can't just include the navigation.xml it will disable the current xml widgets.
Done!
Try this:
public class MainActivity extends BaseActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_describtion);
getLayoutInflater().inflate(R.layout.activity_main, frameLayout);
mDrawerList.setItemChecked(position, true);
}
Plz Don't use it this way
Instead use Google recommended MasterDetailFlow Design
You can read how to implement this on
https://inducesmile.com/android/android-fragment-masterdetail-flow-tutorial-in-android-studio/

How to have ActionBar in PreferenceActivity?

I have an android app with a UserSettingsActivity derived from PreferenceActivity. When I open this activity, no menu bar is shown.
In order to have the 'bar' or 'action bar' on top of the settings screen, I searched for it in google on found this link and this link. Because the first answer seems outdated, I tried the suggestion in the second link which does not work. Therefore I am posting this question again.. Here are the pieces of code I am using:
preferences.xml ( I added just the android.support.v7.widget.Toolbar part):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="#string/abc_action_bar_up_description"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="#string/action_settings"
/>
<ListPreference
android:title="#string/UpdateInterval"
android:summary="#string/UpdateIntervalText"
android:key="updateInterval"
android:defaultValue="24"
android:entries="#array/listArray"
android:entryValues="#array/listValues" />
<ListPreference
android:title="#string/LimitSetting"
android:summary="#string/LimitSettingText"
android:key="limitSetting"
android:entries="#array/limitArray"
android:entryValues="#array/limitValues" />
<Preference
android:key="reset"
android:title="Reset database"
android:summary="This will remove every entry in the database"
/>
</PreferenceScreen>
and UserSettingsActivity.java (I added the onPostCreate part):
public class UserSettingActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.xml.preferences, root, false);
root.addView(bar, 0); // insert at top
bar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
public class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences); // <<-- ERROR HERE
// doing something here
}
}
}
However, I get the following error:
Caused by: java.lang.ClassCastException: android.support.v7.widget.Toolbar cannot be cast to android.preference.Preference
on the marked line. Without the added pieces in those two files the app did work, without the (action)-bar, however...
What seems to be the problem here?
Try this.
Extend your activity with AppCompatActivity
Then add toolbar. Refer here: -http://coderzpassion.com/android-working-with-material-design/
PreferenceActivity is now deprecated. Use PreferenceFragment inside any other activity, which can be extended from any parent activity other than PreferenceActivity

How to get rid of this default splash screen?

I've tried adding splash screens to my app, and while they do show before the main activity, this screen also always shows no matter what:
Here is the code for MainActivity:
public class MainActivity extends Activity implements WelcomeFragment.StartQuestions, QuestionFragment.QuestionsAnswered {
#Override
protected void onCreate(Bundle savedInstanceState) {
Parse.enableLocalDatastore(this);
Parse.initialize(this, "***************************", "*****************************");
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
loadingIcon = (ProgressBar) findViewById(R.id.loadingIcon);
loadingIcon.setVisibility(View.GONE);
checkInternetConnection();
showWelcomeScreen();
}
}
showWelcomeScreen():
public void showWelcomeScreen(){
Fragment fragment = getFragmentManager().findFragmentById(R.id.fragmentContainer);
if (fragment == null){
fragment = new WelcomeFragment();
getFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
} else {
fragment = new WelcomeFragment();
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, fragment)
.commit();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/loadingPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" android:id="#+id/loadingIcon"/>
</RelativeLayout>
In MainActivity, I have already specified the removal of the top bar:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
The app itself doesn't have the bar. Any activity that I place as being default in the manifest (acting as a splash screen) is nevertheless proceeded by the screen shown above when the app is launched.
You can't. Android will always attempt to show something using only the attributes of your theme before your Activity actually loads.
In fact, the correct way to build a splash screen involves taking advantage of that fact and customizing your theme such that what displays during this time is your splash screen.

fragmentactivity with custom surfaceview

i'm programming an android game and therefore im using a class that extends surfaceview.
My Activity looks like this:
public class GameActivity extends FragmentActivity {
private Fragment fragEnd;
private GameView gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
setContentView(gameView);
fragEnd = (FragmentEnd) Fragment.instantiate(this, FragmentEnd.class.getName(), null);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add( WHAR COMES IN HERE??? , fragEnd);
ft.commit();
}
...
The GameView class extends the surfaceview.
My activity_game.xml layout file looks like this:
<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"
tools:context=".GameActivity"
android:id="#+id/myView">
</RelativeLayout>
So there is nothing special defined in my xml.
Now i want to use fragments in the game for options etc. I read some tutorials and added the components to my (Fragment)Activity. But i recieve an IllegalArgumentException: "no view found" while trying to create the fragment. i know that the problem is, that the current view (Surfaceview) cannot be found. in the tutorials, they only worked with layout xmls like writing:
setContentView(R.layout.activity_main);
And then get the component (for example the Relativelayout id "relativeLayoutView") of the activity_main to define the
ft.add( R.id.relativeLayoutView , fragEnd);
But in my case there is nothing in my activity_main because all happens ins the gameView-class.
How do i solve this to display fragments?
Create a layout like this and set to the activity .. Also add an interface in GameView and implement the interface in activity. when you show some thing in fragment , call the interface method from GameView and add the fragment into the activity using R.id.container or other approach is to show the options as dialog.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.examples.workapp.app.GameView
android:id="#+id/gameView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="200dp">
</FrameLayout>
</LinearLayout>

Categories

Resources