SurfaceView and error inflating class - android

I was trying to make an app with a custom view, and i kept getting "error inflating class".
It must be that I am missing some of the basics when it comes to custom views, but I am not sure what. Here is a very simple program with a custom view, what more is needed to make it work?
(Notes: For the sake of this question, I put SurfaceView class inside of the Activity Class. This was not the situation in the larger application. I do not show the AndroidManifest.xml file here, but it is just what was generated by the wizard in eclipse.)
Here is the java:
package com.mypackage;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
public class SimpleCustomViewActivity extends Activity {
class TheView extends SurfaceView{
private static final String TAG = "TheView";
public TheView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(TAG,"TheView(" + context + "," + attrs + ")");
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_layout);
TheView v = (TheView) findViewById(R.id.myview);
}
}
Here is file res/layout/simple_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mypackage.SimpleCustomView.TheView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

When u call your own surfaceView class from the xml file u need to add the following public surfaceView creating methods:
public GameView(Context context) {
super(context);
init(context);
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
If u are using the function setContentView(gv) you only need the first one.

in xml it should be:
<com.mypackage.SimpleCustomView.TheView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.mypackage.SimpleCustomView.TheView>

I believe something like this may work though I haven't tested it:
<View
class="com.mypackage.SimpleCustomView$TheView"
id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

: declare two methods and it should be public!!
public TheView(Context context)
public TheView(Context context, AttributeSet attrs)

Related

how to get font folder in xml or java

hi i want set font but i have a problem
i use calligraphy library for changing font
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/IRANSansMobile.ttf")
.build()))
but this get font from Assets folder!
now i want change bottom navigation font but now i cant access Assets folder!
<style name="Widget.BottomNavigationView" parent="Widget.Design.BottomNavigationView">
<item name="fontFamily">#myfont/iransansmobile</item>
</style>
so this is my question:
1.how can i set font in setDefaultFontPath from res/myfont folder?
2.how can i set font in from Assets folder?
Hello i found this solution from How to use custom font in android xml?
package com.vins.test;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"your_font.ttf");
setTypeface(tf);
}
}
XML:
<com.vins.test.MyTextView
android:id="#+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="This is a text view with the font u had set in MyTextView class "
android:textSize="30dip"
android:textColor="#ff0000"
>
Good luck.

View Custom Layout in grapical layout edit screen

