Button and imageview do not work in a fragment - android

Ok I have a problem, when the button is pressed it is supposed to take a random image from the link and display it in imageView and in my main_activity the code works, then I pass it to the snippet to use in the navigation drawer and it does not It works, I press the button and it does nothing
My code
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_home, container, false)
var bnextButton = view.findViewById(R.id.nextButton) as Button
var memeRandom = view.findViewById(R.id.memeRandomView) as ImageView
bnextButton.setOnClickListener { view ->
// val rnds = (0..99999).random()
val quest1 = "https://proxxcraft.com/"
// var randomGetNumber = quest1 + rnds
Picasso.get().load(quest1).memoryPolicy(MemoryPolicy.NO_CACHE).into(memeRandom)
Picasso.get().isLoggingEnabled = true
}
return view
}
My layaout
<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="match_parent"
tools:context=".ProfileActivity">
<ImageView
android:id="#+id/memeRandomView"
android:layout_width="385dp"
android:layout_height="452dp"
android:layout_marginStart="32dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="32dp"
android:src="#drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.578"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Next"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/memeRandomView"
app:layout_constraintVertical_bias="0.434" />
</androidx.constraintlayout.widget.ConstraintLayout>

Related

How to make Scrollview automatically scroll to the bottom when RadioButton is clicked

