First, I will show you the BottomSheetDialogFragment class.
class BottomSheetReportFragment(var mContext: Context, var postId: String, var commentId: String?, var replyId: String?) : BottomSheetDialogFragment() {
private lateinit var mView: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppBottomSheetDialogTheme)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mView = inflater.inflate(R.layout.bottom_sheet_report, container, false)
return mView
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener { dialogInterface ->
val bottomSheetDialog = dialogInterface as BottomSheetDialog
setupRatio(bottomSheetDialog)
}
return dialog
}
private fun setupRatio(bottomSheetDialog: BottomSheetDialog) {
val bottomSheet = bottomSheetDialog.findViewById<FrameLayout>(R.id.design_bottom_sheet)
val behavior = BottomSheetBehavior.from(bottomSheet!!)
val layoutParam = bottomSheet.layoutParams as ViewGroup.LayoutParams
layoutParam.height = getBottomSheetDialogDefaultHeight()
bottomSheet.layoutParams = layoutParam
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
behavior.peekHeight = getBottomSheetDialogDefaultHeight()
}
private fun getBottomSheetDialogDefaultHeight(): Int {
return getWindowHeight() * 85 / 100
}
private fun getWindowHeight(): Int {
val displayMetrics = mContext.resources.displayMetrics
return displayMetrics.heightPixels
}
}
xml is like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
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:orientation="vertical"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<androidx.core.widget.NestedScrollView
android:id="#+id/nested"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:overScrollMode="never">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="10dp"
android:background="#drawable/selector_button_report"
android:button="#null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="1"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="#+id/second"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="10dp"
android:background="#drawable/selector_button_report"
android:button="#null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="2"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="#+id/third"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="10dp"
android:background="#drawable/selector_button_report"
android:button="#null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="3"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="#+id/fourth"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="10dp"
android:background="#drawable/selector_button_report"
android:button="#null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="4"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="#+id/fifth"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="10dp"
android:background="#drawable/selector_button_report"
android:button="#null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="5"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="#+id/sixth"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="10dp"
android:background="#drawable/selector_button_report"
android:button="#null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="6"
android:textColor="#1C1C1C"
android:textSize="16dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
<RelativeLayout
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="91dp"
android:visibility="gone"
tools:visibility="visible">
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/shape_edittext_comment"
android:includeFontPadding="false"
android:inputType="textMultiLine"
android:lines="2"
android:paddingStart="16dp"
android:paddingEnd="73dp"
android:textColor="#000000"
android:textColorHint="#ADB1BA"
android:textSize="14dp" />
<ImageView
android:id="#+id/send"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_alignTop="#id/input"
android:layout_alignBottom="#id/input"
android:layout_alignParentEnd="true"
android:layout_marginTop="4dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="4dp"
android:backgroundTint="#CBCBCB"
android:src="#android:drawable/ic_menu_send" />
</RelativeLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
The Android keyboard is implemented so that any button from Button id first to sixth is pressed. But the problem here is that when the keyboard is up, the view focus is set on the EditText, but the EditText is obscured by the keyboard. Is there a way for BottomSheetDialogFragment to resize the view like Activity?
[UPDATE]
For reference, the style is applied as follows.
adjustResize
stateAlwaysVisible
adjustResize, stateAlwaysVisible
I tried all three situations, but it didn't work.
<style name="AppBottomSheetDialogTheme" parent="Theme.Design.BottomSheetDialog">
<item name="bottomSheetStyle">#style/AppModalStyle</item>
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize|stateAlwaysVisible</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">#drawable/shape_bottom_sheet</item>
</style>
In your AppBottomSheetDialogTheme, add android:windowSoftInputMode.
<style name="AppBottomSheetDialogTheme">
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
Related
I am Trying to Achieve the Exposed Drop-Down Menu in Android I have tried this one but can't identify where I went wrong.
DropDown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/textViewFeelings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="TextView"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</androidx.appcompat.widget.LinearLayoutCompat>
BottomSheetDialogConnectWifiFragment.kt
class BottomSheetDialogConnectWifiFragment : BottomSheetDialogFragment() {
private lateinit var _binding: FragmentBottomSheetDialogConnectWifiBinding
private val items = listOf("Material", "Design", "Components", "Android")
private lateinit var ActivityContext: Context
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onAttach(context: Context) {
ActivityContext = context
super.onAttach(context)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentBottomSheetDialogConnectWifiBinding.inflate(inflater, container, false)
return _binding.root
//
// val arrayAdapter = ArrayAdapter(_binding.root.context, R.layout.dropdown_item, items)
// _binding.autoCompleteTextFieldSsidType.setAdapter(arrayAdapter)
_binding.autoCompleteTextFieldSsidType.apply {
setAdapter(
ArrayAdapter(
ActivityContext,
R.layout.dropdown_item,
items
)
)
}
}
companion object {
const val TAG = "CustomBottomSheetDialogFragment"
}
}
fragment_bottom_sheet_dialog_connect_wifi
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
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:padding="10dp"
tools:context=".Fragments.BottomSheetDialogConnectWifiFragment">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="2dp"
android:layout_centerHorizontal="true"
android:elevation="10dp"
android:foregroundGravity="center_vertical"
app:cardBackgroundColor="#color/black"
app:cardCornerRadius="100dp">
<!--YOUR CONTENT-->
</androidx.cardview.widget.CardView>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/idTVCourseTracks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Add Network"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="15sp" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilSsid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/idTVCourseTracks"
android:keyboardNavigationCluster="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter The SSID" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textFieldSsidType"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tilSsid"
android:layout_marginTop="12dp">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextFieldSsidType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter The SSID"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textFieldPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textFieldSsidType"
android:layout_marginTop="12dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/idBtnDismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textFieldPassword"
android:layout_marginTop="12dp"
android:text="Connect"
android:textAllCaps="false"
android:theme="#style/ShapeAppearanceOverlay.Material3.Button" />
</RelativeLayout>
</ScrollView>
</com.google.android.material.card.MaterialCardView>
I Created An bottomSheet in Android/kotlin and trying to solve this cant understood where i missing some thing to
i have try with .Apply{}Method And also with the normally but both of them not worked for me
Context:
I am making a Tic Tac Toe game where you press a button and then X or O appears on the button pressed. I have tried making it with the use of fragments since I want to have a navigation bar and, further down the line, I want to have a settings fragment to change stuff.
Problem:
I am getting java.lang.IllegalStateException: Could not find method boardTapped(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'a1'
What I've done:
fragment_game.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.bondesjakk.GameFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey_dark_1">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bondesjakk!"
android:textColor="#64B5F6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.059" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<RelativeLayout
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/a1"
style="#style/button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/a2"
style="#style/button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/a3"
style="#style/button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/b1"
style="#style/button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/b2"
style="#style/button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/b3"
style="#style/button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/c1"
style="#style/button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/c2"
style="#style/button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="#color/grey_dark_1">
<Button
android:id="#+id/c3"
style="#style/button" />
</RelativeLayout>
<androidx.constraintlayout.helper.widget.Flow
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:constraint_referenced_ids="button1,button2,button3,button4,button5,button6,button7,
button8,button9"
app:flow_horizontalGap="1dp"
app:flow_maxElementsWrap="3"
app:flow_verticalGap="1dp"
app:flow_wrapMode="chain"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="square">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">100dp</item>
<item name="android:background">#color/grey_dark_1</item>
</style>
<style name="button">
<item name="android:onClick">boardTapped</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textColor">#color/white</item>
<item name="android:textSize">40sp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:layout_weight">1</item>
</style>
</resources>
GameFragment.kt
class GameFragment : Fragment() {
enum class Turn {
Nought, Cross
}
private var firstTurn = Turn.Cross
private var currentTurn = Turn.Cross
private var boardList = mutableListOf<Button>()
private lateinit var binding: FragmentGameBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = DataBindingUtil.inflate<FragmentGameBinding>(inflater,
R.layout.fragment_game, container, false)
boardList.add(binding.a1)
boardList.add(binding.a2)
boardList.add(binding.a3)
boardList.add(binding.b1)
boardList.add(binding.b2)
boardList.add(binding.b3)
boardList.add(binding.c1)
boardList.add(binding.c2)
boardList.add(binding.c3)
return binding.root
}
fun boardTapped(view: View) {
if(view !is Button)
return
addToBoard(view)
}
private fun addToBoard(button: Button) {
if (button.text != "")
return
if(currentTurn == Turn.Nought) {
button.text = NOUGHT
currentTurn = Turn.Cross
}
else if (currentTurn == Turn.Cross) {
button.text = CROSS
currentTurn = Turn.Nought
}
// }
//private fun setTurnLabel() {
// var turnText = ""
//if (currentTurn == Turn.Cross)
// turnText = "Turn $CROSS"
//else if (currentTurn == Turn.Nought)
// turnText = "Turn $NOUGHT"
}
companion object{
const val NOUGHT = "O"
const val CROSS = "X"
}
}
I have a fragment in which I have a spinner and checkbox.
The layout file need_help_fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/all_fragment_background"
android:clickable="true"
android:focusable="true">
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/parent_top"
app:layout_constraintGuide_percent="0"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/parent_bottom"
app:layout_constraintGuide_percent="1"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/parent_left"
android:orientation="vertical"
app:layout_constraintGuide_percent="0"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/parent_right"
android:orientation="vertical"
app:layout_constraintGuide_percent="1"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/top_title_bottom"
app:layout_constraintGuide_percent=".25"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/top_title_bottom"
app:layout_constraintEnd_toStartOf="#+id/parent_right"
app:layout_constraintStart_toEndOf="#+id/parent_left"
app:layout_constraintTop_toBottomOf="#+id/parent_top">
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/text_view_top"
app:layout_constraintGuide_percent="0"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/text_view_bottom"
app:layout_constraintGuide_percent="1"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/text_view_left"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.07"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/text_view_right"
android:orientation="vertical"
app:layout_constraintGuide_percent=".93"/>
<TextView
android:text="#string/need_help_header_text"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="#+id/title_header_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/text_view_top"
app:layout_constraintBottom_toTopOf="#+id/text_view_bottom"
app:layout_constraintStart_toEndOf="#+id/text_view_left"
app:layout_constraintEnd_toStartOf="#+id/text_view_right"
android:textColor="#color/all_text_color"
android:textSize="18sp"
android:gravity="center">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/mid_content_bottom_guide"
app:layout_constraintGuide_percent=".8"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/top_title_bottom"
app:layout_constraintBottom_toTopOf="#+id/mid_content_bottom_guide"
app:layout_constraintStart_toEndOf="#+id/parent_left"
app:layout_constraintEnd_toStartOf="#+id/parent_right">
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/mid_content_top"
app:layout_constraintGuide_percent="0"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/mid_content_bottom"
app:layout_constraintGuide_percent="1"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/mid_content_left"
android:orientation="vertical"
app:layout_constraintGuide_percent=".07"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/mid_content_right"
android:orientation="vertical"
app:layout_constraintGuide_percent=".93"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/spinner_bottom"
app:layout_constraintGuide_percent=".2"
android:orientation="horizontal"/>
<androidx.appcompat.widget.AppCompatSpinner
android:background="#null"
app:layout_constraintTop_toBottomOf="#+id/mid_content_top"
app:layout_constraintBottom_toTopOf="#+id/spinner_bottom"
app:layout_constraintStart_toEndOf="#+id/mid_content_left"
app:layout_constraintEnd_toStartOf="#+id/mid_content_right"
android:id="#+id/spinner_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center_horizontal | bottom"
android:text="3 Seconds"
android:textColor="#color/all_text_color"
android:textSize="22sp" />
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/checkbox_top"
app:layout_constraintGuide_percent=".4"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/checkbox_bottom"
app:layout_constraintGuide_percent=".6"
android:orientation="horizontal"/>
<CheckBox
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Remove timing, Auto Mode"
app:layout_constraintTop_toBottomOf="#+id/checkbox_top"
app:layout_constraintBottom_toTopOf="#+id/checkbox_bottom"
app:layout_constraintStart_toEndOf="#+id/mid_content_left"
app:layout_constraintEnd_toStartOf="#+id/mid_content_right"
android:layoutDirection="rtl"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textSize="18sp"
android:textColor="#color/all_text_color"
android:id="#+id/remove_timing_auto_mode_check_box"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Code in fragment:
class NeedHelpFragment: Fragment(), View.OnClickListener {
lateinit var homeActivity : MainActivity
var check_box: CheckBox? = null
var spinner_view: Spinner? = null
lateinit var timeSpinnerList: ArrayList<String>
lateinit var timeSpinnerAdapter: ArrayAdapter<String>
override fun onAttach(context: Context) {
super.onAttach(context)
if(context is MainActivity)
homeActivity = context
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var v : View = inflater.inflate(R.layout.need_help_fragment_layout, container, false)
check_box = v.findViewById<CheckBox>(R.id.check_box)
timeSpinnerList = ArrayList()
for(i in 0..10)
timeSpinnerList.add("$i seconds")
timeSpinnerAdapter = ArrayAdapter(homeActivity, android.R.layout.simple_spinner_dropdown_item, timeSpinnerList)
spinner_view = v.findViewById<Spinner>(R.id.spinner_view)
spinner_view!!.adapter = timeSpinnerAdapter
return v
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onClick(view: View?) {
when(view!!.id){
}
}
}
The color code:
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="all_text_color">#6F6F6F</color>
<color name="all_fragment_background">#F8F8F8</color>
When anything is not selected it is fine:
When an item is selected from the spinner, the checkbox disappears:
There is nothing else in the layout file. Just the constraint layout and the guidelines for the checkbox and spinner. The spinner and the checkbox are initialized in the onCreateView of the fragment. The checkbox disappears when I select any item from the spinner. Please help.
I think the problem is about your setting value about constraintLayout options like :
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner_view"
app:layout_constraintBottom_toBottomOf="parent"
I think that was wrong in some cases.
Then for simple example i removed guidelines and i solved it.
You can use this approach and add your guidelines.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/all_fragment_background"
android:clickable="true"
android:focusable="true">
<TextView
android:id="#+id/title_header_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="HEAD"
android:textColor="#color/all_text_color"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatSpinner
android:id="#+id/spinner_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="#null"
android:gravity="center_horizontal|bottom"
android:text="3 Seconds"
android:textColor="#color/all_text_color"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#+id/remove_timing_auto_mode_check_box"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_header_text_view" />
<CheckBox
android:id="#+id/remove_timing_auto_mode_check_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layoutDirection="rtl"
android:paddingLeft="10dp"
android:text="Remove timing, Auto Mode"
android:textColor="#color/all_text_color"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
Currently I am finishing the last details of my project and I am trying to keep a certain part inside a nested scrollview to stay on top of the screen and let only the recyclerview scroll.
This is the code I have right now:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/marktplaats_refresh"
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:tint="#9F000000"
app:layout_constraintCircleRadius="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/offer_bg"/>
<Button
android:id="#+id/verkoopEenProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:background="#null"
android:text="Verkoop een product"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:background="#drawable/light_contact_buttons"
android:hint="Zoek in marktplaats"
android:textAlignment="center"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView33"/>
<TextView
android:id="#+id/textView32"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="Marktplaats"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/verkoopEenProduct"/>
<TextView
android:id="#+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Hier vind u alles wat u nodig\nhebt voor uw praktijk"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView32"/>
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Alle producten"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView14"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/marktplaats_recyclerview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView34"/>
<Button
android:id="#+id/button11"
android:translationX="-20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#null"
android:rotation="180"
android:text="➜"
android:textColor="#color/white"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
And this is my kotlin
class MarktplaatsOverview : ApplicationFragment(){
var products: Array<MarketModel> = arrayOf()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_marktplaats, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
marktplaats_refresh.setOnRefreshListener {
refreshData()
marktplaats_refresh.isRefreshing = false
}
button11.setOnClickListener {
navController.navigate(R.id.action_marktplaatsOverview_to_more)
}
button5.requestFocus()
refreshData()
verkoopEenProduct.setOnClickListener {
navController.navigate(R.id.action_marktplaatsOverview_to_marktplaatsDetail)
}
}
private fun refreshData() {
if (DataStorage.market.isNotEmpty()) {
setupRecyclerView()
} else {
SharedInstance.api.getAllMarketItems {
if (isAdded) {
setupRecyclerView()
}
}
}
}
private fun setupRecyclerView(){
SharedInstance.api.getAllMarketItems {
products = DataStorage.market
val xLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
xLayoutManager.stackFromEnd = true
marktplaats_recyclerview.layoutManager = xLayoutManager
marktplaats_recyclerview.adapter = MarktplaatsAdapter(products)
}
}
}
Does anyone have a solution for this awkward problem? I've been trying to do some restructuring of the swiperefreshlayout, scrollviews and constraints but no succes so far.
I also can't find anything on the internet where someone wants the same as me...
As per your question description, I am assuming that you face an issue that recycler view scroll or take focus automatically while you open screen and you need to stick layout at the top so pass this field on your parent layout.
android:descendantFocusability="blocksDescendants"
or replace your SwipeRefreshLayout
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/marktplaats_refresh"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent">
I hope it helps you.
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.
val currentProgress = progress_progressbar2?.progress ?: 0
if (currentProgress == 0){
started_image?.visibility = VISIBLE
progress_scrollview?.visibilty = GONE
} else {
started_image?.visibility = GONE
progress_scrollview?.visibilty = VISIBLE
navigation_header_container?.setImageResource(R.drawable.header_pink)
}
Instead of bringToFront you manage it using the visibility of the views.
Android Documentation : https://developer.android.com/reference/android/view/View.html#setVisibility(int)
Try this out