This is my source of testing Custom Layout (shows image and label).
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/screen_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3535ff"
android:orientation="vertical"
android:padding="1dp" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="20dp" >
<ImageView
android:id="#+id/screen_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#000000"
android:scaleType="centerInside"
android:src="#drawable/cam_1_20130117_105601_118" />
<TextView
android:id="#+id/screen_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/screen_image"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:background="#95000000"
android:gravity="center_vertical|center_horizontal"
android:text="사출성형기 1호기"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
JAVA CODE
package com.example.testlayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.testscreen.R;
public class CustomScreen extends LinearLayout {
LinearLayout mLayout = null;
TextView mLabel = null;
ImageView mImage = null;
public CustomScreen(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomScreen(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomScreen(Context context) {
super(context);
init();
}
void init() {
LayoutInflater inflater = (LayoutInflater)getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.screen, this, true);
// mLabel = (TextView)findViewById(R.id.screen_label);
// mImage = (ImageView)findViewById(R.id.screen_image);
if (isInEditMode()) {
return;
}
}
}
With this code, I checked on Nexus One. It displays well.
The problem is on editor mode. Exactly on xml editer with preview.
When I add this view, error message appeared.
The following classes could not be instantiated :
- com.example.testlayout.CustomScreen (Open Class, Show Error Log) See the Error Log (Window > Show View) for more details. Tip: Use
View.isInEditMode() in your custom views to skip code when shown in
Eclipse
I want to check it on edit mode.
How can I check well like other views?
you have to add (!isInEditMode()) in each constructor. which tells if it not in edit Mode then initialized the stuff
if(!isInEditMode())
init(context);
and for initializing stuff follow this
also have a look at this

Custom Font on a Android Widget

I'm trying to create a digital clock widget with custom typeface font.
I got clues from here
Solution was by extending TextView and put new attribute for custom font typeface. This is a good solution compared to drawing Canvas and Bitmap solution then pass it to RemoteViews.
I followed every step,
1. create custom class CustomFont.java
2. define style in attrs.xml
3. then put it in main.xml
but I got the following errors
WARN/AppWidgetHostView(18681): updateAppWidget couldn't find any view, using error view
WARN/AppWidgetHostView(18681): android.view.InflateException: Binary XML file line #17: Error inflating class upper.duper.widget.circular.clock.CustomFont
...
...
...
WARN/AppWidgetHostView(18681): Caused by: java.lang.ClassNotFoundException: upper.duper.widget.circular.clock.CustomFont in loader dalvik.system.PathClassLoader
Something missing?
Here I attach the codes
This is CustomFont.java
package upper.duper.widget.circular.clock;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class CustomFont extends TextView {
private static final String TAG = "CustomFont";
public CustomFont(Context context) {
super(context);
}
public CustomFont(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFont);
String customFont = a.getString(R.styleable.CustomFont_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
And this is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:updup="http://schemas.android.com/apk/res/upper.duper.widget.circular.clock"
android:background="#00000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<AnalogClock android:id="#+id/AnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dial="#drawable/widgetdial_white"
android:hand_hour="#drawable/widgethour"
android:hand_minute="#drawable/widgetminute"/>
<upper.duper.widget.circular.clock.CustomFont
android:id="#+id/TIME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:layout_gravity="center"
android:singleLine="true"
android:ellipsize="none"
android:textSize="12sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
updup:customFont="ALPNMAIN.TTF" />
And I put my custom font (ALPNMAIN.TTF) on assets folder already.
This is attrs.xml
<resources>
<declare-styleable name="CustomFont">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
Now I feel this is not possible to have something "custom" for app widget.
Look here
upper.duper.widget.circular.clock.CustomFont
You appear to be attempting to use a custom class in a layout for an app widget. This is not supported.

How to extend ImageButton correctly?

I want to extend the functionality of the ImageButton class, in a new class I choose to call "DialButton". To start off, I simpy extend ImageButton, and added nothing new. In my mind, this should be identical to the normal ImageButton class.
package com.com.com;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageButton;
public class DialButton extends ImageButton{
public DialButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public DialButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public DialButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
}
I insert a DialButton in XML (dont worry about the rest of the file, its irrelevant)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow
android:layout_weight="1">
<DialButton
android:id="#+id/button1"
android:background="#drawable/dialpad"
android:layout_weight="1"/>
And use the XML in my activity:
package com.com.com;
import android.app.Activity;
import android.os.Bundle;
public class Android3 extends Activity {
DialButton button1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maintable);
}
}
Everything compiles and installs in the emulator but when the app starts it force closes on me. If I change the XML component from a DialButton to an ImageButton, everything works fine. Why is this? What is the difference between the ImageButton class and my DialButton class that causes the DialButton component to crash the app?
In your layout file, you have to provide a 'fully-qualified' name for your custom widgets as follows...
<com.mycompany.myapp.DialButton
android:id="#+id/button1"
android:background="#drawable/dialpad"
android:layout_weight="1"/>

Forced Close after adding custom ImageView

I'm trying to add a custom ImageView to my main.xml, but if I start the program it closes with a forced close.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background" >
<test.testpkg.CustomImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:src="#drawable/bg"/>
</LinearLayout>
Java:
package test.testpkg;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.widget.ImageView;
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
Also if I start the program in Debugger after the FC I only get this:
link text
Debugger is useless if you haven't attached the source code of Android. Moreover... it's more useful to provide the logcat output. Anyway, I think you are missing one of the constructors. Try this:
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
// rest of your code....

Categories

Resources