When this program runs on the emulator it works, however, when I run it on my phone the timePicker is referenced as null.
An emulator running API 26 can run it fine, however, at API 28 it does not work. So I think It might be something with API 28 (which also is what my phone runs)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val picker = findViewById<TimePicker>(R.id.timePicker)
val t = picker == null // Is true on phone and is false on emulator
Toast.makeText(this#MainActivity, t.toString(), Toast.LENGTH_LONG).show()
//picker.setIs24HourView(true)
val activity = findViewById<EditText>(R.id.activityName)
val mCallApiButton = findViewById<Button>(R.id.submit)
mCallApiButton?.setOnClickListener{
val time = GetTime(picker)
PostActivity(time, activity.text.toString()) // Function that does other stuff
}
}
}
I need to reference the timePicker so that I can set it to 24H.
Here is a look at the layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent">
<TimePicker android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="clock"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/activityName"
android:singleLine="true" android:hint="Activity" android:textSize="18sp"
android:textAlignment="center" android:autofillHints="Activity"/>
<Button
android:text="Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/submit"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot of the emulator: (Which is how it should look)
Screenshot of the phone:
This is because I use API 28 and not 27 and under. The answer can found here:
https://github.com/xamarin/Xamarin.Forms/issues/5159
Related
I'm writing a simple tip calculator app in android studio ( which is one of the programs in the https://developer.android.com/courses/android-basics-kotlin/course course ).
But I'm getting an unexpected error, unresolved reference: toDouble, when I try to covert a string to a double. Even after pasting the solution code from the website into my application, the error won't go away. I also tried first converting the string to an int using toInt function but I get the same error.
Here is the code inside MainActivity.kt.
package com.example.tiptime
import java.text.NumberFormat
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.tiptime.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.calculateButton.setOnClickListener{ calculateTip() }
}
fun calculateTip() {
val stringInTextField = binding.costOfService.text.toString()
val cost = stringInTextField.toDouble()
}
}
Here is my code in 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"
android:padding="16dp"
tools:context=".MainActivity"
tools:ignore="UseSwitchCompatOrMaterialXml">
<EditText
android:id="#+id/cost_of_service"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:hint="#string/cost_of_service"
android:inputType="numberDecimal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="#string/cost_of_service" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/how_was_the_service"
android:id="#+id/service_question"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cost_of_service"/>
<RadioGroup
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/tip_options"
android:checkedButton="#+id/option_twenty_percent"
app:layout_constraintTop_toBottomOf="#id/service_question"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_twenty_percent"
android:text="#string/amazing_20"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_eighteen_percent"
android:text="#string/great_18"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_fifteen_percent"
android:text="#string/ok_15"/>
</RadioGroup>
<Switch
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/round_up_switch"
android:text="#string/round_up_tip"
android:checked="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tip_options" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:id="#+id/calculate_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/round_up_switch"
/>
<TextView
android:id="#+id/tip_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tip_amount"
app:layout_constraintTop_toBottomOf="#id/calculate_button"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And here is the screenshot of the error
Any help is appreciated!!
Seems like you do not have the latest kotlin support .
Update the Kotlin version to the latest one from :
File ->> Setting -->> Language & Frameworks -->> Kotlin ...
Try after removing
import java.text.NumberFormat
comment
fun calculateTip() {
val stringInTextField = binding.costOfService.text.toString()
val cost = stringInTextField.toDouble()
}
build & clean
uncomment the code in above method you will get correct import
Use
val cost = Double.parseDouble(stringInTextField)
I have been using zahid-ali-shah/SignatureView's SignatureView for a few years now and today implementing it on a new project I am unable to draw the signature and have tested it on both emulator and physical device.
This has been convenient in speeding up the process to capture user signatures as bitmaps, and hope it is just a simple fix. Also am curious if anyone else is having the same issue.
Wouldnt be a good question without some code, here is my view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:title="#string/SIGNATURE_TOOLBAR_TITLE" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/signature_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.kyanogen.signatureview.SignatureView
xmlns:sign="http://schemas.android.com/apk/res-auto"
android:id="#+id/signature_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
sign:penSize="5dp"
sign:backgroundColor="#ffffff"
sign:penColor="#000000"
sign:enableSignature="false"/>
<Button
android:id="#+id/submit"
style="#style/ButtonStyle"
android:layout_width="161dp"
android:layout_height="47dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Submit Signature"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/clear"
style="#style/ButtonStyle"
android:layout_width="142dp"
android:layout_height="43dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The Signature View is a Fragment "SignatureFragment" I am using Binding on the view and the "signature_view" is accessible using _binding
OnViewCreated:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
//Load UI Listeners
attachListeners()
}
attachListeners():
private fun attachListeners() {
//Signature Pad Setup
_binding?.signatureView?.penColor = Color.BLACK
//Load all of the listeners here for organization
_binding?.submit?.setOnClickListener(){
val signatureSubmitAction =
SignatureFragmentDirections.actionSignatureFragmentToRatingFragment()
findNavController().navigate(signatureSubmitAction)
}
_binding?.clear?.setOnClickListener {
confirmClearOfSignatureView()
}
}
confirmClearSignature():
private fun confirmClearOfSignatureView() {
//Create Alert Builder and Control Clear
val clearSignatureDialog = AlertDialog.Builder(context)
clearSignatureDialog.setTitle("Clear Signature?")
clearSignatureDialog.setMessage("Are you sure you want to clear your current signature? This operation is final.")
clearSignatureDialog.setPositiveButton("Yes, Clear") { dialog, which ->
_binding?.signatureView?.clearCanvas()
}
clearSignatureDialog.setNegativeButton("Don't Clear") { dialog, which ->
//Default Action is Dismiss
}
clearSignatureDialog.show()
}
From what I see in the documentation and my past experience, I should with the above be able to at least see the strokes on the screen. I am not able to see anything.
Any suggestions on what might be going on or how to resolve this? I would love to still use this Repo if possible.
I was unable to resolve this with the above library and switched to a better supported one:
implementation 'com.github.gcacace:signature-pad:1.3.1'
Updated Signature Pad
I have looked through many examples but i really cant figure this out. I literally just started working with Kotlin and android studio. Any help would be appreciated
Hey is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#drawable/login_background"
android:gravity="center"
android:padding="30dp"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Enter_No"
android:textSize="22sp"
android:inputType="phone"
/>
<Button
android:id="#+id/btnLogin"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#fff"
android:textColor="#1495d7"
android:text="#string/Sign_in" />
<Button
android:id="#+id/FrgtPass"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="#string/Frgt_pass_prmt"
android:textAllCaps="false" />
</LinearLayout>
MainActivity.kt
package com.example.hackapp1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnLogin.setOnClickListener{}
}
}
Maybe, you did not apply kotlin-android-extension plugin.
It was applied automatically, but not now.
kotlin-android-extension has some issues. It will be deprecated in near future.
See details here.
Instead of kotlin-android-extension ViewBinding and DataBinding is recommended.
You can simply enable like following:
android {
...
viewBinding { enabled = true }
dataBinding { enabled = true }
}
See below links for details.
DataBinding
ViewBinding
I'm running a simple counter (counts how much a button is pressed) on my physical device instead of android emulator and everything seems fine except the toast message seems to be faulty, it shows the message, but when i tap the button that triggers the Toast multiple times, the Toast message has a delay on showing between one tap and another.
This is the Toast:
Toast.makeText(this,"Works", Toast.LENGTH_SHORT).show()
Also the android studio shows this error:
E/GraphicExt: GraphicExtModuleLoader::CreateGraphicExtInstance false
My device is a Xiaomi Redmi Note 8 Pro, running android 10.
Error image
EDIT:
This is the 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">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First app!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginLeft="162dp"
android:layout_marginTop="255dp"
android:layout_marginEnd="162dp"
android:layout_marginRight="162dp"
android:layout_marginBottom="42dp"
android:text="Click me!"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this is the MainActivity:
package com.example.helloworld
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var timesClicked = 0
button.setOnClickListener {
timesClicked += 1
textView.text = timesClicked.toString()
Toast.makeText(this#MainActivity, "Button works", Toast.LENGTH_SHORT).show()
}
}
}
When I am trying to add the image in layout the app is working fine in lollipop but crashes in marshmallow. If that images is removed then app works fine. It gives error when image is inserted and gives error in line SetContentview of kotlin page.
This is XML CODE
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#drawable/back"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:contentDescription="Logo of App"
android:src="#drawable/echoi"
android:id="#+id/img"
/> //When adding this image the app crashes in marshmellow and is working fine in lolipop.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#null"
android:layout_gravity="center_horizontal"
android:onClick="p1"
android:text="1 Player"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#null"
android:layout_gravity="center_horizontal"
android:onClick="p2"
android:text="2 Player"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="0.3"/>
</LinearLayout>
This is Kotlin Code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)//After crashin in logcat it shows this line has error.
}
public fun p1(view: View)
{
val intent: Intent = Intent(applicationContext,Oneplayer::class.java)
startActivity(intent)
}
public fun p2(view: View)
{
val intent: Intent = Intent(applicationContext,Twoplayer::class.java)
startActivity(intent)
}
}
Make sure the ID "#+id/img" is unique and not used anywhere else.
Did you try to remove android:contentDescription attribute?