I want load some Custom Views within my RelativeLayout but don't know how. The code I've tried doesn't work hence does anyone know how to do this correctly?
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/textView0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="20dp"
style="#android:style/TextAppearance.Medium" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="2dp"
style="#android:style/TextAppearance.Medium"
android:layout_below="#id/textView0" />
<View
android:id="#+id/drawing0"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#id/textView1" />
<View
android:id="#+id/drawing1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="2dp"
android:layout_below="#id/drawing0" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Large"
android:layout_below="#id/drawing1" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Large"
android:layout_marginStart="10dp"
android:layout_below="#id/drawing1"
android:layout_toEndOf="#id/textView2" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium"
android:layout_below="#id/textView3" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium"
android:layout_marginBottom="30dp"
android:layout_below="#id/textView4" />
</RelativeLayout>
</ScrollView>
Shapes.java
public class Shapes extends android.support.v4.app.Fragment {
public Shapes() {
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.shapes, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
View v = getView();
assert v != null;
//blue shape drawing
View cv0 = (View)v.findViewById(R.id.drawing0);
v.addView(new BlueShape(getActivity()));
//green shape drawing
View cv1 = (View)v.findViewById(R.id.drawing1);
v.addView(new GreenShape(getActivity()));
super.onActivityCreated(savedInstanceState);
}
}
BlueShape.java
public class BlueShape extends View {
private final Paint mBlue = new Paint();
...
}
GreenShape.java
public class GreenShape extends View {
private final Paint mGreen = new Paint();
...
}
refer to https://developer.android.com/guide/topics/ui/custom-components.html
specifically under
Modifying an Existing View Type
Use the Custom Component
you can change the xml in your RelativeLayout
<View...
to
<your.package.GreenShape...
if you do not know beforehand what the View is going to be, then change it to
<FrameLayout... instead of <View...
and then add the child (GreenShape or BlueShape) programmatically to that Frame layout
cv0, cv1 are not ViewGroup.
Try to change cv0, cv1 to ViewGroup and move onActivityCreated's code to onViewCreated callback method.
here is reference site.
android/view/ViewGroup
I think that the problem is that you are using v.addView() instead of
cv0.addView()
But I would suggest to use the following instead of your View:
<LinearLayout
android:id="#+id/drawing1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="2dp"
android:layout_below="#id/drawing0" />
then, inside the Java code:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
LinearLayout cv0 = (LinearLayout)getView().findViewById(R.id.drawing0);
cv0.addView(new BlueShape(getActivity()), 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
Related
I have a custom layout for the list's cells where you have a checkbox and 3 text elements. I found on Stackoverflow that I should add android:descendantFocusability = "blocksDescendants" on my LinearLayout where the list is but it doesn't change anything.
Here is my XML for the page with the list :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cl_fragment_detail_apero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
tools:context=".ui.home.AperoDetailFragment"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:gravity="left"
android:textSize="18sp" />
<TextView
android:id="#+id/date_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:ems="10"
android:gravity="right"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtView_shopping_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:text="#string/shopping_list_title" />
<Button
android:id="#+id/btn_add_ingredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:clickable="true"
android:focusable="true"
android:padding="15dp"
android:src="#android:drawable/ic_menu_add"
android:text="#string/btn_add_ingredient"
android:textColor="#FFFFFF" />
</LinearLayout>
<ListView
android:id="#+id/list_ingredient"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" />
</LinearLayout>
then in my Java code I set the listener like this :
#Override
public View onCreateView(#NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
final TextView name = (TextView) root.findViewById(R.id.name_apero);
name.setText(detailApero.name);
TextView date = (TextView) root.findViewById(R.id.date_apero);
date.setText(detailApero.date);
// some code that aren't useful right now
//-------------- List of ingredients for the detailed apero
final ListView list_ingredient = (ListView) root.findViewById(R.id.list_ingredient);
LaperoDatabase db = Room.databaseBuilder(root.getContext(),
LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();
IngredientDao dbIngredient = db.getIngredientDao();
//#TODO control if we get the right ingredients
ArrayList<Ingredient> ingredient_of_this_apero = (ArrayList<Ingredient>) dbIngredient.getIngredientsFromApero(detailApero.aid);
IngredientAdapter adapter = new IngredientAdapter(root.getContext(), R.layout.ingredient_cell_layout, ingredient_of_this_apero);
list_ingredient.setAdapter(adapter);
list_ingredient.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Ingredient selected_ingredient = (Ingredient) adapter.getItemAtPosition(position);
Log.e("test","okasodkdaso");
}
});
return root;
}
I also tried using a OnItemLongClickListener but it also does nothing. How can I put a listener for the list items ?
Im trying to add an ImageButton to each row in my ListView (which I implemented via CustomAdapter). Each button should open a different WebView. I can't seem to find any tutorials to do this so any advice/tips/links would be highly apreciated.
EDIT 1:
My Listview XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_x="15dp">
<ImageView
android:layout_width="30dp"
app:srcCompat="#drawable/a"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:id="#+id/local"
android:layout_height="30dp"
android:layout_alignBottom="#+id/versus"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/resultsiz"
android:textColor="#android:color/white"
android:textStyle="normal|bold"
android:gravity="center_vertical"
android:elevation="15dp"
android:layout_alignBaseline="#+id/resultsde"
android:layout_alignBottom="#+id/resultsde"
android:layout_alignRight="#+id/resultsde"
android:layout_alignEnd="#+id/resultsde"
android:layout_marginRight="61dp"
android:layout_marginEnd="61dp" />
<ImageView
android:layout_width="15dp"
app:srcCompat="#drawable/a"
android:id="#+id/versus"
android:layout_height="15dp"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_alignBottom="#+id/visit"
android:layout_toRightOf="#+id/local"
android:layout_toEndOf="#+id/local" />
<ImageView
android:layout_width="30dp"
app:srcCompat="#drawable/a"
android:id="#+id/visit"
android:layout_height="30dp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_alignBottom="#+id/resultsiz"
android:layout_toRightOf="#+id/versus"
android:layout_toEndOf="#+id/versus" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/local"
android:layout_alignStart="#+id/local"
android:id="#+id/dia"
android:textColor="#color/white"
android:textStyle="normal|bold"
android:textAllCaps="true" />
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/resultsde"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp"
android:textColor="#android:color/white"
android:layout_marginTop="19dp"
android:textStyle="normal|bold"
android:textAlignment="viewEnd"
android:gravity="end"
android:layout_width="70dp"
android:layout_below="#+id/dia"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/local"
android:layout_toRightOf="#+id/local"
android:layout_toEndOf="#+id/local"
android:layout_marginTop="155dp"
android:id="#+id/stats" />
</RelativeLayout>
And my CustomAdapter:
protected void onPostExecute(Void aVoid) {
CustomAdapter customAdapter = new CustomAdapter();
lista.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter{
#Override
public int getCount() {
return resultsizq.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
view = getLayoutInflater().inflate(R.layout.customlayout,null);
ImageView versus = (ImageView)view.findViewById(R.id.versus);
ImageView local = (ImageView)view.findViewById(R.id.local);
ImageView visit = (ImageView)view.findViewById(R.id.visit);
TextView dia= (TextView)view.findViewById(R.id.dia);
TextView resultsiz= (TextView)view.findViewById(R.id.resultsiz);
TextView resultsde= (TextView)view.findViewById(R.id.resultsde);
dia.setText(di[position]);
resultsiz.setText(resultsizq[position]);
resultsde.setText(resultsder[position]);
versus.setImageResource(versu[position]);
local.setImageResource(loc[position]);
visit.setImageResource(vis[position]);
if ((position+1)%4==0){
view.setPadding(0,0,0,150);
}
if ((position)%4==0){
view.setPadding(0,150,0,0);
}
return view;
}}
In the getView method, you should assign the onClick function to the Button according to the given position. Probably, you should get the item located at position, generate some url based on the properties of this item, and let this url be opened in the onClick function.
More than that, I have 3 suggestions to improve your code:
Try to use RecyclerView instead of ListView.
Even if you use ListView, have a look at View Holder pattern. This is a good place to start.
Consider using Butterknife, which increases the readability of your code. If I were you, I would keep the item as a property in the view holder object, which has a function annotated with #OnClick(R.id.stats) so that it opens the preferred WebView accordingly.
I hope these help.
I'm trying to get an activity to host a fragment, but after inflating the view of the fragment, when I try to access any widgets via findViewById(), it returns null. So in this case mBillTitle is always null, I am not sure what I am missing?
<LinearLayout 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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#+id/bill_title"
android:textSize="24dp"
android:gravity="left"/>
....
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#+id/bill_detail_title"
android:textSize="20dp"
android:paddingTop="5dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp">
....
</LinearLayout>
...
</LinearLayout>
And this is the fragment code
#Override
public void onCreate(Bundle savedInstanceBundle) {
super.onCreate(savedInstanceBundle);
mBill = new Bill();
}
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup parent,
Bundle savedInstanceBundle) {
View view = inflater.inflate(R.layout.bill_details, parent, false);
mBillTitle = (TextView) view.findViewById(R.id.bill_title);
mBillTitle.setText(mBill.getmShortTitle());
...
return view;
}
android:text="#+id/bill_title"
suppose to be
android:id="#+id/bill_title"
Change android:text to android:id.
I'm creating a small animation app using Android studio. When a button press images should work according to the particular animation.
There's an error with line viewGroup = (viewGroup)findViewById(R.id.ViewGroup);
It shows "Cannot resolve symbol ViewGroup". I typed Alt+Enter and selected a solution. But nothing happened.
public class MainActivity extends Activity {
private ViewGroup viewGroup;
private ImageView imageView, imageView2, imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9,imageView10,imageView11,imageView12;
private Button button,button1;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
imageView= (ImageView) findViewById(R.id.imageView);
imageView2= (ImageView) findViewById(R.id.imageView2);
imageView3= (ImageView) findViewById(R.id.imageView3);
imageView4= (ImageView) findViewById(R.id.imageView4);
imageView5= (ImageView) findViewById(R.id.imageView5);
imageView6=(ImageView) findViewById(R.id.imageView6);
imageView7=(ImageView) findViewById(R.id.imageView7);
imageView8=(ImageView) findViewById(R.id.imageView8);
imageView9=(ImageView) findViewById(R.id.imageView9);
imageView10=(ImageView) findViewById(R.id.imageView10);
imageView11=(ImageView) findViewById(R.id.imageView11);
imageView12=(ImageView) findViewById(R.id.imageView12);
button = (Button)findViewById(R.id.button);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TransitionManager.beginDelayedTransition(viewGroup, new Fade());
fade(imageView, imageView2, imageView3,imageView4,imageView5,imageView6);
}
});
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TransitionManager.beginDelayedTransition(viewGroup, new Slide());
toggle(imageView7,imageView8,imageView9,imageView10,imageView11,imageView12);
}
});}
private static void toggle(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
}
private static void fade(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
Here's my activity_main xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/myAniLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button1"
android:layout_alignBottom="#+id/button"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#mipmap/ic_launcher"
android:layout_alignTop="#+id/imageView2"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView7"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#android:drawable/star_big_on" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView8"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView9"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView10"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView9"
android:layout_alignStart="#+id/button1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView11"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView8"
android:layout_alignEnd="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView12"
android:src="#android:drawable/star_big_on"
android:layout_above="#+id/button"
android:layout_alignStart="#+id/imageView4" />
Your logcat shows
"Cannot resolve symbol 'ViewGroup'"
viewGroup=(viewGroup)findViewById(R.id.ViewGroup); // (ViewGroup)
viewGroup Not declare in your XML
(viewGroup) must be (ViewGroup) . (viewGroup)
The ViewGroup class is a subclass of the View class. ViewGroup
instances work as containers for View instances to group View
instances together.
http://developer.android.com/intl/es/reference/android/view/ViewGroup.html
I don't see an element ViewGroup in the xml. You need to add it in the same xml or include it.
As per your xml there is not an any id with ViewGroup in your xml code. please give id to your layout with ViewGroup and cast it as below,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
there was also a spelling mistake in your line,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
As they pointed out, you have no R.id.ViewGroup in your layout.
If you really need the view group try this:
viewGroup = (ViewGroup)findViewById(android.R.id.content)
The problem is that you have,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
and you have to change it for this,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
The V must be a capital letter.
Also watch for the id of your Viewgroup in your xml, check if it is R.id.Viewgroup or R.id.viewgroup
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.