How to make Scrollview automatically scroll to the bottom when RadioButton is clicked
May I know How to make the Scroll view automatically scroll down to the next_button when any of the Radio Button is clicked ,
in the same time I need to call "setMeal() method from the ViewModel when the Radio Button is clicked.
<?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=".ui.MainMeal">
<data>
<variable
name="viewModel"
type="com.example.mymeal.model.OrderViewModel" />
<variable
name="mainMeal"
type="com.example.mymeal.ui.MainMeal" />
</data>
<ScrollView
android:layout_width="match_parent"
android:id="#+id/scrollView"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp">
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="#+id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioGroup
android:id="#+id/main_meal_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="#+id/royal_lobster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='#{viewModel.menuItems["royal butter lobster"].name}'
android:onClick='#{() -> viewModel.setMeal("royal butter lobster")}'
android:textAppearance="?attr/textAppearanceBody1"
tools:text="Royal Butter Lobster" />
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/royal_lobster_image"
android:layout_width="match_parent"
android:layout_height="194dp"
android:layout_margin="8dp"
app:shapeAppearanceOverlay="#style/cut_corner"
app:srcCompat="#drawable/royal_butter_lobster" />
<TextView
android:id="#+id/royal_lobster_description"
style="#style/Widget.MenuItem.TextView.Info"
android:text='#{viewModel.menuItems["royal butter lobster"].description}'
tools:text="Entree 3 description" />
<TextView
android:id="#+id/royal_lobster_price"
style="#style/Widget.MenuItem.TextView.Info"
android:paddingTop="8dp"
android:text='#{viewModel.menuItems["royal butter
lobster"].getFormattedPrice()}'
tools:text="$0.00" />
<View
style="#style/Widget.MyMeal.Divider"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/royal_lobster_description" />
<RadioButton
android:id="#+id/kebbeh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='#{viewModel.menuItems["kebbeh"].name}'
android:onClick='#{() -> viewModel.setMeal("kebbeh")}'
android:textAppearance="?attr/textAppearanceBody1"
tools:text="Kebbeh" />
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/kebbeh_image"
android:layout_width="match_parent"
android:layout_height="194dp"
android:layout_margin="8dp"
app:shapeAppearanceOverlay="#style/cut_corner"
app:srcCompat="#drawable/kebbeh" />
<TextView
android:id="#+id/kebbeh_description"
style="#style/Widget.MenuItem.TextView.Info"
android:text='#{viewModel.menuItems["kebbeh"].description}'
tools:text="Entree 4 description" />
<TextView
android:id="#+id/kebbeh_price"
style="#style/Widget.MenuItem.TextView.Info"
android:paddingTop="8dp"
android:text='#{viewModel.menuItems["kebbeh"].getFormattedPrice()}'
tools:text="$0.00" />
<View
style="#style/Widget.MyMeal.Divider"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/kebbeh_description" />
<RadioButton
android:id="#+id/shish_tawook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='#{viewModel.menuItems["shish tawook"].name}'
android:onClick='#{() -> viewModel.setMeal("shish tawook")}'
android:textAppearance="?attr/textAppearanceBody1"
tools:text="Shish Tawook" />
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/shish_tawook_image"
android:layout_width="match_parent"
android:layout_height="194dp"
android:layout_margin="8dp"
app:shapeAppearanceOverlay="#style/cut_corner"
app:srcCompat="#drawable/shish_tawook" />
<TextView
android:id="#+id/shish_tawook_description"
style="#style/Widget.MenuItem.TextView.Info"
android:text='#{viewModel.menuItems["shish tawook"].description}'
tools:text="Entree 4 description" />
<TextView
android:id="#+id/shish_tawook_price"
style="#style/Widget.MenuItem.TextView.Info"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text='#{viewModel.menuItems["shish tawook"].getFormattedPrice()}'
tools:text="$0.00" />
</RadioGroup>
</com.google.android.material.card.MaterialCardView>
<View
android:id="#+id/divider"
style="#style/Widget.MyMeal.Divider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cardView" />
<TextView
android:id="#+id/subtotal"
style="#style/Widget.MyMeal.TextView.Subtotal"
android:text='#{#string/subtotal(viewModel.subtotal)}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/divider"
tools:text="Subtotal $5.00" />
<Button
android:id="#+id/next_button"
style="#style/Widget.Order.Button.Next"
android:onClick="#{()-> mainMeal.goToNextFragment()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/cancel_button"
app:layout_constraintTop_toBottomOf="#id/subtotal" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
This is the fragment class file
package com.example.mymeal.ui
class MainMeal : Fragment() {
// Binding object instance corresponding to the fragment_main_meal.xml layout.
private var _binding: FragmentMainMealBinding? = null
private val binding get() = _binding!!
//Get instance of OrderViewModel
private val sharedViewModel: OrderViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentMainMealBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
binding.viewModel = sharedViewModel
binding.mainMeal = this
}
//Navigate to the salad menu fragment
fun goToNextFragment() {
findNavController().navigate(R.id.action_mainMeal_to_saladMenu)
}
//Cancel the order
private fun cancelOrder() {
//Reset the values
sharedViewModel.resetOrder()
//Navigate back to the start fragment
findNavController().navigate(R.id.action_mainMeal_to_startFragment)
}
private fun doNothing() {
return
}
//confirmation before cancel the order
fun showDialog() {
MaterialAlertDialogBuilder(requireContext()).setMessage(getString(R.string.cancel_order))
.setCancelable(true)
.setNegativeButton(getString(R.string.yes)) { _, _ -> cancelOrder() }
.setPositiveButton(getString(R.string.no)) { _, _ -> doNothing() }.show()
}
/**
Clear out the binding object when the view hierarchy associated with the fragment
is being removed
*/
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Use the below code when the radio button is checked
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
Try this:
fun scrollToViewBottom(scrollView: ScrollView, childView: View) {
val delay: Long = 500 //delay to let finish with possible modifications to ScrollView
scrollView.postDelayed({ scrollView.smoothScrollTo(0, childView.bottom) }, delay)
}
Call the function:
radioButton.setOnClickListener {
if (radioButton.isChecked) {
println("checked")
scrollToViewBottom(ScrollViewExplore, Mypage)
}
}

How to fill my recyclerview with adapterdata in my fragment piece?

