Add images dynamically based on choice selected - java

I have the following layout which displays an image and a text string:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/number_icon"
android:layout_height="#dimen/number_icon"
android:scaleType="fitXY"
android:layout_marginTop="#dimen/letter_icon_margin_top" />
<Button
android:id="#+id/btnNumber"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="3"
android:text=""
android:gravity="center"
android:textSize="#dimen/word_size_text"
android:textColor="#000000"
android:textStyle="bold"
android:background="#drawable/borderaction" />
</LinearLayout>
The ivIcon is displaying the images based on user input. It worked great when displaying one image per letter. But how do i fix the above layout so it displays the number of images based on user selection.
#dimen is used to change the view size based on the screen.
For example this is what is looking like when I use it for letter:
For the number, if the user chooses 5 the display should be:
If the user chooses 1 the display should be:
The code that is setting the image for the letter is:
ivLetterIcon = (ImageView) findViewById(R.id.ivIcon);
ivLetterIcon.setImageResource(R.drawable.apple);
How would I accomplish the number images?
Would it be best to have a blank layout without the images and add the ImageView at runtim depending on the number and have it centered? This way the images(s) are centered to the parent view layout?

Well I got an idea maybe you can try this out.
Try using GridView for displaying the number of apples by dynamically changing the column numbers and row numbers depending upon the screen.
For ex, if gridView is the GridView and the entered number is 11, you can do this.
gridView.setNumberOfColumns(5);
gridView.setNumberOfRows(3);
so 5x3 can accomodate 15 elements which can fit in 11 easily.
Then inflate the custom view containing the Apple into the columns.

Make the layout_height for the ImageView a fixed height
--edit--
int dp = TheFixedSize;
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
Imageview.setHeight(px);

Related

set dynamic checkboxes positions in constrain layout programmatically

