Custom LinearLayout component view not displaying content
Hi i have created a custom component view that extends LinearLayout and when i use this view on my xml, it doesnt become visible on the screen.
Here is the main view that is using this custom component
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contentLinearLayout"
android:layout_width="#dimen/width"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ABB0B6" />
<com.jon.ui.EnableView
android:id="#+id/enableContainter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here is the EnableView custom component code:
package com.jon.ui;
/**
*/
public class EnableView extends LinearLayout {
private static View enableView;
private static Button btnContactlessInfoAction;
private static LinearLayout btnMakeContactlessAction;
private static View makeContactlessView;
private static View.OnClickListener enableContaclessBtnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
};
private static View.OnClickListener contactlessHelpBtnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
};
private static ViewGroup viewContainer;
private final Context mContext;
public EnableContactlessView(Context context) {
super(context);
mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_component, this);
setOrientation(LinearLayout.VERTICAL);
}
public EnableContactlessView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_component, this);
setOrientation(LinearLayout.VERTICAL);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
public void initiate() {
btnMakeContactlessAction = (LinearLayout) this.findViewById(R.id.btn);
btnContactlessInfoAction = (Button) this.findViewById(R.id.info_btn);
btnContactlessInfoAction.setOnClickListener(contactlessHelpBtnClick);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
Below is the
View's layout xml i use to inflate from EnableView class called custom_component.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/top_margin"
android:gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="9dp"
android:layout_weight="3"
android:background="#color/button_colour"
android:gravity="center"
android:paddingBottom="9dp"
android:paddingTop="9dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:src="#drawable/icon_symbol" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activate and enable"
android:textColor="#android:color/white" />
</LinearLayout>
<Button
android:id="#+id/info_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical|end"
android:background="#drawable/icon" />
</LinearLayout>
Do not override onLayout if you're leaving it empty, remove that function. onLayout is used to measure the parent and its children, if you leave it empty nothing is getting measured.
Related
I made a ListView with a custom adapter with my own layout, and on this layout there is a text view, a button and three image buttons that are at first with setVisibility.GONE
I am trying to animate this button for when the user clicks on it, it changes its position from right to left to give space for the three image buttons.
The problem is, the only item in which the animation is working is the last item on the list. I want the animation to work in all of the items.
Here's the code for my adapter:
public class ListaAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private ListView lista;
private Button btnAbrir;
private RelativeLayout relativeLayout;
private ObjectAnimator animation;
public ListaAdapter (ArrayList<String> list, Context context, ListView a) {
this.list = list;
this.context = context;
this.lista = a;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list, null);
}
TextView listItemText = (TextView)view.findViewById(R.id.textoItem);
listItemText.setText(list.get(position));
final LinearLayout btnLayout = (LinearLayout)
view.findViewById(R.id.btnLayout);
relativeLayout = (RelativeLayout) view.findViewById(R.id.layout);
btnAbrir = (Button)view.findViewById(R.id.btnAbrir);
ImageButton btnNaoSei = (ImageButton)view.findViewById(R.id.btnNaoSei);
ImageButton btnAceitar =
(ImageButton)view.findViewById(R.id.btnAceitar);
ImageButton btnNegar = (ImageButton)view.findViewById(R.id.btnNegar);
animation = ObjectAnimator.ofFloat(btnAbrir,"x",200);
btnLayout.setVisibility(View.GONE);
btnAbrir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnLayout.setVisibility(View.VISIBLE);
animate();
}
});
return view;
}
private void animate()
{
animation.setDuration(500);
animation.start();
}
}
And this is my custom_list.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/white">
<LinearLayout
android:id="#+id/btnLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_barra">
<ImageButton
android:id="#+id/btnNaoSei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"
android:backgroundTint="#color/compras_barra"
android:contextClickable="false"
app:srcCompat="#drawable/a12" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNaoSei"
android:layout_centerHorizontal="true"
android:text="NÃO SEI"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras">
<ImageButton
android:id="#+id/btnAceitar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:background="#color/compras"
app:srcCompat="#drawable/a11" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btnAceitar"
android:paddingEnd="#dimen/_3sdp"
android:paddingTop="#dimen/_8sdp"
android:text="ACEITAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_texto2">
<ImageButton
android:id="#+id/btnNegar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#color/compras_texto2"
app:srcCompat="#drawable/a10" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:paddingBottom="#dimen/_1sdp"
android:text="NEGAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="#+id/btnAbrir"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/a09"
android:layout_alignTop="#+id/textoItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textoItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:maxWidth="200dp"
android:text="TextView"
android:textColor="#color/compras_texto2" />
</RelativeLayout>
Any idea on how to accomplish this? Thanks in advance.
The things you need to adopt the following method and create a ViewHolder so that your adapter will know each element of each row in the List
The last UI element of the row only animates because this the only last known id of components for your Adapater when he was creating the Views in your List. So it will animates for the last row elements
You have to add ViewHolder nested class inside your Adapter and declare your UI components. Use setTag and getTag methods inside the getView
Overall you have to create your adapter things like this in getView
public class SListAdapter extends ArrayAdapter<String> {
private Context context;
private String[] seCtnColors;
private List<Subscription> item;
private ViewHolder mainViewHolder = null;
private ViewHolder viewHolder;
private LayoutInflater inflater;
private View row;
public SListAdapter(Context c, List<Subscription> subscriptions)
{
super(c, R.layout.row,R.id.rowNameTV);
this.context=c;
this.item = subscriptions;
}
#Override
public int getCount() {
return this.item.size();
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
row = null;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row,parent,false);
viewHolder = new ViewHolder();
initAdapterUI();
setAdapterDataset(position);
return row;
}
private void initAdapterUI() {
viewHolder.animatedGifViewHol = row.findViewById(R.id.row_animated_gif);
viewHolder.alertBarVerticalViewHol = row.findViewById(R.id.alertBarVerticalView);
viewHolder.firstNameTVHol = (TextView) row.findViewById(R.id.rowNameTV);
viewHolder.phoneNumberTVHol = (TextView) row.findViewById(R.id.rowNumberTV);
viewHolder.switchStateTVHol = (TextView) row.findViewById(R.id.switchStateTV);
row.setTag(viewHolder);
}
private void setAdapterDataset(int position) {
mainViewHolder = (ViewHolder) row.getTag();
mainViewHolder.alertBarVerticalViewHol.setBackgroundColor(Color.RED);
mainViewHolder.switchStateTVHol.setTextColor(Color.RED);
mainViewHolder.firstNameTVHol.setText(item.get(position).getFirstName());
mainViewHolder.phoneNumberTVHol.setText(item.get(position).getNumber());
}
public class ViewHolder{
View animatedGifViewHol;
View alertBarVerticalViewHol;
TextView firstNameTVHol;
TextView switchStateTVHol;
TextView phoneNumberTVHol;
}
}
I am a beginner in Android development. I tried to save the state of RecyclerView's LayoutManager using onSaveInstanceState() and onRestoreInstanceState() on the LayoutManager (after going through a lot of StackOverflow questions) but I am unable to do so.I wish to restore the scroll position of the RecyclerView on rotation of the device. I have also tried saving the position of the NestedScrollView used in the layout but couldnt get it to work too.
Fragment Class
public class RecipeDetailFragment extends Fragment {
private static final String BUNDLE_RECIPE_ID = "Recipe";
private static final String BUNDLE_LAYOUT_MANAGER_KEY = "layout";
public StepAdapter mStepAdapter;
public IngredientAdapter mIngredientAdapter;
private Recipe selectedRecipe;
private StepAdapter.StepAdapterOnClickHandler mClickHandler;
#BindView(R.id.rv_recipe_detail_steps)
RecyclerView mStepRecyclerView;
#BindView(R.id.rv_recipe_detail_ingredients)
RecyclerView mIngredientRecyclerView;
public RecipeDetailFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recipe_detail, container, false);
ButterKnife.bind(this, rootView);
mIngredientAdapter = new IngredientAdapter();
mIngredientRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mIngredientRecyclerView.setHasFixedSize(true);
mIngredientRecyclerView.setNestedScrollingEnabled(false);
mStepAdapter = new StepAdapter(getContext(), mClickHandler);
mStepRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mStepRecyclerView.setHasFixedSize(true);
mStepRecyclerView.setNestedScrollingEnabled(false);
if (savedInstanceState != null) {
selectedRecipe = savedInstanceState.getParcelable(BUNDLE_RECIPE_ID);
}
mIngredientAdapter.setIngredientData(selectedRecipe.getIngredients());
mStepAdapter.setStepData(selectedRecipe.getSteps());
mIngredientRecyclerView.setAdapter(mIngredientAdapter);
mStepRecyclerView.setAdapter(mStepAdapter);
return rootView;
}
#Override
public void onViewStateRestored(#Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null) {
Parcelable state = savedInstanceState.getParcelable(BUNDLE_LAYOUT_MANAGER_KEY);
mStepRecyclerView.getLayoutManager().onRestoreInstanceState(state);
}
}
public void setRecipe(Recipe recipe) {
selectedRecipe = recipe;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mClickHandler = (StepAdapter.StepAdapterOnClickHandler) context;
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(BUNDLE_RECIPE_ID, selectedRecipe);
outState.putParcelable(BUNDLE_LAYOUT_MANAGER_KEY, mStepRecyclerView.getLayoutManager()
.onSaveInstanceState());
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/recipe_detail_nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:descendantFocusability="beforeDescendants"
android:fillViewport="true"
android:focusableInTouchMode="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_recipe_detail_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:id="#+id/ingredients_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
cardView:cardCornerRadius="4dp"
cardView:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv_recipe_detail_ingredients_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp"
android:text="#string/label_ingredients"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Widget.ActionMode.Title"
android:textSize="28sp" />
<android.support.v7.widget.RecyclerView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rv_recipe_detail_ingredients"
android:layout_width="match_parent"
android:focusable="false"
android:layout_height="match_parent"
android:paddingBottom="8dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:id="#+id/steps_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
cardView:cardCornerRadius="4dp"
cardView:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv_recipe_detail_steps_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp"
android:paddingBottom="16dp"
android:text="#string/label_steps"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Widget.ActionMode.Title"
android:textSize="28sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_recipe_detail_steps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:paddingBottom="16dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
P.S. This is my first time posting a question here so if I have not posted the question correctly please let me know!
as you are using fragment make sure you not doing fragment transaction when savedInstanceState is not null in activity that host the fragment as on rotation activity is recreated
first of all you need to save the data you pass to adapter in savedInstanceState and if savedInstanceState is not null use that data
and use this custom recyclerview
package eu.f3rog.ui.custom;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
/**
* Class {#link StatefulRecyclerView} extends {#link RecyclerView} and adds position management on configuration changes.
*
* #author FrantisekGazo
* #version 2016-03-15
*/
public final class StatefulRecyclerView
extends RecyclerView {
private static final String SAVED_SUPER_STATE = "super-state";
private static final String SAVED_LAYOUT_MANAGER = "layout-manager-state";
private Parcelable mLayoutManagerSavedState;
public StatefulRecyclerView(Context context) {
super(context);
}
public StatefulRecyclerView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public StatefulRecyclerView(Context context, #Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(SAVED_SUPER_STATE, super.onSaveInstanceState());
bundle.putParcelable(SAVED_LAYOUT_MANAGER, this.getLayoutManager().onSaveInstanceState());
return bundle;
}
#Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
mLayoutManagerSavedState = bundle.getParcelable(SAVED_LAYOUT_MANAGER);
state = bundle.getParcelable(SAVED_SUPER_STATE);
}
super.onRestoreInstanceState(state);
}
/**
* Restores scroll position after configuration change.
* <p>
* <b>NOTE:</b> Must be called after adapter has been set.
*/
private void restorePosition() {
if (mLayoutManagerSavedState != null) {
this.getLayoutManager().onRestoreInstanceState(mLayoutManagerSavedState);
mLayoutManagerSavedState = null;
}
}
#Override
public void setAdapter(Adapter adapter) {
super.setAdapter(adapter);
restorePosition();
}
}
As long as your RecyclerView has an id set, you should not have to do anything to get it to save its scroll state when you rotate your device. Here is a very simple app that demonstrates this.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setAdapter(new MyAdapter());
}
private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.itemview, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.text.setText("" + position);
}
#Override
public int getItemCount() {
return 100;
}
}
private static class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView text;
public MyViewHolder(View itemView) {
super(itemView);
this.text = (TextView) itemView.findViewById(R.id.text);
}
}
}
activity_main.xml:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
itemview.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/ic_android_black_24dp"/>
<TextView
android:id="#+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:textSize="14sp"
tools:text="Hello world"/>
</LinearLayout>
I am having problem with "GridView".If I use 6 items then it is not creating problem while scrolling but as I increase no. of items its behavior is unpredictable.
Please check the following video link for better understanding my problem:
GridView Issues video link
I am attaching my codes here:
activity_achievements.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:background="#color/green_color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/back_iv"
android:text="Acheivements"
android:textColor="#color/white_color"
android:textSize="24sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/back_iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/back" />
<TextView
android:id="#+id/total_achievements_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="0/15"
android:textColor="#color/white_color"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<GridView
android:id="#+id/acheivements_gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />
</RelativeLayout>
achievements_row.xml
<?xml version="1.0" encoding="utf-8"?>
<com.xyz.quitsmoking.view.AchievementsView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<android.support.v7.widget.CardView
android:id="#+id/card_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_view1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
card_view:cardCornerRadius="2dp"
android:background="#color/white_color"
card_view:contentPadding="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:src="#drawable/achievement" />
<TextView
android:id="#+id/title_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:padding="10dp"
android:text="Himalyi"
android:textColor="#color/green_color" />
<TextView
android:id="#+id/desc_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="16sp"
android:padding="10dp"
android:text="5 cigarettes non-smoked"
android:textColor="#color/teal_color" />
</LinearLayout>
</android.support.v7.widget.CardView>
</pairroxz.com.quitsmoking.view.AchievementsView>
AchievementsAdapter.java
public class AchievementsAdapter extends BaseAdapter {
private Context context;
private ArrayList<Achievements> list;
private LayoutInflater inflater;
public AchievementsAdapter(Context context,ArrayList<Achievements> list){
this.context = context;
this.list = list;
initialize();
}
private void initialize() {
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
AchievementsView view = null;
if(view==null) {
view = (AchievementsView) inflater.inflate(R.layout.achievements_row,null);
}else{
view = (AchievementsView) convertView;
}
view.setContents(list.get(position));
notifyDataSetChanged();
return view;
}
}
AchievementsView.java
public class AchievementsView extends RelativeLayout {
private TextView title_tv,desc_tv;
private ImageView image_iv;
public AchievementsView(Context context) {
super(context);
}
public AchievementsView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
initialize();
}
private void initialize() {
image_iv = (ImageView)findViewById(R.id.image_iv);
title_tv = (TextView)findViewById(R.id.title_tv);
desc_tv = (TextView) findViewById(R.id.desc_tv);
}
public void setContents(Achievements achievements){
image_iv.setImageDrawable(getContext().getResources().getDrawable(achievements.getImg()));
title_tv.setText(achievements.getTitle());
desc_tv.setText(achievements.getDesc());
}
}
AchievementsActivity.java
public class AchievementsActivity extends AppCompatActivity implements View.OnClickListener {
private GridView achievements_gv;
private AchievementsAdapter adapter;
private ArrayList<Achievements> list;
DatabaseHelper helper;
private ImageView back_iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_achievements);
initialize();
}
private void initialize() {
helper = DatabaseHelper.getInstance(this);
back_iv = (ImageView) findViewById(R.id.back_iv);
achievements_gv = (GridView) findViewById(R.id.acheivements_gv);
list = new ArrayList<Achievements>();
getData();
setListener();
}
private void setListener() {
back_iv.setOnClickListener(this);
}
private void getData() {
list = helper.getAchievementsList();
//list =
adapter = new AchievementsAdapter(this, list);
achievements_gv.setAdapter(adapter);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_iv:
onBackPressed();
break;
}
}
}
if(view == null)
view.setTag(viewHolder);
viewHolder = (ViewHolder) view.getTag();
don't use this. or send your code
I have this layout in two apps, one inside a RecyclerView and the other in a root activity layout.
Here the layout:
<?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:clickable="true"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="#CFCFCF"
android:minHeight="250dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_dark"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
android:layout_above="#+id/commerceTextView"
>
<ImageView
android:src="#drawable/imagen"
android:id="#+id/commerceImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/commerceTextView"
android:gravity="center"
android:textStyle="bold"
android:textSize="24sp"
android:textColor="#F1F1F1"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:paddingTop="10dp"
android:text="Best food ever"
android:paddingBottom="10dp"/>
</RelativeLayout>
</FrameLayout>
Here the adapter
public class CommercesAdapter extends RecyclerView.Adapter<CommercesAdapter.CommercesViewHolder> {
private final Context context;
private final ImageLoader loader;
private List<CommerceEntity> commercesList;
#Inject
public CommercesAdapter(Context context, ImageLoader loader) {
this.context = context;
this.loader = loader;
}
public void setData(List<CommerceEntity> commercesList) {
this.commercesList = commercesList;
}
#Override
public CommercesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context)
.inflate(R.layout.list_comerces_item, parent, false);
return new CommercesViewHolder(view);
}
#Override
public void onBindViewHolder(CommercesViewHolder holder, int position) {
CommerceEntity commerce = commercesList.get(position);
String imageUri = commerce.getImageUri();
String name = commerce.getName();
// holder.commerceTypeName.setText(name);
//loader.bind(holder.commerceImage, imageUri);
}
#Override
public int getItemCount() {
return commercesList.size();
}
public static class CommercesViewHolder extends RecyclerView.ViewHolder {
public ImageView commerceImage;
public TextView commerceTypeName;
public CommercesViewHolder(View itemView) {
super(itemView);
commerceImage = (ImageView) itemView.findViewById(R.id.commerceImageView);
commerceTypeName = (TextView) itemView.findViewById(R.id.commerceTextView);
}
}
Here the RecyclerView
And here in a root activity layout
Someone knows why this happen? if I add android:centerInParent"true" to the nested FrameLayout the image appears but i don't understand why.
In your adapter, uncomment the two lines that you have commented out;
`
// holder.commerceTypeName.setText(name);
//loader.bind(holder.commerceImage, imageUri);
`
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"