AppCompatImageView cannot be cast to my class - java

I have problem with my android java code, I hope you can help me with this.
I have my class with ImageView parent... some like this:
public abstract class BaseStatus extends ImageView
{
public BaseStatus(Context context){
super(context);
}
}
public class BluetoothStatus extends BaseStatus
{
public BluetoothStatus(Context context){
super(context);
}
}
and I want to get the component as ImageView from the XML, you know...
BluetoothStatus btStatus;
btStatus = (BluetoothStatus) findViewById(R.id.bt_status); // ERROR
XML:
<ImageView
android:layout_width="85dp"
android:layout_height="wrap_content"
android:id="#+id/bt_status"
android:src="#drawable/bluetooth"
android:layout_alignTop="#+id/button3"
android:layout_alignEnd="#+id/lb_laser" />
and i got this message:
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to net.antiradary.radarhunter.activity.status.BluetoothStatus
Why i can not cast ImageView into BluetoothStatus?
Thank you for you answers!

it was enough to do jus this...
<net.antiradary.radarhunter.activity.status.BluetoothStatus
android:layout_width="85dp"
android:layout_height="wrap_content"
android:id="#+id/iv_bluetooth"
android:src="#drawable/bluetooth"
android:layout_alignTop="#+id/button3"
android:layout_alignEnd="#+id/lb_laser" />
and in my class make constructor like this
class BluetoothStatus extends ImageView
{
public BluetoothStatus(Context context, AttributeSet attrs) {
super(context, attrs);
}
}

I know i am late to the party, but just in case some is banging his head for same problem, here is one possible solution that worked for me when i was trying to add zoom functionality in my pager adapter images.
Info: this is not the exact answer, but a possible solution for those who are facing the same problem. I am posting this because i didn't fount this solution approach anywhere in stackOverflow
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/head_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="#drawable/head_image" />
</RelativeLayout>
In the above code, just Replace <ImageView with <com.yourPackage.TouchImageView.
TouchImageView (or any other class) is a custom class (you need to make)which should extend android.support.v7.widget.AppCompatImageView

Related

GridLayout adding customView as a child issue, Android

i have made a simple timetable with GridLayout and it looks like this
Now the idea is to insert required subject into specific row and column. In order to achieve that i have created a class that extends CardView in which i need to insert TextView.
code:
TimeTable.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="8"
android:columnOrderPreserved="true"
android:rowCount="6"
tools:context="com.digiart.schoolapp.fragments.TimetableFragment">
<android.support.v7.widget.CardView
android:id="#+id/timetable_card_space"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_row="0"
app:cardElevation="2dp"
app:contentPadding="5dp">
<TextView
android:id="#+id/timetable_dummy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:visibility="invisible" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/timetable_day1_card"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="0"
app:cardElevation="2dp"
app:contentPadding="5dp">
<TextView
android:id="#+id/timetable_day1_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday"
android:textAppearance="#style/TextAppearance.AppCompat.Body2" />
</android.support.v7.widget.CardView>
.....
........
<android.support.v7.widget.CardView
android:id="#+id/timetable_time1_card"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="1"
app:cardElevation="2dp"
app:contentPadding="5dp">
<TextView
android:id="#+id/timetable_time1_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="09.00"
android:textAppearance="#style/TextAppearance.AppCompat.Body2" />
</android.support.v7.widget.CardView>
.....
......
</GridLayout>
TimetableSubject:
public class TimetableSubject extends CardView {
TextView subjectText;
public TimetableSubject(Context context,int column,int row,int columnSpan,String subjectName) {
super(context);
GridLayout.LayoutParams subjectParam =new GridLayout.LayoutParams();
subjectParam.columnSpec = GridLayout.spec(column,columnSpan);
subjectParam.rowSpec = GridLayout.spec(row);
subjectText = new TextView(context);
CardView.LayoutParams textParams = new CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
subjectText.setText(subjectName);
subjectText.setLayoutParams(textParams);
setLayoutParams(subjectParam);
}
}
Now i get what i need to do, i need to pass row and column to the custom view, and set those as layout params. the issue i think is with the Layout parameters code, i must be messing something up there. Could anyone explain how to set layout params for this situation properly? Thanks.
This is an age old ticket and I know this is marked as solved, but I'll just leave my experience here. I faced a somewhat similar issue: I was using custom views - extended RelativeLayout - inside GridLayout, I wanted to create a Grid with two columns and N rows (rows don't matter in my case). My custom views did not want to stretch horizontally, to take up the space they have. I have tried the support library, the Android X version of the library, weights, gravity - none of these worked.
It turns out that the GridLayout's parameters were set ok. My custom view was the problem. In my case there was no need to subclass a RelativeLayout, simply subclass a LinearLayout and you can still have whatever layout built inside.
Solution:
<androidx.gridlayout.widget.GridLayout
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_margin"
android:layout_marginEnd="#dimen/activity_margin"
app:columnCount="2">
<custom.MenuButtonGrid
android:id="#+id/item1"
app:abg_title="Title1"
app:layout_columnWeight="1"
app:layout_gravity="fill" />
<custom.MenuButtonGrid
android:id="#+id/item2"
app:abg_title="Title2"
app:layout_columnWeight="1"
app:layout_gravity="fill" />
<custom.MenuButtonGrid
android:id="#+id/item3"
app:abg_title="Title3"
app:layout_columnWeight="1"
app:layout_gravity="fill" />
</androidx.gridlayout.widget.GridLayout>
The implementation of MenuButtonGrid:
/**
* A component that creates a card view with an icon and texts.
* They are designed to use as buttons.
*/
class MenuButtonGrid #JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
private var binding =
LayoutButtonGridViewBinding.inflate(LayoutInflater.from(context), this, true)
init {
// do whaterver you want in here
}
}
The inside of layout_button_grid_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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="wrap_content"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
// Content doesn't matter here
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
You should go with TableLayout. In TableLayout you can create any number of rows and column. Go through the TableLayout documentation and read the concepts of rows and column. Here is a very nice tutorial for table layout.
This tutorial is also very helpful for you.
There was an issue with GridLayout weight parameter in Spec. Managed to solve the problem by inserting weight as a float parameter in Spec
Code:
public class TimetableSubjectView extends CardView {
TextView subjectText;
public TimetableSubjectView(Context context, int column, int row, float columnWeight, String subjectName, int color) {
super(context);
GridLayout.Spec rowI = GridLayout.spec(row,1,columnWeight);
GridLayout.Spec colI = GridLayout.spec(column,1,columnWeight);
GridLayout.LayoutParams subjectParam =new GridLayout.LayoutParams(rowI,colI);
subjectParam.setMargins(5,5,5,5);
setBackgroundColor(color);
subjectText = new TextView(context);
CardView.LayoutParams textParams = new CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
subjectText.setText(subjectName);
subjectText.setGravity(Gravity.CENTER);
subjectText.setLayoutParams(textParams);
this.addView(subjectText);
setLayoutParams(subjectParam);
}
}

