How to implement re-sizing a cardview programatically in kotlin - java

Hi every one I am facing an issue while trying to expand the height of card view programatically in kotlin. I have tried to fix this by following some web resources, but those failed.
val layout = findViewById<CardView>(R.id.listGroupCardView)
val params: ViewGroup.LayoutParams = layout.layoutParams
params.height = 100
layout.layoutParams = params
Resulting in "Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams androidx.cardview.widget.CardView.getLayoutParams()' on a null object reference"
My code snippets are:
deviceHeight in 1800..2000 -> {
Log.d(tag, "Inside setUIComponents device height is $deviceHeight")
enterUniqueCode.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)
enterUniqueCode.height= 300
bearingImg.layoutParams.width = 350
bearingImg.layoutParams.height= 350
bearingImg.updatePadding(top=15)
etBarcode.height= 70
indicator.updatePadding(top=0)
etBarcode.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25F)
val param = etBarcode.layoutParams as ViewGroup.MarginLayoutParams
param.setMargins(0,45,0,7)
etBarcode.layoutParams = param
//etBarcode.updatePadding(top = 25)
cvSubmit.layoutParams.height= 150
btnSubmit.layoutParams.height= 150
Log.d(tag, "btnSubmit.height is : ${btnSubmit.height}")
btnSubmit.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25F)
llUniqueCode.layoutParams.height= 250
totalEarningImage.layoutParams.width= 150
totalEarningImage.layoutParams.height= 150
totalEarnings.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15F)
tvTotalPointsEarned.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15F)
llTotalEarnings.layoutParams.height = 150
productCatalogueImage.layoutParams.height = 150
productCatalogueImage.layoutParams.width = 150
productCatalogueImage.updatePadding(top=2)
transactionHistory.layoutParams.height = 150
transactionHistory.layoutParams.width = 150
transactionHistory.updatePadding(top=2)
referAFriend.layoutParams.height = 150
referAFriend.layoutParams.width = 150
referAFriend.updatePadding(top=2)
llBearing.updatePadding(top=12)
}
Here, before the brace ends, I have to resize the height of cardview of the layout which is list_group.xml, in this case here, And the list_group.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="72dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:id="#+id/listGroupCardView"
android:layout_height="#dimen/dp_48"
android:elevation="8dp"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listGroup"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView>
............
</ImageView>
<TextView>
............
</TextView>
</LinearLayout>
</CardView>
</LinearLayout>
I need help to figure out how to re-size this list_group's cardview programatically using kotlin

Set new layout params to the layout ,because the current layout params is null
val layout = findViewById<CardView>(R.id.listGroupCardView)
val params = ViewGroup.LayoutParams (
ViewGroup.LayoutParams.MATCH_PARENT,
100
)
layout.layoutParams = params

Related

Append image to TextView

