Android FrameLayouts in GridLayout not showing - android

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!):

Related

How do I change the progress value of a progress bar programmatically?

I'm trying to create a custom view of some components because I need this combination a lot in my application. Everything in the code works except the progress of my progressBar. I think that it doesn't get the value correctly of the XML file.
In my init void I call the setProgressBar(progress) to provide the current value to my xml file. When I enter for example 'setProgressBar(88)' it works perfectly but not with a value that it has to find in the xml file.
CustomProgressbar.java
public class CustomProgressbar extends RelativeLayout {
#StyleableRes
int index0 = 0;
#StyleableRes
int index1 = 1;
#StyleableRes
int index2 = 2;
TextView titleText;
TextView valueText;
ProgressBar progressBar;
public CustomProgressbar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
inflate(context, R.layout.custom_progressbar_layout, this);
int[] sets = {R.attr.title, R.attr.value, R.attr.progress};
TypedArray typedArray = context.obtainStyledAttributes(attrs, sets);
CharSequence title = typedArray.getText(index0);
CharSequence value = typedArray.getText(index1);
int progress = typedArray.getInteger(index2,0);
typedArray.recycle();
titleText = findViewById(R.id.title_text);
valueText = findViewById(R.id.value_text);
progressBar = findViewById(R.id.progressbar_attr);
setTitleText(title);
setValueText(value);
setProgressBar(progress);
}
public CharSequence getTitleText() {
return titleText.getText();
}
public CharSequence getValueText() {
return valueText.getText();
}
public int getProgressBar() {
return progressBar.getProgress();
}
public void setTitleText(CharSequence value) {
titleText.setText(value);
}
public void setValueText(CharSequence value) {
valueText.setText(value);
}
public void setProgressBar(int value) {
progressBar.setProgress(value);
}
}
custom_progressbar_layout.xml
<RelativeLayout 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="wrap_content">
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/value_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black" />
<ProgressBar
android:id="#+id/progressbar_attr"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="#id/value_text"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:layout_marginBottom="10dp"
android:max="100"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progressTint="#android:color/holo_green_dark"
android:progressDrawable="#drawable/progressbars" />
</RelativeLayout>
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainClass"
android:background="#android:color/background_dark">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<com.x.customprogressbar.CustomProgressbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="ThisIsTheTitle"
app:value="€ 28,30"
app:progress="77" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
And my attributes
<resources>
<declare-styleable name="CustomProgressbar">
<attr name="title" format="string"/>
<attr name="value" format="string"/>
<attr name="progress" format="integer"/>
</declare-styleable>
</resources>
Current results:
The XML file, as it should look. There it works perfectly.
Link1
When I run the app.
Link2
(I'm not allowed yet to add images so I got links.)
try this, styles attributes are getting in the other way
public class CustomProgressbar extends RelativeLayout {
private TextView titleText;
private TextView valueText;
private ProgressBar progressBar;
public CustomProgressbar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
inflate(context, R.layout.custom_progressbar_layout, this);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressbar);
CharSequence title = typedArray.getText(R.styleable.CustomProgressbar_title);
CharSequence value = typedArray.getText(R.styleable.CustomProgressbar_value);
int progress = typedArray.getInteger(R.styleable.CustomProgressbar_progress, 0);
typedArray.recycle();
titleText = findViewById(R.id.title_text);
valueText = findViewById(R.id.value_text);
progressBar = findViewById(R.id.progressbar_attr);
setTitleText(title);
setValueText(value);
setProgressBar(progress);
}
public CharSequence getTitleText() {
return titleText.getText();
}
public CharSequence getValueText() {
return valueText.getText();
}
public int getProgressBar() {
return progressBar.getProgress();
}
public void setTitleText(CharSequence value) {
titleText.setText(value);
}
public void setValueText(CharSequence value) {
valueText.setText(value);
}
public void setProgressBar(int value) {
progressBar.setProgress(value);
}
}

Add onClickListener on constraint layout inside custom layout with binding

