JonatanEkstedt
JonatanEkstedt

Reputation: 925

Android TimePicker looks old when used in Fragment, but has new look in Activity

In my tabbed Android application, I extended a TabActivity and the tabs had different Activities which, when created, set the Content with setContentView(int) like so:

Alarm.java

public class Alarm extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.alarm);
        // ...
    }
}

alarm.xml

<?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="fill_parent"
    android:background="@color/trans_white" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center"
        android:text="example text"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <TimePicker
        android:id="@+id/alarmTimePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="32dp"
        android:background="@color/black"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" />

    <TextView
        android:id="@+id/alarmActiveTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/alarmTimePicker"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:text="@string/replacement_txt"
        android:textSize="18sp" />

    <Button
        android:id="@+id/alarm_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:gravity="bottom"
        android:onClick="showTimePickerDialog"
        android:text="@string/set_alarm_btn_txt"
        android:textSize="20sp" />

</RelativeLayout>

This brought a nice new looking TimePicker New TimePicker

But then I noticed that TabActivity was deprecated and changed to extending a FragmentActivity where the tabs extend Fragment instead of activity, like so:

Alarm.java

public class Alarm extends Fragment implements OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (container == null) {
            return null;
        }
        return (RelativeLayout)inflater.inflate(R.layout.alarm, container, false);
}

In this new structure I inflate the layout.

This changed the look to the old TimePicker look:

Old TimePicker

Why does this change the style of the TimePicker? How can I get the new TimePicker with this new FragmentActivity?

I import the v4 Support Library to support older SDK:s. minSDK is 8. Target is 17. (Changing the minSDK to 16 doesn't help)

Upvotes: 0

Views: 741

Answers (1)

general03
general03

Reputation: 863

If you use ContextThemeWrapper (or not), it may an issue of style in xml

ContextThemeWrapper l_contextThemeWrapper = new ContextThemeWrapper(this, R.style.myStyle);

Don't use parent style

<style name="myStyle">
...
</style>

Upvotes: 2

Related Questions