How to override default name with custom title in android

I developed a custom title bar for my application. This works well for me as well. But there is a problem. The default title is displayed (just for a second) before my custom title bar cancels it. Is there a solution to disable the default title bar that android supports?

My codes were similar ...

window_title.xml in res / layout

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myTitle" android:text="custom title bar" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/titletextcolor" android:gravity ="center"/> 

color.xml in res / values

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="customTheme" parent="android:Theme"> <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground </item> </style> </resources> 

styles.xml in res / values

 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="titlebackgroundcolor">#303010</color> <color name="titletextcolor">#FFFF00</color> </resources> 

themes.xml in res / values

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="WindowTitleBackground"> <item name="android:background">@color/titlebackgroundcolor</item> </style> </resources> 

And I also updated the activity of manifest.xml, like

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.title" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/ic_launcher" android:theme="@style/customTheme" android:label="@string/app_name" > <activity android:name=".CustomTitleActivity" android:theme="@style/customTheme" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

and my onCreate method, which I did as ..

 super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.window_title); final TextView myTitle = (TextView) findViewById(R.id.myTitle); myTitle.setText("Converter"); 

And it works well too

I coded as in http://www.edumobile.org/android/android-programming-tutorials/creating-a-custom-title-bar/ and it works well too.

In any case, disable the default header that appears first?

+6
source share
1 answer

why do you relay on android for the title, just create your title bar as you wish and set it at the top in your main layout and use the following code in onCreate before setting the layout

 requestWindowFeature(Window.FEATURE_NO_TITLE); 

here is an example ... define your title bar in your main layout, for example, if you set main.xml as the activity layout, then your main.xml should look like

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > //here i have used LinearLayout as example your can be Relative or whatever //here my title bars layout starts containing an icon and a text your can containing just text or whatever you want <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/my_title_background" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="15dip" android:src="@drawable/my_icon" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="15dip" android:text="My Application title" android:textColor="#FFFFFF" /> </LinearLayout> //and here rest of your layut for your application functionality </LinearLayout> 

then in my activity a java file that contains the onCreate () method will look like this:

  /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); //restof code for my class 

you don’t need to set anything else for the title, for example, install themes and install other functions or permissions .......... I hope you got it

+1
source

Source: https://habr.com/ru/post/903263/


All Articles