I am an amateur in android studio and kotlin. I am implementing a dashboard where I have frame layout as the bodylayout and is replaced by different fragments when each item on the bottomnavigationview is clicked. However, for one particular item I need to use two view, imageview and a scrollview. So, when the progress bar in the scrollview contains no progress, I have to show the Imageview hiding the scrollview and also disabling the scroll at that time. But When there is some progress in the progressbar in the scrollview, scrollview with it's content should be shown and not the imageview.
I have seen some answers int he same context but it didn't work for me. So, I would like to know how to do it? Am I doing it wrong.
The code snippet is below.
class ProgressFragment : Fragment() {
val TAG = "ProgressFragment"
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_progress, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
progress_progressbar2?.setProgress(5)
progress_progressbar2?.max=15
if (progress_progressbar2?.progress!!.equals(0)){
started_image?.bringToFront()
progress_scrollview?.invalidate()
}
else{
progress_scrollview?.bringToFront()
started_image?.invalidate()
navigation_header_container?.setImageResource(R.drawable.header_pink)
}
}
}
I am calling this fragment in the mainactivity and replacing the framelayout with this fragment in the mainactivity.
The activity_main.xml layout is given below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
tools:context=".MainActivity">
<ImageView
android:id="@+id/navigation_header_container"
android:layout_width="match_parent"
android:layout_height="65dp"
android:scaleY="1.5"
android:scaleX="2"
android:src="@drawable/header_green"
/>
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="75dp"
android:layout_above="@id/bottom_nav"
android:layout_alignParentStart="true"
android:layout_below="@+id/navigation_header_container"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:itemBackground="@color/colorWhite"
app:itemTextColor="@color/nav_item_colors"
app:menu="@menu/bottom_navigation">
</android.support.design.widget.BottomNavigationView>
<TextView
android:id="@+id/header_text"
android:layout_width="156dp"
android:layout_height="39dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:gravity="center"
android:textSize="20dp"
android:textColor="#200"
android:textStyle="bold"
android:text="TextView" />
<!--app:itemIconTint="@color/nav_item_colors"-->
<!--app:itemTextColor="@color/nav_item_colors"-->
</RelativeLayout>
The progress layout is given below.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProgressFragment">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:id="@+id/progress_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/progress_parentrelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/progress_child1relayout"
android:layout_width="190dp"
android:layout_height="280dp">
<TextView
android:id="@+id/quadrant1_textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60sp"
android:layout_marginTop="90dp"
android:gravity="center"
android:text="Min"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progress_progressbar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="140dp"
android:progressDrawable="@drawable/customprogressbar" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/progress_child2relayout"
android:layout_width="190dp"
android:layout_height="280dp"
android:layout_below="@+id/progress_child1relayout">
<TextView
android:id="@+id/quadrant2_textview1"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40sp"
android:layout_marginStart="40sp"
android:layout_marginTop="70dp"
android:text="Challenge Status"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progress_progressbar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="160dp"
android:progressDrawable="@drawable/customprogressbar" />
<TextView
android:id="@+id/quadrant2_textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="42dp"
android:layout_marginTop="130dp"
android:text="Completed"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/quadrant2_textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="190dp"
android:text="open"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/quadrant2_textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="230dp"
android:text="0"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/progress_child3relayout"
android:layout_width="190dp"
android:layout_height="280dp"
android:layout_toRightOf="@+id/progress_child1relayout">
<TextView
android:id="@+id/quadrant3_textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60sp"
android:layout_marginTop="90dp"
android:gravity="center"
android:text="TextView"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/progress_child4relayout"
android:layout_width="190dp"
android:layout_height="280dp"
android:layout_below="@id/progress_child3relayout"
android:layout_toRightOf="@+id/progress_child2relayout">
<TextView
android:id="@+id/quadrant4_textview1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="60sp"
android:layout_marginTop="70dp"
android:text="Pods Mastered"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/circularprogress"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="130dp"
android:progress="20" />
<!--<ProgressBar-->
<!--android:id="@+id/progress_progressbar3"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginTop="120dp"-->
<!--android:layout_marginLeft="50sp"-->
<!--android:indeterminateDrawable="@drawable/ringprogressbar"-->
<!--android:max="100"-->
<!--android:progress="20"-->
<!--style="?android:attr/progressBarStyleLarge" />-->
</RelativeLayout>
<TextView
android:id="@+id/whatdoesthis_mean"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="580dp"
android:clickable="true"
android:gravity="center"
android:text="What does this mean?"
android:textColor="#200"
android:textSize="15dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/child_button"
android:layout_width="344dp"
android:layout_height="79dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="620dp"
android:src="@drawable/pinkcolor"
android:text="child name" />
<TextView
android:id="@+id/child_name_text"
android:layout_width="184dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="11dp"
android:fontFamily="sans-serif"
android:gravity="center"
android:text="Child Name"
android:textColor="#190fdf"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
<ImageView
android:id="@+id/started_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/letsgetstarted"
android:background="#ebcac3c7"
/>
</FrameLayout>
Any help is appreciated.
Fragments don't create the view in OnCreate
unlike the Activity. The same can be understood through the life-cycle of a fragment. Moreover, this is one of the major differences between an activity life-cycle and a fragment life-cycle. So, instead of assigning the values in the OnCreate
, it should be done in OnViewCreated
.
The code snippet could be modified to:
class ProgressFragment : Fragment() {
val TAG = "ProgressFragment"
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_progress, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
progress_progressbar2?.setProgress(12)
progress_progressbar2?.max=15
val currentProgress = progress_progressbar2?.progress ?: 0
if (currentProgress == 0){
started_image?.visibility = View.VISIBLE
progress_scrollview?.visibility = View.GONE
} else {
started_image?.visibility = View.GONE
progress_scrollview?.visibility = View.VISIBLE
navigation_header_container?.setImageResource(R.drawable.header_pink)
}
}
}
The condition and static/dynamic progress assignment can be done in the OnResume
method also.
Thank you Vishnu.