I am trying to achieve the result as shown in Pic1. Tried setting gravity, paddingleft etc but the drawable image comes in the right end and not next to Text(SHown in Pic 2). Can anyone suggest how to align the image next to TextView? Also how to set the size of drawable?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/separator"
android:padding="10dp">
<TextView
android:id="#+id/sms"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:drawableEnd="#drawable/message"
android:drawableRight="#drawable/message"
android:gravity="center"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="SMS" />
<TextView
android:id="#+id/call"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableEnd="#drawable/call"
android:drawableRight="#drawable/call"
android:gravity="center"
android:text="CALL" />
<TextView
android:id="#+id/email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:drawableEnd="#drawable/mail"
android:drawablePadding="3dp"
android:drawableRight="#drawable/mail"
android:text="MAIL" />
Try to build around this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<TextView
android:id="#+id/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:gravity="center"
android:drawableRight="#drawable/message"
android:text="SMS" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<TextView
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/call"
android:gravity="center"
android:text="CALL" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/mail"
android:gravity="center"
android:text="MAIL" />
</LinearLayout>
</LinearLayout>
The main problem is that, you are setting the layoutWidth=0dp and weight 1. This makes every TextView take maximum width according to the weight. The drawableRight/drawableEnd sets the drawable resource to be drawn at the rightmost edge of the view. If you want to keep this layout just wrap it in another viewGroup like #Dhinakaran has shown. Better approach would be to use a tab layout.
Instead of using multiple Linear Layouts, you can use a Relative Layout.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:layout_weight=".33"
android:gravity="center_horizontal|center_vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/textView"
android:layout_toRightOf="#+id/textView"
android:src="#android:drawable/btn_star"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:layout_weight=".33"
android:gravity="center_horizontal|center_vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:src="#android:drawable/btn_star"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:layout_weight=".33"
android:gravity="center_horizontal|center_vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:src="#android:drawable/btn_star"/>
</RelativeLayout>
</LinearLayout>
Result
Created a custom button which doesnt require layout nestings and can align drawable images as required.
Layout file button.xml:
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_button"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center" />
Custom Button Class:
public class DrawableAlignedButton extends RelativeLayout {
private View view;
private Button button;
/**
* #param context
* used to inflate the View.
* #param attrs
* XML defined attributes.
*/
public DrawableAlignedButton(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
/**
* #param context
* used to inflate the View.
*/
public DrawableAlignedButton(final Context context) {
super(context);
init(context, null);
}
/**
* #param context
* used to inflate the View.
* #param attrs
* XML defined attributes.
* #param style
* the style for the View.
*/
public DrawableAlignedButton(final Context context, final AttributeSet attrs, final int style) {
super(context, attrs, style);
init(context, attrs);
}
private void init(final Context context, final AttributeSet attributeSet) {
view = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.button, this, true);
button = (Button) view.findViewById(R.id.custom_button);
String buttonText = null;
int drawableStart = 0;
int drawableEnd = 0;
if (attributeSet != null) {
final TypedArray a = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.CustomButtonStyle, 0, 0);
buttonText = a.getString(R.styleable.CustomButtonStyle_buttonText);
drawableStart = a.getResourceId(R.styleable.CustomButtonStyle_buttonDrawableStart, 0);
drawableEnd = a.getResourceId(R.styleable.CustomButtonStyle_buttonDrawableEnd, 0);
a.recycle();
}
FontUtil.getInstance(context).useNormalRegularFont(button);
if (buttonText != null) {
button.setText(buttonText);
}
button.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, 0, drawableEnd, 0);
}
/**
* Sets the button text.
*
* #param text
* the text to be set.
*/
public void setButtonText(final String text) {
if (button != null) {
button.setText(text);
}
}
/**
* Sets the drawable to the button.
*
* #param drawableStart
* the drawable to set at the beginning of the text.
* #param drawableEnd
* the drawable to set at the end of the text.
*/
public void setDrawableStart(final int drawableStart, final int drawableEnd) {
if (button != null) {
button.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, 0, drawableEnd, 0);
}
}
}
How to use it in the XML :
<com.package.view.DrawableAlignedButton
xmlns:drawableAlignedButton="http://schemas.android.com/apk/res-auto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/transparent_button_selector"
drawableAlignedButton:buttonDrawableStart="#drawable/small_active"
drawableAlignedButton:buttonText="Button Text" />
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 created a new custom view that looks like this
public class SearchCategoryPanel extends RelativeLayout {
private boolean hasRetrieved = false;
private GridLayout categoryContainer;
private TextView scpLoadingText;
public static final int HEIGHT = 360;
private ArrayList<OnCategoryItemClickListener> onCategoryItemClickListeners = new ArrayList<OnCategoryItemClickListener>();
public SearchCategoryPanel(Context context) {
super(context);
init();
}
public SearchCategoryPanel(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(SearchCategoryPanel.class.getSimpleName(), this.getTop() + "");
init();
}
public SearchCategoryPanel(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init() {
inflate(getContext(), R.layout.search_category_panel, this);
this.scpLoadingText = (TextView) findViewById(R.id.sCategoryPanelLoadingText);
this.categoryContainer = (GridLayout) findViewById(R.id.sCategoryContainer);
this.setVisibility(View.GONE);
}
public void show() {
this.setVisibility(View.VISIBLE);
}
public void hide() {
this.setVisibility(View.GONE);
}
public void setProcessing(boolean on) {
if(!on) {
this.scpLoadingText.setVisibility(View.GONE);
} else {
this.scpLoadingText.setVisibility(View.VISIBLE);
}
}
public void addCategoryItemUsingVo(CategoryVo categoryVo) {
CategoryItem item = new CategoryItem(getContext());
item.setCategoryVo(categoryVo);
item.setOnItemClickListener(new CategoryItem.OnClickListener() {
#Override
public void onClick(CategoryVo categoryVo) {
triggerOnCategoryItemClickListener(categoryVo);
}
});
this.categoryContainer.addView(item);
}
public void removeAllItems() {
this.categoryContainer.removeAllViews();
}
public boolean hasRetrieved() {
return hasRetrieved;
}
public interface OnCategoryItemClickListener {
public void onClick(CategoryVo categoryVo);
}
public void setOnCategoryItemClickListener(OnCategoryItemClickListener listener) {
this.onCategoryItemClickListeners.add(listener);
}
private void triggerOnCategoryItemClickListener(CategoryVo vo) {
for(OnCategoryItemClickListener listener : onCategoryItemClickListeners ) {
listener.onClick(vo);
}
}
}
Then, I try to call the getHitRect
Rect rectF = new Rect();
searchCategoryPanel.getHitRect(rectF);
Log.i(ARModeActivity.class.getSimpleName(), "Rect: " + rectF.toString());
The log returns strange value: Rect: Rect(0, 0 - 1920, 1164). this means the relative layout covers the whole screen which is not correct.
The SearchCategoryPanel xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#7F000000"
android:layout_alignParentBottom="true"
android:maxHeight="360dp"
android:layout_height="160dp">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Category"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="24dp"/>
<TextView
android:id="#+id/sCategoryPanelLoadingText"
android:layout_width="wrap_content"
android:textSize="24sp"
android:textColor="#android:color/white"
android:layout_height="match_parent"
android:text="Loading.."/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridLayout
android:id="#+id/sCategoryContainer"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
</GridLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
The relative layout only has 360dp in height, andfull width (look at the xml). but it returns the wrong value. I have read about onMeasure and onSizeChanged, but nothing helped
Here How I call the search_category_panel layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:id="#+id/rootArMode"
tools:context="com.imav.ARModeActivity">
<RelativeLayout
android:id="#+id/rlInfoARMode"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:background="#7F000000"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"
android:textSize="24sp"
android:text="Interaction Guide"/>
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_marginBottom="4sp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Select: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="16sp"
android:text="Tap on object with 1 finger "/>
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_marginBottom="4sp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Move: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="16sp"
android:text="Touch and Drag Object with 1 finger "/>
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_marginBottom="4sp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Rotate: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="16sp"
android:text="Touch and drag object with 2 fingers "/>
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_marginBottom="4sp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Scale: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="16sp"
android:text="Touch and pinch object with 2 fingers "/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<Button
android:id="#+id/btnCloseArMode"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:background="#drawable/ic_clear_white_24dp" />
<RelativeLayout
android:id="#+id/rlOpenActionBar"
android:layout_width="55dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true">
<Button
android:id="#+id/btnOpenActionBar"
android:background="#drawable/ic_keyboard_backspace_white_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="55dp"
android:id="#+id/rlActionBar"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#7F000000"
android:layout_alignParentRight="true">
<Button
android:id="#+id/btnInfoArMode"
android:layout_width="wrap_content"
android:background="#drawable/ic_error_white_24dp"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnAddObjArMode"
android:layout_marginBottom="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_add_white_24dp"
android:layout_below="#+id/btnSaveSceneArMode" />
<Button
android:id="#+id/btnSaveSceneArMode"
android:layout_marginBottom="8dp"
android:layout_width="wrap_content"
android:background="#drawable/ic_add_a_photo_white_24dp"
android:layout_height="wrap_content" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/pbSaveSceneArMode"/>
<Button
android:id="#+id/btnShareARMode"
android:layout_width="wrap_content"
android:background="#drawable/ic_share_white_24dp"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnTrashArMode"
android:layout_width="wrap_content"
android:background="#drawable/ic_delete_white_24dp"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<com.imav.view.SearchCataloguePanel
android:id="#+id/searchCataloguePanelArMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.imav.view.SearchCataloguePanel>
<com.imav.view.SearchCategoryPanel
android:id="#+id/searchCategoryPanelArMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.imav.view.SearchCategoryPanel>
</RelativeLayout>
My first approach would be not to override the onMeasure() in your custom view SearchCategoryPanel. Because you are forcing a different dimension than the ones provided by the xml.
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>
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.
this is my adapter, the getView method is not called even if the array list having elements
public class CarrierSammuryAdapter extends ArrayAdapter<CarrierSummary> {
private final Activity context;
private final ArrayList<CarrierSummary> ite;
private final int lay;
public CarrierSammuryAdapter(Activity context, ArrayList<CarrierSummary> menuLinkList,int layout) {
super(context,layout );
this.context = context;
this.ite = menuLinkList;
this.lay=layout;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public TextView customer;
public TextView attempts;
public TextView successful;
public TextView minutes;
public TextView ASR;
public TextView ACD;
public TextView NER;
public TextView PDD;
}
#Override
public int getCount () {
return ite.size();
}
#Override
public long getItemId (int position) {
return position;
}
#Override
public CarrierSummary getItem (int position) {
return ite.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row layout
final ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();//this gives error !!
rowView=inflater.inflate(R.layout.carriersumamry_item,parent,false);
holder = new ViewHolder();
holder.customer=(TextView)rowView.findViewById(R.id.customer);
holder.attempts=(TextView)rowView.findViewById(R.id.attempts);
holder.successful=(TextView)rowView.findViewById(R.id.successful);
holder.minutes=(TextView)rowView.findViewById(R.id.minutes);
holder.ASR=(TextView)rowView.findViewById(R.id.asr);
holder.ACD=(TextView)rowView.findViewById(R.id.acd);
holder.NER=(TextView)rowView.findViewById(R.id.ner);
holder.PDD=(TextView)rowView.findViewById(R.id.pdd);
// ViewResizing.setListRowTextResizing(rowView, context);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.customer.setText(ite.get(position).getCustomer());
holder.attempts.setText(ite.get(position).getAttempts());
holder.successful.setText(ite.get(position).getSuccessful());
holder.minutes.setText(ite.get(position).getMinutes());
holder.ASR.setText(ite.get(position).getASR());
holder.ACD.setText(ite.get(position).getACD());
holder.NER.setText(ite.get(position).getNER());
holder.PDD.setText(ite.get(position).getPDD());
return rowView;
}
}
and this is how i called it
carrierSummaryList =(ListView)findViewById(R.id.carrierSummary_listview);
CarrierSammuryAdapter adapter = new CarrierSammuryAdapter(CarrierSummaryActivity.this, carriersam,R.layout.carriersumamry_item);
carrierSummaryList.setAdapter(adapter);
I search a lot to solve this issue but no solution, getView method is never called.
this is my XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pdd"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ner"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/acd"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/asr"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minutes"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/successful"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/attempts"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customer"
android:textColor="#color/grey"
/>
</LinearLayout>
I had this exact same problem, to solve it, I simply changed the ArrayAdapter to BaseAdapter, implemented the overriden methods and it worked.
And as khuskal said, you should always use Context instead of Activity.
there are two mistakes..
first is change Activity to Contex in constructor
secondly change
rowView=inflater.inflate(R.layout.single_row,parent,false);
Hope this will help you
/**
* Constructor
*
* #param context The current context.
* #param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
*/
public ArrayAdapter(Context context, int resource) {
init(context, resource, 0, new ArrayList<T>());
}
while you are doing super(context,layout) , this is the constructor that gets called. So adapter data is set to empty list.
Like derek said, Instead you should use super(context,layout,menuLinkList);
/**
* Constructor
*
* #param context The current context.
* #param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
* #param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, List<T> objects) {
init(context, resource, 0, objects);
}
The problem was in my drawerLayout, i put the listView into RelativeLayout but when i change to LinearLayout its working, this is the code
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:orientation="vertical">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/mainbar"
layout="#layout/second_topmain"/>
<ListView
android:id="#+id/carrierSummary_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
></ListView>
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="0dp"
android:id="#+id/mainbar"
layout="#layout/bottom_bar"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearSlider"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:background="#7e7e7e"
android:orientation ="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Carrier Type"
android:textColor="#android:color/white"
android:padding="5dp"
android:background="#color/darkgrey"
/>
<LinearLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#color/darkgrey"
android:layout_height="wrap_content">
<Button
android:id="#+id/customerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/grey"
android:onClick="toggleClicked"
android:background="#drawable/toggleon"
android:text="Customer"
/>
<Button
android:id="#+id/SupplierButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleClicked"
android:textColor="#color/grey"
android:background="#drawable/toggleoff"
android:text="Supplier"
/>
</LinearLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Carrier"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerCarrier"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:background="#color/darkgrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/SpinnerTop"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:onClick="showDateTimePickerFrom"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From Date"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fromDate"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:background="#color/darkgrey"
android:layout_width="wrap_content"
android:onClick="showDateTimePickeTo"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Date"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toDate"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group By Profile"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/byprofilecheck"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>