Create imageview dynamically - java

I can't seem to get these image views working. I wan't to create a few imageviews dynamically but I want them to have a template or so from xml.
My loop looks as so:
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLinearLayout);
for (int i=0; i<10; i++){
ImageView imgView = new ImageView(this);
imgView.setAdjustViewBounds(true);
imgView.setId(R.id.coverview1);
layout.addView(imgView);
Picasso.with(context).load("http://blah.com/image.jpg").into(imgView);
}
I'm creating a few images and just placing some temporary image in them but can't seem change the image size at all.
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainLinearLayout">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/coverview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:maxWidth="20dp"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Taking the advice of the answer below, I added what he said but now am not able to change the scale type.
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setAdjustViewBounds(true);
But the image is still the height of the container and not stretching.

You Need to add LayoutParams for your ChildView Like:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
yourCalculatedWidth, LayoutParams.FILL_PARENT);
params.gravity=Gravity.CENTER_HORIZONTAL;
imgView.setLayoutParams(params);

Related

Android: Box that fills entire screen with content below

I've been trying to figure out how to place a container that fills the entire screen vertically inside a ScrollView that has content below.
Something like this:
[ BOX THAT FILLS ENTIRE SCREEN ]
[ Some other stuff / requires scroll to view ]
This is the content I would like below the box:
http://myupload.dk/handleupload/ce34836ostt
I've tried everything - it seems like i can't position stuff absolute to the container that fills the entire screen. I can make a box that fills the entire screen, but everything i adds below it squeezes the box together.
It works when I add a size to the box, like this:
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:minWidth="25px"
android:minHeight="0px"
android:paddingTop="0dp"
android:fillViewport="true"
android:id="#+id/scroll">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- THIS BOX I WOULD LIKE TO FIT THE ENTIRE SCREEN -->
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/cardsPager" />
<!-- THIS CONTENT SHOULD BE BELOW THE BOX ABOVE -->
<fragment android:name="bonnier.hvadsynes.fragments.DetailsFragment"
android:id="#+id/details_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
Really appreciate your help!
Simon
My idea is to set the height of the screen-filling-box programmatically.
When and Where: depends on what you use e.g.in onResume there surely are better places but for now it works.
How: add an OnGlobalLayoutListener to your viewtreeobserver and then copy your scrollviews height to 'the box'.
<!--Whatever the whole things height should be, e.g. match_parent or 100dp or or or-->
<ScrollView
android:background="#0f0"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollview">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- THIS BOX I WOULD LIKE TO FIT THE ENTIRE SCREEN -->
<!-- Any height works as we overwrite it anyway-->
<View
android:id="#+id/makebig"
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="300dp" />
<!-- THIS CONTENT SHOULD BE BELOW THE BOX ABOVE -->
<View
android:background="#0ff"
android:id="#+id/details_fragment"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</LinearLayout>
</ScrollView>
In your Activity or wherever: e.g. in onResume()
final View makebig = findViewById(R.id.makebig);
final View scrollview = findViewById(R.id.scrollview);
final ViewTreeObserver viewTreeObserver = scrollview.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//only do this all once
scrollview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
makebig.getLayoutParams().height = scrollview.getHeight();
}
});
You can do it this way:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#A5CB3A"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<!-- here goes your textview -->
</LinearLayout>
</LinearLayout>
And in the onCreate method of the Activity
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenHeight = size.y;
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.height = screenHeight; //here you can substract the height of the top bar, if your app has one
layout.setLayoutParams(params);
You are just setting the height of the screen to the inner layout.

Set Android ListView row height to biggest row

I have a ListView displaying some text on my android activity. The problem I am facing is that some text is bigger than the other so the ListView rows don't have the same size.
I would like to have all the rows the size of the biggest. That way all the text will be display and they will all have the same size.
Here is my xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="#+id/favorite_checkbox"
android:layout_width="0dp"
android:layout_height="fill_parent"
style="?android:attr/starStyle"
android:layout_weight="1"
/>
<TextView
android:id="#+id/IdText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
/>
<TextView
android:id="#+id/StationNameText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>
Thanks for your time.
You can get the height of the 'tallest' view programmatically - How to get an Android widget's size after layout is calculated?, and then set that height to your other layouts.
I would do something like this:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int HeightOfTallestView = //method you choose from link above
IdText.setLayoutParams(new LinearLayout.LayoutParams(width, HeightOfTallestView ));
Please let me know if this works for you.

Android Programmatically add an ImageView on top of Another ImageView

