how to set custom title bar TextView Value dynamically in android? - 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)

Related

android.view.InflateException: Binary XML file line #19: Error inflating class android.support.v7.widget.ContentFrameLayout

I want to change title bar text and background color by programmatically. That is why , I have used the following code :
public class SplashScreen extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_splash);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
TextView customTitleText = (TextView) findViewById(R.id.customtitlebar);
customTitleText.setText("Whatever you want in title");
}
}
My customtitlebar.xml is as following :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/titletextcolor">
<ImageView
android:layout_width="25px"
android:layout_height="25px"
android:src="#drawable/ic_launcher">
</ImageView>
<TextView
android:id="#+id/customtitlebar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text=""
android:textColor="#color/titletextcolor"
android:textStyle="bold"
android:background="#color/titlebackgroundcolor"
android:padding="3px" />
</LinearLayout>
But when I want to run the project , I am getting this exception :
03-09 17:21:52.181: E/ActivityThread(15403): RuntimeExceptionjava.lang.RuntimeException: Unable to start activity ComponentInfo{com.asif.gmaillogin/com.asif.gmaillogin.SplashScreen}: android.view.InflateException: Binary XML file line #19: Error inflating class android.support.v7.widget.ContentFrameLayout
How can I solve this exception ? Please help me .
See at line 19 on xml file. TextView android:text="" attr is empty, if you want make it without text remove this attr
<TextView
android:id="#+id/customtitlebar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#color/titletextcolor"
android:textStyle="bold"
android:background="#color/titlebackgroundcolor"
android:padding="3px" />
This line must be the problem
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
You must call requestWindowFeature() before super as well as setContentView().
Your code will be like -
public class SplashScreen extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
TextView customTitleText = (TextView) findViewById(R.id.customtitlebar);
customTitleText.setText("Whatever you want in title");
}
}
P/s, There are requirement to user getWindow().setFeatureInt(); method. Refer this SO link to learn more.

How to set layout Background programatically

