Dynamically Setting Size of ImageView Android - android

I am using the following method to dynamically create Bitmaps from Drawables and asign them to my ImageViews. This method is called repeatedly. The problem is that I cannot set the size of the Images properly, on different phones it looks different, on my Note 3 it looks big, on my friends galaxy core it looks small.
METHOD
public void setImageView(int i,Integer d, LinearLayout layout ) {
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
imageView.setMaxHeight(200);
imageView.setMaxWidth(200);
imageView.setAdjustViewBounds(true);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
parms.setMargins(15, 50, 15, 25);
imageView.setLayoutParams(parms);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), d));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
layout.addView(imageView);
}
USAGE OF METHOD
horizontal_scroll.setVisibility(View.VISIBLE);
LinearLayout layout = (LinearLayout) findViewById(R.id.scroll_view_layout);
setImageView(1, R.drawable.hex_filled, layout);
setImageView(2, R.drawable.hex_filled, layout);
setImageView(3, R.drawable.hex_filled, layout);
setImageView(4, R.drawable.hex_filled, layout);
setImageView(5, R.drawable.hex_filled, layout);
setImageView(6, R.drawable.hex_filled, layout);
setImageView(7, R.drawable.hex_filled, layout);
LAYOUT
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back"
android:orientation="vertical"
android:id="#+id/main_layout"
android:clipChildren="false"
android:weightSum="100"
tools:context=".City_Search">
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:background="#drawable/white_lines"
android:layout_weight="15"
android:layout_height="0dp" >
<LinearLayout
android:id="#+id/scroll_view_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="85"
android:weightSum="100"
android:id="#+id/linear_layout_listview"
android:orientation="vertical"
>
<ListView
android:layout_width="fill_parent"
android:layout_weight="90"
android:id="#+id/followed_cities"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_height="0dp"></ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="10"
android:orientation="vertical"
android:layout_gravity="center"
android:id="#+id/scroll_up"
android:gravity="center"
android:layout_height="0dp">
<ImageView
android:layout_width="30dp"
android:src="#drawable/hex_filled"
android:layout_height="30dp" />
</LinearLayout>
</LinearLayout>

have you tried different scale type.
from google documentation:
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
CENTER_INSIDE Scale the image uniformly (maintain the image's aspect
ratio) so that both dimensions (width and height) of the image will be
equal to or less than the corresponding dimension of the view (minus
padding). The image is then centered in the view.

Related

Android create rectangle with four different colors

