First I have to admit that I am a beginer with the android programing and may not understand things correctly.
Secondly, the problem:
In my app, I've created a loading screen, and with a theard I have tried to restrict the time of this screen so when the time is over - it's move via intent to another screen.
The code:
public class MainActivity extends Activity implements OnClickListener {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
iv.setOnClickListener(this);
}
public void onClick(final View iv) {
// TODO Auto-generated method stub
Thread t1=new Thread(new Runnable() {
#Override
public void run() {
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
}
});
t1.start();
try {
t1.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t1.stop();
//The moving to the other screen
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
} }
The intent itself work, as well as the animation of the loading_screen. However when I writed "t1.stop();" in order to stop the thread - it earsed it.
I understand. I forgot to use finish() and to proceed I have the option of "finally".
Correct code:
public class MainActivity extends Activity implements OnClickListener {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
iv.setOnClickListener(this);
}
#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 void onClick(final View iv) {
// TODO Auto-generated method stub
Thread t1=new Thread(new Runnable() {
#Override
public void run() {
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
}
});
t1.start();
try {
t1.sleep(1000);
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
}
}
}
Related
Anyone can help me to debug where I m going wrong. Test Code may get from:
https://drive.google.com/file/d/0Bz1lc03pNQm6Qkw3bE93dWxjdXc/view?usp=sharing
Scenario, I have 3 activities
Main, Splash and menu. First I call Splash activity for 5 seconds after 5 seconds I call menu activity its not working from menu I want to call Main activity. If I skip Menu activity than its working fine.
Splash Activity
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.dj);
ourSong.start();
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent start = new Intent("com.example.test.menu");
startActivity(start);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
Menu Activity
public class menu extends ListActivity{
String classes[] = {"MainActivity", "example1", "example1", "example1", "example1"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(menu.this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try
{
Class ourclass = Class.forName("com.example.test."+cheese);
Intent ourIntent = new Intent(menu.this, ourclass);
startActivity(ourIntent);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
Main Activity
public class MainActivity extends Activity {
int counter;
Button add, subtruct;
TextView textarea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0 ;
add = (Button)findViewById(R.id.button1);
subtruct = (Button)findViewById(R.id.button2);
textarea = (TextView)findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
textarea.setText("Your total is"+counter);
}
});
subtruct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
textarea.setText("Your total is"+counter);
}
});
}
}
Your code:
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent start = new Intent("com.example.test.menu");
startActivity(start);
}
}
};
timer.start();
Right code:
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent start = new Intent(Splash.this, menu.class);
startActivity(start);
}
}
};
timer.start();
Tip: Don't use lowerCase for Class Names
I have a fragment that has an animation of textView fadein. The animation must start after some time delay say 2 seconds after fragment is loaded. I wrote a code for this. but the animation part is done and then the view is rendered. How can I load the fragment and after some time delay start my animation
My code is as below: Note: the class extends Fragment
Animation animFadein;
MenuClickHelper mClickHelper;
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main_menu,
container, false);
mClickHelper = new MenuClickHelper(rootView, getFragmentManager());
tv = (TextView) rootView.findViewById(R.id.tvPresentation);
animFadein = AnimationUtils.loadAnimation(getActivity()
.getApplicationContext(), R.anim.fade_in);
animFadein.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
if (animation == animFadein) {
Toast.makeText(getActivity().getApplicationContext(),
"Animation Stopped", Toast.LENGTH_SHORT).show();
}
}
});
try {
Thread.sleep(2000);
tv.startAnimation(animFadein);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rootView;
}
Thread.sleep(2000); dont block your main using the sleep method instead you can use the Handler class and use postdelay to delay the animation:
sample:
change this:
try {
Thread.sleep(2000);
tv.startAnimation(animFadein);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
to
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
tv.startAnimation(animFadein);
}
}, 2000); //will start animation in 2 seconds
You can achieve the same result with
android:startOffset="2000"
in your XML animation set file.
I am building a snake game app.
So fur it works, except for the what it shows.
For some reason it shows two snakes: the original snake that moves and can be controlled, and another begginging of a snake that doesn't do anything.
Before that I drew at every cycle in the loop a background color, so it would cover the last canvas, and there was no problem.
Then I decided to divide the snake from the background, so I have made the snake canvas's background transparent and that it would clear the canvas each time it wants to draw a new canvas (each loop-to move the snake).
Here is the code:
The run of the thread:
public void run() {
long stepPerSecond=1000/FPS;
long startTime;
long sleepTime;
while(isRunning){
Canvas c=null;
startTime=System.currentTimeMillis();
try{
c=this.getHolder().lockCanvas();
synchronized (this.getHolder()) {
this.onDraw(c);
}
}
catch(Exception e){
}
finally{
if(c!=null){
this.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime= stepPerSecond-(System.currentTimeMillis()-startTime);
if(sleepTime>0)
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The snake's ondraw (it uses the snake's class on draw):
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(color.transparent, Mode.CLEAR);
snake.onDraw(canvas);
}
The snake's class onDraw:
public void onDraw(Canvas canvas){
switch(dirTransfare(dir)){
case 1://RIGHT
xLoc=xLoc+PART_SIZE;
break;
case 2://UP
yLoc=yLoc-PART_SIZE;
break;
case -1://LEFT
xLoc=xLoc-PART_SIZE;
break;
case -2://DOWN
yLoc=yLoc+PART_SIZE;
break;
}//swich
int x=xLoc;
int y=yLoc;
for(Brick bp:SnakeBody){
bp.setR(0+x, 0+y, PART_SIZE+x, PART_SIZE+y);
canvas.drawRect(bp.getR(),bp.getP());
int xtemp=bp.getX();
int ytemp=bp.getY();
bp.setX(x);
bp.setY(y);
x=xtemp;
y=ytemp;
}//for each
}//onDraw
Here is the mainActivity, which summons the snakeVew and the backgorundView (snake is the game view and the background is the arena)
public class MainActivity extends Activity {
GameView gv;
ImageView left;
ImageView right;
ImageView up;
ImageView down;
Arena a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*requestWindowFeature(Window.FEATURE_NO_TITLE);
v= new GameView1(this);
setContentView(v);
*/
gv= new GameView(this);
a=new Arena(this);
setContentView(R.layout.game_view);
LinearLayout surface = new LinearLayout(this);
LinearLayout backGround = new LinearLayout(this);
surface = (LinearLayout)findViewById(R.id.surface);
backGround = (LinearLayout)findViewById(R.id.background);
surface.addView(gv);
backGround.addView(a);
left=(ImageView) findViewById(R.id.left);
right=(ImageView) findViewById(R.id.right);
up=(ImageView) findViewById(R.id.up);
down=(ImageView) findViewById(R.id.down);
left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="LEFT"&&gv.snake.getDir()!="RIGHT"){
gv.snake.Left();
}
}
});
right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="RIGHT"&&gv.snake.getDir()!="LEFT"){
gv.snake.Right();
}
}
});
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="UP"&&gv.snake.getDir()!="DOWN"){
gv.snake.Up();
}
}
});
down.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="DOWN"&&gv.snake.getDir()!="UP"){
gv.snake.Down();
}
}
});
}
The problem is not in the background (the arena class); I have alredy took it down and confirmed that (it worked the same away, with the bug, even without using the arena class).
Here is how it looks with the bug:
http://i.tinyuploads.com/q7oztz.jpg
(you can see two snakes - the long one is the real one)
The blue background is from class arena.
Any ideas? Tnx for help :D
I am new in Android programming, I used to program microcontroller, now I need a few assistance from you guys.
I want to call two function with delay after each function called repeatedly until the stop button pressed.
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//I want perform these series of function repeatedly
//until stop button pressed
while(true){
bluetoothDisconnect();
delay(3000);
bluetoothConnect();
delay(3000);
if(status == true){
break;
}
}
}
});
btnStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status = true;
}
});
Big Thanks,
You need to thread the code in btnStart otherwise it will lock the GUI. Sorry to not be more helpful, I write Android apps using Mono (C#), I am unsure how to do it in Java.
Try this:
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
status = false;
Thread t = new Thread(new Runnable() {
#Override
public void run() {
doLoop();
}
});
t.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status = true;
}
});
where
private void doLoop(){
do {
bluetoothDisconnect();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
bluetoothConnect();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!status);
}
and status is a global variable, FALSE by default (declared as boolean status = false)
This code works as a simple solution, however I'd suggest you to look at the AsyncTask class:
http://developer.android.com/guide/components/processes-and-threads.html
Create an handler to delay the task .
Create the runnable thread and run your handler like:
Handler mhandler=new Handler();
Runnable mRunnable=new Runnable() {
#Override
public void run() {
bluetoothDisconnect();
btnStart.performClick() ;
}
};
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//I want perform these series of function repeatedly
//until stop button pressed
while(true){
bluetoothconnect();
mhandler.postDelayed(mRunnable,3*1000);
if(status == true){
break;
}
}
}
});
I have three layouts:
Layout1
-->onClick()-->show
Layout2
-->wait three seconds-->show
Layout3
The problem is that Layout2 is not shown. To set the layouts I use
setContentView(int);
The relevant code might be:
public class TrainingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
final Button inputButton = (Button)findViewById(R.id.inputButton);
inputButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
changeLayouts();
}
});
}
public void changeLayouts() {
setContentView(R.layout.layout2);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.layout3);
}
}
My idea was that Android might use something like an "Event-Loop" (like Qt) and my method would block the control to get back to the "Event-Loop" which would make the layout displayed.
But I couldn't find my error.
The problem why your layout2 is not shown is because of TimeUnit.MILLISECONDS.sleep(3000); - what you are doing here is you put your UI thread into sleep, so UI thread cannot process your request to change layout. And when it wakes up - it immediately sets layout3 that's why layout2 is not shown.
You might consider using Handler.postDelayed(Runnable, long) to postpone execution
So this should work as you expected:
public class TrainingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
final Button inputButton = (Button)findViewById(R.id.inputButton);
inputButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
changeLayouts();
}
});
}
public void changeLayouts() {
setContentView(R.layout.layout2);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.layout3);
}
}, 3000);
}
}
Try this, it will surely work
public void changeLayouts() {
setContentView(R.layout.layout2);
Thread Timer = new Thread(){
public void run(){
try{
sleep(3000);
} catch(InterruptedException e){
e.printStackTrace();
} finally {
setContentView(R.layout.layout3);
}
}
}; Timer.start();
}