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 !
Related
I followed the steps in the accepted answer (from #kcoppock) in the following thread.
https://stackoverflow.com/a/15264039/3296263
or
Gridview with two columns and auto resized images
Edit:
Here is the code.
Gridview code:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="#+id/main_category_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>
</FrameLayout>
Grid Item Layout: main_category_list_content.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.praz.notespal.model.SquareImageView
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>
Custom SquareImageView Class:
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());
}
}
Adapter Code:
private final class MyAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;
public MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mItems.add(new Item("Red", R.drawable.tile_alevel));
mItems.add(new Item("Magenta", R.drawable.tile_grade10));
mItems.add(new Item("Dark Gray", R.drawable.tile_grade11));
mItems.add(new Item("Gray", R.drawable.tile_grade9));
mItems.add(new Item("Green", R.drawable.tile_alevel));
mItems.add(new Item("Cyan", R.drawable.tile_grade11));
}
#Override
public int getCount() {
return mItems.size();
}
#Override
public Item getItem(int i) {
return mItems.get(i);
}
#Override
public long getItemId(int i) {
return mItems.get(i).drawableId;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
System.out.println("viewing "+ i);
if (v == null) {
v = mInflater.inflate(R.layout.main_category_list_content, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
Item item = getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item {
public final String name;
public final int drawableId;
Item(String name, int drawableId) {
this.name = name;
this.drawableId = drawableId;
}
}
}
Activity Code where i set the adapter
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView)findViewById(R.id.main_category_list);
gridView.setAdapter(new MyAdapter(this));
}
Instead of showing all 6 items, i only see 2. If i increase the 'numColumns' property to 3, then it shows 3. So it only shows the first row. I placed a console out inside the getView() method in the adapter to see the position of the grid that is being called. To my surprise, what i see is the below output.
I/System.out: viewing item 0
I/System.out: viewing item 1
I/System.out: viewing item 0
I/System.out: viewing item 0
I/System.out: viewing item 0
I/System.out: viewing item 0
As you can see, it repeats 0th position instead of moving to 3rd. After alot of searching I still have not been able to figure out what is going on. Any help is highly appreciated.
Please note that i have created a new question, because i cannot reply to his answer because of reputation points shortage.
I figured out what was wrong (by accident of course). The problem is that My GridView was inside a NestedScrolView. For some reason these 2 do not work well together. The moment I take the GridView out of the NestedScrolView, it works like a charm. I did some further searching on what causes this behavior, but i could not find any lead. Anyway, I don't really want a NestedScrolView, so its all good now. Thanks for everyone who replied. If antyone knows the reason for the above behavior, please explain.
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
first of all, sorry if this a stupid question. I'm not being lazy.
So, the problem is, im trying to implement CardView/RecyclerView in an Android app. I made it, but the problem is that the cards are spaced one from another, and i don't know how to fix it. I explored the code but everything seems to be fine.
The code :
RecyclerView
<android.support.v7.widget.RecyclerView
android:id="#+id/collapsing_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:padding="#dimen/activity_horizontal_margin">
</android.support.v7.widget.RecyclerView>
CardView item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
card_view:cardBackgroundColor="#color/colorAccent"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/cardview.name" />
</android.support.v7.widget.CardView>
</LinearLayout>
Adapter :
public class MyRecyclerViewAdapter extends RecyclerView
.Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<CardViewItem> mDataset;
private static MyClickListener myClickListener;
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView label;
public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.cardview_name);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public MyRecyclerViewAdapter(ArrayList<CardViewItem> myDataset) {
mDataset = myDataset;
}
#Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.example_card_view, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.label.setText(mDataset.get(position).getName());;
}
public void addItem(CardViewItem dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
#Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
Hope you guys can help me. Thanks!
EDIT: So, i found the answer. The coulprit was the android:layout_height="match_parent"
in my cardview item. I change it for "wrap_content" and fixed the problem. Thanks for the help guys! :)
Have you tried ItemDecoration with your RecyclerView ? That's usefull for customize your divider in a RecyclerView
You can look that :
My custom ItemDecoration use getItemOffsets() like this :
public class MyItemDecoration extends RecyclerView.ItemDecoration {
private final int decorationHeight;
private Context context;
public MyItemDecoration(Context context) {
this.context = context;
decorationHeight = context.getResources().getDimensionPixelSize(R.dimen.decoration_height);
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (parent != null && view != null) {
int itemPosition = parent.getChildAdapterPosition(view);
int totalCount = parent.getAdapter().getItemCount();
if (itemPosition >= 0 && itemPosition < totalCount - 1) {
outRect.bottom = decorationHeight;
}
}
}
}
Then you can set your R.dimen.decoration_height to the value what you want.
And in my activity, when I instantiate my RecyclerListView I need to do this :
myList.addItemDecoration(MyItemDecoration(this))
myList.setLayoutManager(LinearLayoutManager(this))
myList.setAdapter(adapter)
I hope this will help you
Remove this line from your CardView item .xml
android:layout_margin="5dp"
You probably wanted it to be padding instead of margin
In order to create spacings in between items, we could use RecyclerView's ItemDecorator's:
addItemDecoration(object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State,
) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) > 0) {
outRect.top = 8.dp // Change this value with anything you want. Remember that you need to convert integers to pixels if you are working with dps :)
}
}
})
A few things to have in consideration given the code I pasted:
You don't really need to call super.getItemOffsets but I chose to, because I want to extend the behavior defined by the base class. If the library got an update doing more logic behind the scenes, we would miss it.
As an alternative to adding top spacing to the Rect, you could also add bottom spacing, but the logic related to getting the last item of the adapter is more complex, so this might be slightly better.
I used an extension property to convert a simple integer to dps: 8.dp. Something like this might work:
val Int.dp: Int
get() = (this * Resources.getSystem().displayMetrics.density + 0.5f).toInt()
// Extension function works too, but invoking it would become something like 8.dp()
CardView by default adds padding. Try using CardView attribute card_view:contentPadding but set the negative values for the attribute like this
card_view:contentPadding="-3"
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!):
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"