Extended activity name not showing with custom style

I override the Activity class as follows:

public class MyCustomActivity extends Activity { ... ... } public class MyActivity extends MyCustomActivity { ... ... } 

And the declaration of activity in the manifest is as follows:

 <activity android:name=".MyCustomActivity" android:label="@string/app_name" > </activity> <activity android:name=".MyActivity" android:label = "ChildActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

My problem is that the action title defined in the manifest in android:label="ChildActivity" does not display with a custom style. How can I do this without calling setTitle(charSeq); in every action?

Edit:

One thing (terrible thing) that I forgot about is that I use a custom style as follows;

 <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/CustomTheme" > ... ... </application> 

Edit (after @Mudassir's answer): I am using a custom title bar as shown below:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dip" android:gravity="center_vertical" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:gravity="left" android:orientation="horizontal" > <ImageView android:id="@+id/imageViewIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/vector_arts" android:layout_marginLeft="-15dp" /> <TextView android:id="@+id/textTitleBar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout> 
+4
source share
3 answers

As I understand your question, you are using a text view (custom style) to show the title text. Thus, in order to show the default header text (defined in the manifest in android:label ) in the custom header line (text view), you must override the setTitle method in the parent (MyCustomActivity) as follows;

 @Override public void setTitle(CharSequence titleText) { // Assuming you text view name is titleTextView in the style XML file TextView customTitle = (TextView) findViewById(R.id.titleTextView); customTitle.setText(titleText); } 

then call this method from onCreate() the same class as follows:

setTitle(getTitle());

This will get the activity label defined in the manifest file and pass it to the setTitle() method, which will show it in your custom header line, which is a text view (customTitle here). Note that this approach will only work if you use a text view to display the title text.

+2
source

You get the same name as in the manifest file in the action header. if you do this:

 <activity android:name=".MyActivity" android:label="ChildActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TestingMyAcivity"></activity> public class TestingMyAcivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } 

}

 public class MyActivity extends TestingMyAcivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_); } 

}

you will get the title "ChildActivity" and the layout in the main_.xml file.

+2
source

You start MyActivity , where you set ChildActivity as the title. MyActivity is a child of MyCustomActivity . While you set the name of the child class, it came to the fore. The activity you see is actually a child activity, not a parent activity. So you will see the name ChildActivity

Thanks Sunil Kumar Sahoo

+1
source

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


All Articles