i want to create a custom view class. But I get an error by run the app.
here my class:
package test.start;
import android.util.AttributeSet;
import android.view.*;
import android.graphics.*;
import android.content.Context;
public class control extends View{
private Paint paint;
public control(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
init();
}
public void init(){
paint = new Paint();
paint.setTextSize(12);
paint.setColor(0xFF668800);
paint.setStyle(Paint.Style.FILL);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawText("TEEEST", 100, 100, paint);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
this.setMeasuredDimension(150,200);
}
}
and the main.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:id="#+id/textView1" android:text="#string/text" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/tableLayout1">
<Button android:id="#+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:background="#drawable/custom_button" android:layout_weight="1"></Button>
<Button android:layout_width="wrap_content" android:id="#+id/button1" android:layout_height="40dip" android:text="#string/button" android:layout_weight="1"></Button>
<test.control
android:id="#+id/control"
android:layout_width="match_parent"
android:layout_height="match_parent"> </test.control>
</TableLayout>
</LinearLayout>
Error Message:
Unable to start activity
ComponentInfo{de.me.start/test.start.StartActivitiy}:
android.view.InflateException: Binary
XML file line #10: Error inflating
class test.start.control
Blockquote
But i can view the control in the graphical layout.
Try providing another version of the constructor:
public control(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
You should declare it in the XML in the following way:
<view class="de.test.start.control"
android:id="#+id/control"
android:layout_width="match_parent"
android:layout_height="match_parent"> </view>
Related
android.view.InflateException: Binary XML file line #6: Error inflating class com.duplicatefilefixer.SimilarPicturesPkg.astickyheader.ui.SquareImageView
I stuck in this I don't know they this coming because I have the class on name path in same package.
This is Java class
package com.duplicatefilefixer.SimilarPicturesPkg.astickyheader.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to width
}
}
And this is my 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.duplicatefilefixer.SimilarPicturesPkg.astickyheader.ui.SquareImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_margin="2dp"
android:src="#drawable/empty_photo" />
<LinearLayout
android:gravity="center"
android:layout_alignBottom="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/size_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:background="#1758B6"
android:text="5KB"
android:gravity="center"
android:visibility="gone"
android:padding="5dp"/>
</LinearLayout>
<CheckBox
android:focusable="false"
android:button="#drawable/duplicate_checkbox"
android:id="#+id/img_chk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image"
android:layout_alignRight="#+id/image"
android:layout_alignEnd="#+id/image"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:layout_marginTop="5dp"/>
</RelativeLayout>
I have created an Activity and applied Theme with Orange colour. I created a custom View object containing a background image and 2 TextView objects. 1 TextView is on Left side and another is on Right side.
I just placed that custom view as Header view on activity.
Background of TextView must be that background image, but it shows Theme orange color as its background.
My Code:
Header.java:
package com.example.themes;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
public class Header extends LinearLayout{
public Header(Context context) {
super(context);
init();
}
public Header(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Header(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init(){
LayoutInflater layInflator = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layInflator.inflate(R.layout.header_layout, this);
}
}
header_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imgView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/action_bar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="left" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="right" />
</RelativeLayout>
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.themes.MainActivity" >
<com.example.themes.Header
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
MainActivity.java:
package com.example.themes;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
You have multiple ways to do that
Use android:scaleType="fitXY" or
Use android:background="#drawable/image" instead of android:src
And the best is to remove that ImageView and apply android:background="#drawable/image" on the RelativeLayout
I have a edit text in which the user enters amount. What I want to do is set a textview value before it that is not editable by the user like "INR" and then the user will enter the amount in front of it. I want edittext to look like the below one. How can I do that?
<EditText
android:id="#+id/Eamnt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="180dp"
android:ems="10"
android:inputType="numberDecimal"
Try to use RelativeLayout which contains a TextView and an EditText. I did something below, try it.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#android:drawable/editbox_background" >
<TextView
android:id="#+id/constant_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal"
android:textStyle="bold"
android:textColor="#C0C0C0"
android:text="INR"
android:background="#android:color/transparent" />
<EditText
android:id="#+id/amount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/constant_text"
android:textColor="#000000"
android:textStyle="bold"
android:background="#android:color/transparent"
android:paddingLeft="5dp"
android:text="100 000" />
</RelativeLayout>
You can use a RelativeLayout contaiing a TextView(for the hint) and an EditText for text input.
This code shows the example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="PasswordHint"
android:textColor="#aaaaaaaa"
android:textSize="30dip" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentLeft="true"
android:background="#00000000"
android:textSize="30dip" >
</EditText>
</RelativeLayout>
My suggestion is just use a background picture, which draws the text you want. Use the 9-patch to limit where user can input. This is not hint, but it can sloved your problem.
MyEditText.java
package com.example.myapplication24.UI;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import com.example.myapplication24.R;
public class MyEditText extends androidx.appcompat.widget.AppCompatEditText {
private String mhint = "";
private Paint mPaint;
private Context mContext;
private float paddingLeft = 0 ;
public MyEditText(Context context) {
super(context);
this.mContext = context;
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.MyEditText);
mhint = a.getString(R.styleable.MyEditText_mhint);
setPadding((int)(mPaint.measureText(mhint)),getPaddingTop(),getPaddingRight(),getPaddingBottom());
a.recycle();
}
public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
init();
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyEditText,defStyleAttr,0);
mhint = a.getString(R.styleable.MyEditText_mhint);
setPadding((int)(mPaint.measureText(mhint)),getPaddingTop(),getPaddingRight(),getPaddingBottom());
a.recycle();
}
private void init() {
mPaint = new Paint();
mPaint.setTextSize(getTextSize());
mPaint.setColor(Color.GRAY);
paddingLeft = getPaddingLeft();
}
private int dp2px(float dp) {
return (int) (mContext.getResources().getDisplayMetrics().density * dp + 0.5f);
}
#Override
protected void onDraw(Canvas canvas) {
//Drawing mhint
canvas.drawText(mhint,dp2px(5),getHeight()*0.65f,mPaint);
super.onDraw(canvas);
}
}
layout.xml
<com.example.myapplication24.UI.MyEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mhint="Username: "
android:text="asd"
></com.example.myapplication24.UI.MyEditText>
attrs.xml
<!--my hint-->
<attr name="mhint" format="string"/>
<declare-styleable name="MyEditText">
<attr name="mhint"/>
</declare-styleable>
</resources>
Hope it can be helpful for latecomers...
demo
I would like to create a full screen header for Android.
I created the image that is 70 * 480 px (& 9patch).
how can I do?
I tried using the galaxy tab .. but it does not fill the screen horizontally.
This is the code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/aaa.bbb"
android:id="#+id/db1_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
<ImageView
android:src="#drawable/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
>
</ImageView>
thanks in advance.
You need to set the android:scaleType and optionally android:adjustViewBounds. Play around with these two attributes to get the desired effect.
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
Use this code to add your ImageView to the xml file
<shush.android.util.AspectImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/header" />
Add this class:
package shush.android.util;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
/**
* #author Sherif elKhatib
*
*/
public class AspectImageView extends View {
public AspectImageView(Context context) {
super(context);
}
public AspectImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AspectImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getBackground().getIntrinsicHeight()
/ getBackground().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
try this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/aaa.bbb"
android:id="#+id/db1_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
<ImageView
android:src="#drawable/header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
>
</ImageView>
I am coding an Android game in which 2 players play at the same time. One player faces the phone in a normal way and the other faces it upside down.
As it is a quiz, I didn't use any canvas or graphics. It contains only 2 linear layouts and one of them is supposed to be upside down. For this I have used:
android:rotation="180"
for one of the layouts.
it showed upside down on the graphical view of my xml in Eclipse but when I run it on an emulator or a phone it is not inverted or rotated to 180 degrees.
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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:clipToPadding="false"
android:rotation="180">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout
Java Code
public class x extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
How can I rotate it without complication. I am presently using api level 15 but i have no restriction on API level.
I would suggest that you create a custom Layout that will handle the rotation on it's onDraw()
public class RotatedLinearLayout extends RotatedLinearLayout {
final boolean topDown;
public RotatedLinearLayout (Context context){
super(context);
}
public RotatedLinearLayout (Context context, AttributeSet attrs){
super(context, attrs);
}
public RotatedLinearLayout (Context context, AttributeSet attrs, int defStyle){
super(context, attrs,defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
#Override
protected void onDraw(Canvas canvas){
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
canvas.save();
if(topDown){
canvas.translate(getWidth(), 0);
canvas.rotate(180);
}else {
canvas.translate(0, getHeight());
canvas.rotate(-180);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
}
When you write your XML, use the VerticalRelativeLayout instead of the other layout you tried to create.
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<path.to.package.RotatedLinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:clipToPadding="false"
>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</path.to.package.RotatedLinearLayout >
</LinearLayout