So i'm trying to create a board game token, and place that token at it's starting point. Just so happens on this level it starts on top of ImageView onetwo. However when i create the token and tell it where to go it shows up under the gridview. Any ideas or help would be appreciated!
I guess its because i'm using RelativeLayout.ALIGN_TOP inside a grid view but i haven't see the alternative.
Java:
public void createToken(){
// Let's create the missing ImageView
ImageView image = new ImageView(this);
// Now the layout parameters, these are a little tricky at first
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(75, 75);
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.onetwo);
params.addRule(RelativeLayout.ALIGN_LEFT, R.id.onetwo);
params.addRule(RelativeLayout.ALIGN_TOP, R.id.onetwo);
params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.onetwo);
image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
image.setImageResource(R.drawable.robo);
// Let's get the root layout and add our ImageView
GridLayout layout = (GridLayout) findViewById(R.id.gridLayout1);
layout.addView(image, params);
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/root" >
...
<GridLayout
android:id="#+id/gridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:columnCount="3"
android:rowCount="5" >
...
<ImageView
android:id="#+id/onetwo"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_columnSpan="1"
android:layout_rowSpan="1"
android:contentDescription="#string/gameboard"
android:gravity="center"
android:src="#drawable/tile" />
...
</GridLayout>
...
</RelativeLayout>
If you want to stack layout elements on top of each other then you should use a FrameLayout.
I ended up creating an imageview programmatically and then placing it inside the relative layout below on top of the imageview:
<GridLayout
android:id="#+id/gridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:columnCount="3"
android:rowCount="5" >
<RelativeLayout
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_columnSpan="1"
android:layout_rowSpan="1"
android:id="#+id/LLoneone">
<ImageView
android:id="#+id/oneone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/gameboard"
android:gravity="center"
android:src="#drawable/tile" />
</RelativeLayout>

ListView with text and image - can I change the XML (or parameters) if no image exists?

I have a list of articles in a listView with text on the left, and an image on the right. If the article doesn't have an image, I'd like the text to go full-width (as opposed to having a marginRight of 60dp).
My noob-java thought process is: I'm already looping through and checking if there's an image... (if there is one, I change which image shows up) within that loop, can I somehow use java to alter the XML of my view to add or remove the imageView and add or remove the margin?
The code I'm using to repeat through and alter the image:
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("", viewHolder.thumbView); //clears previous one
if(article.filepath != null && article.filepath.length() != 0) {
imageLoader.displayImage(
"http://img.mysite.com/processes/resize.php?image=" + article.filepath + "&size=100&quality=70",
viewHolder.thumbView
);
}
My "article_entry_list_adapter.xml":
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for individual news entries in a list -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<!-- Title of the news entry -->
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:textSize="13sp"
android:textStyle="bold"
android:textColor="#drawable/list_title_selector" android:typeface="serif"/>
<!-- Subtitle contains author and date -->
<TextView
android:id="#+id/article_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_alignLeft="#id/article_title"
android:layout_below="#id/article_title"
android:textSize="11sp"
android:textColor="#drawable/list_subtitle_selector" />
<com.sltrib.views.WebImageView
android:id="#+id/article_thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:adjustViewBounds="true"
android:layout_alignParentRight="true"
android:background="#color/super_light_gray"
android:padding="1dp"
android:scaleType="centerCrop" android:cropToPadding="true"/>
</RelativeLayout>
I would suggest changing your xml layout to something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<!-- Title of the news entry -->
<com.sltrib.views.WebImageView
android:id="#+id/article_thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:background="#color/super_light_gray"
android:cropToPadding="true"
android:padding="1dp"
android:scaleType="centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_toLeftOf="#id/article_thumbnail"
android:orientation="vertical" >
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Titol"
android:textColor="#drawable/list_title_selector"
android:textSize="13sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/article_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Subtitol"
android:textColor="#drawable/list_subtitle_selector"
android:textSize="11sp" />
</LinearLayout>
Then, when there is no image, just set the WebImageView visibility to GONE and the text will take the parent's width.
With your example:
viewHolder.thumbView.setVisibility(View.VISIBLE); //to show the image
viewHolder.thumbView.setVisibility(View.GONE); //to hide the image
You can either alter your viewGroup (setting visibility of your imageView to GONE and adding a margin programmatically) or choose to inflate another xml. As you want to do =)
Just while looping setVisiblity(VIEW.Gone) of article_thumbnail.
TextView textView = (TextView) findViewById(R.id.textView1);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
textView.setLayoutParams(llp);

Dynamically adding TextView to ScrollView on Android

I try to make a ScrollView to act like a ListView. So every row will be a TextView added dynamically. but the program crashes. I have a ScrollView inside a LinearLayout. What i do wrong? Thanks.
Here is my code
scroll = (ScrollView)findViewById(R.id.scrollView1);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);
layout.setBackgroundColor(Color.TRANSPARENT);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(HelloWorldActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tx[i].setText("This is the textviewNo" + i);
layout.addView(tx[i]);
}
scroll.addView(layout);
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/timeflag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:gravity="center"/>
<Button
android:id="#+id/pause"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeflag"
android:layout_marginTop="37dp"
android:text="Start" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
</RelativeLayout>
A scrollView Can only have one child. If you try to add more than 1 child to the scroll view then it will crash. You should have the LinearLayout inside of the scrollview and then dynamically add textViews to the linear layout.
You are trying to add LinearLayout1 as its own child's child (being your own grandfather, is a confusing notion).
Change you XML around like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
You cannot add views to a scrollview. You can only add them to a viewgroup, like linear or relativelayout.
It is not exactly clear what you are trying to achieve, but what is wrong with using a listview for this? You could set a max height on the listview. Or you could try wdziemia's suggestion

Categories

Resources