I create checkboxes dynamically within the Constraintlayout. I need to use this layout, not the Linearlayout. So I created the given below methods
order.xml file
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clReasons"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clDepartments">
</androidx.constraintlayout.widget.ConstraintLayout>
orderstatus.java
linearMain =(ConstraintLayout) findViewById(R.id.clReasons);
for(int intReasonCount=0;intReasonCount<reasonsList.size();intReasonCount++)
{
checkBox=new CheckBox(OrderStatus.this);
checkBox.setId(Integer.valueOf(reasonsList.get(intReasonCount)
.get(ReasonInfo.ReasonID))
);
checkBox.setText(String.valueOf(reasonsList.get(intReasonCount)
.get(ReasonInfo.ReasonDesc)));
linearMain.addView(checkBox
);
The output is
All checkboxes are aligned at one position. I need to set this dynamic checkbox are 2 columns at each row(2 checkboxes at each row). Please refer to the image.
You need to set constaints for child views in code.
For example check this answer: ConstraintLayout: change constraints programmatically

How to display rating value(Integer) from an API (Google api) using ratings bar in Android (preferably using Kotlin)?

val rating = jsonObject.getInt("rating")
for instance, a rating of 4, to be displayed as 4 stars in my view.
I am looking for ways to display it programatically as image views or to use Ratingsbar
Use RatingBar, in your view:
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In your Activity:
Use synthetic Just type the id of your RatingBar and it'll get your RatingBar
import kotlinx.android.synthetic.main.your_activity.*
val rating = jsonObject.getInt("rating")
ratingBar.numStars = rating
or just old school:
val rBar: RatingBar = findViewById(R.id.ratingBar)
rBar.numStars = rating
Edit: As you wanted to know about resizing:
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="5"
android:rating="3.5"
android:scaleX=".5"
android:scaleY=".5"
android:transformPivotX="0dp"
android:transformPivotY="0dp" />
You can use scaleX & scaleY to resize accordingly (chose a balanced value of both) the base value is 1 for both. (.5,.5 will make it half of the original size.)
Here's a question about resizing: How can I decrease the size of Ratingbar?

How do I make the foreground attribute for a button work below API 23?

I have two Buttons nested in a LinearLayout. Between these Buttons are two TextViews. In the Xml, I have set the foreground to an image for each of these Buttons.
It runs fine on my device for Api 23. But on other devices below Api 23, the foreground image does not display and instead results in a default white solid color. Is there any way to make these images show using foreground below Api 23?
We have tried FrameLayout but it does not do what we want it to do. Would ImageButtons be a better way to solve this issue?
One of the core functions of our app is that every time a user taps a Button, the size increases and the image stretches accordingly. This is done dynamically in code. If I were to use ImageButtons, I would need to set the layout parameters every time for height and width, rather than one line of code that sets the height.
Any tips would be appreciated!
EDIT: Code I am working with -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="11"
android:background="#android:color/black">
<Button
android:layout_weight="5"
android:id="#+id/firstP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:foreground="#drawable/icebutton"
android:scaleX="1"
android:scaleY="1"/>
<TextView
android:layout_weight="0.5"
android:id="#+id/firstPlayer"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="180"
android:textColor="#android:color/white"
android:background="#android:color/transparent"/>
<TextView
android:layout_weight="0.5"
android:id="#+id/secondPlayer"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:background="#android:color/transparent"/>
<Button
android:layout_weight="5"
android:id="#+id/secondP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:foreground="#drawable/firebutton"
android:scaleX="1"
android:scaleY="1"/>
</LinearLayout>
We found out that there were two issues causing the images to not be shown.
1. The size of the image file was too big, creating an outOfMemory error which in turn resulted in the buttons not displaying the images.
2. The foreground attribute does not work for API 22 and below.
Steps to solving these issues:
1. We reduced the size of the image files.
2. We replaced Button with ImageButton
3. In the XML file we removed the foreground attribute, added a black background, and added the image via the src attribute. The following is a snippet.
<ImageButton
android:layout_weight="5"
android:id="#+id/firstP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:src="#drawable/icebutton"
android:scaleType="fitXY"
android:background="#android:color/black"/>
We then had to change our code to dynamically adjust the height of the buttons to match the new image buttons with the help of this link by setting the LayoutParams:
how to change size of button dynamic in android
Now everything works perfectly!

i Can't Resize Image in XML Android

Why I can't display images in a large size, its results even smaller, is there something wrong with my XML code? or errors are in the java code? And i have "[Accessibility] Missing contentDescription attribute on image" on my XML code in ImageView
This my code :
Detail.xml
<ImageView
android:id="#+id/ivPosterImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="300dp"
android:src="#drawable/large_movie_poster" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ivPosterImage"
android:layout_marginTop="10dp" android:id="#+id/scrollView1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Activity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_box_office_detail);
// Fetch views
ivPosterImage = (ImageView) findViewById(R.id.ivPosterImage);
....
Picasso.with(this).load(movie.getLargePosterUrl()).
placeholder(R.drawable.large_movie_poster).
into(ivPosterImage);
Please help me ...
Its clearly looking that your downloaded image have very small height and width dimension and you have settled the dimension around wrap_content. That's why you are getting this small icon instead of full image.
And again image download libs always use image attribute setSource in their code, which also set image around wrap_content.
Now you have 2 choice to show image wider:
set width = match parent,
height = fixed(any value as per your choice like 100 dp ,200 etc), i have not fixed the width because thats goona create exception on small or large screns.
set image background instead of image src that is by default set by image download libs.
Hopefully this will resolve your issue.

How do I make my ImageView a fixed size regardless of the size of the bitmap

So I'm loading images from a web service, but the size of the images are sometimes smaller or bigger than other images and the visualization looks silly when I put them in a ListView in android. I'd like to fix the size of my ImageView so that it only shows a portion of the image if it's larger than a preset amount. I've tried everything I can think of setting the setMaxWidth/setMaxHeight, setting the scale type to centerCrop, using ClipableDrawable wrapping my BitmapDrawable, setting using Drawable.setBounds(). I've tried setting in the XML and programmatically, but neither worked. I'm very surprised setting max width/height didn't do anything. Below is the XML I'm using ImageView definition in my layout file:
<ImageView android:id="#+id/recipeImage"
android:maxHeight="64px"
android:maxWidth="64px"
android:scaleType="centerCrop"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"/>
But,nothing has worked.
Just delete the maxHeight and maxWidth attributes and enter your units in layout_width and layout_height appropriately. But do not use the px unit - use dp instead, because these are device-independent.
This will fit the image in the specified height and width, irrespective of image size.
<ImageView
android:id="#+id/image5"
android:layout_width="300dp"
android:layout_height="200dp"
android:src="#drawable/image_1"
android:scaleType="fitXY"/>
Get ImageVIews as facebook posts.
Glide.with(context).load(link)
.placeholder(R.drawable.aboutlogo)
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);`
<ImageView
android:id="#+id/img1"
android:src="#drawable/ic_menu_camera"
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"/>

Categories

Resources