This warning is confusing because the highlighted value can be numerically correct—and in current AndroidX, LinearLayoutManager.VERTICAL is literally an alias of RecyclerView.VERTICAL. Lint is not comparing only the number, though. It is following a source-level constant contract. The clean fix starts by matching that contract, then checks versions if the warning should never have appeared.
The warning you are seeing
Must be one of: RecyclerView.HORIZONTAL, RecyclerView.VERTICAL [WrongConstant]
layoutManager.orientation = LinearLayoutManager.VERTICALRead the diagnostic literally
The bracketed
WrongConstantis the lint issue ID.The accepted set comes from an annotation on the receiving parameter or property.
This is commonly reported by the IDE or lint task; it is not necessarily a Java/Kotlin compiler type error.
A build can fail when the project treats lint errors as fatal.
The highlighted source may behave correctly at runtime while still violating or confusing static analysis.
Use the declared RecyclerView constants
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
val layoutManager = LinearLayoutManager(requireContext())
layoutManager.orientation = RecyclerView.VERTICAL
recyclerView.layoutManager = layoutManagerWhy this removes the ambiguity
The property setter ultimately accepts an integer annotated with
@RecyclerView.Orientation.RecyclerView.VERTICALbelongs to the exact constant family named by that annotation.The layout manager is attached once to the target RecyclerView.
requireContext()is safe here only while the Fragment is attached; initialize in an appropriate lifecycle callback.There is no cast or suppression hiding a potentially invalid value.
Constructor form is usually clearer
recyclerView.layoutManager = LinearLayoutManager(
requireContext(),
RecyclerView.VERTICAL,
false
)The three constructor arguments
Contextsupplies resources/configuration needed by the layout manager.Orientation must be
RecyclerView.HORIZONTALorRecyclerView.VERTICAL.reverseLayout = falselays items in the normal resolved direction.Orientation and reverse ordering are different decisions.
The one-argument
LinearLayoutManager(context)constructor already defaults to vertical, so use it when no other setting is needed.
Java version
LinearLayoutManager layoutManager = new LinearLayoutManager(
this,
RecyclerView.VERTICAL,
false
);
recyclerView.setLayoutManager(layoutManager);Java follows the same annotated contract
The orientation is still an
intat the JVM level.Android lint uses AndroidX annotations to restrict which integer constants are meaningful.
thisis an Activity context in this member scope; a listener’sthismay mean something else.The manager controls measurement, child placement, scrolling direction, and recycling policy—not the adapter data.
Why LinearLayoutManager.VERTICAL may be flagged
Current AndroidX source declares LinearLayoutManager.VERTICAL = RecyclerView.VERTICAL and the values are identical. Therefore, a current, consistent toolchain should understand the alias. Older support-library metadata, a lint regression, mixed artifacts, a shadowing import, or stale IDE analysis can lose that relationship. Using RecyclerView.VERTICAL is a precise and harmless source-level fix, but the underlying version mismatch still deserves attention when warnings appear across the project.
What @IntDef contributes
Java bytecode receives an integer, keeping API overhead and compatibility similar to constants.
Source-retained annotation metadata tells lint the permitted values.
The IDE can offer completion and warn when an unrelated constant has the same numeric value.
It cannot make arbitrary runtime input safe by itself; the implementation may still validate and throw.
Kotlin callers also benefit from the Java/AndroidX annotation analysis.
Do not use raw zero and one
// Avoid: the meaning is hidden and lint cannot prove the contract.
layoutManager.orientation = 1
// Clear and checked.
layoutManager.orientation = RecyclerView.VERTICALNamed constants preserve intent
A reviewer can understand
VERTICALwithout remembering numeric encodings.The correct constant participates in lint’s allowed-value analysis.
Copying
0or1from source couples application code to an implementation detail.An unrelated constant that also equals
1is not semantically an orientation.
Check imports before changing dependencies
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerViewKeep the class family consistent
Do not mix obsolete
android.support.v7.widget.*classes withandroidx.recyclerview.widget.*.Watch for application constants named
RecyclerView,VERTICAL, orHORIZONTALthat shadow AndroidX symbols.Use the IDE’s Go to declaration on both the property and constant to inspect their actual artifacts.
Remove wildcard imports when they make the selected symbol ambiguous.
If migrating an old app, finish AndroidX migration rather than patching individual imports indefinitely.
Inspect the dependency graph
./gradlew :app:dependencyInsight --dependency androidx.recyclerview --configuration debugRuntimeClasspath
./gradlew :app:lintDebugThe reports answer different questions
dependencyInsightexplains which RecyclerView version Gradle selected and which dependency requested it.Use the module and variant/configuration names that exist in your project.
lintDebugreproduces debug-variant lint outside the editor.Neither command modifies source code; Gradle may download dependencies and update local caches.
If CI runs another variant or aggregate lint task, reproduce that exact task too.
Align AndroidX and tooling versions
Use one resolved RecyclerView artifact version instead of forcing unrelated versions in different modules.
Review Gradle version catalogs, dependency constraints, BOMs where applicable, and transitive requests.
Keep Android Gradle Plugin, Gradle, Kotlin, JDK, compile SDK, and Android Studio within their documented compatibility ranges.
Do not upgrade blindly just to remove one warning; read release notes and run the full test/lint suite.
If only the IDE reports the issue, compare its inspection/lint environment with the command-line build.
Clean only after inspecting the cause
./gradlew clean :app:lintDebug :app:assembleDebugA clean build is verification, not a design fix
cleanremoves Gradle build outputs for the project; it does not rewrite source or dependency declarations.Lint re-analyzes the selected variant and assemble proves it compiles/packages.
Deleting global Gradle caches is rarely the first answer and can trigger large downloads.
If clean temporarily hides the warning, record versions and seek a reproducible cause.
Run unit/instrumented/UI tests appropriate to the list behavior after the build.
When the value is dynamic
val orientation = if (showCarousel) {
RecyclerView.HORIZONTAL
} else {
RecyclerView.VERTICAL
}
recyclerView.layoutManager = LinearLayoutManager(
context,
orientation,
false
)Both branches stay inside the allowed set
The inferred integer originates only from the two documented orientation constants.
A Boolean feature decision is translated at the UI boundary.
Do not accept an arbitrary integer from navigation, JSON, preferences, or a server and pass it through unchecked.
Configuration changes should recreate/update the manager in a lifecycle-safe way while preserving intended scroll state.
Validate untrusted or persisted input
fun parseOrientation(value: String?): Int = when (value) {
"horizontal" -> RecyclerView.HORIZONTAL
"vertical" -> RecyclerView.VERTICAL
else -> RecyclerView.VERTICAL
}Map external data to domain meaning
External text is converted to one of the two accepted constants.
The fallback is explicit; use an error instead when invalid configuration should fail closed.
Persist semantic strings or a domain enum rather than Android’s numeric constant.
This boundary prevents an invalid integer from reaching
setOrientation, which rejects unsupported values at runtime.
Orientation is not reverseLayout
Vertical/horizontal selects the scrolling and child-layout axis.
reverseLayoutchanges traversal/layout direction relative to the resolved layout direction.stackFromEndcontrols whether content stacks from the end when it does not fill the viewport.Horizontal behavior also interacts with RTL layout direction.
Test LTR and RTL rather than assuming
reverseLayout = truealways means right-to-left.
Grid and staggered layouts use the same family
recyclerView.layoutManager = GridLayoutManager(
requireContext(),
2,
RecyclerView.VERTICAL,
false
)Do not confuse span count with orientation
2is the span count: two columns in a vertical grid.The orientation still uses the RecyclerView constant family.
reverseLayoutremains independent of the number of spans.Validate span counts derived from resources/configuration so zero or negative values do not reach the layout manager.
Should you suppress WrongConstant?
Usually no. Suppression turns off evidence without proving the value is valid. It is defensible only when you have verified the API contract, the value is constrained through a boundary lint cannot follow, and a comment/test records that proof. Prefer a typed adapter or mapping function that makes the contract visible.
// Avoid broad suppression on a class or file.
@Suppress("WrongConstant")
fun configure(value: Int) {
layoutManager.orientation = value
}Why this is fragile
The function accepts every integer and documents no validation.
The suppression can hide future callers passing unrelated flag or resource values.
A runtime invalid orientation can trigger
IllegalArgumentException.A small enum/string-to-constant mapping provides stronger intent and testability.
Verify behavior, not just a green lint task
Vertical mode scrolls vertically and item width/height constraints are appropriate.
Horizontal mode scrolls horizontally and items do not accidentally fill the entire width.
First/last item order matches product expectations in LTR and RTL.
Rotation, process recreation, and list updates preserve acceptable position/state.
Accessibility focus and keyboard/D-pad traversal follow the visual order.
Nested scrolling, snap helpers, item decorations, and gestures use the intended axis.
CI runs the same lint variants and fails on newly introduced issues.
A compact diagnosis flow
Open the full lint report and identify the exact receiver/property.
Go to declaration and read its annotation/allowed constants.
Use
RecyclerView.VERTICALorRecyclerView.HORIZONTALdirectly.Confirm all RecyclerView/LinearLayoutManager imports are AndroidX.
Inspect the resolved dependency and reproduce lint from Gradle.
Align inconsistent dependencies/tooling; clean and rerun the relevant lint/build tasks.
Test orientation, reverse order, RTL, state restoration, and list interaction.
Suppress only with a documented, validated boundary that lint genuinely cannot infer.
If only Android Studio shows the warning
Run the matching Gradle lint task to establish whether the issue is editor-only.
Confirm Android Studio uses the project’s supported JDK and has completed Gradle sync/indexing.
Inspect the symbol declarations rather than trusting a stale quick-fix preview.
After versions and imports are correct, restart analysis/invalidate IDE caches only as a targeted last step.
If only CI fails
Compare the exact Gradle task, variant, JDK, Android SDK, lockfiles, dependency resolution, and lint baseline.
Make CI print the lint report artifact instead of relying on a shortened console line.
Do not add a baseline entry solely to turn the build green; decide whether the issue is new, valid, or an approved legacy exception.
Reproduce in a clean local checkout or container matching CI before changing source semantics.
Primary references
The AndroidX `LinearLayoutManager` reference documents constructors,
setOrientation, allowed values, reverse layout, and the horizontal/vertical constants.The official AndroidX source shows the constants aliasing
RecyclerView.HORIZONTAL/VERTICALand the@RecyclerView.Orientationsetter contract.Android’s lint documentation explains command-line/IDE analysis, issue configuration, baselines, and targeted suppression.
The `RecyclerView` API describes the layout manager’s responsibility for measuring/positioning children and recycling policy.
Comments and corrections