Set tag abbreviations for custom views on XML

In my app I extend LinearLayout and RelativeLayout, so every time I need to declare a layout in XML (which you may know it's quite often) I need to write a long tag, in order to point to the view's package.
So the XML files look like this:
<com.company.material.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.company.material.widget.RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/titlebar_height"
android:background="#color/primary"
android:orientation="horizontal">
<TextView
style="#style/Text.Field.Small"
android:id="#+id/form_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button
style="#style/Button.Main"
android:id="#+id/form_submit"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</com.company.material.widget.RelativeLayout>
<com.company.essentials.view.FormView
android:id="#+id/formview"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
This is chaotic!
Is there any way to abbreviate this? Like AppLinearLayout instead of com.company.material.widget.LinearLayout?
Thanks in advance!
You could move your View class to the android.view package. This would allow you to just write <AppRelativeLayout /> instead of <com.company.material.widget.AppRelativeLayout />, since View tag names without package prefixes are 'auto-completed' to the android.view package.
If you don't want to move your whole class to this package, you may just create a dummy sub-class à la:
package android.view;
public class AppRelativeLayout extends com.company.material.widget.RelativeLayout {
// same constructors as super class
public AppRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
You just have to make sure you don't use the same class names as the Android framework already does, such as RelativeLayout, since that would clash with the existing views and layout names. That's why I named the example above AppRelativeLayout.
If it's worth it to you, you can customize your activity's layout inflater.
public class MyActivity extends Activity implements LayoutInflater.Factory {
#Override public void onCreate(Bundle _icicle) {
super.onCreate(_icicle);
setContentView(R.layout.my_activity);
}
#Override public Object getSystemService(String _name) {
Object service = super.getSystemService(_name);
if(LAYOUT_INFLATER_SERVICE.equals(_name)) {
LayoutInflater myInflater = ((LayoutInflater)service).cloneInContext(this);
myInflater.setFactory(this);
return myInflater;
}
return service;
}
#Override public View onCreateView (String _tag, Context _ctx, AttributeSet _as) {
if("mytag".equals(_tag))
return new MyLinearLayout(_ctx, _as);
else
return null;
}
}
setContentView will call getSystemService to obtain a layout inflater, and for each tag that layout inflater will query each of its factories (including our custom one) to see if the factory knows how to create the object that corresponds to that tag.
There is alternative option as well if you like you can do it like this
<view
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.company.material.widget.LinearLayout"
android:orientation="vertical">