So I've been searching on internet for a while already now but I just can't seem to find the correct topic to help me out with this.
I have the following code which is relevant for this question:
This is my adapter class.
class SmoelenBoekAdapter(var profiles: Array<Profile>) : RecyclerView.Adapter<CustomViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val layout = layoutInflater.inflate(com.otten.nvvofrankversie.R.layout.recyclerview_smoelenboek, parent, false)
return CustomViewHolder(layout)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val positioner = profiles.get(position)
var profileImage = positioner.profile_image
var firstName = positioner.first_name
var lastName = positioner.last_name
var plaats = positioner.place
var adres = positioner.address
holder.view.naamSmoelenBoek.text = firstName.string + " " + lastName.string
}
override fun getItemCount(): Int {
return profiles.size
}
}
class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view)
This is my 'MainActivity' (for this particular fragment)
class SmoelenBoek : ApplicationFragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_smoelenboek, null)
}
/*override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
}*/
}
xml where adapterinfo has to be filled in:
<?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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/profielPicSmoelenBoek"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/business_icon" />
<TextView
android:id="#+id/naamSmoelenBoek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Frank Otten"
android:textColor="#color/colorPrimaryDark"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/profielPicSmoelenBoek"
app:layout_constraintStart_toEndOf="#+id/profielPicSmoelenBoek"
app:layout_constraintTop_toBottomOf="#+id/profielPicSmoelenBoek" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:text=">"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/naamSmoelenBoek" />
</androidx.constraintlayout.widget.ConstraintLayout>
And this is the fragment where I have a recyclerview in which needs to be filled with the above xml:
<?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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView8"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/offer_bg"
android:adjustViewBounds="true"
android:scaleType="center"/>
<Button
android:id="#+id/searchInSmoelenBoek"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="15dp"
android:background="#color/lightGray"
android:hint="Zoek in smoelenboek"
android:padding="10dp"
android:textAlignment="textStart"
android:textColor="#color/colorAccent"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="60dp"
android:text="Vind gemakkelijk alle\naangesloten orthodontisten"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/searchInSmoelenBoek" />
<TextView
android:id="#+id/textView15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:text="Aangesloten orthodontisten"
android:textAlignment="textStart"
android:textColor="#color/browser_actions_title_color"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView8" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/smoelenboekRecyclerView"
android:layout_width="409dp"
android:layout_height="440dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView15"/>
</androidx.constraintlayout.widget.ConstraintLayout>
What am I doing wrong or missing here?
I think I still need to set the adapter correctly, but I can't figure that out either...
Hope anyone can help me!
// Set layout manager for recycler view
smoelenboekRecyclerView.layoutManager = LinearLayoutManager(this);
// Set adapter to recycler view
smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view){
val tvNaamSmolText = view.naamSmoelenBoek
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val positioner = profiles.get(position)
var profileImage = positioner.profile_image
var firstName = positioner.first_name
var lastName = positioner.last_name
var plaats = positioner.place
var adres = positioner.address
holder.tvNaamSmolText.text = firstName.string + " " + lastName.string
}
Try this
class SmoelenBoek : ApplicationFragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
View view = inflater.inflate(R.layout.fragment_smoelenboek, null)
view.smoelenboekRecyclerView.layoutManager = LinearLayoutManager(this);
view.smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
return view
}
}
}
class SmoelenBoek : ApplicationFragment(){
var profiles: Array<Profile> = arrayOf()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_smoelenboek, null)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
SharedInstance.api.getAllUsers {
profiles = it
smoelenboekRecyclerView.layoutManager = LinearLayoutManager(context)
smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
}
}
}
This is the solution to my problem.
I had to put the layoutmanager and adapter in OnviewCreated AFTER the async call (which I forgot to implement so that was part of the problem first) so the layoutmanager could be set and the adapter could fill my recyclerview with items.
Thanks for the help anyways guys!

LinearLayout Add multiple items