I want to set the layout background programatically, depending on the genre of the song to be played in my application.
I tried this:
public class AnswerActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY");
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.hide();
LinearLayout root = (LinearLayout) findViewById(R.id.ctr1);
Bundle data = getIntent().getExtras();
if(data.getString("genre").equals("rock")){
root.setBackgroundResource(R.drawable.wprock);
}
else if(data.getString("genre").equals("pop")){
root.setBackgroundResource(R.drawable.wppop);
}
else if(data.getString("genre").equals("hiphop")){
root.setBackgroundResource(R.drawable.wphiphop);
}
}
But it doesn't work, it's throwing a Null Pointer Exception in the root.setBackgroundResource lines, whenever anyone of these take place.
Any ideas?
Thanks in advance
EDIT: I do have R.drawable.wprock/pop/hiphop, plus I ruled out that possiblity bevause I tried to use a color instead with the setBackgroundColor mehtod and I had the same exception.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#1d1d1d" >
<ImageView
android:id="#+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="130dp"
android:onClick="go"
android:background="#drawable/playbtn" />
</LinearLayout>
You got the error because you root is a new layout that has not been in your Activity :
LinearLayout root = new LinearLayout(this);
Remove the above code and give an android:id to the outer layout in R.layout.play, and use findViewbyId instead.
First you check the root layout id of your xml if it is correct then follow the code
protected void onCreate(Bundle savedInstanceState) {
Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY");
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.hide();
LinearLayout root = (LinearLayout) findViewById(R.id.ctr1);
Bundle data = getIntent().getExtras();
if(data.getString("genre").equals("rock")){
root.setBackgroundResource(R.drawable.wprock);
}
else if(data.getString("genre").equals("pop")){
root.setBackgroundResource(R.drawable.wppop);
}
else if(data.getString("genre").equals("hiphop")){
root.setBackgroundResource(R.drawable.wphiphop);
}
then check
Bundle data = getIntent().getExtras();
data in bundle is null or some thing else
Your xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ctr1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#1d1d1d" >
<ImageView
android:id="#+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="130dp"
android:onClick="go"
android:background="#drawable/playbtn" /></LinearLayout>

when sending strings via put extra the underline under some words has gone

when I send strings via put extra the underlined words will not be underlined
<string name="s_hello_txt">\n{ <u>hello all</u> }\n</string>
MainActivity Button Code
public void c_hello(View v){
Intent hello= new Intent(MainActivity.this,
MainTextActivity.class);
intent_collection_e3tiraf.putExtra("key",getResources().getString(R.string.s_hello_txt));
startActivity(hello);
finish();
}
MainActivityText onCreate Code
textview = (TextView) findViewById(R.id.id_text_txt);
Intent n = getIntent();
String mrng = n.getStringExtra("key");
textview.setText(mrng);
if I put a text with direct string it will be underlined
For Example if I put in the layout of MainActivityText(activity_maintext.xml)
<TextView
android:id="#+id/id_dailyprayers_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/s_hello_txt"
android:textSize="30sp" />
the textview in MainActivityText Show the text(hello all) underlined
any help!!!!
As long as the string still has the underline html you should be able to utilize the Html.fromHtml method to style the string.
textview.setText(Html.fromHtml(mrng));
Actually, the string getResource().getString(R.string.s_hello_txt) is not be underlined.
The best way to add html source code in strings.xml is to use <![CDATA[html source code]]>. Here is an example:
<string name="s_hello_txt"><![CDATA[<u>Text</u>]]></string>
And then use Html.fromHtml(mrng) to show the underlined string
// Try This One This Will Help For Your Acheivement
**String.xml**
<string name="s_hello_txt"><br/>{ <u>hello all</u> }<br/></string>
**activity_main1.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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/s_hello_txt"/>
<Button
android:id="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GoTo Second Activity"/>
</LinearLayout>
**MainActivity1 Activity**
public class MainActivity1 extends Activity {
private Button btnClick;
private TextView txtValue;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
txtValue = (TextView)findViewById(R.id.txtValue);
btnClick = (Button)findViewById(R.id.btnClick);
txtValue.setText(Html.fromHtml(getString(R.string.s_hello_txt)));
btnClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity1.this, MainActivity2.class);
intent.putExtra("EXTRA",getString(R.string.s_hello_txt));
startActivity(intent);
}
});
}
}
**activity_main2.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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
**MainActivity2 Activity**
public class MainActivity2 extends Activity {
private TextView txtValue;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
txtValue = (TextView)findViewById(R.id.txtValue);
txtValue.setText(Html.fromHtml(getIntent().getStringExtra("EXTRA")));
}
}

findResourceById() returning null

I've read a few questions and answers regarding this topic, but none of them applied to my issue.
My problem is in the following code:
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.alt);
}
public void buttonPress(View v){
TextView tv1 = (TextView) findViewById(R.id.text1);
TextView tv2 = (TextView) findViewById(R.id.text2);
Button b1 = (Button) findViewById(R.id.button1);
if((tv1 != null) & (tv2 != null) & (b1 != null)){
tv1.setText(R.string.changed);
tv2.setText(R.string.changed);
b1.setText(R.string.changed);
}
else
Toast.makeText(getApplicationContext(), "VIEW ELEMENTS ARE NOT BEING ASSIGNED", Toast.LENGTH_SHORT).show();
}
alt.xml looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:name="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/stuff" />
<TextView
android:name="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stuff2" />
<Button
android:name="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonText"
android:onClick="buttonPress" />
The IDs are being generated in R.id, but they aren't being assigned to my two TextView elements and my one Button element. (I'm seeing the toast popup every time)
Does anyone have any idea?
Instead of android:name="..." use android:id="#+id/..."
Try replacing
android:name="#+id/text1"
By
android:id="#+id/text1"

Set custom title bar in PreferenceAcivity

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>

Categories

Resources