How to set dynamic width on drawerlayout in Kotlin - android

I have set dynamic width in drawer layout in android. I have all ready tried
drawerlayout=findViewById(R.id.drawerlayout)
rel_dynamic_size=findViewById(R.id.rel_dynamic_size);
val width = resources.displayMetrics.widthPixels / 2
val params: ViewGroup.LayoutParams? = rel_dynamic_size!!.getLayoutParams()
params!!.width = width.toInt()
rel_dynamic_size!!.setLayoutParams(params)

drawerlayout=findViewById(R.id.drawerlayout)
rel_dynamic_size=findViewById(R.id.rel_dynamic_size);
val width = resources.displayMetrics.widthPixels / 1.8
val params: ViewGroup.LayoutParams? = rel_dynamic_size!!.getLayoutParams()
params!!.width = width.toInt()
rel_dynamic_size!!.setLayoutParams(params)

Related

Text Scaling seems to have no effect

private fun setQuestion() {
val mainImg = findViewById<ImageView>(R.id.movieImgView)
val movieNameText = findViewById<TextView>(R.id.movieName)
val movieYearText = findViewById<TextView>(R.id.movieYear)
val movieRatingText = findViewById<TextView>(R.id.movieRatingText)
val seriesOrNoText = findViewById<TextView>(R.id.seriesOrNoText)
val btnNext = findViewById<Button>(R.id.btnNext)
movieList = Constants.getActionMovies()
val movie: ActionMovies? = movieList!![currentPosition - 1]
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels.toFloat()
val width = displayMetrics.widthPixels.toFloat()
mainImg.setImageResource(movie!!.moviePic)
movieNameText.text = movie.movieName
movieNameText.scaleX = width
movieNameText.scaleY = height
movieYearText.text = "(${movie.date.toString()})"
movieRatingText.text = movie.rating
if (movie.series) {
seriesOrNoText.text = "Yes"
} else {
seriesOrNoText.text = "No"
}
if (currentPosition == movieList!!.size) {
btnNext.text = "Return to \n Main Menu"
btnNext.setOnClickListener {
finish()
}
}
}
}
I'm trying to set the width and height of my MovieNameText, however it seems to have no effect at all, any help? I've defined the text size in the xml file to 30sp, I've tried setText and scaleX and textSize
To change the size of the text
movieNameText.textSize = 30f // or whatever value you would like to use
Aslong as your Textview is constrained and layout width and height is set to 'wrap content' it should scale with your app.

How to make make swipe down gesture (scroll) continuous in gesture description accessibility in android

I am trying to scroll down continuously using gesture description using accessibility. Here is my code for swipe down
val displayMetrics = resources.displayMetrics
val height : Int = displayMetrics.heightPixels;
val top : Float = height*0.25.toFloat()
val bottom :Float = height *0.75.toFloat()
val swipePath = Path()
swipePath.moveTo(top, bottom)
swipePath.lineTo(top, top)
val gestureBuilder = GestureDescription.Builder()
gestureBuilder.addStroke(GestureDescription.StrokeDescription(swipePath, 0, 1000))
dispatchGesture(gestureBuilder.build(), null, null)
Please help.
path.moveTo() and path.lineTo() both takes two arguments that are x and y coordinates and to swipe vertically x coordinate of start and end point should be same. You are taking x coordinate from height instead of width and that may create other problems like point outside the screen also. so I modified your code a little bit
val displayMetrics = resources.displayMetrics
val height : Int = displayMetrics.heightPixels
val xCenter: Float = displayMetrics.widthPixels.toFloat()/2
val top : Float = height*0.25.toFloat()
val bottom :Float = height *0.75.toFloat()
val swipePath = Path()
swipePath.moveTo(xCenter, bottom)
swipePath.lineTo(xCenter, top)
val gestureBuilder = GestureDescription.Builder()
gestureBuilder.addStroke(GestureDescription.StrokeDescription(swipePath, 0, 1000))
dispatchGesture(gestureBuilder.build(), null, null)

RecyclerView Fit 5 items with different height and width using custom layout manager

First of all let me show you an image what i am trying achieve exactly :
Now as of above gif i need to build it with recyclerview.
Fit only 5 items at a time on screen.
Center and other 4 items will be scaled as shown in image.
I have tried with custom layout manager like below:
private val shrinkAmount = 0.3f
private val shrinkDistance = 1f
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
val orientation = orientation
if (orientation == VERTICAL) {
val scrolled = super.scrollVerticallyBy(dy, recycler, state)
if (isScaleView()) {
val midpoint = height / 2f
val d0 = 0f
val d1 = shrinkDistance * midpoint
val s0 = 1f
val s1 = 1f - shrinkAmount
for (i in 0 until childCount) {
val child = getChildAt(i)
val childMidpoint = (getDecoratedBottom(child!!) + getDecoratedTop(child)) / 2f
val d = Math.min(d1, Math.abs(midpoint - childMidpoint))
val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0)
child.scaleX = scale
child.scaleY = scale
}
}
return scrolled
} else {
return 0
}
}
But i am getting output as follows:
How can i achive exactly like above gif ?