I faced a strange problem : I can't add more than one object dynamically on a LinearLayout , if they were already things , that works, So That can display multiple views
I tried with different views , there is only problems when i try to push one after the other
(there is two buttons in index 0 and 1 to test for multiple objects on the layout)
CODE :
val view = layoutInflater.inflate(R.layout.sensor_item, container, false)
val view2 = layoutInflater.inflate(R.layout.nosensor_item, container, false)
val insertPoint = viewOfLayout.findViewById(R.id.box_Parent) as LinearLayout
insertPoint.addView(view, 2)
//insertPoint.addView(view2, 3) //works
This Works too :
//insertPoint.addView(view, 3)
insertPoint.addView(view2, 2) //works
This doesn't works :
insertPoint.addView(view, 2)
insertPoint.addView(view2, 3) //doesn't works
Full Code (fragments.kt):
package com.corrupted.radheat.TEMPER
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import kotlinx.android.synthetic.main.sensor_item.view.*
class EditFragment : Fragment() {
lateinit var option : Spinner
lateinit var result : TextView
private lateinit var viewOfLayout: View
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewOfLayout = inflater.inflate(R.layout.edit_fragment, container, false)
//create a view to inflate the layout_item (the xml with the textView created before)
val view = layoutInflater.inflate(R.layout.sensor_item, container, false)
val view2 = layoutInflater.inflate(R.layout.nosensor_item, container, false)
val insertPoint = viewOfLayout.findViewById(R.id.box_Parent) as LinearLayout
insertPoint.addView(view, 2)
insertPoint.addView(view2, 3)
option = view.spinner as Spinner
result = view.textView7 as TextView
val options = arrayOf("A","V")
option.adapter = ArrayAdapter<String>(activity,android.R.layout.simple_list_item_1,options)
option.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
result.text = "0"
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
result.text = options[position]
}
}
return viewOfLayout
}
class InfoFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.info_fragment, container, false)
}
}
class ParamsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.parameters_fragment, container, false)
}
}
Code of the fragment :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/box_Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Code of the added view :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:fillViewport="true"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="12dp"
android:background="#color/background_Item_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:text=".0 C°"
android:textColor="#color/Text_Color_Light"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/textView8"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toTopOf="#+id/textView8"
app:layout_constraintVertical_bias="0.625" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginStart="4dp"
android:text=".0 C°"
android:textColor="#color/Text_Color_Light"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/textView5"
app:layout_constraintStart_toEndOf="#+id/textView5"
app:layout_constraintTop_toTopOf="#+id/textView5"
app:layout_constraintVertical_bias="0.79" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="00"
android:textColor="#color/Text_Color_Light"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageButton"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.937" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:text="#string/ROOMtext"
android:textColor="#color/Text_Color_Light"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/spinner"
android:layout_width="45dp"
android:layout_height="43dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="8dp"
android:background="#color/UI_ITEM_OVERLAP_LIGHT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="40dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_keyboard_arrow_up_black_24dp" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="40dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageButton"
app:srcCompat="#drawable/ic_keyboard_arrow_down_black_24dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="00"
android:textColor="#color/Text_Color_Light"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.652" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.kt :
package com.corrupted.radheat.TEMPER
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.widget.*
class MainActivity : AppCompatActivity() {
private val fragmentManager = supportFragmentManager
private val fragmentTransaction = fragmentManager.beginTransaction()
private val editfragment = EditFragment()
private val infofragment = InfoFragment()
private val parametersfragment = ParamsFragment()
private fun showFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit()
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.action_edit -> {
showFragment(editfragment)
return#OnNavigationItemSelectedListener true
}
R.id.action_info-> {
showFragment(infofragment)
return#OnNavigationItemSelectedListener true
}
R.id.action_Settings-> {
showFragment(parametersfragment)
return#OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
fragmentTransaction.add(R.id.fragment_container, infofragment)
fragmentTransaction.commit()
setContentView(R.layout.activity_main)
val navigation = findViewById<BottomNavigationView>(R.id.activity_main_bottom_navigation)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity.
}
}
Pic of it working : https://imgur.com/gallery/mLb4G5b
Thanks

BaseAdapter doesn't work in Fragment via PagerView

