I tried implementing a custom view and put it in an expandable card view, which is contained inside a RecyclerView. At the start, I open it and it looks quite nice.
Before scroll
But then, I leave it opened and scroll to the bottom of the RecyclerView. When I scroll back to the item, close it and then open it again, the layout is broken.After scroll
Below is the code of the custom view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="60dp" android:background="?android:attr/selectableItemBackground"
android:clickable="true">
<View
android:layout_width="#dimen/session_padding_left"
android:layout_height="#dimen/session_padding_left"></View>
<TextView
android:id = "#+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/lightGray"
android:gravity="center_vertical"
android:layout_weight="6"
android:maxLines = "2"
android:lines="2"
android:textSize="#dimen/extra_small_text_size"
android:ellipsize="end"
android:paddingTop = "10dp"
android:paddingBottom = "10dp"
android:text = "Word-of-Mouth Behaviour in Mobile Social Media "/>
<TextView
android:id = "#+id/tv_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:textSize="#dimen/extra_small_text_size"
android:textColor="#color/lightGray"
android:layout_weight = "4"
android:text="11:30 - 12:00"/>
<ImageButton
android:layout_width="#dimen/dimen_image_button_add_remove"
android:layout_height="#dimen/dimen_image_button_add_remove"
android:layout_gravity="center|right"
android:scaleType="fitCenter"
style = "?android:attr/borderlessButtonStyle"
android:src = "#drawable/ic_add_agenda"
/>
</LinearLayout>
And the view class:
package au.com.leremede.acis2016.views;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
import au.com.leremede.acis2016.R;
public class PresentationView extends LinearLayout {
private TextView tvTitle;
private TextView tvTime;
public PresentationView(Context context) {
super(context);
init(context);
}
public PresentationView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
private void init(Context context) {
setOrientation(HORIZONTAL);
inflate(context, R.layout.view_presentation, this);
tvTitle = (TextView)findViewById(R.id.tv_title);
tvTime = (TextView)findViewById(R.id.tv_time);
}
public void setTitle(String title)
{
tvTitle.setText(title);
}
public void setTime(String time)
{
tvTime.setText(time);
}
}
I solved this problem myself using the Percent Support Library
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="60dp" xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?android:attr/selectableItemBackground"
android:clickable="true">
<View
android:id="#+id/empty_view"
android:layout_width="#dimen/session_padding_left"
android:layout_height="#dimen/session_padding_left"
></View>
<ImageButton
android:id="#+id/btn_add_remove_agenda"
android:layout_width="#dimen/dimen_image_button_add_remove"
android:layout_height="#dimen/dimen_image_button_add_remove"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
style="?android:attr/borderlessButtonStyle"
android:src="#drawable/ic_add_agenda"
/>
<android.support.percent.PercentRelativeLayout android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/empty_view"
android:layout_toLeftOf="#id/btn_add_remove_agenda">
<TextView
android:id="#+id/tv_title"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#color/lightGray"
android:layout_alignParentLeft="true"
app:layout_widthPercent="60%"
android:maxLines="2"
android:lines="2"
android:textSize="#dimen/extra_small_text_size"
android:ellipsize="end"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="Word-of-Mouth Behaviour in Mobile Social Media "/>
<TextView
android:id="#+id/tv_time"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="right"
android:layout_alignParentRight="true"
app:layout_widthPercent="40%"
android:textSize="#dimen/extra_small_text_size"
android:textColor="#color/lightGray"
android:text="11:30 - 12:00"/>
</android.support.percent.PercentRelativeLayout>
</RelativeLayout>
Related
i want 0 to 9 buttons in two row.Each button have overlay of another small button in right bottom corner.when i click the button,count will be displayed on overlay button.sorry for my bad english thanks in advance
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/expandable2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignBottom="#id/btn0"
android:layout_alignRight="#id/btn0"
android:background="#FFFFFF"
android:text="0" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
My output
any other suggestion will be appreciated
Try setting it like this
android:layout_marginLeft="-20dp"
android:layout_marginTop="10dp"
Use a textview for showing the count because a button over a button is meaningless.
Anyways how to show it.
<RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "60dp">
<Button
android:layout_width = "match_parent"
android:layout_height="match_parent"
android:id="#+id/btn"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:id="#+id/countbtn"
android:layout_alignParentBottom="true"
android:layout_alignParentRight= "true"/>
</RelativeLayout>
use this view in your Grid or RecyclerView Adapter to make it 10 times as you wish to show.
incase you want to use TextView to show Count remove the countBtn and make it a textView with last two attributes same.
Use a framelayout
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/icon_image"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/black"
android:text="0" />
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="bottom|right"
android:background="#color/white"
android:layout_marginLeft="6dp"
android:src="#drawable/ic_goal_check" />
</FrameLayout>
After that you can reuse this layout for all your buttons. To reuse this in your xml, create a custom view as follows :
public class YourButton extends FrameLayout {
private Button buttonOne, buttonTwo;
public YourButton(Context context) {
super(context);
this.context = context;
initializeViews();
}
public YourButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GoalButton);
typedArray.recycle();
initializeViews();
}
private void initializeViews() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.your_button_layout, this);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
buttonOne = (TextView) this.findViewById(R.id.button_one);
buttonTwo = (ImageView) this.findViewById(R.id.button_two);
}
public void setCount(String count){
buttonTwo.setText(count);
}
}
now you can use in your layout as
<yourPackageName.YourButton
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This will keep your main layout simple and tidy.
I am trying to use a custom component in my app. This is the code:
Layout file (select_method_layout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/encabezadometodo"
android:id="#+id/encabezadometodo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"/>
<es.infaplic.aguaics.Componentes.ComponenteSelectMethod
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/methodfile"
android:layout_below="#+id/textexplicacion"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<es.infaplic.aguaics.Componentes.ComponenteSelectMethod
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/methodservice"
android:layout_below="#+id/methodfile"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/layoutmethodservice"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/servicemethod"
android:id="#+id/service"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/layoutmethodfile"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#id/layoutmethodservice">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/filemethod"
android:id="#+id/file"
android:gravity="center"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/textexplicacion"
android:id="#+id/textexplicacion"
android:layout_centerHorizontal="true"
android:layout_below="#+id/encabezadometodo"
android:gravity="center"/>
The class that controls the custom component:
public class ComponenteSelectMethod extends RelativeLayout {
private ImageView imagen;
private TextView text;
public ComponenteSelectMethod(Context context) {
super(context);
inicializar();
}
public ComponenteSelectMethod(Context context, AttributeSet attrs) {
super(context, attrs);
inicializar();
}
private void inicializar() {
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li =
(LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.componente_select_method_layout, this, true);
imagen=(ImageView)findViewById(R.id.imageiconoselectmethod);
text=(TextView)findViewById(R.id.textmethod);
}
public void setImage(int icono){
this.imagen.setBackgroundResource(icono);
}
public void setText(String text){
this.text.setText(text);
}
}
And, in the main class, I am using it this way:
final AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle(getString(R.string.firsttime));
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.select_method_layout, null);
dialog.setView(dialoglayout);
ComponenteSelectMethod cFile=(ComponenteSelectMethod)dialoglayout.findViewById(R.id.methodfile);
ComponenteSelectMethod cService=(ComponenteSelectMethod)dialoglayout.findViewById(R.id.methodservice);
LinearLayout layoutFile=(LinearLayout)dialoglayout.findViewById(R.id.layoutmethodfile);
LinearLayout layoutService=(LinearLayout)dialoglayout.findViewById(R.id.layoutmethodservice);
cFile.setImage(R.drawable.icono_archivo_agu);
cFile.setText(getString(R.string.textexplicacionfile));
cService.setImage(R.drawable.icono_directorio);
cService.setText(getString(R.string.textexplicacionservice));
final AlertDialog methodDialog=dialog.show();
layoutFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
settings.edit().putString("method", "file").apply();
methodDialog.dismiss();
proceder();
}
});
layoutService.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
settings.edit().putString("method", "service").apply();
methodDialog.dismiss();
proceder();
}
});
EDIT:
componente_select_method_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/imageiconoselectmethod"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="#+id/textmethod"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageiconoselectmethod"
android:layout_toEndOf="#+id/imageiconoselectmethod"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"/>
</RelativeLayout>
With this, when I open the xml file, it fails rendering. It stays in "Rendering..." and does nothing more. If I access the Text mode in layout editing, and do anything (like hitting space bar) Android Studio stops responding.
Is something wrong with my custom component? Anybody knows how to avoid Android Studio to hang?
Thank you.
I have a custom EditText component (compound component, based on LinearLayout) and I am using multiple instances in the same Activity.
The custom component works as expected, however, when I rotate the ui, the text that was entered in the second component is suddenly copied to the first component. Otherwise, both custom components function exactly as expected.
Both components have unique id in the activity layout.
Here is the code for my customer component:
public class MyEditText extends LinearLayout{
private EditText mEditText;
TextView mTextCounter;
private int errorIconRes;
private String textHint;
private boolean showLength;
private int maxCount;
private int lines;
private boolean phoneField;
private String errorMessage;
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZipEditText, 0, 0);
phoneField = a.getBoolean(R.styleable.ZipEditText_phone, false);
errorIconRes = a.getInt(R.styleable.ZipEditText_icon_err, R.drawable.form_error_icon);
textHint = a.getString(R.styleable.ZipEditText_hint);
maxCount = a.getInt(R.styleable.ZipEditText_maxLength, 100);
lines = a.getInt(R.styleable.ZipEditText_lines, 1);
showLength = a.getBoolean(R.styleable.ZipEditText_showLength, false);
a.recycle();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.comp_zip_edittext, this, true);
mEditText = (EditText) v.findViewById(R.id.text_field);
mEditText.addTextChangedListener(getTextWatcher());
if (textHint!= null)mEditText.setHint(textHint);
mImageViewError = (ImageView) v.findViewById(R.id.icon_error);
mTextCounter = (TextView) v.findViewById((R.id.text_counter));
if (lines>1 && !phoneField) {
mEditText.setLines(lines);
mEditText.setSingleLine(false);
}
if (showLength) {
mTextCounter.setVisibility(View.VISIBLE);
mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxCount)});
mEditText.addTextChangedListener(getTextWatcherLength());
} else {
mTextCounter.setVisibility(View.GONE);
}
Here is the layout file of my custom component:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<EditText
android:id="#+id/text_field"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="left"
android:paddingLeft="10dp"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:layout_alignParentTop="true"
/>
<ImageView
android:id="#+id/icon_error"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/form_error_icon"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:layout_alignRight="#id/text_field"
android:layout_alignTop="#id/text_field"
android:visibility="gone"/>
</RelativeLayout>
<TextView
android:id="#+id/text_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:layout_gravity="right"
android:visibility="gone"/>
</LinearLayout>
And here is my layout file for the activity:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:zip="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:background="#color/white" >
<TextView
android:id="#+id/error_msg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/home_image_red"
android:background="#color/zip_error_gray"
android:text="Something went wrong"
android:visibility="gone"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:background="#color/white" >
<TextView
android:id="#+id/upsell"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="18sp"
android:text="Sign in to use all our features."
/>
<com.myproject.android.components.MyEditText
android:id="#+id/request_phone"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="5dp"
android:gravity="left"
android:singleLine="true"
zip:phone="true"
zip:hint="#string/hint_phone"/>
<com.myproject.android.components.MyEditText
android:id="#+id/request_comments"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="5dp"
android:gravity="left"
zip:lines="3"
zip:maxLength="120"
zip:showLength="true"
zip:hint="#string/hint_comments"/>
<CheckBox
android:id="#+id/checkbox_request_visit"
style="#style/contact_label"
android:layout_centerInParent="true"
android:text="#string/check_home_visit"
android:checked="false" />
<Button
android:id="#+id/request_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="Request a Showing"
android:textColor="#color/pbz_button_primary_foreground_color"
android:textStyle="bold"
android:background="#drawable/button_registration"
android:gravity="center"/>
<TextView
android:id="#+id/terms_link"
style="#style/FinePrint"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:text="#string/request_showing_terms"
/>
</LinearLayout>
</LinearLayout>
I figured out how to do this: Instead of loading the component via id, parse the tree and load it by the position in the tree:
Instead of
mEditText = (EditText) v.findViewById(R.id.text_field);
Do this:
LinearLayout linearLayout = (LinearLayout) getChildAt(0);
mEditText = (EditText) relativeLayout.getChildAt(0);
This way, you avoid loading the wrong component when the activity / fragment is re-created after the rotation
The problem is in the same Ids of your EditText components. Try to remove it id,and create you custom components separately by inflating each view from xml, and adding it to complex layout.
I have the following layout file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/second_grey">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="#+id/categoryName"
android:text="#string/sport"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="12dp"
android:padding="2dp"
android:id="#+id/button"
android:text="#string/see_more"
android:textSize="12sp"
android:background="#drawable/blue"
android:textColor="#android:color/white" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="0.99">
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img1"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/football"
android:padding="8dp"
android:id="#+id/tv1"
android:layout_below="#+id/imgContainer1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img2"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/basketball"
android:padding="8dp"
android:id="#+id/tv2"
android:layout_below="#+id/imgContainer2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer3">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img3"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tennis"
android:padding="8dp"
android:id="#+id/tv3"
android:layout_below="#+id/imgContainer3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
And I put it in a ListView in this layout:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/categories"></ListView>
then, with a simple Adapter, I just set the layout of each element to the layout file.
It looks like this:
But if I just do instead of the ListView, it looks like this
EDIT: Notice the change of Sport for FĂștbol, that's just my mistake, it has nothing to do with the actual layout.
EDIT2:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Category category = mList.get(position);
if (convertView == null)
convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.category, parent, false);
TextView categoryName = (TextView) convertView.findViewById(R.id.categoryName);
categoryName.setText(category.name);
categoryName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Italic.ttf"));
return convertView;
}
EDIT3:
package com.favega.groups;
import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLayout extends LinearLayout {
public SquareLayout(Context context) {
super(context);
}
#TargetApi(11)
public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
}
int size = width > height ? height : width;
setMeasuredDimension(size, size);
Clearly you are setting Measured Dimension to the smallest of width and height!
Since you gave width and height as match_parent, we can assume that one of the value got as zero from parent. As your intention is to make a square image, I prefer you relate this SquareLayout to the child(image) itself rather that the parent. By using wrap_content.
Set each tag of view in converView.setTag(viewName); before returning covertView.
Try May help.Thanks!!
I'm trying to create a counter as a compound control.
TallyItem.java
package com.anna.mytallykeeper;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
public class TallyItem extends LinearLayout {
private Button decrementButton;
private Button incrementButton;
private TextView descriptionText;
private TextView countText;
public TallyItem(Context context) {
super(context);
LayoutInflater inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflate.inflate(R.layout.tallyitem_layout, this);
/*
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(service);
li.inflate(R.layout.tallyitem_layout, this, true);
*/
//Get the fields
decrementButton = (Button)findViewById(R.id.decrement);
incrementButton = (Button)findViewById(R.id.increment);
descriptionText = (TextView)findViewById(R.id.description);
countText = (TextView)findViewById(R.id.count);
decrementButton.setOnClickListener(buttonListener);
incrementButton.setOnClickListener(buttonListener);
}
public TallyItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
private OnClickListener buttonListener = new OnClickListener() {
#Override
public void onClick(View v) {
Button button = (Button)v;
int count = Integer.parseInt(countText.getText().toString());
if (button.getText().equals("-")) {
count--;
} else if (button.getText().equals("+")) {
count++;
}
countText.setText("" + count);
}
};
}
tallyitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/decrement"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="center"
android:text="-"
android:textSize="30sp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Item 1"
android:textSize="25sp"
/>
<TextView
android:id="#+id/count"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textSize="60sp"
/>
</LinearLayout>
<Button
android:id="#+id/increment"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="center"
android:text="+"
android:textSize="30sp"
/>
</LinearLayout>
main.xaml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<com.anna.mytallykeeper.TallyItem
android:id="#+id/tallyItem1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</merge>
I am at a loss as to why it won't show up in main.xml.
Thanks for any help.
your view need a constructor with the AttributeSet passed in. Change your constructor to this:
public TallyItem(Context context, AttributeSet attr) {
super(context, attr);
...
}
and get rid of the constructor with the single argument.
try this in your main.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<com.anna.mytallykeeper.TallyItem
android:id="#+id/tallyItem1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</merge>