How do I create a colored Rectangle like above one. Is it possible to control the length of each colored rectangle from java code? (above figure has a single rectangle with 4 colored inside it). I've been searching for answers since a few hours & was unable to find a solution.
EDIT:
I'm using AppCompatActivity and here's content_main.xml & activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:id="#+id/parentis"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#dc3838"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#c4c11f"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#2c64e7"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#a11a7d"/>
</LinearLayout>
</RelativeLayout>
activity_main.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
Finally java code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
View child1= new View(this);
View child2= new View(this);
View child3= new View(this);
View child4= new View(this);
LinearLayout parent = (LinearLayout) findViewById(R.id.parentis);
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.15f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.15f;
child2.setLayoutParams(childParam2);
LinearLayout.LayoutParams childParam3 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam3.weight = 0.20f;
child3.setLayoutParams(childParam3);
LinearLayout.LayoutParams childParam4 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam4.weight = 0.50f;
child4.setLayoutParams(childParam4);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
parent.addView(child3);
parent.addView(child4);
}
you can display colored rectangles on the screen by simply setting the backgroundcolor of imageviews:
imageview.SetBackground(R.color.mycolor);
Then you just need to put 4 of those ImageViews inside a LinearLayout and change their size programmatically.
first, you should get the screen size:
private Point getScreenSize(){
WindowManager windowManager = (WindowManager) YamsaferApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
}
then write the following code:
int width = getScreenSize().x;
int redWidth = width / 4;
int orangeWidth = width / 4;
int greenWidth = width / 4;
int cyanWidth = width / 4;
LinearLayout container = new LinearLayout(getContext());
container.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT,70);
container.setLayoutParams(params);
// create the red area:
View redView = new View(getContext());
redView.setBackgroundColor(Color.RED);
redView.setLayoutParams(new LinearLayout.LayoutParams(redWidth,70));
container.addView(redView);
// create the orangeArea:
View orangeView = new View(getContext());
orangeView.setBackgroundColor(Color.RED);
orangeView.setLayoutParams(new LinearLayout.LayoutParams(orangeWidth,70));
container.addView(orangeView);
//create the green area
View greenView = new View(getContext());
greenView.setBackgroundColor(Color.RED);
greenView.setLayoutParams(new LinearLayout.LayoutParams(greenWidth,70));
container.addView(greenView);
create the cyan area:
View cyanView = new View(getContext());
cyanView.setBackgroundColor(Color.RED);
cyanView.setLayoutParams(new LinearLayout.LayoutParams(cyanWidth,70));
cyanainer.addView(cyanView);
If you would like to have a simpler layout with just one view and be able to change the sizes of the rectangles dynamically you can create a simple custom view. Override the onDraw(Canvas canvas) method and inside you can do something like this:
int rectWidth = getWidth() / 4;
canvas.drawRect(0, 0, rectWidth, getHeight(), redPaint);
canvas.drawRect(rectWidth, 0, 2 * rectWidth, getHeight(), orangePaint);
canvas.drawRect(2 * rectWidth, 0, 3 * rectWidth, getHeight(), greenPaint);
canvas.drawRect(3 * rectWidth, 0, getWidth(), getHeight(), pinkPaint);
You can create paints like this:
redPaint = new Paint();
redPaint.setColor(*Your red color here*);
If you need the black border around the smaller rectangles you can just add some margin to the drawRect(...) calls above and add canvas.drawARGB(255,0,0,0) above them. For round rectangles you can use the drawRoundRect(...) methods.
use this for same size rectangle .
rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#dc3838"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#c4c11f"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#2c64e7"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#a11a7d"/>
</LinearLayout>
and this different size rectangle....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="80dp"
android:layout_height="100dp"
android:background="#dc3838"/>
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#c4c11f"/>
<View
android:layout_width="50dp"
android:layout_height="100dp"
android:background="#2c64e7"/>
<View
android:layout_width="90dp"
android:layout_height="100dp"
android:background="#a11a7d"/>
</LinearLayout>
output like that .......
Note:-set id to each one and change its size using LayoutPrams....
Take a linear layout with android:weightsum="1" .Then create 4 different views inside the linear layout with android:weight=".25"
Set your desired color for each views.
Do not forget to set the orientation of linear layout as horizontal
Sample code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
</LinearLayout>
This will distribute each color equally.If you not want to distribute each color equally then you can redistribute weight for each views.
To set the view length consisting each color:
First create a blank linear layout in your xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:weightSum="1"
android:id="#+id/ll_parent"
android:orientation="horizontal">
</LinearLayout>
Then in the java file:
LinearLayout parent=(LinearLayout)findViewById(R.id.ll_parent)‌​;
View child1= new View(this);
View child2= new View(this);
View child3= new View(this);
View child4= new View(this);
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.25f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.25f;
child2.setLayoutParams(childParam2);
LinearLayout.LayoutParams childParam3 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam3.weight = 0.25f;
child3.setLayoutParams(childParam3);
LinearLayout.LayoutParams childParam4 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam4.weight = 0.25f;
child4.setLayoutParams(childParam4);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
parent.addView(child3);
parent.addView(child4);

How to set GridView row height?

I have a GridView, and setting the number of columns and the overal width of the grid layout. Need items with 66dp x 66dp size. But somehow items are squared, but smaller the 66x66. What am I forget?
int zz = .. // number of columns comes from outside
gridview.setNumColumns(zz);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)gridview.getLayoutParams();
linearParams.width=66*zz;
gridview.setLayoutParams(linearParams);
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:columnWidth="66dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="horizontal"
android:stretchMode="none"
android:verticalSpacing="0dp">
</GridView>
item prototype
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="66dp" android:layout_height="66dp">
<ImageView
android:id="#+id/icon"
android:layout_width="66dp"
android:layout_height="66dp"
android:padding="0dp"
android:background="#android:color/holo_orange_light"/>
</RelativeLayout>
shreen shot
You can Use This..
int width = 66*zz;
view.setLayoutParams(new GridView.LayoutParams(GridView.width, YOUR_HEIGHT));
LinearLayout.LayoutParams require value in pixel not dp
int gridItemWithInDp = 66;
int gridItemWithInPx = (int) (gridItemWithInDp * Resources.getSystem().getDisplayMetrics().density);
linearParams.width=gridItemWithInPx *zz;
gridview.setLayoutParams(linearParams);
You should set the width of gridview in dp either from code or from declared into resource xml
from code:
final float density = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) ((66 * zz) * density + 0.5f);
or from xml
int pixels = getResources().getDimensionPixelSize(R.dimen.example_dimen);
gridview.setNumColumns(zz);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)gridview.getLayoutParams();
linearParams.width=pixels;
gridview.setLayoutParams(linearParams);
Set item prototype width and height MATCH_PARENT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:background="#android:color/holo_orange_light"/>
</RelativeLayout>

Android layout scaling (for elements of a given size in dp)