I've tried for two day now to append an image to a TextView, but it didn't work. I have tried this for Android 6/API Level 23. ImageSpan, Html.fromHtml and other Methods did not work. So is it possible to append image (Bitmap) between other words in a TextView on Android 6?
EDIT
Here is the XML:
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:text="------"
android:gravity="bottom|left"
android:typeface="monospace"
android:id="#+id/mainTextView1"
android:textIsSelectable="true"/>
And image span Code:
SpannableStringBuilder ssb = new SpannableStringBuilder(mOutEditText.getText()); ssb.setSpan(new ImageSpan(bitmapArray.get(0), ImageSpan.ALIGN_BASELINE), cursorPosition, cursorPosition+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mOutEditText.setText(ssb, BufferType.SPANNABLE);
Add image in xml
<TextView
android:drawableBottom="#drawable/ic_add_circle_black_24dp"
android:drawableEnd="#drawable/ic_add_circle_black_24dp"
android:drawableLeft="#drawable/ic_add_circle_black_24dp"
android:drawablePadding="#drawable/ic_add_circle_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
programmatically
textView1.setCompoundDrawables(left,top,right,bottom);
Or only left
textView1.setCompoundDrawables(left,null,null,null);
I have found the solution now for Api 21, 22, 23+:
private void appendImage(Bitmap bmp)
{
tv.setTransformationMethod(null);
SpannableString ss = new SpannableString(" ");
ss.setSpan(new ImageSpan(bmp, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append(ss);
}
And in the XML:
android:textAllCaps="false"
The error was that in api level 21+ you have to set the TransformationMethod null.
If you want to set the background of the TextView use the following methods:
txt.setBackgroundResource(int rsid);
txt.setBakgroundDrawable(Drawable object);
If by append, you mean you want to add it to the end of the TextView, then use an ImageView for the image. Or you can overlay your TextView in an ImageView
It is not possible to add images between text in a TextView. You can add an image a TextView at the start and end. This is done with
android:drawableLeft

Android- Loop over IDs with Java

I wanted to find different TextViews, which are depending on an integer.
Let's say I have got three TextViews:
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
And in my MainActivity I have got following:
String resourceText = "R.id.text";
int textCounter = 1;
TextView text1 = (TextView) findViewById(resourceText + textCounter);
So the result should be that I am able to access different TextViews over the textCounter but it doesn't work.
But using this code gives me an error which says that the findViewByID function expects an integer.
You can use getIdentifier() to convert string version of a resource id .
You just need to modify your code in this way.
String resourceText = "R.id.text";
int textCounter = 1;
textViewResId=getResources().getIdentifier(resourceText+textCounter, "id", getPackageName());
TextView text1 = (TextView) findViewById(textViewResId);

Simple image gallery with HorizontalScrollView

I'm writing an image gallery with horizontal scrolling. Images must be added programatically.
I use a custom horizontal scroll view to process and add images as in the following code:
public void setViewList(Integer linearLayoutId, Integer[] imageIdList,
Activity activity) {
displayMetrics = ImageUtils.getDisplayMetric(activity);
LinearLayout ll = (LinearLayout) findViewById(linearLayoutId);
for (Integer imgId : imageIdList) {
ImageView imageView = new ImageView(getContext());
imageView.setImageResource(imgId);
imageView.setScaleType(ImageView.ScaleType.MATRIX);
Integer[] displayMetrics = ImageUtils.getDisplayMetric(activity);
ImageUtils.scaleImage(imageView, displayMetrics[0], displayMetrics[1]);
Integer[] dstDimension = ImageUtils.createDimension();
ImageUtils.getImageSize(imageView, dstDimension);
getImageSizeList().add(dstDimension);
ll.addView(imageView);
}
}
As you can see I scale an image with use the following method (call ImageUtils.scaleImage(imageView, displayMetrics[0], displayMetrics[1])):
public static void scaleImage(ImageView imView, int screenWidth, int screenHeight) {
Drawable temp = imView.getDrawable();
Bitmap imBitmap = ((BitmapDrawable)temp).getBitmap();
int imWidth = imBitmap.getWidth();
int imHeight = imBitmap.getHeight();
float xScale = ((float) screenWidth) / imWidth;
float yScale = ((float) screenHeight) / imHeight;
float scale = xScale <= yScale ? xScale : yScale;
Matrix scaleMatrix = new Matrix();
scaleMatrix.postScale(scale, scale);
Bitmap scBitmap = Bitmap.createBitmap(imBitmap, 0, 0, imWidth, imHeight, scaleMatrix, true);
BitmapDrawable scDrBitmap = new BitmapDrawable(imView.getResources(), scBitmap);
imView.setImageDrawable(scDrBitmap);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
layoutParams.gravity = Gravity.CENTER;
imView.setLayoutParams(layoutParams);
}
My main.xml layout is pretty simple:
<?xml version="1.0" encoding="utf-8"?>
<android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="horizontal"
android:id="#+id/mainLayout"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView>
Where the android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView is implementation of a simple custom HorizontalScrollView.
For testing I use Samsung Galaxy S3 and images with the following resolution: (1) 1290*990 (2) 1221*900. What it looks like:
In many cases everything is displayed fine but from time to time I get the wrong result: the first image divides a screen with the following one at app start time and I don't have any idea why it happens.
Thanks for help.
I think you should use ViewPager from the support library v4 rather than this custom horizontal scroll view in this case. It's designed for that purpose.
Here is how to use it https://developer.android.com/training/animation/screen-slide.html.
The problem was resolved with use gravity = Gravity.FILL_HORIZONTAL | Gravity.CENTER_VERTICAL, that i set programmatically on a image view (method ImageUtils.scaleImage).

Android - different aspect ratios of imageviews in scaled and original images

I have two imageviews:
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"
android:src="#drawable/ic_logo"/>
<ImageView
android:id="#+id/logo_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo_small"/>
logo_small is just a resized version of logo (width and height have been resized to 75%). I've noticed that logo.getGlobalVisibleRect(logoRect) returns dimensions of different aspect ratio, compared to logo_small.getGlobalVisibleRect(logoSmallRect). Why is it so?
EDIT:
Here's some more code, as requested:
final ImageView logo = (ImageView) findViewById(R.id.logo);
final ImageView logo_small = (ImageView) findViewById(R.id.logo_small);
Rect logoRect = new Rect();
Rect logoSmallRect = new Rect();
logo.getGlobalVisibleRect(logoRect);
logo_small.getGlobalVisibleRect(logoSmallRect);
Log.d("LOGO", "logo aspect ratio " + ((float)logoRect.width()/ logoRect.height()) + ", logo_small aspect ratio " + ((float)logoSmallRect.width()/ logoSmallRect.height()));
And what I get is:
D/LOGO﹕ logo aspect ratio 1.0682492, logo_small aspect ratio 1.2081686

How do I set android:layout_weight on a text view programmatically?

I have a table layout in XML that does exactly what I want:
<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#drawable/evenrow">
<TextView android:text="Check" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_weight="1"></TextView>
I am trying to add the table row programmatically, but I can't seem to get the layout_weight set. Here is the java code:
TableRow.LayoutParams trlayout = new TableRow.LayoutParams (TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
TableLayout.LayoutParams tablelayout = new TableLayout.LayoutParams (TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT);
TableRow tr = new TableRow(this);
tr.setLayoutParams(trlayout);
TextView check = new TextView(this);
check.setText(String.format("%5.2f", i));
tr.addView(check);
I thought the 3rd param to new TableRow.LayoutParams was the default weight for the row's children, but it is not working.
What am I missing?
Well in a regular XML if you set wrap_content on the dimension of orientation you want to expand in then layout weight has no effect.
Change the width parameter to "0dp" and then your layout weight should work.

Categories