Hello I have tried to find some help with this I'm using the correct import statement import androidx.compose.ui.Modifier but I get the error message e: /Users/lebo/AndroidStudioProjects/BasicsCodelab/app/src/main/java/com/example/basicscodelab/MainActivity.kt: (36, 68): Unresolved reference: dp
My code looks like this but when I add padding it stops working
package com.example.basicscodelab
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.basicscodelab.ui.theme.BasicsCodelabTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
BasicsCodelabTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
}
}
#Composable
fun Greeting(name: String) {
Surface(color = MaterialTheme.colorScheme.primary){
Text(text = "Hello $name!", modifier = Modifier.padding(24.dp))
}
}
#Preview(showBackground = true)
#Composable
fun DefaultPreview() {
BasicsCodelabTheme {
Greeting("Android")
}
}
I tried updating my grade version and jetpack compose in the source gradle file and in the app gradle file and nothing has helped. I've been stuck for hours
Import dp like this
import androidx.compose.ui.unit.dp
Related
I am beginner in Android development, and I am stuck for a while with this problem.
When I am trying to run my app on physical device through Android studio I get this error:
E/e.tipcalculato: open libmigui.so failed! dlopen - dlopen failed: library "libmigui.so" not found
So, the app runs but nothing shows up, except blank surface. Situation is all the same on virtual devices but without an error. Here is my code:
package com.example.tipcalculator
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.tipcalculator.ui.theme.TipCalculatorTheme
import com.example.tipcalculator.MyApp as MyApp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApp {
TopHeader()
}
}
}
}
#Composable
fun MyApp(content: #Composable () -> Unit) {
TipCalculatorTheme {
Surface(
color = MaterialTheme.colors.background,
) {
}
}
}
#Preview
#Composable
fun TopHeader(totalPerPerson: Double = 134.0) {
Surface(
modifier = Modifier
.fillMaxWidth()
.height(150.dp)
.clip(shape = RoundedCornerShape(corner = CornerSize(12.dp))),
color = Color(0xDAF0A6E5)
) {
Column(
modifier = Modifier.padding(12.dp), verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val total = "%.2f".format(totalPerPerson)
Text(text = "Total per person", style = MaterialTheme.typography.h5)
Text(
text = "$$total",
style = MaterialTheme.typography.h4,
fontWeight = FontWeight.ExtraBold
)
}
}
}
I already restarted everything, cleaned and rebuild code, checked APIs.
I just want to be able to run the apps on emulator or physical device.
I have added the blue-alliance spectrum color picker to my simple note app. Now, I can change the background color using the color picker. And the card color also changes according to the selected color. Right now, the problem is whenever I click on the note to edit and click save, then the color of the Material card change automatically to white, and then if I again click on that note to edit right now it automatically set the color as white. Right now, how can I solve this?
EditNoteFragment.kt
package com.example.noteapp.ui.Fragments
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.text.format.DateFormat
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.fragment.navArgs
import com.example.noteapp.R
import com.example.noteapp.ViewModel.NotesViewModel
import com.example.noteapp.databinding.BottomsheetlayoutBinding
import com.example.noteapp.databinding.FragmentEditnotesFragmemtBinding
import com.example.noteapp.model.Notes
import com.example.noteapp.ui.Adapter.NotesAdapter
import com.example.noteapp.utils.hideKeyboard
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.transition.MaterialContainerTransform
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.absoluteValue
class EditnotesFragmemt : Fragment() {
val oldNotes by navArgs<EditnotesFragmemtArgs>()
lateinit var binding : FragmentEditnotesFragmemtBinding
val viewModel : NotesViewModel by viewModels()
private var color = -1
private lateinit var result : String
private val job = CoroutineScope(Dispatchers.Main)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = FragmentEditnotesFragmemtBinding.inflate(layoutInflater,container,false)
binding.editingedittextnote.setText(oldNotes.data.notes)
binding.editingedittexttitle.setText(oldNotes.data.title)
binding.editFragmentxmlid.setBackgroundColor(oldNotes.data.color)
val animation = MaterialContainerTransform().apply {
drawingViewId = R.id.createFragment
scrimColor = Color.TRANSPARENT
duration = 300L
}
sharedElementEnterTransition=animation
sharedElementReturnTransition=animation
binding.backbutton.setOnClickListener {
requireView().hideKeyboard()
Navigation.findNavController(it).popBackStack()
}
binding.fabcolorpick.setOnClickListener {
val bottomSheetDialog = BottomSheetDialog(
requireContext(),
R.style.BottomSheetDialogTheme
)
val bottomSheetView : View = layoutInflater.inflate(
R.layout.bottomsheetlayout,
null,
)
val bottomSheetBinding = BottomsheetlayoutBinding.bind(bottomSheetView)
bottomSheetBinding.apply {
colorpicker.apply {
setSelectedColor(color)
setOnColorSelectedListener {
value->
color = value
binding.apply {
editFragmentxmlid.setBackgroundColor(color)
toolbarfragmentnotecontent.setBackgroundColor(color)
bottombar.setBackgroundColor(color)
activity?.window?.statusBarColor = color
}
bottomSheetBinding.bottomSheetparent.setCardBackgroundColor(color)
}
}
bottomSheetparent.setCardBackgroundColor(color)
}
bottomSheetView.post{
bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
bottomSheetDialog.setContentView(bottomSheetBinding.root)
bottomSheetDialog.show();
}
binding.btneditdonenotes.setOnClickListener {
updateNotes(it)
}
try {
binding.editingedittextnote.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus)
{
binding.bottombar.visibility = View.VISIBLE
binding.editingedittextnote.setStylesBar(binding.styleBar)
}else{
binding.bottombar.visibility = View.GONE
}
}
}catch (e: Throwable) {
Log.d("TAG", e.stackTraceToString())
}
return binding.root
}
private fun updateNotes(it: View?) {
val title = binding.editingedittexttitle.text.toString()
val notes = binding.editingedittextnote.text.toString()
val data = Notes(
oldNotes.data.id,
title = title,
notes = notes,
color= color,
)
result = "Note Saved"
setFragmentResult(
"key",
bundleOf("bundleKey" to result)
)
viewModel.updateNotes(data)
Toast.makeText(context, "Edit Note Successfully", Toast.LENGTH_SHORT).show()
Navigation.findNavController(it!!).navigate(R.id.action_editnotesFragmemt_to_homeFragment)
}
}
enter image description here
enter image description here
enter image description here
enter image description here
I'm making a simple Note making app for a project. Right now, when I create any note this comes at the end position. But I want to sort it out with time. So that whenever I will make any note, that will come to the first position. How can I do that?
HomeFragment.kt
package com.example.noteapp.ui.Fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.noteapp.R
import com.example.noteapp.ViewModel.NotesViewModel
import com.example.noteapp.databinding.FragmentHomeBinding
import com.example.noteapp.ui.Adapter.NotesAdapter
class HomeFragment : Fragment() {
lateinit var binding : FragmentHomeBinding;
val viewModel : NotesViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
// here the code is written to show the notes;
binding = FragmentHomeBinding.inflate(layoutInflater,container,false);
viewModel.getNotes().observe(viewLifecycleOwner) { notesList ->
binding.revallNotes.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
binding.revallNotes.adapter = NotesAdapter(requireContext(), notesList)
}
binding.btnaddnotes.setOnClickListener{
Navigation.findNavController(it).navigate(R.id.action_homeFragment_to_createFragment)
}
return binding.root;
}
}
Hi im new in android and i read that i can directly reference id from activity_main to mainAcctivity.kt without using findviewbyid.When i do that i get an error but my ids are correct:
This is the error Unresolved reference: button and Unresolved reference: textView
This is my code
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get reference to button
//val btnClickMe = findViewById(R.id.button) as Button
//val myTextView = findViewById(R.id.textView) as TextView
var timesClicked = 0
// set on-click listener
button.setOnClickListener {
timesClicked += 1
textView.text = timesClicked.toString()
Toast.makeText(this#MainActivity, "Hello Don.", Toast.LENGTH_SHORT).show()
}
}
}```
Please help out,i tried to import android.widget.button and textView that option is not available but when i hardcode the text appears inactive.
You can make use of ViewBinding to reduce the use of findViewById. You can find an example here.
You can go through this blog to have more insight on how to use ViewBinding.
For the above code, you can resolve the issue using the code below:
val myTextView = findViewById<TextView>(R.id.textView)
val btnClickMe = findViewById<Button>(R.id.button)
Hi guys i have problem with getting http request if i write "http://10.0.1.42:8080/state"(this is my ip) on emulator chrome this working, but if i want getting http request aplication close. Can someone help me?
import android.content.Intent
import android.os.Bundle
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import kotlinx.coroutines.runBlocking
import java.util.*
class kmnActivity : AppCompatActivity() {
val EXTRA_TEXT = "com.example.application.example.EXTRA_TEXT"
var uuid = UUID.randomUUID().toString()
var response = "nothing"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kmn)
val textView = findViewById<TextView>(R.id.textView3)
val kamenButton = findViewById<ImageButton>(R.id.kamen)
val papierButton = findViewById<ImageButton>(R.id.papier)
val nozniceButton = findViewById<ImageButton>(R.id.noznice)
kamenButton.setOnClickListener {
get_state()
textView.setText(response)
// openRollActivity()
}
papierButton.setOnClickListener {
openRollActivity()
}
nozniceButton.setOnClickListener {
openRollActivity()
}
}
fun get_state(){
val client = HttpClient()
runBlocking {
response = client.get<String>("http://10.0.1.42:8080/state") //Here not working
}
client.close()
}
fun openRollActivity() {
val intent = Intent(this#kmnActivity, rollActivity::class.java)
intent.putExtra("id", uuid)
startActivity(intent)
}
}
Or if somebody have better way, He can said me. Thanks for your help.
android:usesCleartextTraffic="true"
Add this code AndroidManifest.xml file
could refer to this https://medium.com/#son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6 u will get the clear idea over it
another possible fix will be
instead of HTTP can use https