ViewHolder views flash/blink OnBind

Issue
Every time I call the OnBind() method inside my holders, views within them will flash or blink or maybe even fade.
I'm just pretty lost as to why, I've tried a number of things.
Changing them to have null as a background or have them all be a solid color.
Making sure nothing is gone at any point.
Removed any animations.
Asked Google a thousand times, a thousand ways.
Searched on here.
Thoughts
I've always thought that I would have to eventually try and hide it with animations and styling. Seeing it as just a side effect of the binding.
This is part of a larger project, obviously. So I still have to do all the animations, that's why I haven't done it for this yet. Figured I'd just ask and see if anyone could help find the solution another way in the meantime.
Any help, tips or advice would be great. Hopefully the code links work and are build able for everyone.
Thanks,
Jon.
Code
I'm not sure how much code I need to add in here, so I'll go small at first. If more should be added please let me know and I will. However I put it all on Github and Dropbox(Example Apk & Zip).
Links are at the bottom.
HeaderHolder.java
public class HeaderHolder extends BaseHolder {
#Bind(R.id.header_title_text)
TextView _titleTextView;
#Bind(R.id.header_status_image)
ImageView _statusImageView;
#BindDrawable(R.drawable.ic_selected)
Drawable _statusSelected;
#BindDrawable(R.drawable.ic_non_selected)
Drawable _statusNonselected;
private Header _header;
public HeaderHolder(View root, HolderCallBacks callbacks) {
super(null, root, callbacks);
}
#Override
public void OnBind(Base model) {
this._header = (Header) model;
String n = model._name();
this._titleTextView.setText(n);
this._statusImageView.setImageDrawable(this._header._iconset()._selected()
? this._statusSelected : this._statusNonselected);
}
#OnClick(R.id.header_item_wrapper)
public void _headerClick(View view) {
this._callbacks.OnHolderClick(view, this._header);
}
}
IconsetHolder.java
public class IconsetHolder extends BaseHolder {
#Bind(R.id.iconset_icon_recycler)
RecyclerView _iconsRecycler;
private AdapterCallBacks _adapterCallbacks;
public IconsetHolder(Context context, View root, AdapterCallBacks callbacks) {
super(context, root, null);
this._adapterCallbacks = callbacks;
}
#Override
public void OnBind(Base model) {
Iconset i = (Iconset) model;
this._iconsRecycler.setLayoutManager(new GridLayoutManager(
this._context, i._span(), GridLayoutManager.HORIZONTAL, false));
this._iconsRecycler.setAdapter(new ModelsAdapter(i._icons(), this._adapterCallbacks));
}
}
item_header.xml
<?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:id="#+id/header_item_wrapper"
android:layout_width="wrap_content"
android:layout_height="56dip"
android:background="#595959"
tools:context=".views.adapters.holders.HeaderHolder">
<TextView
android:id="#+id/header_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:textColor="#fff8f8f8"
android:gravity="center_vertical"
android:text="ICONSET"
android:textSize="24sp"
android:layout_centerVertical="true" />
<ImageView
android:id="#+id/header_status_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="12dp"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
item_iconset.xml
<?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:id="#+id/iconset_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
tools:context=".views.adapters.holders.IconsetHolder">
<android.support.v7.widget.RecyclerView
android:id="#+id/iconset_icon_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:background="#595959"
android:layout_marginBottom="16dp"
/>
</RelativeLayout>
Example Links Removed
RecyclerView has some built in animations to it, using the DefaultItemAnimator. Specifically when you call notifiyItemChanged() it does a fade animation for the changing of the data in the ViewHolder. If you would like to disable this you can use the following:
RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator(); // your recycler view here
if (animator instanceof DefaultItemAnimator) {
((DefaultItemAnimator) animator).setSupportsChangeAnimations(false);
}
This will disable the item changed animation (the fade you are seeing).

Custom linear layout not showing

