Android : Android Custom View is not loading

on Saturday, October 11, 2014


I am new to android app development , trying to learn custom view development . Below is the simple layout where i have created a button , click of which will launch Game activity .Game activity has custom layout . My issue is , on click of the button , it starts new activity but it do not shows up anything . It loads with empty layout .I am expecting android to load a circle on click of button.


MainActivity.java



package com.shashi.customview2;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;


public class MainActivity extends Activity implements OnClickListener
{

@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View gamebutton=(View) findViewById(R.id.hi);
gamebutton.setOnClickListener(this);
}

public void onClick(View v)
{
Intent intent =new Intent(this,Game.class);
startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}




***game.java***



package com.shashi.customview2;

import android.app.Activity;

import android.os.Bundle;

public class Game extends Activity
{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

setContentView(R.layout.custlayout);

}

}

***Custom View class:***


package com.shashi.customview2;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Paint.Style;

import android.util.AttributeSet;

import android.view.View;

public class custview extends View
{

private int circleCol;

private Paint circlePaint;



public custview(Context context ,AttributeSet attrs)
{
super(context,attrs);
circlePaint = new Paint();
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.custview, 0, 0);
try {
//get the text and colors specified using the names in attrs.xml
circleCol = a.getInteger(R.styleable.custview_circleColor, 0);
}
finally
{
a.recycle();
}

// TODO Auto-generated method stub
}


protected void OnDraw(Canvas canvas)

{

int viewWidthHalf = this.getMeasuredWidth()/2;

int viewHeightHalf = this.getMeasuredHeight()/2;

int radius = 0;

if(viewWidthHalf>viewHeightHalf)

radius=viewHeightHalf-10;

else
radius=viewWidthHalf-10;


circlePaint.setStyle(Style.FILL);
circlePaint.setAntiAlias(true);

circlePaint.setColor(circleCol);

canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);

}


}

***custom Layout:***


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:custom="http://schemas.android.com/apk/res/com.shashi.customview2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<com.shashi.customview2.custview
android:id="@+id/custView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
custom:circleColor="#ff0099"/>


</LinearLayout>

0 comments:

Post a Comment