Questions tagged [android-jetpack-compose]
Jetpack Compose is Android’s modern toolkit for building native declarative UI made by Google. If you're targeting desktop with Compose for Desktop, use both this and [compose-desktop] tags.
12,544
questions
189
votes
12
answers
121k
views
How to get Context in Jetpack Compose
fun createListItem(itemIndex: Int) {
Padding(left = 8.dp, right = 8.dp, top = 8.dp, bottom = 8.dp) {
FlexRow(crossAxisAlignment = CrossAxisAlignment.Center) {
expanded(1.0f) {
...
182
votes
13
answers
57k
views
Type 'State<List<User>?>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
I'm trying to get a value from LiveData with observeAsState in jetpack compose, but I get a weird error
Type 'State<List?>' has no method 'getValue(Nothing?,
KProperty<*>)' and thus it ...
172
votes
9
answers
122k
views
How to add Margin in Jetpack Compose?
How exactly can you add Margin in Jetpack Compose?
I can see that there is a Modifier for padding with Modifier.padding(...) but I can't seem to find one for margins or am I blind?
Someone guide me ...
165
votes
19
answers
226k
views
Jetpack Compose - Column - Gravity center
I'm creating a layout with Jetpack Compose and there is a column. I would like center items inside this column:
Column(modifier = ExpandedWidth) {
Text(text = item.title)
Text(text = ...
151
votes
24
answers
122k
views
Hilt Unsupported metadata version in Kotlin
I was tried to run my code in Kotlin 1.5.10
With plugin as
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
and dependencies as below
...
128
votes
9
answers
64k
views
How to disable ripple effect when clicking in Jetpack Compose
In Jetpack Compose, when you enable clickable {} on a modifier for a composable, by default it enables ripple effect for it. How to disable this behavior?
Example code
Row(modifier = Modifier
...
122
votes
10
answers
65k
views
How to close the virtual keyboard from a Jetpack Compose TextField?
I'm using the Jetpack Compose TextField and I want to close the virtual keyboard when the user press the the action button (imeActionPerformed parameter).
val text = +state { "" }
TextField(
...
114
votes
12
answers
136k
views
How to load Image from drawable in Jetpack compose?
I have tried below code but it reflects nothing in the UI, I'm missing anything here?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
111
votes
7
answers
35k
views
Content padding parameter it is not used
I recently started working with Jetpack Compose. I've got the following composable:
@Composable
fun SearchScreen(navController: NavHostController) {
Scaffold(
topBar = { SearchBar() },
...
110
votes
5
answers
76k
views
How to use Compose inside Fragment?
The documentation describes how to create UI Jetpack Compose inside Activity.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(...
107
votes
3
answers
57k
views
Android Jetpack Compose Icons doesn't contain some of the material icons
There're many oft-used material icons in androidx.compose.material.icons.Icons but some are missing. Just as an example there is no print icon.
...
import androidx.compose.material.Icon
import ...
106
votes
7
answers
68k
views
Request Focus on TextField in jetpack compose
How to auto focus on textfield in jetpack compose. When i click on textfield and start typing on it. Meantime when i click back button,then when i'm try to click on textfield, nothing happens.
val ...
104
votes
6
answers
82k
views
Screen width and height in Jetpack Compose
I am wondering how to get the screen width and height with Jetpack Compose?
Is there any way you can retrieve the screen dimensions other than
getWindowManager().getDefaultDisplay()?
101
votes
13
answers
27k
views
Jetpack compose can't preview after updating to 1.0.0-rc01
this happened to me only when I updated to 1.0.0-rc01.
it says:
The following classes could not be found:
- androidx.compose.ui.tooling.preview.ComposeViewAdapter (Fix Build Path, Edit XML, Create ...
99
votes
2
answers
20k
views
var value by remember { mutableStateOf(default) } produce error, why?
I'm referring to the example in https://developer.android.com/jetpack/compose/state.
When I code
var expanded by remember { mutableStateOf(false) }
It errors stating
Type 'TypeVariable(T)' has no ...
95
votes
6
answers
47k
views
What is the difference between "remember" and "mutableState" in android jetpack compose?
I'm new in jetpack compose and trying to understand the difference between remember and mutableStateOf
In other words the deference between this line
val text = remember{ mutableStateOf("") ...
94
votes
13
answers
51k
views
Jetpack Compose Text hyperlink some section of the text
How can i add hyperlink to some section of the text of Text component?
With buildAnnotatedString i can set link section blue and underlined as in image below, but how can i also turn that section ...
93
votes
3
answers
48k
views
When should I use Android Jetpack Compose's Surface composable?
There's a Surface composable in Jetpack Compose which represents a material surface. A surface allows you to set up things like background color or border but it seems that the same might be done ...
90
votes
4
answers
49k
views
How to show ellipsis (three dots) at the end of a Text line in Android Jetpack Compose?
Whether I use androidx.compose.foundation.text.BasicText or androidx.compose.material.Text, if there isn't enough space for a text it wraps to the next line, for example:
@Composable
fun ...
90
votes
7
answers
26k
views
Bottom Nav Bar overlaps screen content in Jetpack Compose
I have a BottomNavBar which is called inside the bottomBar of a Scaffold.
Every screen contains a LazyColumn which displays a bunch of images.
For some reason when I finish scrolling, the BottomNavBar ...
88
votes
9
answers
97k
views
background color on Button in Jetpack Compose
Button(backgroundColor = Color.Yellow) {
Row {
Image(asset = image)
Spacer(4.dp)
Text("Button")
}
}
I can not figure out why I can't use background color on ...
88
votes
6
answers
109k
views
How to create rounded border Button using Jetpack Compose
I need to add border with rounded corner in Button using Jetpack Compose
Like :
87
votes
1
answer
59k
views
How can I specify an exact amount of spacing between children in a Jetpack Compose Column?
I am trying to create a Column in Jetpack Compose with a specific amount of spacing between each child element, such as 10.dp. In SwiftUI, this is relatively simple. I would just create a VStack with ...
86
votes
3
answers
33k
views
Jetpack Compose - Unresolved reference: observeAsState
I'm learning Jetpack Compose and I was trying to make a View Model for my @Composable.
In documentation (https://developer.android.com/codelabs/jetpack-compose-state#3) for observing state changes in ...
82
votes
13
answers
48k
views
Why am I getting Backend Internal error: "Exception during IR lowering error" when using Jetpack Compose clickable-Modifier?
I am creating a custom Checkbox within a Surface which has a Modifier.clickable:
Surface(
modifier = Modifier
.clickable(
enabled = enabled,
...
80
votes
8
answers
76k
views
Jetpack compose - how do I refresh a screen when app returns to foreground
I need to automatically refresh an Android Compose screen when the app returns to the foreground.
I have an that requires permissions and location services.
If the user has switched any of these off a ...
79
votes
9
answers
113k
views
How to upgrade an Android project to Java 11
I am using the latest Android Studio Arctic Fox 2020.03.01 Canary 8 and AGP 7, and I want to convert my project to use Java 11. Apparently just doing the following does not work as mentioned on https:/...
78
votes
3
answers
35k
views
Toggle password field jetpack compose
Hi I am trying to change visualTransformation dynamically when the user click on see password button. I can manage to filter password but couldn't achive to show in plain text. Any idea for that ? ...
77
votes
24
answers
82k
views
how to change statusbar color in jetpack compose?
how to make status bar color transparent in compose like here:
it has the same color but with a little bit shade.
77
votes
7
answers
80k
views
Match Width of Parent in Column (Jetpack Compose)
By default, Column {} has the width of it's largest child element. How can I make all other elements to fit the width of the parent Column? If I use Modifier.fillMaxWidth() the elements will take up ...
76
votes
8
answers
54k
views
Color from hex string in jetpack compose
How to parse hex string e.g. #9CCC65 in Color class in jetpack compose.
P.S: option seem to be missing in jetpack compose package
Current Workaround:
Exported parseColor() method from standard ...
74
votes
5
answers
97k
views
Jetpack Compose - Centering Text
I am using Jetpack Compose to create a simple flash card.
The idea is that you click the flash card and it will give you the answer.
However, I am stuck on a basic problem.
Unfortunately... I could ...
73
votes
8
answers
32k
views
Using rememberCoroutineScope() vs LaunchedEffect
Context
In Jetpack compose, we have the option of using rememberCoroutineScope() as well as using the LaunchedEffect composable in order to use coroutines / run suspend functions (show snackbars etc).
...
72
votes
2
answers
53k
views
Draw a line in jetpack compose
Using XML layout, you could use a View object with colored background to draw a line.
<View
android:width="match_parent"
android:height="1dp"
android:background="#000000" />
How can ...
71
votes
3
answers
58k
views
How to convert Dp to pixels in Android Jetpack Compose?
Most of Jetpack Compose API uses Dp as a dimensions unit, but sometimes a pixel value is required. How can I convert a dp value to px? Just for an example there's graphicsLayer() modifier that accepts ...
70
votes
15
answers
45k
views
Jetpack Compose on Kotlin 1.5.0
I've updated to Kotlin 1.5 last week, and after yesterday having seen the intention of Google to make Jetpack Compose the preferred option for designing UIs, I wanted to do some testing.
The issue is ...
70
votes
5
answers
29k
views
How to set the inputType for a TextField in Jetpack Compose
I'd like to restrict what the user can type in a TextField in Jetpack Compose. How do I do that?
The equivalent in xml is inputType:
<EditText
android:layout_width="wrap_content"
...
69
votes
7
answers
85k
views
Weights in Jetpack compose
Is it possible to do weights in Jetpack Compose? For example, I'd like to set it up in such a way that one item is weighted as 1/3 of a layout, and the other takes up the remaining 2/3.
In the XML/...
69
votes
4
answers
45k
views
Jetpack Compose how to remove EditText/TextField underline and keep cursor?
Hi I need to remove the underline in my TextField because it look ugly when the TextField is circular. I have sat the activeColor to transparent, but then the cursor wont show (because it's ...
68
votes
9
answers
42k
views
Compose-Navigation: Remove previous composable from stack before navigating
I'm using compose-navigation(alpha09) to handle the navigation between composables
I want to remove the Splash screen when moving to the next destination (I don't want the back pressed to get back to ...
68
votes
4
answers
69k
views
What does Jetpack Compose remember actually do, how does it work under the hood?
Checking out codelab's basic tutorial there is a snippet to increase counter on button when clicked
@Composable
fun MyScreenContent(names: List<String> = listOf("Android", "there&...
68
votes
5
answers
45k
views
Android Jetpack compose gradient
Is it possible to draw a gradient over an image drawable using Jetpack Compose?
fun HeroCover() {
Column {
val image = +imageResource(R.drawable.edge_of_tomorrow_poster)
Container(...
68
votes
5
answers
43k
views
How to use string resources in Android Jetpack Compose?
Let's say I have the following strings.xml resource file:
<resources>
<!-- basic string -->
<string name="app_name">My Playground</string>
<!-- basic ...
68
votes
9
answers
38k
views
remove default padding on jetpack compose textfield
I want to customize TextField composable in Jetpack Compose. I am trying to achieve the result in the image below, but somehow TextField has some default paddings which i couldn't find how to change ...
67
votes
18
answers
35k
views
Android Compose: How to use HTML tags in a Text view
I have as string from an outside source that contains HTML tags in this format:
"Hello, I am <b> bold</b> text"
Before Compose I would have CDATA at the start of my HTML String, ...
67
votes
1
answer
24k
views
ComponentActivity vs AppCompatActivity in Android Jetpack Compose
When using Jetpack Compose, by default it extends the ComponentActivity class. But after trying some samples I switched to AppCompatActivity and everything seemed to work fine. So I wonder what the ...
65
votes
3
answers
80k
views
How do I create a Jetpack Compose Column where a middle child is scrollable but all of the other children are always visible?
I am creating a Jetpack Compose Dialog that contains a Column where all of the elements should always be visible, except for the third element, which is a Text that should be scrollable if the text ...
63
votes
9
answers
76k
views
Show custom alert dialog in Jetpack Compose
I am searching how create custom dialog in Jetpack Compose. In XML or Material Design we can create easily custom Dialog in which we can take user input, radio button etc. but i am not finding such ...
63
votes
5
answers
93k
views
Error: "@Composable invocations can only happen from the context of a @Composable function"
I'm trying to show a toast message when clicking on a toolbar action, but I got this error
@composable invocations can only happen from the context of a
@composable function
Code:
@Composable
fun ...
62
votes
10
answers
38k
views
Button Long Press Listener in Android jetpack compose
I am having an Android Composable UI with a Button.
How can I track button long press events? I got it working for the Text long press, but for Button, It is not working. Same way like below if I ...