Is there a file xml in the android studio:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<imageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent"
android:scaleType="fitXY" />
<imageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent"
android:scaleType="fitXY" />
</LinearLayout>
I need to have the full width of 2 square buttons, how can this be done?
You can make your custom view here by Extending ImageButton View.In onMeasure(), you can set the height of the ImageButton equal to its width. you can use this class in your XML as well.
Here is the snippet:-
public class SquareImageButton extends ImageButton {
public SquareImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width=getMeasuredWidth();
setMeasuredDimension(width,width);
}
}
Well firstly you'll need a square image to put into the imageButton. Then you'll set AdjustViewBounds to true. and then set the scaletype to FitXY. I use linear layouts and use the weights and views to adjust the size and position. Good for scaling and the imageButton will square. This is under my ImageButton in the xml:
android:scaleType="fitXY"
android:layout_weight="1"
android:adjustViewBounds="true"
1. Programmatically get the width of the display.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
2. Finally, use this width value to make Square Button by setting its new LayoutParams(width, width).
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width);
imageButton1.setLayoutParams(params);
imageButton2.setLayoutParams(params);
3. Use ScrollView as a container of your LinearLayout to make your layout scroll-able to show both button on UI and update XML as below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_light"
android:scaleType="fitXY" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_light"
android:scaleType="fitXY" />
</LinearLayout>
</ScrollView>
OUTPUT:
FYI, ImageButton1 is RED colored and ImageButton2 is BLUE colored. Both are in Square shape.
Hope this will help~
Related
Why does Dialog created with Binding ignores android:layout_height and layout_marginHorizontal? I guess it ignores something more, but thats what I have problems with.
Dialog Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginHorizontal="12dp"
android:background="#color/white"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/dialogTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:src="#drawable/ic_settings_outline_28dp" />
</LinearLayout>
Java Code
Dialog defaultDialog = new Dialog(this);
defaultDialog.setContentView(R.layout.dialog_test);
defaultDialog.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.dialog_background));
defaultDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
defaultDialog.setCancelable(true);
Dialog dialogWithBinding = new Dialog(this);
DialogTestBinding dialogTestBinding = DialogTestBinding.inflate(getLayoutInflater());
dialogWithBinding.setContentView(dialogTestBinding.getRoot());
dialogWithBinding.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.dialog_background));
dialogWithBinding.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialogWithBinding.setCancelable(true);
findViewById(R.id.showDefaultDialog).setOnClickListener(view -> {
TextView dialogTV = defaultDialog.findViewById(R.id.dialogTV);
dialogTV.setText("Dialog");
defaultDialog.show();
});
findViewById(R.id.showBindingDialog).setOnClickListener(view -> {
dialogTestBinding.dialogTV.setText("Dialog with Binding");
dialogWithBinding.show();
});
dialog_background is simple transparent shape
Results
The reason is because you added this line:
dialogWithBinding.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
The logic behind it is this line:
defaultDialog.setContentView(R.layout.dialog_test);
sets the window content view. And when you call setLayout it changes the layout_width and layout_height and overrides the values you put in the xml:
public void setLayout(int width, int height) {
final WindowManager.LayoutParams attrs = getAttributes();
attrs.width = width;
attrs.height = height;
dispatchWindowAttributesChanged(attrs);
}
So my suggestion is to remove the setLayout line of code.
So my problem is: when I add many items to the Recycler View, the button below which has to add more items, dissapear because he is pusshed off from the screen by the recycler. before add and after. There is no button :/
here is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="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"
tools:context=".AddActivityFormEvent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="14"
android:id="#+id/dateField"
android:singleLine="true"
android:hint="#string/data"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:drawableLeft="#drawable/ic_today"
android:onClick="showDatePickerDialog"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="time"
android:singleLine="true"
android:hint="#string/czas"
android:id="#+id/timeField"
android:layout_alignTop="#+id/dateField"
android:layout_toRightOf="#+id/dateField"
android:layout_toEndOf="#+id/dateField"
android:drawableLeft="#drawable/ic_time"
android:onClick="showTimePickerDialog"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/relativeLayout"
android:layout_below="#+id/dateField">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/activityRecyclerView"
android:elevation="15dp"
android:layout_below="#+id/relativeLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addActivityEvent"
android:text="Dodaj aktywność"
android:layout_gravity="bottom"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/testButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
By the way, how to get actual position of the view holder? I ask because when I delete item first time, it return correct index, but each next time it is one more, for example when I delete first item(index 0) it returns 1
here is an adapter with view holder:
public class ActivityFormEventAdapter extends RecyclerView.Adapter<ActivityFormEventAdapter.ActivityFormEventViewHolder>{
ArrayList<ActivityFormEvent> adapter_list = new ArrayList<>();
AddActivityFormEvent addActivityFormEvent;
private static Button button;
Context ctx;
public ActivityFormEventAdapter(ArrayList<ActivityFormEvent> adapter_list,Context ctx){
this.adapter_list=adapter_list;
this.ctx=ctx;
addActivityFormEvent= (AddActivityFormEvent) ctx;
button= (Button) addActivityFormEvent.findViewById(R.id.testButton);
}
public ActivityFormEventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_form_event_card,parent,false);
ActivityFormEventViewHolder activityFormEventViewHolder = new ActivityFormEventViewHolder(view,addActivityFormEvent);
return activityFormEventViewHolder;
}
#Override
public void onBindViewHolder(ActivityFormEventViewHolder holder, final int position) {
holder.activityName.setText(adapter_list.get(position).getActivityForm().getName());
holder.duration.setText(adapter_list.get(position).getTime());
holder.burnedKcalPerHour.setText(adapter_list.get(position).getActivityForm().getBurnedKcalPerHour());
holder.burnedKcal.setText(adapter_list.get(position).getBuriedKcal());
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
adapter_list.remove(position);
notifyItemRemoved(position);
}
});
}
#Override
public int getItemCount() {
return adapter_list.size();
}
public static class ActivityFormEventViewHolder extends RecyclerView.ViewHolder{
AddActivityFormEvent addActivityFormEvent;
EditText activityName;
EditText duration;
EditText burnedKcalPerHour;
EditText burnedKcal;
ImageButton deleteButton;
public ActivityFormEventViewHolder(View itemView,AddActivityFormEvent addActivityFormEvent) {
super(itemView);
activityName = (EditText) itemView.findViewById(R.id.activityName);
duration = (EditText) itemView.findViewById(R.id.duration);
burnedKcalPerHour = (EditText) itemView.findViewById(R.id.burnedKcalPerHour);
burnedKcal = (EditText) itemView.findViewById(R.id.kcalExercise);
deleteButton = (ImageButton) itemView.findViewById(R.id.deleteButton);
this.addActivityFormEvent = addActivityFormEvent;
}
}
}
Okay try this, here bottom view must have minimum height = height of Buttom that attached and it's height must be programmatically set to height of all view - recyclerview content height. Try to change height of bottom view to see the difference in preview.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout"
android:layout_below="#+id/dateField">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/linearLayout"
android:layout_above="#+id/bottom_space_holder"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activityRecyclerView"
android:elevation="15dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addActivityEvent"
android:text="Dodaj aktywność"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout"
android:layout_alignParentStart="true" />
<View
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="#+id/bottom_space_holder"
android:text="Dodaj aktywność"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
This happens because you place the Button below the RecyclerView using layout_below and the RecyclerView's height is set to wrap_content. This means that once the RecyclerView's content exceeds the screens height, the Button will fall off the screen, because it is placed below the RecyclerView.
To fix this issue, you should align your Button to the parent's bottom (using layout_alignParentBottom) and layout your RecyclerView above the button (using layout_above and layout_alignParentTop), this way the Button will always be placed and visible at the bottom of your screen.
I build a custom dialog to pop up an image with close button like this:
As you can see the dialog is not fitting the dialog layout and there's this white borer on the top and on the right, so how can i get rid the white border thing and make my custom dialog fit?
<?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="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<LinearLayout
android:id="#+id/linearMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="-50dp" >
<ImageView
android:id="#+id/imgMain"
android:layout_width="300dp"
android:layout_height="350dp"
android:gravity="center"
android:background="#drawable/eairh_dialog"
/>
<!---add your views here-->
</LinearLayout>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:clickable="true"
android:src="#drawable/close_button" />
</FrameLayout>
</LinearLayout>
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Use Above Code in your Dialog class to make Dialog TransParent
Remove margin Top and right on LinearLayout:
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
To make the Dialog bigger, you can set those parameters to MATCH_PARENT.
int screenWidth = 0, screenHeight = 0 ;
Display display = thid.getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_DeviceDefault);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.cart_empty);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = screenWidth;
lp.height = screenHeight;
dialog.getWindow().setAttributes(lp);
dialog.show();
I have a problem and I can't solve it, I will appreciate it if somebody would help me. I want to put two images on the top of the screen. Both images should have width = screen_width/2 (just because when the screen is smaller, images should resize).I've tried this, but it isn't enough. Thank you!
<RelativeLayout
android:id="#+id/imagelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/r"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:background="#drawable/back" />
<TextView
android:id="#+id/resulttext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:text="Hello"
android:layout_alignLeft="#id/r"
android:layout_alignTop="#id/r"
android:layout_alignRight="#id/r"
android:layout_alignBottom="#id/r"/>
</RelativeLayout>
For reference : http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0CC0QtwIwAg&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DmlojNcvoW68&ei=RoX4U8zwLdbvaKmOgMgH&usg=AFQjCNE5rRlHfVw-n_jgZAXEhK-Q1dd_Mg&sig2=mt8454k4FzqoVBprIqorNg&bvm=bv.73612305,d.d2s
<RelativeLayout //Change this to a linear layout
android:id="#+id/imagelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2" > //add this to linear layout attribute
<ImageView
android:id="#+id/r"
android:layout_width="0dp" //change width to 0dp (in case of horzontal layout)
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:background="#drawable/back"
android:layour_weight = "1"/> //add this to both view
<TextView
android:id="#+id/resulttext"
android:layout_width="0dp" //change width to 0dp (in case of horzontal layout)
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:text="Hello"
android:layout_alignLeft="#id/r"
android:layout_alignTop="#id/r"
android:layout_alignRight="#id/r"
android:layout_alignBottom="#id/r"
android:layour_weight = "1"/> //add this to both view/>
</RelativeLayout> //This again has to be linear layout
Use LinearLayout instead/inside RelativeLayout and set the weight property to 0.5 in each child view. you should also use layout:gravity to set the child view on the top.
Try this:
private int columnWidth;
//get Screen Size
public int getScreenWidth() {
int columnWidth ;
WindowManager wm = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
columnWidth = point.x;
return columnWidth;
}
//set height and width of Imageview
int height=columnWidth;
int width=columnWidth;
img.setLayoutParams(new RelativeLayout.LayoutParams(height,width));
Hope this may help you
Here is the XML for the layout in which I want my custom view to appear.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:id="#+id/widget273"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/csp_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Your Source"
android:textSize="40sp"
android:textColor="#ffc83200"
android:gravity="center"
android:paddingTop="15dip"
android:paddingBottom="75dip"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<com.bookcessed.booksearch.SearchProviderButton
android:id="#+id/csp_spb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/csp_tv_title"
rs:providerName="BOOKP" />
</RelativeLayout>
Here is the custom view's XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/pv_rl_strings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/pv_tv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_toRightOf="#+id/pv_tv_and"
android:textSize="18sp"
android:textColor="#ff11ab37"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/pv_tv_and"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and "
android:textSize="18sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#ff000000"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/pv_tv_browse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browse"
android:textSize="18sp"
android:textColor="#ff0077bb"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/pv_tv_and" />
<ImageView
android:id="#+id/pv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pv_tv_search"
android:layout_centerInParent="true"
android:layout_alignTop="#+id/pv_tv_and"
android:src="#drawable/bookp_logo" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/pv_rl_genres"
android:layout_width="wrap_content"
android:layout_below="#+id/pv_rl_strings"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/pv_genre1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<ImageView
android:id="#+id/pv_genre2"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre1" />
<ImageView
android:id="#+id/pv_genre3"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre2" />
<ImageView
android:id="#+id/pv_genre4"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre3" />
<ImageView
android:id="#+id/pv_genre5"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre4" />
<ImageView
android:id="#+id/pv_genre6"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre5" />
<ImageView
android:id="#+id/pv_genre7"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre6" />
<ImageView
android:id="#+id/pv_genre8"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre7" />
<ImageView
android:id="#+id/pv_genre9"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre8" />
</RelativeLayout>
</RelativeLayout>
Here is the class SearchProviderButton:
public class SearchProviderButton extends LinearLayout {
private Connector connector;
private Context context;
private View inflatedView;
public SearchProviderButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.SearchProviderButton);
connector = SearchProvider.valueOf(a.getString(R.styleable.SearchProviderButton_providerName)).getConnector();
this.context = context;
setFocusable(true);
setBackgroundColor(Color.WHITE);
setVisibility(VISIBLE);
//setOnClickListener(listenerAdapter);
setClickable(true);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
LayoutInflater li = LayoutInflater.from(context);
inflatedView = li.inflate(R.layout.provider_view, null);
ImageView logo = (ImageView)inflatedView.findViewById(R.id.pv_logo);
logo.setImageResource(connector.getLogoDrawableID());
TextView searchTV = (TextView)inflatedView.findViewById(R.id.pv_tv_search);
TextView andTV = (TextView)inflatedView.findViewById(R.id.pv_tv_and);
if(!connector.isSearchSupported()){
andTV.setText("");
searchTV.setVisibility(GONE);
}
setgenreIcons();
}
public Connector getConnector(){
return connector;
}
public void setConnector(Connector connector){
this.connector = connector;
}
private void setgenreIcons(){
int[] genreIconDrawables = {R.id.pv_genre1,R.id.pv_genre2, R.id.pv_genre3,
R.id.pv_genre4, R.id.pv_genre5, R.id.pv_genre6, R.id.pv_genre7,
R.id.pv_genre8, R.id.pv_genre9};
ArrayList<Genre> availgenre = connector.getAvailablegenres();
availgenre.remove(Genre.ALL);
int counter = 0;
for(int genreIVid : genreIconDrawables){
ImageView curgenreImageView = (ImageView)inflatedView.findViewById(genreIVid);
if(counter < availgenre.size() - 1){
curgenreImageView.setImageResource(availgenre.get(counter).getDrawable());
} else {
curgenreImageView.setVisibility(GONE);
}
counter++;
}
}
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
if (gainFocus == true){
this.setBackgroundColor(Color.rgb(255, 165, 0));
} else {
this.setBackgroundColor(Color.WHITE);
}
}
}
Here is the code for the class loading the xml that contains my custom component:
bookPServiceProviderButton = (SearchProviderButton)findViewById(R.id.csp_spb_1);
bookPServiceProviderButton.setOnClickListener(SPBOnClickListener);
bookPServiceProviderButton.setConnector(new bookPConnector());
bookPServiceProviderButton.setVisibility(View.VISIBLE);
EDIT: After the first comment, I added this code:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightSpec = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
Now it has a width and a height, but nothing is showing up inside of it!
Your custom layout view is not appearing because you're not putting anything into it. In your onFinishInflate you have the line inflatedView = li.inflate(R.layout.provider_view, null); But you don't add that to your view. You have two options to add the views to your custom layout view.
Change your custom view to extend RelativeLayout, change the enclosing RelativeLayout to <merge> in your provider_view.xml, and fix your findViewId lines in to this.findViewId(...) since the views will be inflated into your layout.
In your layout xml do:
<com.bookcessed.booksearch.SearchProviderButton
android:id="#+id/csp_spb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/csp_tv_title"
rs:providerName="BOOKP">
<!-- include this so it's added to your custom layout view -->
<include layout="#layout/provider_view" />
</com.bookcessed.booksearch.SearchProviderButton>
provider_view becomes:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/pv_rl_strings"
.
.
.
</merge>
SearchProviderButton:
public class SearchProviderButton extends RelativeLayout{
.
.
.
#Override
protected void onFinishInflate() {
super.onFinishInflate();
//the <include> in the layout file has added these views to this
//so search this for the views
ImageView logo = this.findViewById(R.id.pv_logo);
logo.setImageResource(connector.getLogoDrawableID());
TextView searchTV = (TextView)this.findViewById(R.id.pv_tv_search);
TextView andTV = (TextView)this.findViewById(R.id.pv_tv_and);
if(!connector.isSearchSupported()){
andTV.setText("");
searchTV.setVisibility(GONE);
}
setgenreIcons();
}
.
.
.
Or you could properly inflate the view in onCreate by layoutInflater.inflate(R.layout.provider_view, this, true). That call will inflate the referenced layout into the passed ViewGroup, in this case your custom view, and add the inflated Views to it. Then you can fix up the findViewId calls in your onFinishInflate.
TC... Sometimes it wont show in the preview unless you run it.
For wrap_content to work, you need to properly implement the measure and layout functions for your custom View. See How Android Draws Views for details on what these methods mean.
My guess would be that getMeasureWidth() and getMeasureHeight() functions for your view are always returning 0.