Set custom title bar in PreferenceAcivity - android

I am using PreferenceActivity, how can I set a custom title bar? Not only text but background color, size - the whole layout.

PreferenceActivity extends ListActivity, and when you inflate the preferences from xml with addPreferencesFromResource(), it puts the stuff into the standard ListView that ListActivity uses.
So basically, you can use setContentView() to specify a layout, but you need to have a ListView in it with the id "#+android:id/list".
So using kleini's example code:
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.login_settings);
setContentView(R.layout.login_settings_layout);
}
You would need a ListView in login_settings_layout.xml that looks something like:
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

This is the only thing that worked for me. Rest of the above stuff did not fetch desired results on my 4.3 Nexus Tablet.
I could not really recreate a proper actionbar like widget, but was able to put a large 'Settings' title on top of the PreferenceActivity, with below steps.
I got the hint from another stackoverflow answer and am just making it more elaborate.
In PreferenceActivity class (remove the existing titlebar)
public class Settings extends PreferenceActivity· {
...
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); // This goes first
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
In res/xml/settings.xml, I would like to draw attention to the first PreferenceCategory
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:layout="#layout/settings_titlebar" />
<PreferenceCategory android:title="Notifications">
<Preference .....
In res/layout/settings_titlebar.xml
Navals-MacBook-Pro:ver_ui_0.2 Naval$ vi res/layout/settings_titlebar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/header_background"
android:enabled="false">
<TextView android:src="#drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:padding="4dip"
android:text="Settings"
android:textSize="32sp"
/>
</RelativeLayout>

public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
}
}

Ben's method worked out well for me!!. Here is the code
public class PreferenceCustomTitleActivity extends PreferenceActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
/** Customize your background, textsize, cachetint, dividers
for your list view in the xml **/
setContentView(R.layout.layout_with_simple_listview_only);
ListView list = (ListView) findViewById(android.R.id.list);
View preferenceHeader = getLayoutInflater().inflate(
R.layout.preference_header, null);
list.addHeaderView(preferenceHeader);
}
}

Awesome, worked fine for "NO_TITLE":
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.login_settings);
setContentView(R.layout.login_settings_layout);
}

Or like this:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
addPreferencesFromResource(R.xml.preferences);
}
With a customtitlebar.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customTitleBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/CustomWindowTitle">
</TextView>

Related

Multine line label on settings

I would like to crate a settings Activity with items with Title and subtitle.
I've checked a lot in documentation and even other posts here and people say it's not possible... android default settings page has a lot of it:
sorry for the picture in portuguese, but what is written there doesn't matter, the point is the settings page built on android has plenty of titile/subtitle items (and it happens in many other screens)
I would like to achieve it using android default Preference api. is that possible?
Yes it is possible. One fast solution is the following (that might be improvied and tuned according your needs):
public class SettingsActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.settings_fragment, new PrefsFragment()).commit();
}
}
public static class PrefsFragment extends PreferenceFragment {
public PrefsFragment(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
}
activity_settings.xml
<?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">
<fragment
android:id="#+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.vb.myapplication.SettingsActivity$PrefsFragment" />
</LinearLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="keyCategory"
android:title="titleCategory">
<CheckBoxPreference
android:key="keyCheckPreference"
android:title="titlePreference"
android:summary="summaryPreference"
android:defaultValue="false"/>
</PreferenceCategory>
</PreferenceScreen>

NullPointerException when trying to load a button

I am trying to open a new activity. When I do I get a null pointer exception. Everything works untill the following code gets executed.
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
Here is a better look at the code.
public class DisplayTests extends ListActivity {
private DbManagement mdbManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
private OnClickListener AnsRmTest = new OnClickListener(){
#Override
public void onClick(View v) {
try{
System.out.println("fart");
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
};
I am not sure why the activity would bomb out when it reaches the creation of a button.
Here is the XML as well
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="194dp" >
</ListView>
</FrameLayout>
<Button
android:id="#+id/btnNewExam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/strNewExam" />
<Button
android:id="#+id/btnRmExams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnRmTests"
android:layout_alignBottom="#+id/btnRmTests"
android:layout_alignParentRight="true"
android:text="#string/strRmExams" />
<Button
android:id="#+id/btnRmTests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnNewExam"
android:layout_alignBottom="#+id/btnNewExam"
android:layout_toLeftOf="#+id/btnRmExams"
android:text="#string/strRmTest" />
</RelativeLayout>
So with that being said I am the following code
setContentView(R.layout.moretime);
When I did that I get the following error:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
I do have a list view as you can see about but it is called listView1 and I am not sure why I am getting this error message. Thanks in advance.
You need to set the content of your layout to the activity first then initialize views.
privae Button RmTests;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); // missing
RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
... // rest of the code
}
You can findViewById of the current view hierarchy set to the activity. Since you have not set the content to the activity, when you initialize your button you get NullPointerException.
ALso
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list" (or list if it's in code)
In your xml replace
<ListView
android:id="#+id/listView1"
By
<ListView android:id="#android:id/list"
Also do you require a FrameLayout? Why not have just the listview and the buttons in the relative layout.
http://developer.android.com/reference/android/app/ListActivity.html
Edit:
From the comment below
You can do as below
public class MainActivity extends ListActivity {
String s[] = {"A","B","C","D","E"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, s);
setListAdapter(adapter);
}
}
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate().
If you need to have other views then you can define them in xml extend ListActivity in which case you need to have listview with id "#android:id/list". Like i can have a list and a button at the button. So i need to have a listview with id as "#android:id/list".
Where do you tell your activity which xml it is supposed to load?
Try:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
replace "R.layout.main" with your xml file title. E.g. if it's Main_Layout.xml, replace it with "R.layout.Main_Layout"
Write
setContentView(R.layout.your_layout);
after
super.onCreate(savedInstanceState);
try be adding setContentView(R.layout.your_layout_name); after super.onCreate(savedInstanceState);
You have to set the setContentView .Here is the Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
}
I think in you Activity inside onCreate methode you forget to give setContentView
Before initializing the UI components you must set view for activity inside onCreate methode
ie setContentView(R.layout.yourxml)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourxml);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
Additional Info
also since your are using ListActivty you must provide listview id as
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="194dp" />
Otherwise it will cause another error