so I'm trying to make a PagerView via Fragments containing a multiple BaseAdapters in them, but I ran into a little problem with BaseAdapter, I have multiple Fragment, but I'll post 1 as an example
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
listViewSpells?.adapter = AllSpells()
return inflater.inflate(R.layout.activity_spell, container, false)
}
}
class AllSpells: BaseAdapter() {
override fun getCount(): Int {
return player.learnedSpells.size/5+1
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItem(position: Int): Any {
return "TEST STRING"
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
val rowMain: View
val index:Int = if(position == 0) 0 else{
position*5
}
if (convertView == null) {
val layoutInflater = LayoutInflater.from(viewGroup!!.context)
rowMain = layoutInflater.inflate(R.layout.row_spells_managment, viewGroup, false)
val viewHolder = ViewHolder(rowMain.buttonSpellsManagment1,rowMain.buttonSpellsManagment2,rowMain.buttonSpellsManagment3,rowMain.buttonSpellsManagment4)
rowMain.tag = viewHolder
} else rowMain = convertView
val viewHolder = rowMain.tag as ViewHolder
viewHolder.buttonSpellsManagement1.setBackgroundResource(player.learnedSpells[index]!!.drawable)
viewHolder.buttonSpellsManagement2.setBackgroundResource(player.learnedSpells[index+1]!!.drawable)
viewHolder.buttonSpellsManagement3.setBackgroundResource(player.learnedSpells[index+2]!!.drawable)
viewHolder.buttonSpellsManagement4.setBackgroundResource(player.learnedSpells[index+3]!!.drawable)
viewHolder.buttonSpellsManagement1.setOnClickListener {
}
viewHolder.buttonSpellsManagement2.setOnClickListener {
}
viewHolder.buttonSpellsManagement3.setOnClickListener {
}
viewHolder.buttonSpellsManagement4.setOnClickListener {
}
return rowMain
}
private class ViewHolder(val buttonSpellsManagement1: Button, val buttonSpellsManagement2: Button, val buttonSpellsManagement3: Button, val buttonSpellsManagement4: Button)
}
I was originally using activities, but in the case of PagerView I've changed it to Fragment. What I know is, that I can't call this etc. what I found in java codes was in some cases getActivity() instead of this, which I couldn't find in Kotlin. So by saying that it could be in the viewGroup!!.context in the BaseAdapter.
Any help will be appreciated, thanks.
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
android:screenOrientation="landscape">
<android.support.constraint.Guideline
android:id="#+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<android.support.constraint.Guideline
android:id="#+id/guideline30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:orientation="horizontal"
app:layout_constraintEnd_toStartOf="#+id/listViewSpells"
app:layout_constraintGuide_percent="0.4"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="87dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/guideline7"
app:layout_constraintGuide_percent="0.1925"
app:layout_constraintStart_toEndOf="#+id/guideline11" />
<android.support.constraint.Guideline
android:id="#+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="83dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/guideline10"
app:layout_constraintGuide_percent="0.05"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="86dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/guideline14"
app:layout_constraintGuide_percent="0.4825"
app:layout_constraintStart_toEndOf="#+id/guideline7" />
<android.support.constraint.Guideline
android:id="#+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="7dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/imageView3"
app:layout_constraintGuide_percent="0.63"
app:layout_constraintStart_toEndOf="#+id/guideline13" />
<android.support.constraint.Guideline
android:id="#+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.774" />
<android.support.constraint.Guideline
android:id="#+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.92" />
<android.support.constraint.Guideline
android:id="#+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="83dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintGuide_percent="0.34"
app:layout_constraintStart_toEndOf="#+id/guideline10" />
<ListView
android:id="#+id/listViewSpells"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline14"
app:layout_constraintStart_toEndOf="#+id/guideline30"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewInfoSpell"
android:layout_width="0dp"
android:layout_height="141dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline9"
app:layout_constraintDimensionRatio="H,1:1.25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline30" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="75dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline30"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toBottomOf="#+id/imageView3" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="0dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="7dp"
app:layout_constraintDimensionRatio="H,"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/guideline14"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I think you need to do your adapter works after declaring a view. In activity, after calling onCreate method we can get a view for our other works. Its slighting different in fragments. We can have view after inflating your view.
First, create a view as a first line of your onCreateView function.
val view = inflater!!.inflate(R.layout.activity_spells, container, false)
Second, return that view from your onCreateView function.
Finally, you can put(use) your other works between those. In that way, you can use the viwe (your fragment) as a variable view. Your SpellManagement class will be looks like this :
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.R.layout.activity_spells, container, false)
\**
Your other codes or works here
*\
listViewSpells?.adapter = AllSpells()
return view
}
}
I think this might helps. Cheer!
So... this was REALLY strange to me, the answer was to create a new ListView with the old nowadays not very used "view.FindViewById(...)" thingie, which I've never used before because of the plugins, that allows you to straight up write the name of the component such as "listView.adapter = ..." instead of external defining it by "view.findViewById(...)" thingie. Also with the combination of this answer by MinnKhant.The final code looks as follows:
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.activity_spell_managment, container, false)
val listView:ListView = view.findViewById(R.id.listViewSpells)
listView.adapter = AllSpells()
return view
}
}
Cheers
Edit: or just write view.listViewSpells.adapter = AllSpells(), instead of findViewById(...)

