How to extend ImageButton correctly? - android

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"/>

Related

How can I add 'FontAwesome' icon on Android Action Toolbar Menu?

Is it possible to set an Icon in ActionBar "Text" using FontAwesome?
I have tried this way...
My menu item like that..
<item
android:id="#+id/action_chat"
android:orderInCategory="100"
android:title="#string/action_chat"
app:showAsAction="always"
android:actionLayout="#layout/chat_menu_icon"/>
My 'chat_menu_icon.xml'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<com.bitmakers.techmonster.textview.FontAusomeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:id="#+id/test"
android:gravity="center|bottom"
android:text="#string/action_chat"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
My Custom Font Class
package com.bitmakers.techmonster.textview;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class FontAusomeTextView extends TextView {
public static Typeface m_typeFace = null;
public FontAusomeTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
if (isInEditMode()) {
return;
}
loadTypeFace(context);
}
public FontAusomeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (isInEditMode()) {
return;
}
loadTypeFace(context);
}
public FontAusomeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (isInEditMode()) {
return;
}
loadTypeFace(context);
}
private void loadTypeFace(Context context) {
if (m_typeFace == null)
m_typeFace = Typeface.createFromAsset(context.getAssets(),
"fonts/fontawesome-webfont.ttf");
this.setTypeface(m_typeFace);
}
}
It is working when i using it in normal textview
You can use a nice library called android-iconify to achieve your goal.
Here is how you implement it on Toolbar/Actionbar items:
your_menu_layout.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:id="#+id/item_settings"
showAsAction="ifRoom"
android:title="#string/title"/>
</menu>
Inside your onCreateOptionsMenu method:
menu.findItem(R.id.item_settings).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_cog)
.colorRes(R.color.your_color)
.actionBarSize());
And you are good to go :)
Use Custom ActionBar
create your own layout and include that in xml using
<include.../>

Android: How to apply android:inputType globally on all EditText

how do I globally turn off auto-suggestions on all EditTexts of an application?
In other words, how do I do this android:inputType="text" on all EditText?
<EditText android:id="#+id/et1"
android:inputType="text">
</EditText>
you can create an custom edittext and use this customview in your layouts
package com.talha.examples;
import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.widget.EditText;
public class CustomEditText extends EditText {
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setInputType(int type) {
// TODO Auto-generated method stub
super.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
<com.talha.examples.CustomEditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
You manually modify each EditText. If you're creating them dinamically (eg. via code), then you already have references and can change the inputType at creation time.
If you're creating them via xml, you problably have just a couple of buttons to modify and it's not even worth the time posting on stackoverlfow about it.

Use tab to change focus in an android application

i am new to android programming.
I want to use TAB(in keyboard) to change focus to the next edittext box.
Please tell me how to do it.
Use the Xml attribute android:nextFocusDown in all your EditText
If you want to use this programmatically (without using Xml attribute)
private void setUpView(){
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
editText3=(EditText)findViewById(R.id.editText3);
}
private void setDownFocus(){
editText1.setNextFocusDownId(R.id.editText2);
editText2.setNextFocusDownId(R.id.editText3);// you can give focus to any id
editText3.setNextFocusDownId(R.id.editText1);
}
In onCreate() call setUpView() before setDownFocus()
You can use this XML on your layout
<EditText
android:id="#+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter Name"/>
<requestFocus />
I think that you want to intercept the TAB key press and jump to the next element on screen.
What I did was to extend EditText and override the "onKeyDown" method so it would send the focus to the next element.
Here is my simple extension to EditText:
package my.package;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.EditText;
public class TabExitEditText extends EditText {
public TabExitEditText(Context context) {
super(context, null);
}
public TabExitEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TabExitEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_TAB) {
focusSearch(FOCUS_RIGHT).requestFocus();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
In the layout you just set
<my.package.TabExitEditText
...
android:nextFocusRight="#id/nextRightFocusId"
/>
You can customize to use other focus direction order.

SurfaceView and error inflating class

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)

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