Three FrameLayout's of different background colors to form a strip.
Each FrameLayout has the size specified in dp.
[
The strip is long and does not fit on the screen.
How to scale the strip to maintain the aspect ratio and without changing the dimensions dp?
[
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:measureAllChildren="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:rotation="0">
<FrameLayout
android:layout_width="200dp"
android:layout_height="42dp"
android:background="#508da6"
/>
<FrameLayout
android:layout_width="400dp"
android:layout_height="42dp"
android:background="#494949"
/>
<FrameLayout
android:layout_width="240dp"
android:layout_height="42dp"
android:background="#ff0000"
/>
</LinearLayout>
</FrameLayout>
There is no need to inflate three whole FrameLayout just for that.View will do just fine. Also you should not give fixed widths since there is no way to make it work for all screen sizes.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:measureAllChildren="true">
<LinearLayout
android:id="#+id/strip"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layoutDirection="ltr"
android:rotation="0">
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#508da6"
/>
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#494949"
/>
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
/>
</LinearLayout>
</FrameLayout>
set height on runtime
LinearLayout stripLayout = (LinearLayout)findVieById(R.id.strip);
stripLayout .getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() {
#Override
public void onGlobalFocusChanged(View oldFocus, View newFocus) {
// TODO Auto-generated method stub
int width = stripLayout .getWidth();
int height = //calculate your height//
FrameLayout.LayoutParams params = stripLayout .getLayoutParams();
params.height = height;
stripLayout .setLayoutParams(params);
if (Build.VERSION.SDK_INT < 16) { stripLayout .getViewTreeObserver().removeGlobalOnLayoutListener(this); }
else { stripLayout .getViewTreeObserver().removeOnGlobalLayoutListener(this); }
}
});

Images in ViewPager don't fill the screen

I am receiving a set of URL images from API to show as slider so I am using viewpager to do my job and when I am trying to set Images in viewpager to fill screen according to imagesize this doesn't happen at all.
All the red line is huge spacing.. I am not sure why this there?
I am trying do the following in XML:
<RelativeLayout
android:id="#+id/pagerIndicator"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/searchlayout"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:orientation="vertical">
<TextView
android:id="#+id/noData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/nointernet"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="italic"
android:visibility="gone" />
<android.support.v4.view.ViewPager
android:id="#+id/myviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<FrameLayout
android:id="#+id/progressContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_centerVertical="true">
<com.ylg.librarys.ProgressWheel xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:id="#+id/progress_wheel"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:visibility="visible"
app:matProg_barColor="#color/colorPrimary"
app:matProg_progressIndeterminate="true" />
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/pagerLayoutIndicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/pagerIndicator"
android:layout_marginBottom="5dp"
android:layout_marginTop="2dp">
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:fillColor="#color/orangeText"
app:pageColor="#color/whitetext"
app:strokeColor="#color/orangeText" />
</RelativeLayout>
PageAdapter looks like this:
#Override
public Object instantiateItem(ViewGroup container, int position)
{
progressContainer.setVisibility(View.GONE);
wheel_layout.setVisibility(View.GONE);
noData = (TextView)rootView.findViewById(R.id.noData);
noData.setVisibility(View.GONE);
ImageView imageView = new ImageView(getActivity());
imageLoader.DisplayImage(bannerItems.get(position).getbannerImage(), 0, imageView);
LayoutParams imageParams = new LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(imageParams);
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);
layout.addView(imageView);
final int page = position;
layout.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),
"Page " + page + " clicked",
Toast.LENGTH_LONG).show();
}});
container.addView(layout);
return layout;
}
Strangely If I don't give the height for the relativeLayout in which viewpager is sitting I don't see the image at all. that is If I set:
<RelativeLayout
android:id="#+id/pagerIndicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/searchlayout"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:orientation="vertical">
I only get PageIndicator. Above ViewPager with image is not shown at all.
Not sure why this happening? How do I set the image match_parent so that it shows full without any gap?
Try setting imageView's ScaleType .
For Example,
imageView.setScaleType(ScaleType.FIT_XY);
try also other ScaleTypes,
ScaleType.CENTER
ScaleType.CENTER_INSIDE
ScaleType.FIT_CENTER
Hope this work.
Add this in your instantiateItem method
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
This will fill the whole screen with image removing whitespaces.
You can also try other methods
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
There can't be fulfill your need.if a view pager contains an imageview, it needs a fixed height and width to imageview.you can set the scaletype as fitStart or fitEnd to make the gap before the image or after the image.it will solve a little gap problem.
Thank you.

How to set layout margin dynamically on android

my app contains 3 imageviews and i used a textview as a place holder,here is my layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#layout/back"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SplasActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=" " />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/list_sher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/list_shaer" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/list_fav" />
then i used below codes to change image sizes,it works fine but when i set margins for my textview, everything goes crazy. textview stays but i cant see any imageview !
private void setsize(){
// this codes work fine //
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w=dm.widthPixels;
int h=dm.heightPixels;
int h2=(h/7);
int w2=w-((w*10)/100);
int h3=(h/20)+1;
LinearLayout.LayoutParams pram=new LinearLayout.LayoutParams(w2,h2);
img1.setLayoutParams(pram);
img2.setLayoutParams(pram);
img3.setLayoutParams(pram);
// the problem is here //
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0, h3, 0, 0);
ll.addView(txt1,layoutParams);
}
The second parameter of method LayoutParams() should be WRAP_CONTENT, represent height of the layout, and MATCH_PARENT means that the view wants to be as big as its parent, that cause your images disappear:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

Categories

Resources