Make Square images in Volley.NetworkingImageView - android

I have the following xml implementation, but i would like to make the images bigger and square. Sorry if my question is easy, please consider that I started a couple of weeks on Android development.
Grid.xml
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>
GridItem.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textColor="#android:color/white"
android:background="#55000000"/>
</FrameLayout>
Here is my adapter class code if needed:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
}
NetworkImageView imageView = (NetworkImageView) convertView.findViewById(R.id.picture);
TextView textView = (TextView) convertView.findViewById(R.id.text);
ImageRecord imageRecord = getItem(position);
imageView.setImageUrl(imageRecord.getUrl(),mImageLoader);
textView.setText(imageRecord.getTitle());
return convertView;
}

Start by setting a fixed size for your NetworkImageView:
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/picture"
android:layout_width="120dp"
android:layout_height="120dp"
android:scaleType="centerCrop"/>

Related

RecycleView does not scroll to the last items

I have a List View with Vertical Scroll, its item contains RecyclerView which will scroll Horizontally.
But when i scroll horizontal, RecyclerView does not allow me to scroll to the last item in the list (the picture below). What's happened with it? Even, i forced it scroll to the last item by setting linearLayoutManager.setStackFromEnd(true); it still does not work, only scroll to half of the 8th item, not the last item in list of 10
Edit: add codes:
list.xml
<RelativeLayout 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: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="com.phongponix.trackingbodybuilding.MainActivity$PlaceholderFragment">
<ListView android:id="#+id/lvExerciseList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="50dp"
></ListView>
</RelativeLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="20dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imgExcercisePhoto"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView android:text="aaa"
android:id="#+id/tvExerciseName"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvExcerciseRecords"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:layout_margin="10dp"
android:paddingRight="50dp"
android:scrollbars="horizontal">
</android.support.v7.widget.RecyclerView>
</TableRow>
</TableLayout>
</LinearLayout>
list_item_horizontal.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dp"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingRight="10dp">
<TextView
android:id="#+id/tvTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Code for List Adapter:
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
view.setTag(viewHolder);
recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(trackingPlanHorizontalAdapter);
recyclerView.setNestedScrollingEnabled(false);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (data.size() > 0) {
ExerciseModel exercise = (ExerciseModel) data.get(i);
viewHolder.title.setText(exercise.getTitle());
} else {
viewHolder.title.setText("No data");
}
return view;
}
Code for Recycle Adapter:
public TrackingPlanHorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_horizontal, parent, false);
return new TrackingPlanHorizontalViewHolder(itemView);
}
#Override
public void onBindViewHolder(TrackingPlanHorizontalViewHolder holder, int position) {
holder.textView.setText(horizontalList.get(position));
}
Yes i found your problem,there is problem regarding your list_item.xml change your list item with below code,
<?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"
android:padding="20dp">
<LinearLayout
android:id="#+id/first_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:src="#mipmap/ic_launcher"
android:id="#+id/imgExcercisePhoto"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="#+id/tvExerciseName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="aaa" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_toRightOf="#+id/first_panel"
android:id="#+id/rvExcerciseRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

setVisibility(View.Gone) work but the view still own the space

I make a CustomAdapter extends BaseAdapter.In it's getView() method , I use ViewHolder. And I set a clickListener with a TextView to set a view (call it A)gone and another view (call it B)visible , but when I click the TextView , the A GONE but it leave a space ,so the B cannot match the parent.
My code like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null){
convertView = mLayoutInflater.inflate(R.layout.customlayout,parent,false);
viewHolder = new ViewHolder();
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView = (TextView) convertView.findViewById(R.id.textview);
viewHoler.a = (LinearLayout) convertView.findViewById(R.id.a);
viewHoler.b = (LinearLayout) convertView.findViewById(R.id.b);
viewHolder.textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.a.getVisibility() == View.GONE){
viewHolder.b.setVisibility(View.GONE);
viewHolder.a.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}else {
viewHolder.a.setVisibility(View.GONE);
viewHolder.b.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
}
});
convertView.setTag(viewHolder);
return convertView;
}
the customlayout code is
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button a"/>
</LinearLayout>
<LinearLayout
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button b"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
It should be show like this way when I click the textView
but it always like another way below , just like the A view still take the space ,that like call setVisibility(View.INVISIBLE) or not setVisibility(View.GONE)
the A view is not appear because that although B view has disappeared but it still take the space
Why will it behave like that? How should I do to solve it ?
Thank you for your help.
Try with wrap_content on your LinearLayouts.
Like this -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button a"/>
</LinearLayout>
<LinearLayout
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button b"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Android add badge to list

I have a list,and I want to add badge to each item.I am using this library:https://github.com/jgilfelt/android-viewbadger
And this is my code:
static class ViewHolder {
TextView nameView;
ImageView imageView;
BadgeView badge;
}
public void placeRandomUsers(final String search) {
randomAdapter = new ArrayAdapter<JsonObject>(this, 0) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.random_bars, null);
holder = new ViewHolder();
holder.nameView=(TextView)convertView.findViewById(R.id.tweet);
holder.badge = new BadgeView(getContext(), holder.nameView);
holder.badge.setTextColor(Color.BLACK);
holder.badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
holder.imageView=(ImageView)convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position >= getCount() - 3 && search.equals("") == true) {
//loadRandomUsers("");
}
JsonObject user=getItem(position);
String name=user.get("name").getAsString();
String image_url="http://domain/photos/profile/thumb/"+user.get("photo").getAsString();
holder.nameView.setText(name);
Ion.with(holder.imageView)
.placeholder(R.drawable.twitter)
.load(image_url);
holder.badge.setText("1");
holder.badge.show();
return convertView;
}
};
ListView listView = (ListView)findViewById(R.id.list);
listView.setAdapter(randomAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Log.w("Pos","asd"+randomAdapter.getItem(position).get("name").getAsString());
startChat(randomAdapter.getItem(position));
}
});
loadRandomUsers(search);
}
And this is my random_bars.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/twitter"/>
<LinearLayout
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
style="#android:style/TextAppearance.Medium"
android:id="#+id/handle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/tweet"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
When I add badge to each item,nameView is not showing.Here the output:
If I remove the badge codes.Output is:
Here you can see.Image is showing but name is not showing when I use the badge class.What can be cause this ?
I changed my random_bars.xml code to this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/twitter"/>
<LinearLayout
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
style="#android:style/TextAppearance.Medium"
android:id="#+id/handle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/tweet"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Problem fixed.

why my GridView is showing only one column?

I don't see why I only get 1 column in my grid view no matter what I do.. frustrating.
activity's 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="vertical">
<GridView
android:id="#+id/gridView_album"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
for each item in the grid view (this is inflated in my adapter)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_albumTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="album title" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="105dp"
android:layout_height="100dp"
android:src="#drawable/abs__ab_share_pack_holo_light" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/abs__ab_solid_light_holo" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/abs__ab_solid_light_holo" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/abs__ab_solid_light_holo" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/abs__ab_solid_light_holo" />
</LinearLayout>
</LinearLayout>
and my adapter's getView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
JsonAlbum jsonAlbum = (JsonAlbum) getItem(position);
ArrayList<JsonAlbumImage> jsonAlbumImageArray = jsonAlbum.album_image_list;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.album_list_row, null);
}
TextView textView_albumTitle = (TextView) v.findViewById(R.id.textView_albumTitle);
textView_albumTitle.setText(jsonAlbum.title);
ImageView imageView1 = (ImageView)v.findViewById(R.id.imageView1);
ImageView imageView2 = (ImageView)v.findViewById(R.id.imageView2);
ImageView imageView3 = (ImageView)v.findViewById(R.id.imageView3);
ImageView imageView4 = (ImageView)v.findViewById(R.id.imageView4);
ImageView imageView5 = (ImageView)v.findViewById(R.id.imageView5);
ArrayList<ImageView> imageViewList = new ArrayList<ImageView>();
imageViewList.add(imageView1);
imageViewList.add(imageView2);
imageViewList.add(imageView3);
imageViewList.add(imageView4);
imageViewList.add(imageView5);
int imageCount = Math.min( imageViewList.size(), jsonAlbumImageArray.size());
for(int i=0; i < imageCount; ++i)
{
ImageView imageView = imageViewList.get(i);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
JsonAlbumImage jsonAlbumImage = jsonAlbumImageArray.get(i);
imageLoader.displayImage(jsonAlbumImage.url_image, imageView, options);
}
Log.i("debug", "width: "+ Integer.toString(v.getWidth()));
return v;
}
Hi This is my Gridview code
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:numColumns="2"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
My imageview code:
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="150px"
android:layout_height="150px"
android:layout_marginRight="50px"
android:src="#drawable/ic_launcher" >
</ImageView>
I am getting 2 columns..Lets u try for this attributes..
Maybe your images are too wide to take place in two columns on your screen?
BTW it's very expensive (memory) to create views each time - take a look on ViewHolder pattern.

android gridview fit the size in to screen

The gridview of my application has an image and a text below it for each grid.
I need to make sure that the complete list of grid fit in to the screen (no overflow at bottom).
Here is how i have defined my gridview
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:layout_below="#+id/text1view"
android:gravity="center"
android:background="#drawable/home_bg"
/>
and each grid item is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dip"
android:layout_margin="0dip"
>
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:padding="0dip"
android:gravity="center_horizontal"
android:layout_below="#+id/grid_item_image"
android:layout_alignParentBottom="true"
>
</TextView>
</LinearLayout>
I have tried several ways to set the grid view layout param, but i am not getting the right output. Please help me here. the gridview adapter code is
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
Log.i(TAG, holder.text1.toString());
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
int h = mContext.getResources().getDisplayMetrics().densityDpi;
convertView.setLayoutParams(new GridView.LayoutParams(h-50, h-20));
//holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
//holder.image.setScaleType(ImageView.ScaleType.FIT_XY); ......
Try this:-
grid_item.xml
<?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">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center">
<ImageView android:id="#+id/imgIcon" android:src="#drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView android:text="TextView" android:id="#+id/txtDesc" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/mobGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
try
android:layout_width="fill_parent"
android:layout_height="fill_parent"
for your GridView instead of wrap_content!

Categories

Resources