I am making a custom layout, but it's not showing, and i do not know why.
Here is the XML file where the class is defined
<com.example.name.gw2applicaton.SpecializationView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="65dp"
android:layout_height="match_parent">
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="yop2" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="yop2" />
</LinearLayout>
</com.example.name.gw2applicaton.SpecializationView>
Here is the class, just a constructor
public class SpecializationView extends LinearLayout {
public SpecializationView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.layout_specialization, this, true);
}
}
And finally where the class is used
<?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="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.example.name.gw2applicaton.SpecializationView
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
</com.example.name.gw2applicaton.SpecializationView>
</LinearLayout>
</LinearLayout>
The SpecializationView is not visible, I do not know why.
What am I doing wrong here?
That's not how it works for a custom view, as you are trying to do. Use this convention instead:
<?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="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- just include the layout you defined else where -->
<include layout="#layout/layout_specialization"/>
</LinearLayout>
Where layout_specialization.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="65dp"
android:layout_height="match_parent">
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="yop2" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="yop2" />
</LinearLayout>
Note: You should use custom view definitions when you need to modify an existing view or viewgroup to have special programatic functionality, such as positioning, dynamic content, niche widget, etc... When you want to use a view like you are where it is just using existing widget functionality, do as I described. The include xml tag is great for defining an xml layout and re-using it through your project so there is a minimized duplication of code.
EDIT:
The reason you layout is not showing by the way is you have only defined the constructor for programmatically creating a view (via java code, not xml). To allow for an xml definition of your custom view extend the class as follows with the additional constructors needd:
public class SpecializationView extends LinearLayout {
/* Programmatic Constructor */
public SpecializationView(Context context) {
super(context);
init(context, null, 0);
}
/* An XML Constructor */
public SpecializationView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
/* An XML Constructor */
public SpecializationView(Context context, AttributeSet attrs, int resId) {
super(context, attrs, resId);
init(context, attrs, resId);
}
/**
* All initialization happens here!
*/
private void init(Context context, AttributeSet attrs, int resId){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.layout_specialization, this, true);
}
}
This definition now includes the xml ability to create the custom view (which should now probably work for you). The reason it will work is now you send the attribute set, or the attributes definied via xml to the constructor. Since you didn't include it, it doesn't know what to do for your custom view when defined in xml and you cannot access the layout's attributes that you may define as custom.

Custom Views, to insert them dynamically to Layout in Java

I can't stress that often enough, I am new to Android and Java in general :-)
And these xml layouts are giving me headaches.
The code what you see consist of two ImageViews and two TextViews inside a RelativeLayout, together they form a layout which for me works as a "custom button". When I copy and paste it inside my layout it works almost the way I want.
How can I use this part of xml-layout dynamically in my code whenever I need a button like that and still be able to change certain properties, like the text inside the textviews?
I hope you understand what I mean, my first language is not english.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:src="#drawable/box" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/myImageView"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="S-"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/myImageViewText"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="your turn!"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="-10dp"
android:cropToPadding="false"
android:src="#drawable/icon" />
</RelativeLayout>
Ok to start extend your own view like this one:
I do have a Button made with an ImageView and a TextView in a LinearLayout designed in xml:
XML
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:adjustViewBounds="true"
android:padding="3dp"
android:scaleType="fitStart" />
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginLeft="10dp"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#android:color/white" />
</merge>
ViewObject called
ViewMenuButton
public class ViewMenuButton extends View{
private TextView tvText;
private ImageView ivImage;
public ViewMenuButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ViewMenuButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ViewMenuButton(Context context) {
super(context);
init();
}
private void init() {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//here you can inflate a own XML for that View
inflater.inflate(R.layout.view_menubutton, this, true);
this.tvText= (TextView)this.findViewById(R.id.tvText);
this.ivImage = (ImageView)this.findViewById(R.id.ivImage);
}
public void setText(String text){
if(this.tvText != null){
tvText.setText(text);
}
}
//... and so on
}
Whenever you want to use it in your xml make sure to give the View the complete package like this:
Usage XML
<com.your.package.views.ViewMenuButton
android:id="#+id/menu_bt_local"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:background="#drawable/action_button"
android:textSize="#dimen/text_cell" >
If you want to use it in a code just make it like this:
Usage JAVA
LinearLayout rootView =
(LinearLayout) findViewById(R.id.mainLayout); //Or sth like this
ViewMenuButton vmb = new ViewMenuButton(this);
rootView.add(vmb);
//or if you already have it in XML
ViewMenuButton vmb = (ViewMenuButton) findViewById(R.id.myVmbtID);
You can even go more in detail defining your own attributes to use in XML, like setting source of the Image, changing Text, changing TextColor etc pp Tutoiral
Create your custom View subclass. Inflate your layout in it's constructor, find necessary views there and create setters/getters of necessary properties. Then each time you need this custom button you'll be able to create it through code or xml using this subclass. If you need to be able to change some properties from xml too you may want to declare styleable attributes for your custom view. I think you can find lots of tutorials about how to create custom views.
Here's the common tutorial. Here you can read about custom attributes. And finally here is the best example of what I mentioned above.
You can inflate your layout and add it to another layout
RelativeLayout layout = new RelativeLayout(this);
View childButton = getLayoutInflater().inflate(R.layout.child_button, null);
layout.addView(childButton);
Now you can find the child views in the button layout as
TextView textViewInnerChild = (TextView)childButton.findViewById(R.Id. textInnerView):
Then you can change the value or properties of the inner child views
textViewInnerChild.set text("your text")

Categories