I am trying to add an onClickListener on a constraint layout that I have it embedded in my layout with no luck.
I have a class and a layout called view_button that contains these elements:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<android.support.constraint.ConstraintLayout
android:id="#+id/main_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_button"
android:clickable="true"
android:focusable="true">
<ImageView
android:id="#+id/icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="16dp"
android:src="#drawable/ic_add_24_vector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dip"
android:fontFamily="#font/lato_regular"
android:gravity="center_vertical"
android:text="START"
android:textColor="#color/color_white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/icon"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
And my corresponding Java class
public class SprintLaunchButton extends ConstraintLayout {
private TextView mTextView;
private ImageView mIconImageView;
protected CharSequence attr_text = null;
protected Drawable attr_image_drawable = null;
public SprintLaunchButton(Context context) {
super(context);
inflateView(context);
init(null);
setView(context);
}
public SprintLaunchButton(Context context, AttributeSet attrs) {
super(context, attrs);
inflateView(context);
init(attrs);
setView(context);
}
public SprintLaunchButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
inflateView(context);
init(attrs);
setView(context);
}
private void inflateView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_sprint_button, this, true);
mTextView = findViewById(R.id.text);
mIconImageView = findViewById(R.id.icon);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(params);
}
private void init(AttributeSet attrs) {
consumeAttrs(getContext().obtainStyledAttributes(attrs, R.styleable.SprintLaunchButtonLayout));
}
public TextView getTextView() {
return mTextView;
}
private void consumeAttrs(TypedArray styledAttrs) {
if (styledAttrs == null) {
return;
}
attr_text = styledAttrs.getString(R.styleable.SprintLaunchButtonLayout_buttonText);
attr_image_drawable = styledAttrs.getDrawable(R.styleable.SprintLaunchButtonLayout_iconImage);
styledAttrs.recycle();
}
public void setIconImage(#DrawableRes int id) {
try {
if (mIconImageView != null) {
mIconImageView.setImageDrawable(getContext().getDrawable(id));
}
} catch (Resources.NotFoundException e) {
}
}
public void setButtonText(#StringRes int id) {
try {
if (mTextView != null) {
mTextView.setText(getContext().getString(id));
}
} catch (Resources.NotFoundException e) {
}
}
protected void setView(Context context) {
if (attr_text == null) {
mTextView.setText("");
} else {
mTextView.setText(attr_text);
}
if (attr_image_drawable == null) {
mIconImageView.setImageDrawable(null);
} else {
mIconImageView.setImageDrawable(attr_image_drawable);
}
}
}
When I add my custom layout to my application using my Java class as follows:
<com.netexlearning.play.view.SprintLaunchButton
android:id="#+id/main_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd=" app:layout_constraintBottom_toBottomOf="#+id/thumbnail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/thumbnail">
</com.netexlearning.play.view.SprintLaunchButton>
I am able to use and see this new created layout inside the main layout of my application, but the problem stars when trying to add an onClickClickListener.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_detail_sprint, dataBindingComponent);
mViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(SprintDetailViewModel.class);
mBinding.mainAction.setOnClickListener(view -> Timber.e("test");
}
This listener is never called, even if I add the onClickListener on the TextView itself..
I have to change my custom layout and remove the TextView an ImageView an add a Button, then in my MainActivity class add the onClickListener to be able to fire the action like so:
mBinding.mainAction.getButton.setOnClickListener(view -> Timber.e("test");
My question is.. is there a way to fire an onClickListener on a custom constraint layout?. Even if I add a method to retrieve my textView or imageView the onClickListener doesn't work.
Thank you so much, if you have any question do not hesitate to ask me.
SprintLaunchButton must implement View.OnClickListener interface . And I believe you must do it from the class you created your View Variables

Image height stretched (SimpleDraweeView)

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

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"

android: custom view layout wrapping

I want to have two custom views like this:
class LoadingView extends RelativeLayout{
//this view has a progressBar and can show and hide it.
void setIsLoading(){
// shows/hides progress bar
}
// some code ...
}
class OtherView extends LoadingView{
//some code ...
}
LoadingView has a layout like this:
<RelativeLayout>
<FrameLayout
id="#+id/content">
<!--this is where content goes-->
</FrameLayout>
<ProgressBar
id="#+id/pb"/>
</RelativeLayout>
so that any custom view that inherits from it will be injected into FrameLayout
so if OtherView has it's own layout , it will be nested inside FrameLayout automatically and you will be able to call myOtherView.setIsLoading(true/false)
How would you suggest doing this ?
Keep a reference to the content FrameLayout when inflating the first view.
public class LoadingView extends RelativeLayout {
private ProgressBar progressBar;
private FrameLayout contentFrame;
public LoadingView(Context context) {
super(context);
initialize();
}
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
private void initialize() {
View root = inflate(getContext(), R.layout.loading_view, this);
progressBar = (ProgressBar) root.findViewById(R.id.progress_bar);
contentFrame = (FrameLayout) root.findViewById(R.id.content_frame);
}
public void setLoading(boolean loading) {
progressBar.setVisibility(loading ? VISIBLE : GONE);
}
protected FrameLayout getContentFrame() {
return contentFrame;
}
}
Then use getContentFrame as the parent view when inflating the child view.
public class OtherView extends LoadingView {
public OtherView(Context context) {
super(context);
initialize();
}
public OtherView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public OtherView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
private void initialize() {
View root = inflate(getContext(), R.layout.other_view, getContentFrame());
}
}
loading_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
other_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:text="Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="Subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
And use it like this
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<com.example.client.ui.OtherView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

Categories

Resources