Setting top margin iText Android

I am using iTextg to split an image to span multiple pages, but I am not able to set Top Margin for each page. I tried setting margins in Document() constructor, I also tried setMargins()method before I use the document.open() method. Also adding empty lines or paragraph is not working. I think I am doing something wrong here.
private fun addImage(pdfWriter: PdfWriter, document: Document, byteArray: ByteArray) {
var image: Image? = null
try {
image = Image.getInstance(byteArray)
} catch (e: Exception) {
e.printStackTrace()
}
image!!.scaleToFit(PageSize.A4)
// image.setAbsolutePosition(0f, 0f)
val content = pdfWriter.directContent
val width = PageSize.A4.width.toDouble()
val heightRatio = image.height * width / image.width
var nPages: Int = (heightRatio / PageSize.A4.height).toInt()
val difference = heightRatio % PageSize.A4.height
while (nPages >= 0) {
document.newPage()
// document.add(Paragraph("Empty space\n\n\n"))
document.add(Chunk.NEWLINE)
content.addImage(image, width, 0.0, 0.0, heightRatio, 0.0,
-((--nPages * PageSize.A4.height) + difference - 20f))
}
}

Call getMeasuredWidth() or getWidth() for RecyclerView return 0 on data binding

I'm using data binding to setup a RecyclerView. Here is the binding adapter:
fun setRecyclerDevices(recyclerView: RecyclerView, items: List<Device>, itemBinder: MultipleTypeItemBinder,
listener: BindableListAdapter.OnClickListener<Device>?) {
var adapter = recyclerView.adapter as? DevicesBindableAdapter
if (adapter == null) {
val spannedGridLayoutManager = SpannedGridLayoutManager(orientation = SpannedGridLayoutManager.Orientation.VERTICAL,
spans = getSpanSizeFromScreenWidth(recyclerView.context, recyclerView))
recyclerView.layoutManager = spannedGridLayoutManager
recyclerView.addItemDecoration(SpaceItemDecorator(left = 15, top = 15, right = 15, bottom = 15))
adapter = DevicesBindableAdapter(items, itemBinder)
adapter.setOnClickListener(listener)
recyclerView.adapter = adapter
} else {
adapter.setOnClickListener(listener)
adapter.setItemBinder(itemBinder)
adapter.setItems(items)
}
}
getSpanSizeFromScreenWidth needs the recycler's width to do some calculation. But it always returns 0.
I tried to apply a ViewTreeObserver like this:
recyclerView.viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
recyclerView.viewTreeObserver.removeOnGlobalLayoutListener(this)
val spannedGridLayoutManager = SpannedGridLayoutManager(orientation = SpannedGridLayoutManager.Orientation.VERTICAL,
spans = getSpanSizeFromScreenWidth(recyclerView.context, recyclerView))
recyclerView.layoutManager = spannedGridLayoutManager
}
})
Or use post like this:
recyclerView.post({
val spannedGridLayoutManager = SpannedGridLayoutManager(orientation = SpannedGridLayoutManager.Orientation.VERTICAL,
spans = getSpanSizeFromScreenWidth(recyclerView.context, recyclerView))
recyclerView.layoutManager = spannedGridLayoutManager
})
Code of getSpanSizeFormScreenWidth:
private fun getSpanSizeFromScreenWidth(context: Context, recyclerView: RecyclerView): Int {
val availableWidth = recyclerView.width.toFloat()
val px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300f, context.resources.displayMetrics)
val margin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 15f, context.resources.displayMetrics)
return Math.max(1, Math.floor((availableWidth / (px + margin)).toDouble()).toInt()) * DevicesBindableAdapter.WIDTH_UNIT_VALUE
}
But it still returns 0 despite my RecyclerView being displayed on the screen (not 0).
Any ideas?
In inspecting the code, it appears that your RecyclerView may actually be fine, but your logic in getSpanSizeFromScreenWidth may not be.
It looks like this: Math.floor((availableWidth / (px + margin)).toDouble()).toInt() will always be 0 when availableWidth is less than (px + margin). This will then cause getSpanSizeFromScreenWidth to return 0.
Breaking it down:
Math.floor - rounds a double down to a whole number
availableWidth / (px + margin) - will be a low number (a fraction of availableWidth)
Therefore, you're going to get 0 at times especially on smaller screens and/or smaller density screens.
Does that make sense? May not be this issue, but I'd start there. It's hard to tell you exactly the issue without knowing the whole context, but that's likely your issue.
If that is not your issue, could you say what your value is for availableWidth, px, and margin during execution?

Categories

Resources