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"
Related
I'm trying to create a custom view that contains a button and a progressbar. This is the 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="wrap_content">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/selector_button_primary"
android:padding="#dimen/size_s"
android:text=""
android:textColor="#android:color/white"
android:textSize="#dimen/size_m" />
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateTint="#android:color/white"
android:indeterminateTintMode="src_atop"
android:padding="#dimen/size_xxs"
android:elevation="2dp"
android:visibility="gone"/>
</FrameLayout>
And I created a custom view with
public class ProgressButton extends FrameLayout {
private boolean isLoading = false;
ProgressBar progressBar;
Button button;
String text;
#Override
public void setOnClickListener(#Nullable OnClickListener l) {
super.setOnClickListener(l);
button.setOnClickListener(l);
}
public void setText(String text){
this.text = text;
button.setText(text);
}
public boolean isLoading() {
return isLoading;
}
public void setLoading(boolean loading) {
isLoading = loading;
if (isLoading) {
button.setText("");
button.setClickable(false);
progressBar.setVisibility(VISIBLE);
} else {
progressBar.setVisibility(GONE);
button.setText(text);
button.setClickable(true);
}
System.out.println("Progressbutton: IsLoading "+isLoading);
}
public ProgressButton(Context context) {
super(context);
initView();
}
public ProgressButton(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public ProgressButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
System.out.println("initview");
View inflated = inflate(getContext(), R.layout.loader_button_primary, this);
progressBar = inflated.findViewById(R.id.progressbar);
button = inflated.findViewById(R.id.button);
}
}
and use it like this
val bt_sign_in = findViewById(R.id.bt_startup_sign_in) as ProgressButton
bt_sign_in.setText("Log in")
bt_sign_in.setOnClickListener{
bt_sign_in.setLoading(!bt_sign_in.isLoading)
}
And now something very weird happens:
- The text is appearing and dissapearing as how I expect.
- The progressbar is never showing up
- but when I set android:visibility="visible" in the XML file, I do see the progressbar as I expect. But after clicking it stays invisible.
Why does it stay hidden programmatically?
You have two clicklistener and with your logic
bt_sign_in.setLoading(!bt_sign_in.isLoading)
your toggle the progressbar and text.
Is VISIBLE and GONE imported correctly? From View Package?
I want to make widget which includes EditText and TextView and looks like this:
.
To achieve this I've created RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<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="48dp">
<EditText
android:id="#+id/editTextHint"
style="#style/CustomEditTextTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="14dp"
android:textColor="#color/textColor"
android:textSize="16sp"
android:hint="To my first character"
tools:text="Тип клиента">
</EditText>
<TextView
android:id="#+id/textViewContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editTextHint"
android:layout_alignParentEnd="true"
android:layout_above="#id/editTextHint"
tools:text="Продавец"
android:fontFamily="#font/proxima_nova_regular"
android:textSize="16sp"
android:layout_marginEnd="4dp"
android:textColor="#color/colorPrimary"/>
</RelativeLayout>
The above RelativeLayout works great and it looks as planned. To make Compound view I've removed </RelativeLayout> tag and wrapped it into the </merge> (I don't apply code as it's identical except of RelativeLayout tag)
To work with view I've written MyCustomEditText class which extendes RelativeLayout
public class CustomEditText extends RelativeLayout {
private EditText editTextHint;
private TextView textViewEntry;
private String hintText;
private String inputText;
private String starterText;
private OnCustomEditTextListener listener;
public String getHintText() {
return hintText;
}
public String getInputText() {
return inputText;
}
public void setHintText(String hintText) {
editTextHint.setText(hintText);
}
public void setInputText(String inputText) {
textViewEntry.setText(inputText);
}
public CustomEditText(Context context) {
super(context);
initializeViews(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray;
typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);
hintText = typedArray.getString(R.styleable.CustomEditText_hintText);
inputText = typedArray.getString(R.styleable.CustomEditText_inputText);
starterText = typedArray.getString(R.styleable.CustomEditText_starterText);
typedArray.recycle();
initializeViews(context);
}
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_edit_text, this);
setMinimumHeight(48);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
editTextHint = findViewById(R.id.editTextHint);
textViewEntry = findViewById(R.id.textViewContent);
bindDataToChildViews();
editTextHint.setOnClickListener(v -> listener.onClick());
}
private void bindDataToChildViews() {
editTextHint.setText(hintText);
if ((inputText == null)||(inputText.isEmpty())) {
textViewEntry.setText(starterText);
} else {
textViewEntry.setText(inputText);
}
}
public void setHintClickListener(OnCustomEditTextListener listener) {
this.listener = listener;
}
public interface OnCustomEditTextListener {
void onClick();
}
}
I'm trying tu use this view in other layouts but the positioning of text view is ugly:
I thought that extending view from RelativeLayout would be enough for correct positioning. So I need your help to define where I'me getting wrong. How to position elements correctly?
P.S. The replacement of RelativeLayout for merge tag was made to optimize drawing of views and avoiding unnecessary nesting layouts
First of all you dont need to have
android:layout_above="#id/editTextHint"
And add
android:layout_gravity="center_vertical"
to editTextHint and textViewContent
Also possibly you should remove
android:layout_marginEnd="4dp"
or change a bit it's value
In the image above, I have shown that when the user touches the drop-down spinner it will call the web api for getting data for the spinner. Then, that moment, I want to show the loader only on the spinner view on the left or right somewhere on the view itself like in the image, rather than on whole screen when it is getting data from the web service dynamically and hide that progress bar later when web service completely hit at the end (Ignore that search bar in image).
Just create an custom adapter for your spinner. Follow the instructions found here How to create Spinner-list using CustomAdapter in android .
Put the loading view in the layout inflated in the getView method in the adapter, and manipulate it via a callback from your async task used for fetching the result.
In this i am showing loader on start button and hiding loader when stop button is pressed so you can use according to your need.So , for this i have made three class CustomSpinner,Spinner_Custom_adapter and Activity class for using it
In main layout file
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<www.your_packagename.com.spinnerwithloaderex.CustomSpinner
android:id="#+id/custm_spnr"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:background="#drawable/dropdown_create_sales"
android:paddingRight="15dp"
android:text="Hello World!" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<Button
android:id="#+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/stop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
</LinearLayout>
CustomSpinner class
public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {
private Spinner_Custom_adapter spinner_custom_adapter;
public CustomSpinner(Context context) {
super(context);
}
public CustomSpinner(Context context, int mode) {
super(context, mode);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
super(context, attrs, defStyleAttr, mode);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode, Resources.Theme popupTheme) {
super(context, attrs, defStyleAttr, mode, popupTheme);
}
public void setItems(Activity activity, ArrayList<String> spnr_Arr) {
spinner_custom_adapter = new Spinner_Custom_adapter(activity, spnr_Arr);
setAdapter(spinner_custom_adapter);
}
public Spinner_Custom_adapter getSpinner_custom_adapter() {
return spinner_custom_adapter;
}
public void showLoader() {
setEnabled(false);
spinner_custom_adapter.showLoader(true, true);
}
public void dismissLoader() {
setEnabled(true);
spinner_custom_adapter.showLoader(false, true);
}
}
Custom_Adapter class
public class Spinner_Custom_adapter<T> extends ArrayAdapter<T> {
private LayoutInflater flater;
private ProgressBar spinner_progress;
private TextView txtTitle;
private Boolean showOrNot = false;
Spinner_Custom_adapter(Activity context, ArrayList<T> list) {
super(context, R.layout.loader_spinner_lt, R.id.title, list);
flater = context.getLayoutInflater();
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = flater.inflate(R.layout.loader_spinner_lt, parent, false);
}
Object object = getItem(position);
String rowItem = null;
if (object instanceof String) {
rowItem = (String) object;
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(rowItem);
ProgressBar spinner_progress = (ProgressBar) convertView.findViewById(R.id.spinner_progress);
this.txtTitle = txtTitle;
this.spinner_progress = spinner_progress;
showLoader(showOrNot, false);
return convertView;
}
void showLoader(Boolean showOrNot, boolean notifyListOrNot) {
if (txtTitle != null && spinner_progress != null) {
this.showOrNot = showOrNot;
spinner_progress.setVisibility(showOrNot ? View.VISIBLE : View.GONE);
if (notifyListOrNot) {
notifyDataSetChanged();
}
}
}
}
Spinner single view layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/spinner_progress"
android:ellipsize="end"
android:singleLine="true"
android:text="Strawberry"
android:textColor="#CC0033"
android:textSize="16dp" />
<ProgressBar
android:id="#+id/spinner_progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
and for using it
custm_spnr = (CustomSpinner) findViewById(R.id.custm_spnr);
ArrayList<String> items = new ArrayList<>();
items.add("Abcdefg");
items.add("hijklm");
items.add("nopqr");
items.add("stu");
items.add("vwxyza1b1c1");
items.add("d1e1f11g1h1");
custm_spnr.setItems(MainActivity.this, items);
findViewById(R.id.start_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
custm_spnr.showLoader();
}
});
findViewById(R.id.stop_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
custm_spnr.dismissLoader();
}
});
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
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!):