androidcustom-titlebar

Android customized default title bar is not showing in 4.x devices


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:
enter image description here

On device 4.1.2:
enter image description here

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

  • 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.