I'm developing an application where I set up an activity for the settings.
I tried to use the android studio template to create an activity setting but the structure of the code that is being created is too complex.
Can anyone tell me how to create a settingsActivity similar to the android studio template but without headers?
Thanks in advance for the answer.
Greetings.
To create a single settingActivity you can try the below pseudo code:
public class SettingsUI extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).commit();
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
PreferenceManager.setDefaultValues(this, R.xml.settings_preference, false);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preference);
}
}
}
Then settings_preference.xml code:
PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="sample_key"
android:title="#string/sample_title"
android:inputType="number"
android:summary="#string/sample_summary"/>
<EditTextPreference
android:key="sample2_key"
android:title="#string/sample2_title"
android:inputType="number"
android:summary="#string/sample2_summary"/>
</PreferenceScreen>
In the styles.xml file, please make one style tag if you want to remove actionbar(you said header) in only specific activity.
<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar" />
And goto AndroidManifest file, please apply above style to your activity something like this.
<activity android:name=".SettingActivity" android:theme="#style/AppTheme1"></activity>
if you want to remove actionbar in all of your activities, then you can change AppTheme style in the styles.xml file like below.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/mainColor</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
... ...
Related
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, BaseActivity.class);
startActivity(i);
}
and my next activity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_base);
}
I still see blue bar with application name at top.
I tried changing my application manifest to use a no-title theme and the program crashed because the activities inherit from AppCompatActivity.
How do I inherit from AppCompatActivity and get rid of the application title name up top?
It's the actionbar, you can use the theme Theme.AppCompat.NoActionBar or Theme.AppCompat.Light.NoActionBar or a theme which descents from them.
To make disappear is the action bar also this should work:
getSupportActionBar().hide();
Try this...
If you don't want the action bar in your activity then,
Create a style
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and apply this style for the activity in Manifest for which you don't need action bar,
<activity android:name=".BaseActivity"
android:theme="#style/AppTheme.NoActionBar"></activity>
Do this in your onCreate() method.
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here);
this refers to the Activity
OR
You can modify your AndroidManifest.xml:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
You can use this for each activity in your project.
OR
also change from style.xml
<style name="generalnotitle" parent="general">
<item name="android:windowNoTitle">true</item>
</style>
Hope this helps you.
I need my actionbar to be ready before setContentView because it is used by the navDrawerFragment but at this point:
public class BaseActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
Log.d("", getActionBar().toString());
setContentView(R.layout.activity_base);
}
It is returning null
My theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/light_blue</item>
<item name="colorPrimaryDark">#color/dark_blue</item>
<item name="colorAccent">#color/dark_blue</item>
<item name="android:windowBackground">#color/white</item>
<item name="android:positiveButtonText">#color/white</item>
<item name="android:windowActionBar">true</item>
</style>
and the declaration at manifest:
<activity
android:name=".controller.activity.BaseActivity"
android:label="#string/app_name" >
</activity>
Your BaseActivity must extends from ActionBarActivity and not Activity
public class BaseActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
Use getSupportActionBar(); to get the ActionBar
Your problem is either one of these two things, or both. At least they should be, unless I'm insane..
Either the problem is that you are calling
getActionBar()
before you set the contentView
So change it to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_base);
Log.d("", getActionBar().toString());
}
OR
It is that you need to call
getSupportActionBar()
Try both and tell me which works!
You're using the support library (AppCompat), so you have to call getSupportActionBar()
I want to hide the divider between preferences in fragment.
The code is below:
1.SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
super.onCreate(savedInstanceState);
SettingsFragment settingsFragement = new SettingsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, settingsFragement, "settings");
transaction.commitAllowingStateLoss();
}
2.SettingsFragment.java
public class SettingsFragment extends PreferenceFragment{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
3.settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<Preference
android:key="preference_a"
android:title="preferenceA"/>
<Preference
android:key="preference_b"
android:title="preferenceB"/>
<ListPreference
android:key="list_preference_c"
android:title="list_preferenceC"/>
</PreferenceCategory>
</PreferenceScreen>
There are system default dividers amoung the preferences.
I want to hide the dividers.
How to hide the dividers? Thanks a lot.
Thanks for everyone`s answer. But,the question is that I cannot get the listview from the activity and fragment.
ListView list = (ListView) findViewById(android.R.id.list);
if you are using PreferenceFragmentCompat , it is using Recycle view so you have to use this code . This automatically hides the divider
#Nullable
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
setDivider(new ColorDrawable(Color.TRANSPARENT));
setDividerHeight(0);
}
These solutions didn't work for me. Here's what I did in PreferenceFragmentCompat:
#Override
public void setDivider(Drawable divider) {
super.setDivider(new ColorDrawable(Color.TRANSPARENT));
}
#Override
public void setDividerHeight(int height) {
super.setDividerHeight(0);
}
Define custom preference theme without dividers in your style.xml:
<style name="PrefsTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="android:divider">#null</item>
<item name="android:dividerHeight">0dp</item>
</style>
and don't forget to use it in your main app theme which is also in style.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="preferenceTheme">#style/PrefsTheme</item>
</style>
You can remove your divider by style.
<style name="PreferenceFragment.Material">
<item name="android:divider">#null</item>
</style>
First: if used the listview in xml
set the android:dividerHeight="0dp" in listview in xml file ,
second:
ListView list = (ListView) findViewById(android.R.id.list);
list.setDivider(new ColorDrawable(Color.TRANSPARENT)); // or some other color int
list.setDividerHeight((int) getResources().getDisplayMetrics().density);
I have the following code which results in error message that I *can not combine FEATURE_CUSTOM_TITLE with other title features* (which I am not to my knowledge...)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String customTitleStr = getIntent().getExtras().getString("Title");
Boolean customTitleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if ( (customTitleSupported) && (customTitleStr != null) ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.override_titlebar);
final TextView myTitleText = (TextView) findViewById(R.id.myTitleText);
if ( myTitleText != null ) {
myTitleText.setText(customTitleStr);
}
}
I have tried place setContentView(R.layout.); both before and after this.requestWindowFeature but no change.
My manifest targets this:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"
/>
For reference (don't know if related), I also have trouble theming my titlebar. (Simply ignored.)
Just in case it is relevant, some code snippets for my theming:
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/replace__logo__app_android"
android:label="#string/MicAppName"
android:theme="#style/MicTheme"
android:name="com.examples.example.MicApp"
>
styles.xml:
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
// ...
<style name="MicTheme" parent="AppTheme">
<item name="android:windowTitleSize">44dip</item>
<item name="android:windowTitleBackgroundStyle">#style/MicWindowTitleBackground</item>
<item name="android:windowTitleStyle">#style/MicWindowTitle</item>
</style>
Please try with this code.
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class CustomTitleBar extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
}
for more information about how to set custom title bar please follow below tutorials.
http://www.edumobile.org/android/android-programming-tutorials/creating-a-custom-title-bar/
http://www.powenko.com/en/?p=214
http://7thursdays.wordpress.com/2012/10/14/how-to-make-a-custom-title-with-android/
hope this helps you.
EDIT
here is the working screenshot.
I have xml file that defines some preference screens like the following example
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="root_preferencescreen">
<PreferenceScreen android:key="general_sett" android:title="general settings" />
....
<PreferenceScreen android:key="extras_sett" android:title="extras settings" />
</PreferenceScreen>
I would like to be able to increase the font size for the text of the preference screen , but because the within a preference screen there is no android:textsize tag , i have no idea how to accomplish that !
You can simply add android:textSize inside your theme for preference screen:
e.g :
<style name="settingsTheme" parent="PreferenceThemeOverlay">
<item name="colorAccent">#color/color_ten</item>
<item name="android:background">#color/colorPrimaryLight</item>
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
<item name="android:textColorPrimary">#color/colorTextBodyLight</item>
<item name="android:textColorSecondary">#color/colorTextCaptionLight</item>
<item name="android:textSize">14sp</item>
</style>
Your SettingsActivity class :
public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
toolbar = (Toolbar)findViewById(R.id.toolbar_settings);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager().beginTransaction()
.replace(R.id.bodylayout, new PrefsFragment())
.commit();
}
#Override
protected void onPause() {
super.onPause();
}
}
PrefsFragment class :
public class PrefsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreatePreferences(Bundle bundle, String s) {
}
#Override
public void onCreate(Bundle savedInstanceState) {
final Context myContext = this.getActivity();
//set your theme
myContext.setTheme(R.theme.settingsTheme);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settingspreference);
//do other stuffs
}
//.....
}
You can make a TextView layout xml that looks something like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/listSeparatorTextViewStyle"
android:textColor="#android:color/white"
android:id="#+android:id/title"
/>
and set the layout of your preference category in your preferences.xml like this:
<PreferenceCategory
android:title="Category Title"
android:layout="#layout/pref_category"
/>
As long as the TextView has the id #+android:id/title you can make the layout look however you want. There is also a way to do this with styles that I haven't quite figured out.