i'm trying to write my very first android app. the only programing i have done in the past was some html 4 many years ago (before cms was a thing)
it is essentially a check list but i want to be able to have multiple lists seperated by a list name supplied by the user. i have an input text box called "island name" but i cant figure out how to capture that text from the user and save it for future use...
<?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="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="189dp"
android:text="temp call islander page"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="125dp"
android:layout_marginBottom="271dp"
android:onClick="loadHome"
android:text="home"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="island name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="7dp"
android:layout_marginBottom="8dp"
android:text="north"
app:layout_constraintBottom_toTopOf="#+id/checkBox2"
app:layout_constraintEnd_toEndOf="#+id/checkBox2" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginBottom="88dp"
android:text="south"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/editText" />
println("your island name is $name")
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="19dp"
android:text="you entered $name"
app:layout_constraintStart_toEndOf="#+id/checkBox2"
app:layout_constraintTop_toBottomOf="#+id/checkBox2" />
</androidx.constraintlayout.widget.ConstraintLayout>
To put it simply, in Android dev (Android Studio) you have your view, which is your .xml file and your program logic is written in your .kt or .java file then you refer to your respective view from the .kt oe ,java file.
e.g
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textView"
android:layout_height="wrap_content"
android:layout_width="200dp"/>
<Button
android:id="#+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
MainActivity.kt :
import kotlinx.android.synthetic.main.activity_main.* //this will reference all views in activity_main -
//Android studio usually auto imports this when you simply type control name
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
saveBtn.setOnClickListener {
saveDetails()
}
}
fun saveDetails(){
var userText = textView.text.toString()
textView4.setText(userText)
//do something with text
}
}
For those who is experiencing the same issue you can use sharedPreferences. As an example:
private lateinit var sp: SharedPreferences
private lateinit var editor: SharedPreferences.Editor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nameEditText = findViewById(R.id.etName)
ageEditText = findViewById(R.id.etAge)
sp = getSharedPreferences("my_sf", MODE_PRIVATE)
editor = sp.edit()
}
override fun onPause() {
super.onPause()
val name = nameEditText.text.toString()
val age = ageEditText.text.toString().toInt()
editor.apply {
putString("my_name", name)
putInt("my_age", age)
commit()
}
}
override fun onResume() {
super.onResume()
val name = sp.getString("my_name", null)
val age = sp.getInt("my_age", 0)
nameEditText.setText(name)
if (age!=0) ageEditText.setText(age.toString())
}
Related
I am currently working on a currency converter in Android Studio. The error that I faced was that if I were to put the current input, which is "US Dollars" in the editTextCurrency box (enter currency box) and click on the convert button, the application would crash. However, if I were to put a wrong input for example "US" in the editTextCurrency(Enter currency box), the application did not crash but print out the message on the TextView which is intended to. I am not sure how I should solve this issue, I hope someone could point out the error that I made. Thanks.
kt file
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import edu.singaporetech.travelapp.databinding.ActivityCurrencyConverterBinding
/**
* Activity that displays UI to convert currency
*/
class CurrencyConverterActivity : AppCompatActivity() {
private lateinit var binding: ActivityCurrencyConverterBinding
val TAG: String = "CurrencyConverterActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCurrencyConverterBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.editTextCurrency
binding.editTextRate
binding.editTextSingDollar
binding.currencyTextView
binding.rateTextView
binding.singdollarTextView
binding.convertCurrencyButton
binding.currencyOutputTextView
binding.convertCurrencyButton.setOnClickListener {
if (binding.editTextCurrency.text.toString() == "US Dollars"){
binding.currencyOutputTextView.visibility = View.VISIBLE
var currencyResult = calculateRate(binding.editTextSingDollar.toString().toFloat(), binding.editTextRate.toString().toFloat())
binding.currencyOutputTextView.text = getString(R.string.display_currency, binding.editTextSingDollar.toString().toFloat(), currencyResult)
}else{
binding.currencyOutputTextView.text = getString(R.string.invalid_currency)
binding.currencyOutputTextView.visibility = View.VISIBLE
}
}
}
}
private fun calculateRate(value: Float, exchangeRate: Float): Float {
// TODO What's the formula you need?
return value * exchangeRate
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/currencyTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/currency"
android:textSize="24sp" />
<EditText
android:id="#+id/editTextCurrency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/input_currency"
android:inputType="textPersonName"
android:textSize="20sp" />
<TextView
android:id="#+id/rateTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/rate"
android:textSize="24sp" />
<EditText
android:id="#+id/editTextRate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/input_rate"
android:inputType="numberDecimal"
android:minHeight="48dp"
android:textSize="20sp" />
<TextView
android:id="#+id/singdollarTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/singapore_dollars"
android:textSize="24sp" />
<EditText
android:id="#+id/editTextSingDollar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/input_singapore_dollars"
android:inputType="numberDecimal"
android:textSize="20sp" />
<Button
android:id="#+id/convertCurrencyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/convert" />
<TextView
android:id="#+id/currencyOutputTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAlignment="center"
android:visibility="invisible" />
</LinearLayout>
string.xml
<resources>
<string name="invalid_currency">Invalid Currency</string>
<string name="display_currency">%.1f SGD is %.1f Singapore Dollars</string>
</resources>
Changes
binding.convertCurrencyButton.setOnClickListener {
if (binding.editTextCurrency.text.toString() == "US Dollars"){
binding.currencyOutputTextView.visibility = View.VISIBLE
var currencyResult = calculateRate(binding.editTextSingDollar.text.toString().toFloat(), binding.editTextRate.text.toString().toFloat())
binding.currencyOutputTextView.text = getString(R.string.display_currency, binding.editTextSingDollar.text.toString().toFloat(), currencyResult)
}else{
binding.currencyOutputTextView.text = getString(R.string.invalid_currency)
binding.currencyOutputTextView.visibility = View.VISIBLE
}
}
Error message that I got from logcat
java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText
This code is where I am creating Alert dialog. whenever I press ok it crashes .password_dialog has 1 editText and 2 buttons.
private fun passwordCheck(position: Int){
val view=LayoutInflater.from(requireContext()).inflate(R.layout.password_dialog,null,false)
val builder=AlertDialog.Builder(requireContext())
with(builder){
setTitle("Enter your Pin")
setPositiveButton("Ok"){dialog,which->
if(pin_text.text.toString()=="1234"){
Toast.makeText(requireContext(),"Right Pin",Toast.LENGTH_LONG).show()
}
else{
pin_text.requestFocus()
pin_text.error="Incorrect"
}
}
setCancelable(false)
setNegativeButton("Cancel"){dialog,which->
dialog.dismiss()
}
setView(view)
show()
}
}
Create a layout with the view, for example, an EditText that only accepts numbers and two buttons, accept or cancel.
Then inflate the view
val view = LayoutInflater.from(context).inflate(R.id.layout, null, false)
Then with that view you get the reference of the buttons and the EditText with
val button = view.findViewById<Button>(R.id.button)
//I'm not going to declare all views, but assuming you already did.
button.setOnClickListener {
if (editText.text.toString.toInt() == 1234 //your correct pin) {
//CORRECT PIN TODO
} else {
editText.requestFocus()
editText.error = "Incorrect pin"
}
}
Then create the dialog and add that view to it
AlertDialog.Builder(context).setView(view).create().show()
Do not add the validation methods in the accept button of the dialog.
They are what are declared as follows.
setPositiveButton("Accept"){diálog, which -> }
This is the way by which you can create your own dialog with custom layout.
Implement your PIN UI in custom_dialog.xml and your logic in Kotlin file.
Here is example how you can create custom Dialog.
custom_dialog.xml
<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="wrap_content"
android:background="#424242">
<TextView
android:id="#+id/popup_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16sp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16sp"
android:layout_marginBottom="10dp"
android:text="Custom Dialog Box"
android:textColor="#color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/linearLayoutOpt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16sp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16sp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/popup_dialog">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="#+id/no_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="18dp"
android:text="No"
android:textStyle="bold"
android:textAllCaps="false"
android:textColor="#color/white" />
<Space
android:layout_width="32sp"
android:layout_height="12sp" />
<TextView
android:id="#+id/yes_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="18dp"
android:text="Yes"
android:textStyle="bold"
android:textAllCaps="false"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showDialog()
}
private fun showDialog() {
val customDialog = Dialog(this)
customDialog.setContentView(R.layout.custom_dialog)
customDialog.window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
val yesBtn = customDialog.findViewById(R.id.yes_opt) as TextView
val noBtn = customDialog.findViewById(R.id.no_opt) as TextView
yesBtn.setOnClickListener {
//Do something here YOUR LOGIC
customDialog.dismiss()
}
noBtn.setOnClickListener {
customDialog.dismiss()
}
customDialog.show()
}
}
example app screenshot
I am pretty much a beginner in app development. I am creating a GPA calculator. I was able to create a button that would create a new EditText view for each time it was tapped. but I don't know how can get the values from those EditTexts separately so I can use them to calculate GPA. I looked up every similar question here and on the internet in general, but none of them helped me. Here is my code:
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/layout_list"
android:layout_width="match_parent"
android:layout_height="500dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:drawableRight="#drawable/ic_baseline_add_24"
android:text="Add"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
</LinearLayout>
<Button
android:id="#+id/calculate_button"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Calculate"
app:layout_constraintLeft_toLeftOf="#id/layout_list"
app:layout_constraintTop_toBottomOf="#id/layout_list" />
<TextView
android:id="#+id/result"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="13dp"
android:hint="GPA:0.0"
android:textSize="20dp"
app:layout_constraintStart_toEndOf="#id/calculate_button"
app:layout_constraintTop_toBottomOf="#id/layout_list"
tools:text="GPA:0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
new_layout.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/gpa_points"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/custom_input"
android:hint=" GPA point"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/credit_points"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/custom_input"
android:hint=" ETCS"
android:inputType="number"
app:layout_constraintStart_toEndOf="#+id/gpa_points"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/close"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="18dp"
android:background="#drawable/ic_baseline_close_24"
app:layout_constraintLeft_toRightOf="#id/credit_points"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.gpacalculator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
class MainActivity : AppCompatActivity() {
private var layout: LinearLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
layout = findViewById(R.id.layout_list)
val addButton: Button = findViewById(R.id.add_button)
addButton.setOnClickListener { addView() }
val calculateButton: Button = findViewById(R.id.calculate_button)
calculateButton.setOnClickListener { calculate() }
}
private fun addView() {
val gpaView: View = layoutInflater.inflate(R.layout.new_layout, null, false)
layout?.addView(gpaView)
}
i added this function to my code
fun calculate(){
var total = 0.0
var etcs = 0.0
layout.children.forEach { newLayout ->
// Find the gpa_points EditText in the child layout
val gpaPointsEditText = newLayout.findViewById<EditText>(R.id.gpa_points) as? EditText
val creditsEditText = newLayout.findViewById<EditText>(R.id.credit_points) as? EditText
// Parse the text to be able to perform calculations
val gpaPoints = gpaPointsEditText?.text.toString().toDouble()
val credits = creditsEditText?.text.toString().toDouble()
total+=gpaPoints*credits
etcs+=credits
}
var gpa = total/etcs
val result:TextView = findViewById(R.id.result)
result.text=gpa.toString()
}
but it gives me an error like this
java.lang.NumberFormatException: For input string: "null"
atsun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.example.gpacalculator.MainActivity.calculate(MainActivity.kt:46)
at com.example.gpacalculator.MainActivity.onCreate$lambda1(MainActivity.kt:27)
at com.example.gpacalculator.MainActivity.$r8$lambda$1xRU0rpJNKxzUpdKrPz49s5lowk(Unknown Source:0)
at com.example.gpacalculator.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
You can iterate on the child views of layout, and retrieve the GPA.
// Get a decimal format for the current locale
private val decimalFormat = DecimalFormat.getInstance()
// Iterate on the children of layout
layout.children.forEach { newLayout ->
// Find the gpa_points EditText in the child layout
val gpaPointsEditText = newLayout.findViewById<EditText>(R.id.gpa_points) as EditText
// Parse the text to be able to perform calculations
val gpaPointsStr = gpaPointsEditText.text.toString()
val gpaPoints = decimalFormat.parse(str)
}
To learn more about layouts :
https://developer.android.com/guide/topics/ui/declaring-layout
So I'm doing a project and I'm completely lost. I have seen how to do data binding with TextViews but I am being asked to do it with EditText Views with Two Way Data Binding. I got up to here so far with it.
The XML File.
<?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">
<data>
<variable
name="myShoe"
type="com.udacity.shoestore.product.Shoe" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/title_detail_view"
style="#style/title_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="#string/add_shoe_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/shoe_name"
style="#style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="#string/shoe_name_string"
android:inputType="text"
android:textSize="30sp"
android:text="#={myShoe.name}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_detail_view" />
<EditText
android:id="#+id/shoe_size"
style="#style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/size_string"
android:inputType="number|numberDecimal"
android:textSize="15sp"
android:text="#={myShoe.size}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/shoe_name" />
<EditText
android:id="#+id/company_name"
style="#style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/company_string"
android:inputType="text"
android:textSize="15sp"
android:text="#={myShoe.company}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/shoe_size" />
<EditText
android:id="#+id/shoe_description"
style="#style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/description_string"
android:inputType="text"
android:textSize="15sp"
android:text="#={myShoe.description}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/company_name" />
<Button
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimaryDark"
android:text="#string/cancel_string"
android:textColor="#android:color/white"
app:layout_constraintBaseline_toBaselineOf="#+id/savee_button"
app:layout_constraintEnd_toStartOf="#+id/savee_button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/savee_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="88dp"
android:backgroundTint="#color/colorPrimaryDark"
android:text="#string/save_string"
android:textColor="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/shoe_description" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
I was told to implement it to a fragment and it should work. But I'm not sure how. Here's the fragment
class ShoeDetailsFragment : Fragment() {
private val viewModel: ActivityViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentShoeDetailsBinding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_shoe_details,
container, false
)
//initializing the button and clearing the views once canceled
binding.cancelButton.setOnClickListener { v: View ->
v.findNavController().navigateUp()
binding.shoeName.text.clear()
binding.shoeSize.text.clear()
binding.companyName.text.clear()
binding.shoeDescription.text.clear()
}
//initializing the button and saving the info to transfer to the shoeList
binding.saveeButton.setOnClickListener { v: View ->
v.findNavController().navigateUp()
val name = shoe_name.text.toString()
val size = shoe_size.text.toString()
val brand = company_name.text.toString()
val details = shoe_description.text.toString()
viewModel.addShoe(name, size, brand, details)
}
return binding.root
}
}
I am open to any ideas to initialize binding properties so i can use it in both the layout and the fragment. Or am I looking at this the wrong way?
P.S. The XML File is being represented in this fragment
I also did this project for my NanoDegree.
Inside my ViewModel I created 3 variables for each EditText:
MutableLiveData - to update the value inside the viewModel
LiveData to expose value outside viewModel e.g. in a fragment (you won't really need this)
Public Variable to monitor value of MutableLiveData and expose this to your xml thus achieving the 2-Way Binding.
Then I and would create a Shared ViewModel to share data between ShoeDetailsFragment and ShoeListingFragment .
Inside the SharedViewModel
I created 3 variables for each EditText (this is just the first 2 Edittexts):
class MySharedViewModel : ViewModel() {
private val _name = MutableLiveData<String>()
val name: LiveData<String>
get() = _name
var edShoeName = ""
private val _size = MutableLiveData<Double>()
val size: LiveData<Double>
get() = _size
var edSize = ""
......}
For the xml I did exactly what you have done but used the 3rd variable for the 2-Way Data Binding:
<EditText
android:id="#+id/shoe_name"
style="#style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="#string/shoe_name_string"
android:inputType="text"
android:textSize="30sp"
android:text="#={mySharedViewModel.edShoeName}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_detail_view" />
<EditText
android:id="#+id/shoe_size"
style="#style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/size_string"
android:inputType="number|numberDecimal"
android:textSize="15sp"
android:text="#={mySharedViewModel.edCompany}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/shoe_name" />
I have seen you have included this line of code on your ShoeDetailFragment code:
binding.saveeButton.setOnClickListener { v: View -> ....}
In my case I did it inside SharedViewModel instead:
fun onSaveButtonClick() {
//check value entered for size and set it to 0 if blank
if (edSize == "") {
edSize = "0"
}
//update MutableLiveData with values read live from the EditText
_name.value = edShoeName
_size.value = edSize.toDouble()
//save shoeObject to the _shoeList MutableLiveData
_shoeList.value?.add(Shoe(edShoeName, edSize.toDouble(), edCompany, edDescription))
}
Using DataBinding I moved the onClick bit to xml:
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginBottom="75dp"
android:background="#drawable/custom_button_background"
android:onClick="#{()->sharedViewModel.onSaveButtonClick()}"
You can also refer to my project.
I have spent time to search to find the working solution. Some doesn't apply to my case and some doesn't work. That is why I am posting this here.
I am trying to pick a date using the DatePickerFragment in a fragment. I was following the directions explained in Android Developer's site here. I am not getting the code compiled even after following all the steps.
By the way, I am using Navigation Graph to switch between fragment.
Here is what I did and what I faced problem on:
My DatePickerFragment
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val dayOfMonth = c.get(Calendar.DAY_OF_MONTH)
return DatePickerDialog(requireActivity(), this, year, month, dayOfMonth)
}
override fun onDateSet(view: DatePicker?, year: Int, month: Int, day: Int) {
// Do something with the date chosen by the user
// I want to update the button text of my AddNewTransactionFragment with the date picked
}
}
My AddNewTransactionFragment
The supportFragmentManager is not recognized.
class AddNewTransactionFragment : Fragment(R.layout.fragment_add_new_transaction) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fun showDatePickerDialog(v: View) {
val newFragment = DatePickerFragment()
newFragment.show(supportFragmentManager, "datePicker")
// here this 'supportFragmentManager' is red and the code won't compile with the
// error: Unresolved reference: supportFragmentManager.
// and the function showDatePickerDialog is grayed out even though I have
// android:onClick="showDatePickerDialog" in the corresponding xml file.
}
}
}
My xml file of fragment AddNewTransactionFragment
Here, the button android:onClick= attribute has a method but it is red, which is not recognized from the Fragment code.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundGray"
android:padding="25dp"
tools:context="com.sukajee.splitexpense.AddNewTransactionFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textViewAddNew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="10dp"
android:text="Add New Transaction"
android:textColor="#color/colorAccent"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewTransactionDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Date"
android:textColor="#color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textViewAddNew" />
<Button
android:id="#+id/buttonTransactionDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Friday, 01 January 2021"
app:layout_constraintStart_toStartOf="parent"
android:onClick="showDatePickerDialog"
app:layout_constraintTop_toBottomOf="#id/textViewTransactionDate" />
<EditText
android:id="#+id/editTextDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:hint="Description (Grocery, Food, Laundry etc.)"
android:maxLines="1"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/buttonTransactionDate" />
<EditText
android:id="#+id/editTextStore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:hint="Store Name"
android:maxLines="1"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editTextDescription" />
<EditText
android:id="#+id/editTextAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:hint="Amount"
android:inputType="numberDecimal"
android:maxLines="1"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editTextStore" />
<EditText
android:id="#+id/editTextNote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:hint="Note"
android:inputType="textMultiLine"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editTextAmount" />
<Button
android:id="#+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editTextNote" />
<Button
android:id="#+id/buttonCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cancel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/buttonSubmit" />
<Button
android:id="#+id/buttonClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Clear"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/buttonCancel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
This is what explained in the developer's site. What could be the reason for me not getting the required output even after following everything described?
Please help.
You are trying to use the supportFragmentManager from a Fragment but the supportFragmentManager is only available from an Activity specifically an AppCompatActivity or FragmentActivity.
From a Fragment you could use the childFragmentManager as a FragmentManager but i've never tried to open a DialogFragment from a Fragment.
You have used supportFragmentManager which is available in activity, and also you wrote a method inside another method.
Try with this.
class AddNewTransactionFragment : Fragment(R.layout.fragment_add_new_transaction) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
someButton.setOnClickListener{v->showDatePickerDialog(v)}
}
fun showDatePickerDialog(v: View) {
val newFragment = DatePickerFragment()
newFragment.show(childFragmentManager, "datePicker")
// here this 'childFragmentManager' is red and the code won't compile with the
// error: Unresolved reference: supportFragmentManager.
// and the function showDatePickerDialog is grayed out even though I have
// android:onClick="showDatePickerDialog" in the corresponding xml file.
}
}