I write a simple extend view class,the code is following:
public class MyView extends View
{
public MyView(Context context)
{
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
protected void OnDraw(Canvas canvas)
{
super.onDraw(canvas);
}
}
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" >
<com.main.sufaceview.MyView
android:id="#+id/myview"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#drawable/tt"/>
</RelativeLayout>
When entry the virtual machine. It shows the breakdown:
why this is? i think the custom view should very easy.I did not add other code,it has the error.
Edit:the logcat displays:
I think you need another constructor, at least, thats what I read in other posts.
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
Related
I am able to set image in Non-Custom ImageView but unable to set Image in Custom ImageView, app loads successfully but unable to see the Image, below is the code.
public class MainActivity : AppCompatActivity
{
public ImageTestView imageView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
imageView = FindViewById<ImageTestView>(Resource.Id.imageView1);
imageView.SetImageResource(Resource.Mipmap.line_indent);
}
}
<RelativeLayout
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">
<ImageViewTestProject.ImageTestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView1" />
</RelativeLayout>
public class ImageTestView : ImageView
{
public ImageTestView(Context context,IAttributeSet attr) :
base(context,attr)
{
}
}
Update
In the end, adding the image background through XML ended up solving the issue.
android:background="#mipmap/imagename"
Well I think the reason it's not working is that of the unavailable constructors and improper initialization
public class ImageTestView : ImageView
{
Context mContext;
public ImageTestView (Context context) : base(context)
{
init(context, null);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
{
init(context, attrs);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
init(context, attrs);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
init(context, attrs);
}
private void init(Context ctx, Android.Util.IAttributeSet attrs)
{
mContext = ctx;
}
}
Then use this custom imageview in your app like:
<RelativeLayout
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">
<ImageViewTestProject.ImageTestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView1" />
</RelativeLayout>
And then you can set the image resource to it like this:
var imageView = FindViewById<ImageTestView>(Resource.Id.imageView1);
imageView?.SetImageResource(Resource.Mipmap.line_indent);
Revert in case this does not work or in case of queries
1.First thing you not doing anything in custom ImageTestView class.
2.And setting the image Resource is wrong.either you have to taking from Drawable folder it should be.
imageView.SetImageResource(Resource.Drawable.line_indent);
I don't know in Xamarin Android.But if you know Android please refer this Example.it's bit similar to xamarin Android
http://www.java2s.com/Code/Android/UI/extendsImageView.htm
I am trying to create this:
But unfortunately I'm only being able to get this
This question is not about the shadow or the font, I can easily adjust those I guess. What is driving me crazy is to remove the margins (or padding) between the blue line and the edges of the marker.
The relevant code is:
On ClusterRenderer that extends DefaultClusterRenderer
#Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
markerOptions.
icon(BitmapDescriptorFactory.fromBitmap(prepareIcon(item))).
position(new LatLng(item.getPosition().latitude, item.getPosition().longitude)).
anchor(iconGenerator.getAnchorU(), iconGenerator.getAnchorV());
super.onBeforeClusterItemRendered(item, markerOptions);
}
private Bitmap prepareIcon(MyItem item) {
markerDecoratorView.setPrice(getPriceSpannedText(item.getPrice()));
iconGenerator.setContentView(markerDecoratorView);
iconGenerator.setContentPadding(-2, -2, -2, 0); // This does not make any difference
return iconGenerator.makeIcon();
}
The custom view
public class MarkerDecoratorView extends LinearLayout {
#Bind(R.id.marker_item_price)
TextView markerItemPrice;
public MarkerDecoratorView(Context context) {
super(context);
init(context);
}
public MarkerDecoratorView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MarkerDecoratorView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MarkerDecoratorView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(Context context) {
inflate(context, R.layout.map_marker_view, this);
this.setOrientation(VERTICAL);
if (isInEditMode()) return;
ButterKnife.bind(this);
}
public void setPrice(SpannableString text) {
markerItemPrice.setText(text);
}
public void setPrice(String text) {
markerItemPrice.setText(text);
} }
}
The view layout is:
<?xml version="1.0" encoding="utf-8"?>
<merge 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:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/tab_indicator" />
<TextView
android:id="#+id/marker_item_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/MapMarker"
android:gravity="center"
android:padding="4dp"
tools:text="SAR 300" />
</merge>
EDIT: I tried also with a 9patch in the custom view (xml and class). It works in Android Studio preview, but not inside the marker.
EDIT 2: I worked this out by adding a regular icon, without 9patch. I keep this question open because this topic might be relevant to others. So if anyone has the solution please post! :)
What is correct way to extend EditText?
The problem is following:
I have empty application from template with two EditText:
<?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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="one"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="two"/>
</LinearLayout>
It works fine:
Then I create my custom view from EditText:
public class CuteEditText extends EditText {
public CuteEditText(Context context) {
this(context, null);
}
public CuteEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CuteEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// some special initialization will be here
}
}
And when I change EditText to my CuteEditText, interface works incorrectly:
The problem is not only with view ot UI. If I type something in first EditText and than touch the second one, nohing happens: input will continue in first.
The same behaviour if I inherite CuteEditText from AppCompatEditText.
What is wrong?
Sources for experiment are available at https://github.com/tseglevskiy/EditTextExperiment
Your construtors are broken. This is how it should look:
public class CuteEditText extends EditText {
public CuteEditText(Context context) {
super(context);
}
public CuteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CuteEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
You don't need the third constructor overload. The first one is for creating the view programmatically and the second one is for creating the view from xml. Those two should be enough for most cases.
public class CuteEditText extends EditText {
public CuteEditText(Context context) {
this(context, null);
}
public CuteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
Why I can not put and see a custom view in a layout?
public class MainActivity extends Activity {
A a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = (A) findViewById(R.id.a);
setContentView(a);
}
class A extends View {
Paint paint;
A(Context context) {
super(context);
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawRect(10, 20, 60, 100, paint);
}
}
}
And please see the following image:
View A is an inner class of the Activity class, so the defination in the layout file is a little different.
Use this pattern:
<view class="{package}.{ParentClass}${innerClass}" />
In your case:
<view class="com.example.tec.test.MainActivity$A" />
Notice it's lowercase "view" not "View". And the inner class A should be declared as "public static" like this:
public class MainActivity extends Activity {
// Activity's methods
public static class A extends View {
public A(Context context) {
this(context, null);
}
public A(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public A(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// A's methods
}
}
Override the below constructor on your view class and see if it works. When you include the view in xml you need to include the xml constructor in the view.
View(Context context, AttributeSet attrs)
public A(Context context, AttributeSet attrs) {
super(context, attrs);
}
public A(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
I solved my problem using your help.Thank you. The following code is for other questioner.
<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">
<view class="com.example.tec.test.A"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/a"
/>
</RelativeLayout>
And the following java code:
public class MainActivity extends Activity {
A a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a= (A) findViewById(R.id.a);
a = new A(this);
setContentView(a);
}
}
class A extends View {
Paint paint;
public A(Context context) {
super(context);
}
public A(Context context, AttributeSet attrs) {
super(context, attrs);
}
public A(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas) {
paint = new Paint();
paint.setColor(Color.RED);
canvas.drawRect(10, 20, 60, 70, paint);
}
}
In my activity I have a custom made control. In onCreate I inflate the activity's view as normal with:
setContentView(R.layout.image_viewer);
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<MyCustomImageView
android:id="#+id/tivImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
And here is a reduced piece of code for MyCustomTextView:
public class MyCustomImageView extends ImageView
{
Context context;
public MyCustomImageView(Context context)
{
super(context);
sharedConstructing(context);
}
public MyCustomImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
sharedConstructing(context);
}
}
The exception that gets generated reads:
android.view.InflateException: Binary XML file line #7: Error inflating class MyCustomImageView
you need to add the package name:
<com.my.package.MyCustomTextView
android:id="#+id/tivImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
You have to create constructors calling the superclass constructors, because they will be called by the inflater:
public class MyView extends View {
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
And you have to put the full package name in your XML:
<com.mypackagename.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />