Image height stretched (SimpleDraweeView) - android

Recently I'm playing around with some popular android libs, and I need a help with StaggeredGridLayout & Fresco.
Let me show you what I'm getting wrong
As you can see, these images height is streched
I've tried changing layout_height params in XML/via java, but I'm not getting good results.
Everything is up on my Github repo
Some snippets
public class GiphyView
extends RecyclerView
implements GiphyPresenter.ViewBind {
#Inject
public GiphyPresenter mGiphyPresenter;
private GiphyAdapter mAdapter;
public GiphyView(Context context) {
super(context);
initView(context);
}
public GiphyView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public GiphyView(Context context, #Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
private void initView(Context context) {
CustomApplication.get(context).getGiphyComponent().inject(this);
mAdapter = new GiphyAdapter();
setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
setAdapter(mAdapter);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mGiphyPresenter.attachVu(this);
}
#Override
protected void onDetachedFromWindow() {
ImagePipeline imagePipeline = Fresco.getImagePipeline();
imagePipeline.clearCaches();
mGiphyPresenter.detachVu();
super.onDetachedFromWindow();
}
#Override
public void notifyRangeInserted(int start, int count) {
mAdapter.notifyItemRangeInserted(start, count);
}
private class GiphyAdapter extends RecyclerView.Adapter<GiphyViewHolder> {
#Override
public GiphyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new GiphyViewHolder(
LayoutInflater.from(parent.getContext())
.inflate(R.layout.giphy_cell, parent, false));
}
#Override
public void onBindViewHolder(GiphyViewHolder holder, int position) {
holder.setGif(mGiphyPresenter.getItemImage(position));
}
#Override
public int getItemCount() {
return mGiphyPresenter.getSize();
}
}
class GiphyViewHolder extends ViewHolder {
#BindView(R.id.gif)
SimpleDraweeView mGif;
GiphyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
void setGif(String url) {
/*ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
.setRotationOptions(RotationOptions.autoRotate())
.setLowestPermittedRequestLevel(ENCODED_MEMORY_CACHE)
.build();*/
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(url)
.setAutoPlayAnimations(true)
//.setImageRequest(request)
.build();
mGif.setController(controller);
}
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="6dp"
card_view:cardUseCompatPadding="true">
<!-- 4:3 aspect ratio
fresco:viewAspectRatio="1.33" -->
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/gif"
android:layout_width="match_parent"
android:layout_height="match_parent"
fresco:failureImage="#drawable/ic_error_black_24dp"
fresco:placeholderImage="#drawable/ic_android_black_24dp"/>
</android.support.v7.widget.CardView>
PSA: I've already tried to play around with wrap_content, but got no luck (yes, I know Fresco doesn't support it).

I guess you need to set the dimensions of the parent card view to wrap_content and you do not need the parent LinearLayout. Just use something like:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="6dp"
card_view:cardUseCompatPadding="true">
<com.facebook.fresco.SimpleDraweeView ... />
</android.support.v7.widget.CardView>
I have added a sample app that shows how to use CardView in a recycler view with a grid layout here: https://github.com/facebook/fresco/blob/master/samples/showcase/src/main/java/com/facebook/fresco/samples/showcase/drawee/DraweeRecyclerViewFragment.java

Related

How to populate data in AutoCompleteTextView?

I have to show two section in AutoCompleteTextView (Something like this):
I have created a custom layout which have two CardViews and each CardView have three TextViews. Right now I am not distributing the section on the basis of type. The whole data is loaded into one section.
Activity
final AutocompleteLocalityAdapter adapterLocalities = new AutocompleteLocalityAdapter(context,
R.layout.support_simple_spinner_dropdown_item, new ArrayList<Locality>());
AutocompleteLocalityAdapter
public class AutocompleteLocalityAdapter extends ArrayAdapter<Locality> {
public AutocompleteLocalityAdapter(Context context, int layout, List<Locality> localities) {
super(context, layout, localities);
this.localities = localities;
updateList("");
}
In updateList method I am making a new network call to fill the data in Locality class.
What do I need to do to categories the search result as per given image? ArrayAdapter is not going to work here for sure.
The possible solution I am thinking here is:
Replace ArrayAdapter to RecyclerViewAdapter.
Any hint will be appreciable.
The possible makeshift to this solution is PopUpWindow. Inside the PopUpWindow I put two RecyclerView and populate them through network calls.
dashboard_profile_popup_window
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/margin_low"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/locationCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="#dimen/corner_radius"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/landmark"
android:textStyle="bold" />
<ListView
android:id="#+id/localityView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/landmarkCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="#dimen/corner_radius"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/location"
android:textStyle="bold" />
<ListView
android:id="#+id/landmarkView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
CustomWidget
public class CustomAutoCompleteView extends EditText {
private Context context;
TextListViewAdapter locationAdapter;
TextListViewAdapter landmarkAdaper;
PopupWindow pwindow;
ClickListener clickListener;
public CustomAutoCompleteView(Context context) {
super(context);
this.context = context;
setCustomization();
}
public void closeWindow(){
pwindow.dismiss();
}
public CustomAutoCompleteView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setCustomization();
}
public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
setCustomization();
}
public void updateList(List<LocalityEntity> locationList, List<LocalityEntity> landmarkList) {
if (pwindow == null) {
initPopupWindow();
}
locationAdapter.updateList(locationList);
landmarkAdaper.updateList(landmarkList);
}
public void initPopupWindow() {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.dashboard_profile_popup_window, null);
ListView landmarkRecyclerView = (ListView) layout.findViewById(R.id.localityView);
ListView localityRecyclerView = (ListView) layout.findViewById(R.id.landmarkView);
landmarkRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = ((TextView) view.findViewById(R.id.localityText)).getText().toString();
String gid = ((TextView) view.findViewById(R.id.localityGID)).getText().toString();
clickListener.placeSelected(gid);
}
});
localityRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = ((TextView) view.findViewById(R.id.localityText)).getText().toString();
String gid = ((TextView) view.findViewById(R.id.localityGID)).getText().toString();
clickListener.placeSelected(gid);
}
});
landmarkRecyclerView.setAdapter(landmarkAdaper);
localityRecyclerView.setAdapter(locationAdapter);
pwindow = new PopupWindow(context);
pwindow.setContentView(layout);
pwindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
pwindow.setWidth(this.getWidth());
pwindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
pwindow.setFocusable(true);
pwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
pwindow = null;
}
});
pwindow.showAsDropDown(this);
}
private void setCustomization() {
locationAdapter = new TextListViewAdapter(getContext());
landmarkAdaper = new TextListViewAdapter(getContext());
initPopupWindow();
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public interface ClickListener {
void placeSelected(String gid);
}
}
Now call this customViewWidget through following code:
place_pop_up.setClickListener(this);
place_pop_up.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
private Timer timer = new Timer();
private final long DELAY = 2000;
#Override
public void afterTextChanged(final Editable s) {
if (s.length() > 3) {
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
getAutoCompleteSearchResult(s.toString());
}
}, DELAY);
}
}
});
In getAutoCompleteSearchResult make the network call and call place_pop_up.updateList(locality, landmark);

Android FrameLayouts in GridLayout not showing

I'm trying to make something like the following screenshot (it's from an assignment from school):
As mentioned in the assignment, I have an Activity PlayActivity:
public class PlayActivity extends ActionBarActivity {
#Bind(R.id.activity_play_textView_score)
TextView txtScore;
#Bind(R.id.activity_play_board)
Board board;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
ButterKnife.bind(this);
}
}
The xml-layout for this activity:
<?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">
<TextView
android:id="#+id/activity_play_textView_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/activity_play_textView_score_placeholder"/>
<com.charlotteerpels.game2048.Board
android:id="#+id/activity_play_board"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
We also had to make a class Card, which is an extension of a FrameLayout:
public class Card extends FrameLayout {
#Bind(R.id.card_frame_layout_textView)
TextView txtNumber;
private int number;
public void setTxtNumber(TextView txtNumber) {
this.txtNumber = txtNumber;
}
public TextView getTxtNumber() {
return this.txtNumber;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return this.number;
}
public Card(Context context, AttributeSet attributeSet, int defaultStyle) {
super(context, attributeSet, defaultStyle);
inflateLayout();
ButterKnife.bind(this);
}
public Card(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
inflateLayout();
ButterKnife.bind(this);
}
public Card(Context context) {
super(context);
inflateLayout();
ButterKnife.bind(this);
}
private void inflateLayout() {
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.card_frame_layout, this, true);
}
}
The xml-layout for this class:
<?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"
android:background="#color/card_frame_layout_background"
android:layout_margin="4dp">
<TextView
android:id="#+id/card_frame_layout_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/card_frame_layout_textView_placeholder"
android:textSize="40sp"
android:layout_gravity="center"/>
</FrameLayout>
At least we had to make a class Board, which is an extension of a GridLayout:
public class Board extends GridLayout {
Card[][] cardBoard;
public Board(Context context, AttributeSet attributeSet, int defaultStyle) {
super(context, attributeSet, defaultStyle);
initBoard(context);
}
public Board(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
initBoard(context);
}
public Board(Context context) {
super(context);
initBoard(context);
}
public void initBoard(Context context) {
//Set the settings for the GridLayout (the grid is the board)
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.board_grid_layout, this, true);
//Initialize the cardBoard[][] and populate it
cardBoard = new Card[4][4];
for(int rij=0; rij<4; rij++) {
for(int kolom=0; kolom<4; kolom++) {
cardBoard[rij][kolom] = new Card(getContext());
}
}
int cardMeasure = getCardMeasure(context);
addCardBoardToGridLayout(cardMeasure);
}
private void addCardBoardToGridLayout(int cardMeasure) {
for(int rij=0; rij<4; rij++) {
for(int kolom=0; kolom<4; kolom++) {
Card card = cardBoard[rij][kolom];
addView(card, cardMeasure, cardMeasure);
}
}
}
private int getCardMeasure(Context context) {
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
return screenWidth/4;
}
}
The xml-layout for the Board:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/board_grid_layout_background"
android:columnCount="4"
android:rowCount="4">
</GridLayout>
But all I'm getting is this:
I'm also using Butterknife (website: http://jakewharton.github.io/butterknife/)
for binding resources, but mostly to bind views and elements from the xml-layouts.
Can anybody help me?
Found what was wrong!
So in the xml-layout from Card, I changed the layout_width and layout_height from the TextView:
<?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"
android:background="#color/card_frame_layout_background"
android:layout_margin="4dp">
<TextView
android:id="#+id/card_frame_layout_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/card_frame_layout_textView_placeholder"
android:textSize="40sp"
android:layout_gravity="center"/>
</FrameLayout>
The "addCardBoardToGridLayout(int cardMeasure)" changed to the following:
private void addCardBoardToGridLayout(int cardMeasure) {
setRowCount(4);
setColumnCount(4);
removeAllViews();
for(int rij=0; rij<4; rij++) {
for(int kolom=0; kolom<4; kolom++) {
Card card = cardBoard[rij][kolom];
addView(card, cardMeasure, cardMeasure);
}
}
}
Now it looks like this (and it's exactly like I wanted it!):

How to set WearableListView item height

I make WearableListView list. Problem is that setting android:layout_height="20dp" doesn't help
How to set height in this case? In Android Wear sample projects Notifications and Timer they also just set atribute android:layout_height="80dp". But I tried to set in the projects android:layout_height="20dp" but it didn't help! (below is my project source code):
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<base.mobitee.com.mobiteewatch.adapter.HolesListItemLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center_vertical" >
<TextView
android:id="#+id/text_hole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:textSize="#dimen/list_item_text_size" />
</base.mobitee.com.mobiteewatch.adapter.HolesListItemLayout>
HolesListItemLayout.java:
public class HolesListItemLayout extends LinearLayout
implements WearableListView.OnCenterProximityListener {
private TextView mName;
private final int mFadedTextColor;
private final int mChosenTextColor;
public HolesListItemLayout(Context context) {
this(context, null);
}
public HolesListItemLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HolesListItemLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
mFadedTextColor = getResources().getColor(R.color.grey);
mChosenTextColor = getResources().getColor(R.color.black);
}
// Get references to the icon and text in the item layout definition
#Override
protected void onFinishInflate() {
super.onFinishInflate();
mName = (TextView) findViewById(R.id.text_hole);
}
#Override
public void onCenterPosition(boolean animate) {
mName.setTextSize(18);
mName.setTextColor(mChosenTextColor);
}
#Override
public void onNonCenterPosition(boolean animate) {
mName.setTextColor(mFadedTextColor);
mName.setTextSize(14);
}
}
HolesListAdapter.java:
public class HolesListAdapter extends WearableListView.Adapter {
private final Context mContext;
private final LayoutInflater mInflater;
public HolesListAdapter(Context context) {
this.mContext = context;
mInflater = LayoutInflater.from(context);
}
#Override
public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new WearableListView.ViewHolder(
mInflater.inflate(R.layout.list_item_hole, null));
}
#Override
public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
TextView text = (TextView) holder.itemView.findViewById(R.id.text_hole);
text.setText(mContext.getString(R.string.hole_list_item) + " " + (position + 1));
text.setTextColor(mContext.getResources().getColor(android.R.color.black));
holder.itemView.setTag(position);
}
#Override
public int getItemCount() {
return Preferences.HOLES;
}
}
The WearableListView is hard-coded to only display three items at a time - it measures the item height by dividing the list view's height by three ... so there isn't a practical way of doing what you want.
I suggest making the text font larger ...
I've got idea. Insert list into FrameLayout container. And by changing height of container list item height is changed. Result:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerInParent="true">
<android.support.wearable.view.WearableListView
android:id="#+id/wearable_list_game_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="0dp"
android:scrollbars="none"/>
</FrameLayout>
</RelativeLayout>
Add one LinearLayout inside and set
android:layout_height="50dp"
android:layout_margin="5dp"

setImageBitmap in gridview does not scale the image to fit

Note: This problem only happens in gridview, if you setimagebitmap in a single imageview, it works well.
See the following pictures, The left picture is what I want, and the picture on right is what actually I get.
The only difference is the way I set the image, if I set the image as below, I get the correct result(left picture above).
holder.image.setImageResource(imagesArray.get(position));
But if I set the image with bitmap as below, the image not scaled as I want(the right picture above)
holder.imageView.setImageBitmap(bitmap);
Why I set the image as bitmap instead of drawable?
because my images were encrypted, I need to decrypt them before load into the imageview. the basic step is:
depryct the image into byte arrya.
create bitmap from the bytes.
load the bitmap into imageview.
Note, the problem was not caused by the decryption, I have test with an normal image without decryption, it does not work either.
Here is the code
GridViewActivity
public class GridViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
GridView gridView = (GridView)findViewById(R.id.grid_view);
ImageAdapter imageAdapter = ImageAdapter.getInstance();
imageAdapter.setContext(this);
gridView.setAdapter(imageAdapter);
for (int i = 0; i < 50; i++) {
int resourceId = getResources().getIdentifier("myimage", "drawable", getPackageName());
imageAdapter.imagesArray.add(resourceId);
}
}
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
private static ImageAdapter imageAdapter = null;
private Bitmap bitmap;
private Context mContext;
public ArrayList<Integer> imagesArray = new ArrayList<Integer>();
private ImageAdapter() {
}
public static ImageAdapter getInstance() {
if (imageAdapter == null) {
imageAdapter = new ImageAdapter();
}
return imageAdapter;
}
public void setContext(Context context) {
mContext = context;
}
#Override
public int getCount() {
return imagesArray.size();
}
#Override
public Object getItem(int position) {
return imagesArray.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
row = inflater.inflate(R.layout.row_grid, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView)row.findViewById(R.id.image);
row.setTag(holder);
}
else {
holder = (ViewHolder)row.getTag();
}
// Create bitmap from image in assets folder.
String imageFileName = "myimage.jpg";
AssetManager assetMgr = mContext.getAssets();
try {
InputStream fis = assetMgr.open(imageFileName);
bitmap = BitmapFactory.decodeStream(fis);
}
catch (Exception ex) {
Log.i("zdd", ex.getLocalizedMessage());
}
holder.imageView.setImageBitmap(bitmap);
//holder.image.setImageResource(imagesArray.get(position));
return row;
}
static class ViewHolder {
ImageView imageView;
}
}
SquareImageView
public class SquareImageView extends ImageView
{
public SquareImageView(Context context)
{
super(context);
}
public SquareImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
Here is the layout
activity_gridview.xml
<?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">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7f7f7f"
android:numColumns="4"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</GridView>
</RelativeLayout>
row_grid.xml
<?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"
android:padding="5dp"
android:background="#ffffff">
<com.jiyuzhai.setimagebitmaptest.SquareImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
Try changing the row item's parent size to "match_parent".
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#ffffff">
<com.jiyuzhai.setimagebitmaptest.SquareImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
Try removing "padding" property from linear layout (or setting it to 0dp) and setting scaleType to fitXY in SquareImageView.

Android CardView Not Displaying Content

I am trying to simply place a TextView inside a CardView, but to no avail. All I need to do is make two numbers appear inside the two CardViews. At the moment, with code below, the two CardViews appear, but do not show any text / content inside them.
I have tried many different combinations but none seem to be working! Placing the TextView inside a LinearLayout does not work, nor does making it a direct child of the CardView.
Here's my current code:
calendar_view.xml
<?xml version="1.0" encoding="utf-8"?>
<hoo.box.facecalendar.SquareCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true"
android:id="#+id/calendar_view_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/calendar_view_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:textColor="#color/primary_text"/>
</RelativeLayout>
</hoo.box.facecalendar.SquareCardView>
SquareCardView.java
public class SquareCardView extends CardView {
public SquareCardView(Context context) {
super(context);
}
public SquareCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
setMeasuredDimension(width, width);
}
}
CalendarAdapter.java
public class CalendarAdapter extends RecyclerView.Adapter<CalendarAdapter.CalendarViewHolder> {
private LayoutInflater inflater;
private Date curDate;
private List<Date> dateList;
public CalendarAdapter(Context context) {
inflater = LayoutInflater.from(context);
curDate = new Date();
dateList = new ArrayList<Date>();
}
#Override
public CalendarViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.calendar_view, viewGroup, false);
CalendarViewHolder viewHolder = new CalendarViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(CalendarViewHolder calendarViewHolder, int i) {
if (dateList.size() <= i) {
Date tempDate = new Date(curDate);
tempDate.addDays(i + 1);
dateList.add(i, tempDate);
}
Date iDate = dateList.get(i);
calendarViewHolder.TEXTVIEW.setText(Integer.toString(iDate.day));
}
#Override
public int getItemCount() { return dateList.size() + 1; }
class CalendarViewHolder extends RecyclerView.ViewHolder {
public final TextView TEXTVIEW;
public final SquareCardView CARDVIEW;
public CalendarViewHolder(View itemView) {
super(itemView);
TEXTVIEW = (TextView) itemView.findViewById(R.id.calendar_view_day);
CARDVIEW = (SquareCardView) itemView.findViewById(R.id.calendar_view_card);
}
}
}
Just a prior thanks to anyone who can help me! This has been eating away at me for days!
After about 3 hours of playing with it, I found the solution! It was a problem with SquareCardView's onMeasure() not resizing the inner layout bounds. Below is what I altered it to:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
There is problem with your support library which you are using for cardview. The compiled sdk version and support lib version should be same.
for example:
In build.gradle
if compileSdkVersion 23 then
in dependencies you should use
compile 'com.android.support:cardview-v7:23.0.0'
Check both the versions in build.gradle.
ps: if you use support lib version 24 studio suggest you the following
"this support library should not use a different version (24) than the compilesdkversion (23)"
best luck !

Categories

Resources