How to set Height Width of ImageView programmatically in android?
Asked Answered
S

3

16

How to increase or decrease the size of imageview,textview or anyother by programmatically in linear layout in getview() of slide menu.

I got slide menu through androidhive tutorials

Exactly in slide menu I need to add a user profile pic and his name ,location as like in facebook,google and other social sites for this tried a lot with (getlayoutparams(),setheight()...) even though with a lot of method but I can't set it in side menu list.

What ever method but no changes in the profile pic and name.

Here is my code:

mport android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NavDrawerListAdapter extends BaseAdapter {
ImageView imgIcon;
LinearLayout linearposition;
private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;

public NavDrawerListAdapter(Context context,
ArrayList<NavDrawerItem> navDrawerItems) {
this.context = context;
this.navDrawerItems = navDrawerItems;
}

@Override
public int getCount() {
return navDrawerItems.size();
}

@Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);

linearposition = (LinearLayout) convertView
.findViewById(R.id.linearposition);
}
NavDrawerItem label = navDrawerItems.get(position);
if (position == 0) {
newsection(linearposition, label);
}
imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
TextView txtCount = (TextView) convertView.findViewById(R.id.counter);

imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
txtTitle.setText(navDrawerItems.get(position).getTitle());

// displaying count
// check whether it set visible or not
if (navDrawerItems.get(position).getCounterVisibility()) {
txtCount.setText(navDrawerItems.get(position).getCount());
} else {
// hide the counter view
txtCount.setVisibility(View.GONE);
}

return convertView;
}

@SuppressLint("NewApi"
private void newsection(LinearLayout linearposition, NavDrawerItem label) {
// int height = 50;
// int width =50;
ImageView image = new ImageView(context);
// image.setImageResource(R.drawable.profile_circle);
// image.setPaddingRelative (5, 5, 5, 5);
// image.setMaxHeight(40);
// image.setMaxWidth(40);
// image.getLayoutParams().height = 20;
// image.requestLayout();
android.view.ViewGroup.LayoutParams layoutParams = image
.getLayoutParams();
layoutParams.width = 20;
layoutParams.height = 20;
image.setLayoutParams(layoutParams);
linearposition.addView(image);

// android.view.ViewGroup.LayoutParams layoutParams =
// image.getLayoutParams();
// LinearLayout.LayoutParams layoutParams = new
// LinearLayout.LayoutParams(0, 0);
// image.setLayoutParams(layoutParams);
// layoutParams.width = 30;
// layoutParams.height = 30;
// image.setLayoutParams(new LayoutParams(width,height));
// LinearLayout.LayoutParams layoutParams = new
// LinearLayout.LayoutParams(100, 100);
// iv.setLayoutParams(layoutParams);
// image.setLayoutParams(layoutParams);
// linearposition.addView(image);

}
}

Layout:

 <?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="48dp"
    android:background="@drawable/list_selector" >

    <LinearLayout
        android:id="@+id/linearposition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        androidrientation="vertical" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon"
        androidrc="@drawable/home_icon" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        androidaddingRight="40dp"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/list_item_title" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:background="@drawable/counter_bg"
        android:textColor="@color/counter_text_color" />

</RelativeLayout>

If anyone have have idea about this please guide me friends.

Spandrel answered 2/7, 2014 at 4:38 Comment(1)
I was facing the same issue but this solution helped me https://mcmap.net/q/63387/-set-imageview-width-and-height-programmaticallyCaledonian
E
31

To Set Width and Height Programmetically and make sure that you have not given fixed width and height to the parent layout of imageview

android.view.ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 80;
layoutParams.height = 80;
imageView.setLayoutParams(layoutParams);
Elgar answered 2/7, 2014 at 4:42 Comment(5)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="48dp" android:background="@drawable/list_selector"> <LinearLayout android:layout_width="60dp" android:layout_height="60dp" androidrientation="vertical" android:id="@+id/linearposition" /> </RelativeLayout>Spandrel
this my layout but no change.Spandrel
which layout is parent of your imageview ?Elgar
RelativeLayout is parent in that i tried to set a linear layout <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" androidrientation="vertical" android:id="@+id/linearposition" /> and insert imageview by codewise throught passing id referenceSpandrel
i'ad updated my code and layout plz see it and help me to fix it.Spandrel
W
1

This simple way to do your task:

setContentView(R.id.main);    
ImageView iv = (ImageView) findViewById(R.id.left);
int width = 60;
int height = 60;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);

and another way if you want to give screen size in height and width then use below code :

setContentView(R.id.main);    
Display display = getWindowManager().getDefaultDisplay();
ImageView iv = (ImageView) findViewById(R.id.left);
int width = display.getWidth(); // ((display.getWidth()*20)/100)
int height = display.getHeight();// ((display.getHeight()*30)/100)
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);

hope use full to you.

see my answer:-

Set ImageView width and height programmatically?

Wartburg answered 2/7, 2014 at 4:43 Comment(6)
i'ad tried with your answer already but no changes in size it default image_view.getLayoutParams().height = 20; if set this as (0)n also no the size of image remains same i was little bit confused that all these will work in slide menu layout or notSpandrel
@Spandrel on which lineWartburg
excatly on this line image_view.getLayoutParams().height = 20;Spandrel
i'm not creating imageview in XML passing it through code in linear layout to XML plz see my updated code.Spandrel
@Spandrel will you try my answer ?Wartburg
yes i'ad gone with your link answer but the above answer your passing imageview id in code created imageview in XMLSpandrel
C
0

Perhaps you could try setting the minHeight of the parent container (the LinearLayout in your case) to the height you would like for the image and set the height to WRAP_CONTENT?

Clarineclarinet answered 2/7, 2014 at 4:42 Comment(1)
can you give me any xml sample to try it outSpandrel

© 2022 - 2024 — McMap. All rights reserved.