I'm struggling with a custom DialogFragment layout defined as below:
<?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="360dp"
android:background="#color/colorDarkerGreen"
android:minWidth="280dp"
android:orientation="vertical">
<ImageView
android:id="#+id/ivNoRetails"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:scaleType="centerInside"
android:src="#drawable/no_retails_nearby_dialog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:text="#string/no_retails_nearby"
android:textAlignment="center"
android:textColor="#color/colorBaseWhite"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ivNoRetails" />
<TextView
android:id="#+id/tvHopeGetThere"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="#string/hope_we_get_there"
android:textAlignment="center"
android:textColor="#color/colorBaseWhite"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="#+id/textView2"
app:layout_constraintStart_toStartOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btClose"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:text="#string/understood"
android:textColor="#color/colorBaseWhite"
app:backgroundTint="#android:color/transparent"
app:cornerRadius="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvHopeGetThere"
app:strokeColor="#color/colorBaseWhite"
app:strokeWidth="2dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I the preview from the layout editor, I can see the text of tvHopeGetThere TextView displayed as bold, as expected.
However, when I show the dialog in my activity, the text is displayed as with a normal textStyle.
DialogFragment:
class NoRetailsNearbyDialogFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.no_retails_alert_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btClose.setOnClickListener {
dismiss()
}
}
companion object {
fun newInstance(): NoRetailsNearbyDialogFragment {
return NoRetailsNearbyDialogFragment()
}
}
}
Display of the dialog in my fragment:
val fragment = NoRetailsNearbyDialogFragment.newInstance()
fragment.show(fragmentManager, "")
I really can't understand why the textview has this strange behavior.
there is one method, you can set textview typeface by calling setTypeface method,
see the following
your can do it programatically as:
textView.setTypeface(null, Typeface.BOLD);
or you can do it by html approach as:
TextView t = new TextView(mContext);
t.setText(Html.fromHtml("<b>This is bold</b>"));
Problem was related to Calligraphy library that was overriding common textStyle behavior. Once I removed it, everything started to work fine :)
Related
I'm trying to vertically align my TextView to the centre using constraints within my MaterialCardView but it won't move at all for some reason. Does anyone know what's causing this problem to occur? Do constraints need to be used at all to achieve this? The affected component is the MaterialCardView with a white border. The word 'Information' + the drawable don't seem to be exactly in the vertical centre for some reason. It looks like there is a bit more space above the text view than below it for some strange reason (indicated by the orange line).
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myCardViewA"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/myConstraintLayoutA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageButton
android:id="#+id/myImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_chevron_down"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/myTextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toEndOf="#+id/myImageButton"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/myTextViewSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toEndOf="#+id/myImageButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/myTextViewTitle"
app:layout_constraintHorizontal_bias="0.0"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Button"
android:textAllCaps="false"
android:padding="12dp"
app:layout_constraintTop_toBottomOf="#+id/myTextViewSubtitle"
app:layout_constraintBottom_toTopOf="#+id/myCardViewB"
app:layout_constraintStart_toEndOf="#+id/myImageButton"
app:layout_constraintHorizontal_bias="0.0" />
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myCardViewB"
android:clickable="false"
android:focusable="false"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#android:color/transparent"
app:contentPadding="12dp"
app:cardElevation="0dp"
app:layout_constraintTop_toBottomOf="#+id/myButton"
app:layout_constraintStart_toEndOf="#+id/myImageButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/myTextViewDescription"
app:layout_constraintHorizontal_bias="0.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/myConstraintLayoutB"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/myTextViewinCardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.0"
android:text=""
style="#android:style/TextAppearance.Medium"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="#+id/myTextViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/myTextViewTitle"
app:layout_constraintTop_toBottomOf="#+id/myCardViewB"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
Kotlin
class MyFragment : androidx.fragment.app.Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.my_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
val imageGetter = Html.ImageGetter { name ->
val resId = when (name) {
"door" -> { R.drawable.ic_door }
else -> { throw IllegalArgumentException("what is $name") }
}
ResourcesCompat.getDrawable(resources, resId, requireActivity().theme)?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
}
val info = getString(R.string.information)
val htmlTxt = "<img src=\"door\"/> $info
val myTxt = Html.fromHtml(htmlTxt, Html.FROM_HTML_MODE_COMPACT, imageGetter, null)
super.onActivityCreated(savedInstanceState)
}
}
Just remove app:layout_constraintHorizontal_bias="0.0"
The maximum is app:layout_constraintHorizontal_bias="1.0"
When you set "0.0" that mean you want it to the left and when it is "1.0" that mean it is to the right.
I'm using multiple TextInputLayout in each element in a Recycling View.
I noticed that navigating to the fragment containing this list is slow and scrolling for the first few seconds is also slow. As soon as I remove the TextInputLayout the performance drastically improves.
Is there a way to render the TextInputLayout in a recycling view with a good performance?
I mostly want to use this layout for its animations and design.
This is my layout for a individual item in recycling view:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="16dp"
android:background="#FFFFFF"
android:elevation="8dp">
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_baseline_add_24" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Bench Press"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="28dp"
android:text="Remove Exercise"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
android:baselineAligned="false">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField1"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:hint="Reps">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField2"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:hint="Sets">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField3"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:hint="Rest">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment code:
class EditRoutineFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_edit_routine, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = EditRoutineListAdapter()
requireView().rv_list.apply {
this.adapter = adapter
layoutManager = LinearLayoutManager(context)
setHasFixedSize(true)
}
}
}
Recycling View Adapter
class EditRoutineListAdapter: RecyclerView.Adapter<EditRoutineListAdapter.ViewHolder>() {
class ViewHolder(itemView: ViewGroup): RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater
.from(parent.context)
.inflate(R.layout.rv_edit_routine_list_item, parent, false) as ViewGroup
return ViewHolder(view)
}
override fun getItemCount() = 16
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
}
}
You can leftout the ImageButton and use the built in option like:
app:endIconMode="custom"
app:endIconDrawable="#drawable/ic_check_circle_24dp"
app:endIconContentDescription="#string/content_description_end_icon"
Check also RecyclerView on Bind method taking too long(that is, onBindViewHolder(VH, int)) should be very simple, and take much less than one millisecond for all but the most complex items.
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've created a custom view called SectionRowView which extends a ConstraintLayout which is as simple as:
class SectionRowView : ConstraintLayout {
constructor(context: Context?) : super(context) {
inflate(context, R.layout.section_row_view, this)
}
}
SectionRowView inflates it's view from sectionrowview.xml:
<merge 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:id="#+id/container">
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:id="#+id/topBorder" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
android:background="#979797"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="32dp"
android:id="#+id/checkBox" android:layout_marginTop="15dp"
app:layout_constraintTop_toBottomOf="#+id/topBorder" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="30dp"
android:text="test test test test test test"/>
</merge>
In a fragment I then add this custom view to a TableRow inside a TableLayout:
class CustomizeQuizFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_customize_quiz, container, false)
val tableView = view.findViewById<TableLayout>(R.id.tableView)
setupTable(tableView)
return view
}
private fun setupTable(tableView: TableLayout) {
val layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)
val tableRow = TableRow(context)
tableRow.layoutParams = layoutParams
val sectionView = SectionRowView(context)
sectionView.layoutParams = layoutParams
tableRow.addView(sectionView)
tableView.addView(tableRow)
}
}
The xml layout for my fragment looks like this:
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/container">
<TextView
android:text="Test"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/title" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="70dp" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="32sp" android:gravity="center_horizontal|top" android:layout_marginStart="30dp"
android:layout_marginEnd="30dp" android:textColor="#3C6FF3"/>
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_marginTop="34dp"
app:layout_constraintTop_toBottomOf="#+id/title" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:id="#+id/tableView" android:background="#00EC0909"/>
<Button
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/startButton"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent" android:background="#drawable/customstartbutton"
android:textColor="#EFFBFD" android:textAllCaps="false" android:textSize="26sp"
android:layout_marginBottom="30dp" app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
When I run this, I can see in the layout inspector that the SectionRowView's width is not extending to match_parent like it's told to. It's parent element the TableRow has a fullscreen width like its supposed to:
Using SectionRowView inside of a LinearLayout works just fine with the width extending to full screen. It seems to only have trouble inside a TableRow.
What am I doing wrong?
A Button in Fragment A is given an onClickListener to go to Fragment B in Fragment A's onStart:
override fun onStart() {
enterMemberIDButton = view!!.findViewById(R.id.enter_member_id_button)
enterPlateButton = view!!.findViewById(R.id.enter_plate_button)
enterMemberIDButton.setOnClickListener {
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.main_view_pager, FragmentB.newInstance())?.addToBackStack(null)?.commit()
}
}
However, the button click loads Fragment C, as confirmed by a debug line I placed in Fragment C's onStart.
Here is Fragment A's xml:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:id="#+id/welcome_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="#+id/button_holder"
android:layout_marginBottom="64dp" android:layout_marginTop="64dp"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:text="#string/welcome_member"
android:textColor="#color/header_brown"
android:textSize="36sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/welcome_text" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toTopOf="#id/please_select_text"/>
<TextView
android:text="#string/please_enter_member_id"
android:textSize="32sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/please_select_text" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/welcome_container"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="parent" app:layout_constraintVertical_bias="0.2"/>
</android.support.constraint.ConstraintLayout>
<LinearLayout
android:id="#+id/button_holder"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:weightSum="3" android:layout_marginTop="128dp"
app:layout_constraintTop_toBottomOf="#+id/welcome_container" android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp">
<Button
android:text="#string/enter_member_id"
android:tag="15"
android:background="#drawable/default_button_bg"
android:textStyle="bold"
android:textSize="36sp"
android:padding="15dp"
android:textAllCaps="false"
android:textColor="#color/title_gray"
android:layout_margin="16dp"
android:layout_width="0dp"
android:layout_height="200dp" android:id="#+id/enter_member_id_button" android:layout_weight="1"/>
<Button
android:text="#string/enter_plate"
android:tag="14"
android:background="#drawable/default_button_bg"
android:textStyle="bold"
android:textSize="36sp"
android:padding="15dp"
android:textAllCaps="false"
android:layout_margin="16dp"
android:textColor="#color/title_gray"
android:layout_width="0dp"
android:layout_height="200dp" android:id="#+id/enter_plate_button" android:layout_weight="1"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
And FragmentB (MemberIDFragment):
class MemberIDFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_member_id, container, false)
}
override fun onStart() {
super.onStart()
val editText: TextInputEditText = view!!.findViewById(R.id.member_edit_text)
val keyboard: SoftKeyboard = view!!.findViewById(R.id.enter_id_keyboard)
println("ID Fragment34")
keyboard.init(context!!)
val inputConnection = editText.onCreateInputConnection(EditorInfo())
keyboard.inputConnection = inputConnection
}
companion object {
#JvmStatic
fun newInstance() =
HaveCodeFragment().apply {
}
}
}
Is there anything else that could be rerouting this to the wrong fragment?
The MemberIDFragment is returning an instance of the HaveCodeFragment().
Should change the newInstance() method of the MemberIDFragment to:
companion object {
#JvmStatic
fun newInstance() = MemberIDFragment()
}