Activity of android

I am working with the xml file ,through which i am accessing a string through a variable passkey.later i am assigning it to strPassword. i am getting error for setListAdapter.Here is my code..
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="passkey">
<item>1234</item>
</string-array>
</resources>
and
public class AdminLogin extends Activity
{
public void onCreate(Bundle bdlActivity)
{
String[] strPassword = getResources().getStringArray(R.array.passkey);
setListAdapter(new ArrayAdapter<String>(this,R.layout.adminlogin,
R.id.Editpassword, strPassword))
//button events
}
You need to extend your class with ListActivty. You cant instantiate ListView in simple activity.
You can do it from a simple Activity too:
say you have a main layout, like:
mylayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="#+id/btn_ok" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="OK" android:layout_alignParentBottom="true" />
<ListView android:id="#+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_above="#id/btn_ok" />
</RelativeLayout>
This would be the layout of your activity, with a list view inside of it (#id/listview)
Your Activity's onCreate method should look like this:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
final ListView listView = (ListView) findViewById(R.id.listview);
listView.setListAdapter(new ArrayAdapter<String>(this, R.layout.adminlogin,
R.id.Editpassword, strPassword))
}

how to change the string values of sting.xml file

i hava a layout file like
test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/test_string" />
</RelativeLayout>
and java file like this.
test1.java
public class test1 extends Activity {
String temp="This is String"; //which change dynamically
TextView tempview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tempview=(TextView)findViewById(R.id.test_string);
tempview.setText(temp);
setContentView(R.layout.test);
}
}
what i am trying to do is show the value of temp in activity as above code. but it throws an NullPointerException. so how to do. how to set the values of sting.xml???
thanks in advance..
setContentView(R.layout.test); should come before (TextView)findViewById(R.id.test_string);

how to set custom title bar TextView Value dynamically in android?

friends,
i have created custom title bar using following titlebar.xml file with code
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#color/titletextcolor"
android:layout_marginLeft="25px"
android:paddingTop="3px"
/>
and java code to display custom title bar on each activity.
#Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
now i want to set textview value dynamically in each activity can any one guide me how can i achieve this?
using findviewbyid here i dont get reference of that textview to set value because
main layout does not contains any textbox with such a name but mytitle.
any help would be appriciated.
This is the way to set the custom title:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("========= NEW TITLE ==========");
myTitleText.setBackgroundColor(Color.GREEN);
}
}
SDK 2.1+
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("========= NEW TITLE ==========");
}
Have you tried the setTitle() method of your Activity?
my_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#d4e9a9">
<ImageView android:src="#drawable/jetpack"
android:layout_width="wrap_content" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:id="#+id/back"
android:layout_height="wrap_content" android:layout_alignParentTop="true" />
<TextView android:id="#+id/title" android:layout_width="wrap_content"
android:gravity="center_vertical" android:textSize="20px"
android:textColor="#ffffff" android:layout_alignParentRight="true"
android:text="New Title" android:background="#a5c639"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:padding="9dip" android:layout_margin="5dip" />
</RelativeLayout>
Code:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
((TextView)findViewById(R.id.title)).setText("gradient shadow");
findViewById(R.id.back).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
((TextView)findViewById(R.id.title)).setText("loce");
}
});
Because custom title default is fixed you should write yourself a theme:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#style/MyTheme">
Try the following:
#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);
TextView mytitletext = (TextView) findViewById(R.id.myTitle);
mytitletext.setText("========= NEW TITLE ==========");
}
You can use the hierarchyviewer to see if your view is accessible (you will see its id in the hierarchy graph)

Categories

Resources