In DrawingTheBall class making some animation stuff..
In SUrfaceViewExample class Touchevent will be detecetd..
my problem is i am not able to link MainActivity and SurtfaceViewExample ..
no issues with DrawingTheBall..
MAIN CLASS:...
package maddy.first;
import android.app.Activity;
import android.os.Bundle;
public class Madhu1Activity extends Activity {
/** Called when the activity is first created. */
Drwwingtheball v;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v = new Drawingtheball(this);
setContentView(v);
}
}
CLASS SurfaceViewExample:
package maddy.first;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class SurfaceViewExample extends Activity implements OnTouchListener{
OurView v;
Bitmap ball;
float x,y;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v=new OurView(this);
v.setOnTouchListener(this);
ball=BitmapFactory.decodeResource(getResources(),R.drawable.tennis_ball);
x = y = 0;
setContentView(v);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable{
Thread t;
SurfaceHolder holder;
boolean isItOk=false;
public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder=getHolder();
}
public void run() {
// TODO Auto-generated method stub
while( isItOk ==true)
{
//drawing
if(holder.getSurface().isValid()) {
continue;
}
Canvas c=holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(ball, x-(ball.getWidth()), y-(ball.getHeight()), null);
holder.unlockCanvasAndPost(c);
}
}
public void pause()
{
isItOk=false;
while(true) {
try {
t.join();
}catch(InterruptedException e) {
e.printStackTrace();
}
break;
}
}
public void resume()
{
isItOk=true;
t=new Thread(this);
t.start();
}
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
Class DrawingTheBall:
package maddy.first;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class DrawingTheBall extends View {
Bitmap bball;
int x,y;
public DrawingTheBall(Context context) {
super(context);
bball=BitmapFactory.decodeResource(getResources() ,R.drawable.tennis_ball);
x = 0;
y = 0;
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Rect ourRect=new Rect();
ourRect.contains( 0, 0,canvas.getWidth(),canvas.getHeight()/2);
Paint blue=new Paint();
blue.setColor(Color.RED);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(ourRect, blue);
if(x < canvas.getWidth())
x+=10;
else
x=0;
if(y<canvas.getHeight())
y+=10;
else
y=0;
Paint p=new Paint();
canvas.drawBitmap(bball,x,y,p);
invalidate();
}
}
}
Your question is not clear. What actually do you want to link?
You want to start activity from another another?
Or you want to pass some parameter from one to another activity?
If you want to run SurfaceViewExample activity from Main activity -
This is the code -
startActivity(new Intent(this, SurfaceViewExample.class)
finish();
And AndroidManifest.xml file should be -
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="Madhu1Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="SurfaceViewExample"></activity>
</application>
And if you want to pass parameter from one activity to another -
This is the code -
startActivity(new Intent(this, SurfaceViewExample.class)
.putExtra("key", [value])); // if you want to pass class the class should be Serializable, otherwise you can pass value like a hash map.
//in other activity convert it to class-
<classname> obj = (<classname>)getIntent().getSerializableExtra("key");
If this are not sufficient then tell me what actually want.
Enjoy..
I think it throws some exception otherwise your layout has some error, so that it is unable to set the content view.
This is the easiest way to keep your SurfaceViewExample class as main activity follow it -
goto application AndroidManifest.xml file and then -
Application tab -> select launch activity(that is your main activity) ->
Name*(right side) -> browse(take for a while to loading activity list) - >
select SurfaceViewExample -> remove previous SurfaceViewExample activity if already added.
set a default layout in SurfaceViewExample setContentView(layout.main); // if it exists
And run your application to check it is able to show your layout.
If it can run successfully, then check out your DrawingTheBall code.
Have fun...
Related
I was trying to run some test on SurfaceViewer with SurfaceViewer having its own thread and everything worked just fine. Then I changed my code to change orientation of screen using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
In the code below: I have an activity which uses Fragments. I have another class which extends SurfaceView class. Problem starts when I add setRequestedOrientation. After I add setRequestedOrientation. Value of running at in FastRender Class run method is always false. If I comment line with setRequestedOrientation , it runs fines.
package com.example.testpractise;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
public class SurfaceViewTest extends ActionBarActivity {
static int counter;
static {
Log.i("Intializing class Surface Test","counter");
System.out.println("Statrting here");
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
counter++;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
counter++;
Log.i("Counter vlase",String.valueOf(counter));
System.out.println("value of counter"+counter);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_surface_view_test);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.surface_view_test, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
FastRenderView renderView;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View rootView = inflater.inflate(
// R.layout.fragment_surface_view_test, container, false);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
BitmapCollection.initializeBitMapCollection(getActivity());
renderView = new FastRenderView(getActivity());
return renderView;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
renderView.resume();
}
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
renderView.pause();
}
static class FastRenderView extends SurfaceView implements Runnable {
Thread renderThread = null;
SurfaceHolder holder;
static volatile boolean running = false;
int x=0;
int y=0;
public FastRenderView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
public void pause()
{
running = false;
while(true)
{
try{
renderThread.join();
}catch (InterruptedException i){
}
}
}
public void resume()
{
running = true;
renderThread = new Thread(this);
renderThread.setName("RendererThread");
renderThread.start();
running=true;
}
#Override
public void run() {
// TODO Auto-generated method stub
int sleepCounter=0;
while(!running)
{
try {
if(sleepCounter <5)
{
sleepCounter++;
Thread.sleep(5000);
}else{
break;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while(running)
{
if(!holder.getSurface().isValid())
{
continue;
}
Canvas canvas=holder.lockCanvas();
canvas.drawRGB(255, 0, 0);
holder.unlockCanvasAndPost(canvas);
}
}
}
}
}
Sorry if my question doesn't match the format. This is my first time. Any pointers on what is wrong and why would be greatly appreciated.
Thanks
If you just want to lock the Activity to landscape, you can do it in Manifest by adding the android:screenOrientation="landscape" attribute to the activity element.
My app consist on moving a picture when the user speaks. I have done this, but what I want to do now is that I want to set a image as background for my app. I am working with canvas, as you can see in the class I have included below. So how can I set a background using canvas and not influencing the movement of my picture. Or is there any possibility to connect this class with a xml file where I can define the background?
Thanks in advance
Here is the class:
package com.example.prova1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MoveBalloon extends Activity {
Bitmap balloon;
DrawBalloon myView;
float x,y,sensorX, sensorY;
SensorManager sm;
Microphone mic;
public class DrawBalloon extends SurfaceView implements Runnable {
SurfaceHolder ourHolder ;
Thread ourThread = null;
boolean isRunning=true;
public DrawBalloon(Context context) {
super(context);
ourHolder= getHolder();
}
public void pause() {
isRunning=false;
while(true){
try{
ourThread.join();
} catch (InterruptedException e){
e.printStackTrace();
}
break;
}
ourThread=null;
}
public void resume(){
isRunning=true;
ourThread = new Thread(this);
ourThread.start();
}
#Override
public void run (){
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
updateMic();
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(balloon, sensorX, sensorY,null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/*sm= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(o);
sm.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
}*/
mic = new Microphone();
balloon = BitmapFactory.decodeResource(getResources(), R.drawable.images);
sensorX=150;
sensorY=350;
//x=y=sensorX=sensorY=0;
myView= new DrawBalloon (this);
myView.resume();
setContentView(myView);
}
/*#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensorX=event.values[0];
sensorY=event.values[1];
}
*/
public void updateMic(){
int level = mic.getLevel();
sensorY-=level;
}
#Override
public void onBackPressed() {
finish();
}
}
You can use an xml layout instead of setting content view with a java object :
setContentView(R.layout.move_balloon);
and add your custom view (DrawBalloon) in the xml layout by specifying it's package location, and change the background by setting an image to the background of the root element :
<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"
tools:context=".MoveBalloon"
android:background="#drawable/ic_launcher"
>
<com.example.prova1.MoveBalloon.DrawBalloon
android:id="#+id/drawBalloon1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="182dp" />
</RelativeLayout>
but for this to work , your custom view must have a special constructor in able to be inflated from xml layout file :
public DrawBalloon(Context context, AttributeSet attrs) {
super(context, attrs);
ourHolder= getHolder();
}
Hey guys i am getting to start an intent if a user clicks on specific location.at first touch he opens a menu and on second he opens the activity.The problem is that many copy's of same intent are started
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
public class gfx extends Activity implements OnTouchListener{
Bitmap a,b;
gfx1 drw;
String a1;
boolean flag=false,flag1=false,flag2=false;
Canvas c1;
float x=0,y=0,z=0,bitx=0,bity=0;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
drw = new gfx1(this);
drw.setOnTouchListener(this);
setContentView(drw);
}
public class gfx1 extends View implements Runnable {
public gfx1(Context context) {
super(context);
// TODO Auto-generated constructor stub
a = BitmapFactory.decodeResource(getResources(),
R.drawable.greenball);
}
void callin(String a1)
{
Intent inte = new Intent(a1);
startActivity(inte);
}
#Override
protected void onDraw(Canvas c1) {
// TODO Auto-generated method stub
super.onDraw(c1);
c1.drawColor(Color.YELLOW);
b=Bitmap.createScaledBitmap(a,c1.getWidth()/3, c1.getHeight()/3, true);
bitx=(c1.getWidth()/2)-(a.getWidth()/2);
bity=(c1.getHeight()/2)-(a.getHeight()/2);
c1.drawBitmap(a, bitx, bity, null);
Paint textPaint = new Paint();
textPaint.setARGB(50, 254, 10, 50);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(30);
if(flag)
{
c1.drawText("clicked",300, 300,textPaint);
c1.drawBitmap(b,(c1.getWidth()/2)-(b.getWidth()/2),(c1.getHeight()/2)+(a.getHeight()/2), null);
c1.drawBitmap(b,(c1.getWidth()/2)-(b.getWidth()/2),(c1.getHeight()/2)-(a.getHeight()/2)-(b.getHeight()), null);
}
float bitbx1=(c1.getWidth()/2)-(b.getWidth()/2);
float bitbx2=(c1.getWidth()/2)+(b.getWidth()/2);
float bitby1=(c1.getHeight()/2)-(b.getHeight())-(a.getHeight()/2);
float bitby2=((c1.getHeight()/2)-(a.getHeight()/2));
if(flag2)
{
c1.drawText("Opening...please wait", 600, 600, textPaint);
flag1=true;
if(flag1)
{
a1="com.example.claci.MAINACTIVITY";
callin(a1);
}
flag1=false;
}
if((x>bitx&&x<bitx+(a.getWidth()))&& (y>bity&&y<bity+(a.getHeight())))
{
flag=true;
}
if((x>bitbx1&&x<bitbx2)&&(y>bitby1&&y<bitby2))
{
if(flag)
{
flag2=true;
}
}
invalidate();
}
#Override
public void run() {
// TODO Auto-generated method stub
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
x=event.getX();
y=event.getY();
break;
}
return true;
}
}
A much better structure would be to have your menu creation, and subsequent activity creation called from onTouch method. As psink mentioned, that is not the proper use of onDraw, and it unnecessarily links tow very unrelated things.
I would also use two class variables-- one that is a flag for if the menu exists yet, and another that is a reference to the newly created activity. If an activity already exists, you don't create another one. When the existent activity completes, though, it needs to return a result to this activity so you can clear that reference and be ready to create a new one when needed.
Where in this code would I put a thread delay, that will happen after the completion of onCreate(), which means also after the completion/showing of onDraw()? Afterwards I will be calling grid.clearPattern() which clears the pattern drawn on the canvas when grid.displayPattern() was called. So afterwards I will still need to be able to modify the canvas.
package com.patterns;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class PlayGame extends Activity implements View.OnTouchListener {
int size;
Grid grid;
PatternView patternview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
size = getIntent().getExtras().getInt("size");
patternview = new PatternView(this);
setContentView(patternview);
Handler pauser = new Handler();
pauser.postDelayed(new Runnable() {
public void run() {
patternview.clearDraw();
}
}, 2000);
patternview.setOnTouchListener(this);
}
public class PatternView extends View {
Paint paint = new Paint();
public PatternView(Context context){
super(context);
}
protected void clearDraw() {
Log.d("debug", "clearDraw called");
grid.clearPattern();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
grid = new Grid(size, size, getWidth(), getWidth(), canvas, paint);
grid.createPattern();
grid.displayPattern();
Log.d("debug", "lines drawn");
grid.setBoard();
Log.d("debug", "board set");
}
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
//Log.d("debug", "screen touched");
grid.screenTouch(arg1);
grid.fillActiveRectangles();
return false;
}
}
Maybe stick the call to grid.clearPattern() into an android.os.Handler? Had a similar app-pausing problem and this did the trick for me. So stick something like this at the end of onCreate() -- the 3500 is a pause in milliseconds, choose the value that you want.
Handler pauser = new Handler();
pauser.postDelayed (new Runnable() {
public void run() {
grid.clearPattern();
}
}, 3500);
Could it be like this?
grid.createPattern();
grid.displayPattern(canvas, paint);
Thread.sleep(2000);
But it will be a pain...
Ok so I am really new to Android, and to get going with things, I paid a developer to build me a simple app, and now I'm trying to wade through it to make sense of it.
The problem I am trying to solve is the initial launch screen the app goes to. I got this to change by going into the Android manifest file and changing this value:
<activity android:label="#string/app_name" android:name="Home">
to this value:
<activity android:label="#string/app_name" android:name="Languages">
And when I deploy the app to my device, it loads the Languages class just fine! The problem is that when I leave the app and then try and go back in, it doesn't even try to launch the app, it just goes back to the home screen.
There isn't anything that tells me what is going on, and attaching the debugger for after the app is initially closed won't work either.
Does anybody have any ideas as to what might be going on?
Here is the code from the Android Manifest file:`
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:label="#string/app_name" android:name="Language">
<intent-filter>
<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="About"></activity>
<activity android:theme="#android:style/Theme.Light" android:name="Language"></activity>
<activity android:name="Volume" android:theme="#android:style/Theme.Light"></activity>
<activity android:name="Book" android:theme="#android:style/Theme.Light"></activity>
<activity android:name="Chapter" android:theme="#android:style/Theme.Light"></activity>
<activity android:name="Verse" android:theme="#android:style/Theme.NoTitleBar"></activity>
<activity android:name="Settings"></activity>
</application>
`
Then here is the code for the Home class which originally launched first:`package com.elan.reader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.elan.reader.util.DatabaseHelper;
import com.elan.reader.util.DatabaseHelper_Spa;
public class Home extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
Button aboutBtn,readBtn;
Home me;
DatabaseHelper myDatabaseAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
me=this;
aboutBtn = (Button) findViewById(R.id.about);
aboutBtn.setFocusable(true);
aboutBtn.setOnClickListener(this);
readBtn = (Button) findViewById(R.id.read);
readBtn.setFocusable(true);
readBtn.setOnClickListener(this);
myDatabaseAdapter = DatabaseHelper.getInstance(me);
if(!myDatabaseAdapter.databaseReady()){
try{
myDatabaseAdapter.copyDatabase2();
DatabaseHelper_Spa myDatabaseAdapter_spa = DatabaseHelper_Spa.getInstance(me);
myDatabaseAdapter_spa.copyDatabaseSpanish2();
}catch(Exception e){
e.printStackTrace();
}
myDatabaseAdapter.databaseReady();
}else{
//myDatabaseAdapter.close();
if(myDatabaseAdapter.getSaveVerse()[0]!=null||!myDatabaseAdapter.getSaveVerse()[0].equals("")){
String data[]=myDatabaseAdapter.getSaveVerse();
Intent intent=new Intent(Home.this, Verse.class);
intent.putExtra("language",data[0]);
intent.putExtra("volume_id",data[1]);
intent.putExtra("chapter",data[2]);
intent.putExtra("book",data[3]);
intent.putExtra("book_id",data[4]);
intent.putExtra("multiple_languages",false);
startActivity(intent);
}
}
}
#Override
public void onClick (View view){
if(view==aboutBtn){
startActivity(new Intent(Home.this, About.class));
}else if(view==readBtn){
ProgressBar seek = (ProgressBar) findViewById(R.id.seek);
seek.setVisibility(View.VISIBLE);
TextView msg = (TextView) findViewById(R.id.loading);
msg.setVisibility(View.VISIBLE);
new Thread(){
public void run(){
Looper.prepare();
if(myDatabaseAdapter.getSaveVerse()[0]==null||myDatabaseAdapter.getSaveVerse()[0].equals("")){
startActivity(new Intent(Home.this, Language.class));
}else{
String data[]=myDatabaseAdapter.getSaveVerse();
Intent intent=new Intent(Home.this, Verse.class);
intent.putExtra("language",data[0]);
intent.putExtra("volume_id",data[1]);
intent.putExtra("chapter",data[2]);
intent.putExtra("book",data[3]);
intent.putExtra("book_id",data[4]);
intent.putExtra("multiple_languages",false);
startActivity(intent);
}
Looper.loop();
}
}.start();
}
}
}`
And then here is the code for the new class I need to launch first:`package com.elan.reader;
import java.util.ArrayList;
import com.elan.reader.util.DatabaseHelper;
import com.elan.reader.util.DatabaseHelper_Spa;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Language extends ListActivity {
/** Called when the activity is first created. */
private ArrayList<SettingsObject> m_orders = null;
private AboutAdapter m_adapter;
private Runnable viewOrders;
String[] listData={"English","French"};
//Bitmap[] listImage;
private Runnable returnRes = new Runnable() {
#Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_orders.size();i++)
m_adapter.add(m_orders.get(i));
}
m_adapter.notifyDataSetChanged();
}
};
ImageView backBtn;
static boolean running=false;
static long startTime=0;
static Chronometer timer ;
static long hit_id=-1;
static int stopped=0;
Context me;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title_list_menu);
me=this;
DatabaseHelper myDatabaseAdapter;
myDatabaseAdapter = DatabaseHelper.getInstance(me);
if(!myDatabaseAdapter.databaseReady()){
try{
myDatabaseAdapter.copyDatabase2();
DatabaseHelper_Spa myDatabaseAdapter_spa = DatabaseHelper_Spa.getInstance(me);
myDatabaseAdapter_spa.copyDatabaseSpanish2();
}catch(Exception e){
e.printStackTrace();
}
myDatabaseAdapter.databaseReady();
}else{
//myDatabaseAdapter.close();
if(myDatabaseAdapter.getSaveVerse()[0]!=null||!myDatabaseAdapter.getSaveVerse()[0].equals("")){
String data[]=myDatabaseAdapter.getSaveVerse();
Intent intent=new Intent(Language.this, Verse.class);
intent.putExtra("language",data[0]);
intent.putExtra("volume_id",data[1]);
intent.putExtra("chapter",data[2]);
intent.putExtra("book",data[3]);
intent.putExtra("book_id",data[4]);
intent.putExtra("multiple_languages",false);
startActivity(intent);
}
}
// listImage=new Bitmap[]{BitmapFactory.decodeResource(getResources(), R.drawable.sharing),BitmapFactory.decodeResource(getResources(), R.drawable.contact_us),BitmapFactory.decodeResource(getResources(), R.drawable.about)};
m_orders = new ArrayList<SettingsObject>();
this.m_adapter = new AboutAdapter(this, R.layout.language_row, m_orders);
setListAdapter(this.m_adapter);
viewOrders = new Runnable(){
#Override
public void run() {
getOrders();
}
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
}
#Override
protected void onListItemClick(ListView li, View v, int position, long id) {
super.onListItemClick(li, v, position, id);
Intent intent=new Intent(Language.this, Volume.class);
intent.putExtra("language",""+position);
startActivity(intent);
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("onRestart");
//loadImages();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println(" onResume");
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("onStart");
}
private class AboutAdapter extends ArrayAdapter<SettingsObject> {
private ArrayList<SettingsObject> items;
public AboutAdapter(Context context, int textViewResourceId, ArrayList<SettingsObject> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.language_row, null);
}
final SettingsObject o = items.get(position);
if (o != null) {
TextView title = (TextView) v.findViewById(R.id.title);
if (title != null) {
title.setText(o.getName());
}
}
return v;
}
}
private void getOrders(){
try{
m_orders = new ArrayList<SettingsObject>();
for(int i=0;i<listData.length;i++){
SettingsObject temp = new SettingsObject(listData[i]);
m_orders.add(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(returnRes);
}
class SettingsObject {
private String name;
public String getName() {
return name;
}
public SettingsObject(String name) {
this.name = name;
}
}
}
`
Not sure if this is the cause of your problem but in your AndroidManifest.xml you have 2 activity entries for Language. You should remove the second entry.
<activity android:theme="#android:style/Theme.Light" android:name="Language"></activity>
Try adding Log.d outputs in different parts of the code to follow what is being executed and what does not.
What does myDatabaseAdapter.databaseReady(); do? Why do you have the following code?
if(!myDatabaseAdapter.databaseReady()) {
// try catch bock
myDatabaseAdapter.databaseReady();
}
What does startActivity(intent); do in the else part of the same if block?
In your settings>>developer options>> select app to be debugged, you should probably just change that to no app, because the app would only start if you have a debugger from eclipse attached to your device. If you change it to No Application, the app can be launched at any time you wish. I hope it helps. :)