After spending hours, i got solution to my problem.
Adding this android:theme=@android:style/Theme.Light
theme to activity is fixed problem and now same customize title bar shown as above device 2.2 screenshot, is shown across all versions properly.
Android customized default title bar is not showing in 4.x devices
-
29-03-2022 - |
Question
I had customized the default title bar and it's showing as expected in 2.x, 3.x devices, but it's not showing in 4.x devices.
On device 2.2:
On device 4.1.2:
This is the code inside onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView localTextView = (TextView) getWindow().findViewById(
android.R.id.title);
if (localTextView != null) {
ViewParent localViewParent = localTextView.getParent();
if ((localViewParent != null)
&& ((localViewParent instanceof FrameLayout))) {
View localView = ((LayoutInflater) getSystemService("layout_inflater"))
.inflate(R.layout.logout_button, null);
UIImageButton localUIImageButton = (UIImageButton) localView
.findViewById(R.id.logoutButton);
Rect localRect = new Rect();
Window localWindow = getWindow();
localWindow.getDecorView().getWindowVisibleDisplayFrame(
localRect);
int i = localRect.top;
int j = localWindow.findViewById(android.R.id.title).getTop() - i;
PrintStream localPrintStream = System.out;
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = Integer.valueOf(j);
localPrintStream.printf("%d", arrayOfObject);
localUIImageButton.setMaxHeight(j);
((FrameLayout) localViewParent).addView(localView);
}
}
}
This is the manifest code:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I want the title bar in 4.x devices to be similar as in 2.x.
Solution 2
OTHER TIPS
For Android 4.0 and above you need to customize the action bar. see: http://developer.android.com/guide/topics/ui/actionbar.html
Something along the lines of this.
ActionBar actionBar = getSupportActionBar();
LayoutInflater inflator = (LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.logout_button, null); // your logout button
actionBar.setCustomView(v);
A quick way to fix this is to change the android:targetSdkVersion back down to a pre honeycomb version. Then the app will be rendered using the compatibility mode and the title bar should match the older devices.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow