Reputation: 13
I have made a countdown timer with Days, hours, minutes and seconds. It is working correctly when used as a Activity, but now I have to put this in a fragment and I cannot use SetContentView. How do I still make it work? Any help is appreciated. Thanks!
Here is my code:
public class FragmentOne extends Fragment {
private TextView tvDay, tvHour, tvMinute, tvSecond, tvEvent;
private LinearLayout linearLayout1, linearLayout2;
private Handler handler;
private Runnable runnable;
public static Fragment newInstance(Context context) {
FragmentOne f = new FragmentOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, container,false);
linearLayout1 = (LinearLayout) getActivity().findViewById(R.id.ll1);
linearLayout2 = (LinearLayout) getActivity().findViewById(R.id.ll2);
tvDay = (TextView) getActivity().findViewById(R.id.txtTimerDay);
tvHour = (TextView) getActivity().findViewById(R.id.txtTimerHour);
tvMinute = (TextView) getActivity().findViewById(R.id.txtTimerMinute);
tvSecond = (TextView) getActivity().findViewById(R.id.txtTimerSecond);
tvEvent = (TextView) getActivity().findViewById(R.id.tvevent);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
Date futureDate = dateFormat.parse("2016-2-19");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
tvDay.setText("" + String.format("%02d:", days));
tvHour.setText("" + String.format("%02d:", hours));
tvMinute.setText("" + String.format("%02d:", minutes));
tvSecond.setText("" + String.format("%02d", seconds));
} else {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
tvEvent.setText("Event Start");
handler.removeCallbacks(runnable);
handler.removeMessages(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 0);
return root;
}
}
And my XML for the same:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background" >
<LinearLayout
android:id="@+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="@+id/tvevent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:singleLine="true"
android:text="Android Event Start"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="#00000000"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="horizontal"
android:visibility="visible" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="50sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="50sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="50sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Upvotes: 0
Views: 158
Reputation: 2825
Replace getActivity() with this -
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, container,false);
linearLayout1 = (LinearLayout) root.findViewById(R.id.ll1);
......................
Upvotes: 0
Reputation: 132992
but now I have to put this in a fragment and I cannot use SetContentView
getActivity()
return Activity
Context and calling findViewById
using getActivity
means accessing View's from Activity layout instead of Fragment which is returned from onCreateView
method.
So to get it work use root
instead of getActivity()
to access View's from Fragment
Layout :
linearLayout1 = (LinearLayout) root.findViewById(R.id.ll1);
Do same for accessing other View's.
Upvotes: 1