i try to make my image view position dynamic base on image height. but i stuck to get the image height from drawable.. help me to solve it thanks..
this is my code :
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_wall, null, true);
TextView txtProfileUser = (TextView) rowView.findViewById(R.id.txtWallProfile);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtWallContentTitle);
TextView txtContent = (TextView) rowView.findViewById(R.id.txtWallContent);
ImageView imgWallProfile = (ImageView) rowView.findViewById(R.id.ivWallProfile);
ImageView imgWallContent = (ImageView) rowView.findViewById(R.id.ivWallContent);
txtTitle.setText(web2[position]);
txtContent.setText(web3[position]);
txtProfileUser.setText(web[position]);
imgWallProfile.setImageResource(imageId[position]);
imgWallContent.setImageResource(imageId2[position]);
GetSize gz = new GetSize();
Integer h = gz.getImageSize(imageId[position]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(20,h, 0, 0);
imgWallProfile.setLayoutParams(lp);
return rowView;
}
and this my getImageSize method :
public Integer getImageSize(Integer d){
Drawable bd = getResources().getDrawable(d);
int height=bd.getIntrinsicHeight();
return height;}
If I understand correctly you want to get the width and height of your image view instead of the bitmaps width and height. Layout parameters allows you to access the imageView and view and modify it's parameters.
public int getImageSize(View myView) {
RelativeLayout.LayoutParams myParams = (RelativeLayout.LayoutParams) myView.getLayoutParams();
return myParams.height;
}
Related
I have a proper list view which has an adapter that extends BaseAdapter.
Everything works fine, but I need a margin of 30 dp for index 0. How to achieve this?
I tried the below 2 code but it crashes.
View subView = createButtonView((MenuButton) getItem(position));
if(position==0){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)subView.getLayoutParams();
layoutParams.setMargins(30, 0, 0, 0);
subView.setLayoutParams(layoutParams);
}
return subView;
public View getSubView(int position){
View subView = createButtonView((MenuButton) getItem(position));
if(position==0){
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
subView.setLayoutParams(layoutParams);
}
return subView;
}
Also I cannot use a headerView because the child view might not always be a same view, its will be dynamic.
simple alternative is use
SpaceView with 30dp width in xml( space view is very light view in android)
and adjust the visiblity for positions
if (position == 0)
yourxmlSpaceView.setVisibility(View.VISIBLE);
else
yourxmlSpaceView.setVisibility(View.GONE);
for 30dp you can use this method to convert int to pixels
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = context.getResources()
.getDisplayMetrics();
int px = Math.round(dp
* (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
In the adapter class check for position zero and specify your layout parameters inside that if clause and add it to the convertView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mcontext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.adapter_main, convertView, false);
}
if(position == 0){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)subView.getLayoutParams();
layoutParams.setMargins(30, 0, 0, 0);
convertView.setLayoutParams(layoutParams);
}
return view;
}
your listview can add a view to fake space with height is 30dp.
View:
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/fake_view"
/>
adapter:
public View getView(int position, View v, ViewGroup parent){
....
View fakeView = v.findViewById(R.id.fake_view);
fakeView.setVisibility(position == 1? View.VISIBLE : View.GONE)
....
}
I'm creating a FrameLayout and then I add two Views (an ImageView and a TextView ).
My code on getView() :
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView textView1;
int new_width = wid/2;
int textview_id=0;
FrameLayout frame_layout = new FrameLayout(mContext); // Instantiate the parent
frame_layout.setLayoutParams(new GridView.LayoutParams(android.widget.FrameLayout.LayoutParams.MATCH_PARENT,
android.widget.FrameLayout.LayoutParams.MATCH_PARENT));
textView1 = new TextView(mContext); textView1.setId(position);
textView1.setText("Set Wallpaper");
textView1.setTextColor(mContext.getResources().getColor(R.color.set_wallpaper_colors));
textView1.setVisibility(View.INVISIBLE);
textView1.setTypeface(textView1.getTypeface(), Typeface.BOLD);
FrameLayout.LayoutParams textview_params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
textview_params.gravity = Gravity.CENTER;
FrameLayout.LayoutParams imageview_params = new FrameLayout.LayoutParams(
new_width -5, new_width -5 );
imageView = new ImageView(mContext);
//imageView.setLayoutParams(new GridView.LayoutParams(new_width - 5, new_width -5));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
imageView.setImageResource(mThumbIds[position]);
frame_layout.addView(imageView, imageview_params);
frame_layout.addView(textView1, textview_params);
return frame_layout;
}
How should I change it to use the ViewHolder class. There isn't a layout to inflate.
With your code it should be something like this
static class ViewHolder {
TextView textView1;
ImageView imageView1;
}
#Override
public View getView(final int row, View convertView, final ViewGroup parent) {
ViewHolder holder = null;
FrameLayout frame_layout = (FrameLayout) convertView;
if (frame_layout == null) {
holder = new ViewHolder();
holder.textView1 = new TextView(mContext);
holder.imageView1 = new ImageView(mContext);
frame_layout.setTag(holder);
}
else {
holder = (ViewHolder) frame_layout.getTag();
}
holder.textView1.setText("Set Wallpaper");
holder.textView1.setTextColor(mContext.getResources().getColor(R.color.set_wallpaper_colors));
holder.textView1.setVisibility(View.INVISIBLE);
holder.textView1.setTypeface(holder.textView1.getTypeface(), Typeface.BOLD);
FrameLayout.LayoutParams textview_params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
textview_params.gravity = Gravity.CENTER;
FrameLayout.LayoutParams imageview_params = new FrameLayout.LayoutParams(
new_width -5, new_width -5 );
holder.imageView1.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView1.setPadding(0, 0, 0, 0);
holder.imageView1.setImageResource(mThumbIds[position]);
frame_layout.addView(holder.imageView1, imageview_params);
frame_layout.addView(holder.textView1, textview_params);
return frame_layout;
}
I'm creating an app which should list some snowboard tricks and when one clicks on a trick, a popup should appear with information on the trick and how to do it (with text and maybe a youtube vid).
How does one realise this?
Here is a code for Creating a custom list adapter with a popup window that pop up when you press a certain image in the ListView row:
Here is the Adapter:
public class tasksRepositoryAdapter extends ArrayAdapter<Task>
{
private ArrayList<Task> list;
public tasksRepositoryAdapter(Context context, int textViewResourceId, List<Task> tasksRepository)
{
super(context, textViewResourceId, tasksRepository);
this.list = new ArrayList<Task>();
for (Task task : tasksRepository)
{
this.list.add(task);
}
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row;
final ViewHolder holder = new ViewHolder();
tfRobotoRegular = Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf");
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflator.inflate(R.layout.new_row, null);
holder.tvTitle = (TextView) row.findViewById(R.id.text_title);
String title = tasksRepository.get(position).getTitle();
if (title.length()>25)
{
title = title.substring(0, 24);
title = title + "...";
}
holder.tvTitle.setText(title);
holder.tvTitle.setTypeface(tfRobotoRegular);
holder.tvDate = (TextView) row.findViewById(R.id.text_date);
holder.tvDate.setText(tasksRepository.get(position).getDate());
holder.tvDate.setTypeface(tfRobotoRegular);
holder.tvTime = (TextView) row.findViewById(R.id.text_time);
holder.tvTime.setText(tasksRepository.get(position).getTime());
holder.tvTime.setTypeface(tfRobotoRegular);
holder.tvDescription = (TextView) row.findViewById(R.id.text_description);
String description = tasksRepository.get(position).getDescription();
if (description.length()>46)
{
description = description.substring(0, 45);
description = description + "...";
}
holder.tvDescription.setText(description);
holder.tvDescription.setTypeface(tfRobotoRegular);
holder.tvId = (TextView) row.findViewById(R.id.text_id);
holder.tvId.setText(String.valueOf(tasksRepository.get(position).getId()));
holder.tvId.setTypeface(tfRobotoRegular);
holder.tvLocation = (TextView) row.findViewById(R.id.text_location);
holder.tvLocation.setText(tasksRepository.get(position).getCity());
holder.llRowLayout = (LinearLayout) row.findViewById(R.id.llRowLayout);
holder.imCalendar = (ImageView) row.findViewById(R.id.iCalendar);
holder.imClock = (ImageView) row.findViewById(R.id.iClock);
holder.imLocation = (ImageView) row.findViewById(R.id.iLocation);
holder.imTaskStatusButton = (ImageView) row.findViewById(R.id.iTaskStatusButton);
holder.imTaskStatusButton.setTag(position);
holder.imTaskStatusButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
int[] location = new int[2];
currentRowId = position;
currentRow = v;
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
v.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
point = new Point();
point.x = location[0];
point.y = location[1];
showStatusPopup(TasksListActivity.this, point);
}
});
String status = tasksRepository.get(position).getStatus();
Log.d(TAG, "The status of the current row: "+ status );
setStatusColorImages(status, holder.imClock, holder.imCalendar, holder.imLocation, holder.llRowLayout);
return row;
}
}
Here is the ViewHolder:
static class ViewHolder
{
RelativeLayout rlTitle;
LinearLayout llRowLayout;
TextView tvId;
TextView tvTitle;
TextView tvDate;
TextView tvTime;
TextView tvDescription;
TextView tvLocation;
ImageView imClock;
ImageView imCalendar;
ImageView imLocation;
ImageView imTaskStatusButton;
}
And the PopUp Window:
// The method that displays the popup.
private void showStatusPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);
// Creating the PopupWindow
changeStatusPopUp = new PopupWindow(context);
changeStatusPopUp.setContentView(layout);
changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setFocusable(true);
// Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
int OFFSET_X = -20;
int OFFSET_Y = 50;
//Clear the default translucent background
changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
Hello everyone I have a problem.
I want to do a GridView with a Custom Loyout so i used the layoutInflater and i did this:
private ImageView prima;
private ImageView seconda;
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
prima = (ImageView) v.findViewById(R.id.imageView1);
prima.getLayoutParams().height = width / 3;
prima.getLayoutParams().width = width / 3;
seconda = (ImageView) v.findViewById(R.id.imageView2);
seconda.getLayoutParams().height = width / 3;
seconda.getLayoutParams().width = width / 3;
v.setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
v.setPadding(0, 0, 0, 0);
} else {
v = convertView;
}
prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....
return v;
};
When i run my application the image are arranged random and there are many black space with no images.
What i did wrong ?
Your View and convertView are not linked.
Try with ViewHolder concept like this:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
convertView = li.inflate(R.layout.icon, null);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
v = new ViewHolder();
v.prima = (ImageView) convertView .findViewById(R.id.imageView1);
v.prima.getLayoutParams().height = width / 3;
v.prima.getLayoutParams().width = width / 3;
v.seconda = (ImageView) convertView .findViewById(R.id.imageView2);
v.seconda.getLayoutParams().height = width / 3;
v.seconda.getLayoutParams().width = width / 3;
convertView .setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
convertView .setPadding(0, 0, 0, 0);
convertView.setTag(v);
} else {
v = (ViewHolder)convertView.getTag();
}
v.prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....
//v.seconda
return convertView;
};
class ViewHolder{
ImageView prima;
ImageView seconda;
}
You forgot to apply images to
seconda = (ImageView) v.findViewById(R.id.imageView2);
prima.setImageResource(....);
I'm trying to surround the top and bottom of my ImageView in a Gallery with a layout containing a tiled background. The top part seems to be working, but the bottom layout doesn't show. Any ideas why? Here's my getView():
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout container = new RelativeLayout(galleryContext);
RelativeLayout topReel = new RelativeLayout(galleryContext);
RelativeLayout bottomReel = new RelativeLayout(galleryContext);
topReel.setBackgroundResource(R.drawable.film_top);
bottomReel.setBackgroundResource(R.drawable.film_bottom);
ImageView imageView = null;
if (convertView != null) { //we can reuse the view!
imageView = (ImageView) convertView;
} else {
imageView = new ImageView(galleryContext); //boo we have to make a new view
}
//Get a scaled Bitmap so that it doesn't use up all our memory
Bitmap setBitmap = loadScaledBitmap(imageList[position].getAbsolutePath(), imageSizeInPixels);
//set up our ImageView inside the gallery to display our Bitmap...
imageView.setImageBitmap(setBitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(galleryBackground);
RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(imageSizeInPixels, 20);
topParams.addRule(RelativeLayout.ABOVE, imageView.getId());
topParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(imageSizeInPixels, 20);
bottomParams.addRule(RelativeLayout.BELOW, imageView.getId());
bottomParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(imageSizeInPixels, imageSizeInPixels);
imageParams.addRule(RelativeLayout.BELOW, topReel.getId());
imageParams.addRule(RelativeLayout.ABOVE, bottomReel.getId());
imageParams.addRule(RelativeLayout.CENTER_VERTICAL);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
container.addView(topReel, topParams);
container.addView(bottomReel, bottomParams);
container.addView(imageView, imageParams);
return container;
}
This solves the problem, hopefully someone can use this:
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout container = new RelativeLayout(galleryContext);
ImageView imageView = null;
if (convertView != null) { //we can reuse the view!
imageView = (ImageView) convertView;
} else {
imageView = new ImageView(galleryContext); //boo we have to make a new view
}
//Get a scaled Bitmap so that it doesn't use up all our memory
Bitmap setBitmap = loadScaledBitmap(imageList[position].getAbsolutePath(), ((int)(imageSizeInPixels * .75)));
//set up our ImageView inside the gallery to display our Bitmap...
imageView.setImageBitmap(setBitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundColor(Color.BLACK);
RelativeLayout borderImg = new RelativeLayout(galleryContext);
borderImg.setPadding(10, 5,10, 5);
borderImg.setBackgroundColor(0xff000000);
borderImg.addView(imageView);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(imageSizeInPixels, imageSizeInPixels);
imageParams.addRule(RelativeLayout.CENTER_VERTICAL);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
container.setBackgroundResource(R.drawable.repeat_reel);
container.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels+40));
container.addView(borderImg, imageParams);
return container;
}