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.
android-jetpack-compose
12,343
questions
185
votes
12
answers
119k
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) {
...
181
votes
13
answers
56k
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 ...
169
votes
9
answers
120k
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 ...
164
votes
19
answers
222k
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 = ...
149
votes
22
answers
118k
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
...
125
votes
8
answers
62k
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
64k
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(
...
113
votes
12
answers
133k
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?) {
...
110
votes
7
answers
34k
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() },
...
109
votes
5
answers
75k
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(...
106
votes
3
answers
56k
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 ...
105
votes
7
answers
67k
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 ...
103
votes
6
answers
80k
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()?
99
votes
2
answers
19k
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 ...
99
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 ...
94
votes
6
answers
46k
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("") ...
93
votes
13
answers
50k
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 ...
92
votes
3
answers
47k
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 ...
89
votes
4
answers
48k
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 ...
88
votes
7
answers
25k
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 ...
87
votes
9
answers
94k
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 ...
87
votes
6
answers
107k
views
How to create rounded border Button using Jetpack Compose
I need to add border with rounded corner in Button using Jetpack Compose
Like :
86
votes
1
answer
57k
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 ...
84
votes
3
answers
32k
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 ...
80
votes
12
answers
47k
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,
...
79
votes
8
answers
74k
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
112k
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:/...
77
votes
24
answers
80k
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
3
answers
34k
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
7
answers
78k
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 ...
74
votes
8
answers
52k
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 ...
73
votes
5
answers
95k
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
7
answers
31k
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
52k
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
57k
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
83k
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
44k
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
4
answers
67k
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
44k
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
42k
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 ...
67
votes
18
answers
34k
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
23k
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 ...
67
votes
9
answers
37k
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 ...
66
votes
9
answers
41k
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 ...
65
votes
3
answers
77k
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
75k
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 ...
62
votes
5
answers
90k
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 ...
61
votes
5
answers
45k
views
How to draw a circular image in Android Jetpack Compose?
Let's say I have a rectangular avatar image like the one below, how can I force it to be drawn as a circle in Jetpack Compose?