Shared element fragment transition not working

I have two Fragments hosted in one Activity. They are dead simple:
class OneFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.fragment_one, container, false)
val sharedView = view.findViewById<View>(R.id.shared_view)
ViewCompat.setTransitionName(sharedView, "test")
view.findViewById<Button>(R.id.go_button).setOnClickListener {
val fragmentTransaction = fragmentManager.beginTransaction()
val oneFragment = fragmentManager.findFragmentById(R.id.fragment_container_frame_layout)
val twoFragment = TwoFragment()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val moveTransition = TransitionInflater.from(activity).inflateTransition(android.R.transition.move)
oneFragment.exitTransition = Fade()
val transitionSet = TransitionSet()
transitionSet.addTransition(TransitionInflater.from(activity).inflateTransition(android.R.transition.move))
transitionSet.duration = 600
twoFragment.sharedElementEnterTransition = transitionSet
twoFragment.enterTransition = Fade()
fragmentTransaction.addToBackStack(TwoFragment::class.java.simpleName)
fragmentTransaction.addSharedElement(sharedView, "test")
fragmentTransaction.replace(R.id.fragment_container_frame_layout, twoFragment)
fragmentTransaction.commit()
}
else {
fragmentTransaction.addToBackStack(TwoFragment::class.java.simpleName)
fragmentTransaction.replace(R.id.fragment_container_frame_layout, twoFragment)
fragmentTransaction.commit()
}
}
return view
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="com.kravtsov.transitiontest.MainActivity">
<View
android:id="#+id/shared_view"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerInParent="true"
android:background="#android:color/holo_blue_dark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/shared_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="HELLO"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="GO"
tools:ignore="HardcodedText" />
</RelativeLayout>
And the second one:
class TwoFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.fragment_two, container, false)
val sharedView = view.findViewById<View>(R.id.shared_view)
ViewCompat.setTransitionName(sharedView, "test")
return view
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="com.kravtsov.transitiontest.MainActivity">
<View
android:id="#+id/shared_view"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_below="#+id/anchor"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_blue_dark" />
<TextView
android:id="#+id/anchor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="WORLD"
tools:ignore="HardcodedText" />
</RelativeLayout>
As you can see they have common shared view, that i want to animate during transition. The problem is - animation never appears. I can see that enter and exit fade transition for the rest of elements working correctly. Only shared element transition do not work.
I've searched web a lot and mimic a lot of guiedes... No answers i found still. Does anyone face such an issue? Where shoud i start to fix this bug?
Your code is working for me. I've used your layout and kotlin code, and it works seamlessly. One thing I took care of is importing all classes of support packages.

Categories

Resources