how to set visabilty time to image in android - android

hi i am doing one app here when i click button that time i need to display image.that image should be visable in 5sec.after 5 sec after that image should need to be invisable.i trieb but i am not getting after 5 sec how to invisable that image.any one suggest me. i using below code.
Demo1 .class
public class Demo1 extends Activity {
/** Called when the activity is first created. */
Button b1;
ImageView i1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.homebutton);
i1=(ImageView)findViewById(R.id.imageView1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
i1.setVisibility(View.VISIBLE);
}
});
}
}

use handler.postDelayed method to delay some operation to some time, so for your operation use following:
i1=(ImageView)findViewById(R.id.imageView1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
i1.setVisibility(View.VISIBLE);
Handler.postDelayed(new Runnable(){public void run(){ i1.setVisibility(View.INVISIBLE);}, 5000);
}
});

Related

Activity Display but controls with be enabled after 5 seconds In android

I am displaying one activity in this one image view is there and only image is display to the user. user will not allow to do any task such as like or comment.
the screen will on 5 seconds hold.
after that controls on that screen will be enabled and workable
even user does not allow to go back from the screen till the 5 seconds will not be completed.
how do i implement this functionality
I have written the code
public class AdvertActivity extends Activity implements OnClickListener {
ImageView ivEAdvertUserImage,ivEAdvertImage;
TextView tvEAdvertDislike,tvEAdvertLike,tvEAdvertHeading;
EditText etEAdvertComments;
Button btnEAdvertSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advert);
InitUI();
tvEAdvertLike.setOnClickListener(this);
tvEAdvertDislike.setOnClickListener(this);
btnEAdvertSubmit.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tvEAdvertLike:
tvEAdvertLike.setTextColor(color.Blue);
break;
case R.id.tvEAdvertDislike:
tvEAdvertDislike.setTextColor(color.Blue);
etEAdvertComments.setEnabled(true);
btnEAdvertSubmit.setEnabled(true);
break;
case R.id.btnEAdvertSubmit:
Intent in =new Intent(AdvertActivity.this,EAdFragment.class);
startActivity(in);
finish();
break;
default:
break;
}
}
public void InitUI(){
ivEAdvertUserImage=(ImageView)findViewById(R.id.ivEAdvertUserImage);
ivEAdvertImage=(ImageView)findViewById(R.id.ivEAdvertImage);
etEAdvertComments=(EditText)findViewById(R.id.etEAdvertComments);
tvEAdvertDislike=(TextView)findViewById(R.id.tvEAdvertDislike);
tvEAdvertLike=(TextView)findViewById(R.id.tvEAdvertLike);
tvEAdvertHeading=(TextView)findViewById(R.id.tvEAdvertHeading);
btnEAdvertSubmit=(Button)findViewById(R.id.btnEAdvertSubmit);
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advert);
InitUI();
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// Will be called after 5000ms
tvEAdvertLike.setOnClickListener(this);
tvEAdvertDislike.setOnClickListener(this);
btnEAdvertSubmit.setOnClickListener(this);
}
}, 5000);
}
This way your Listeners will be set after 5 seconds. Before that, the buttons etc. will have no effect if someone clicks them.
(Can not test my code right now, but it should work)

how do i update the database table on the basis of checkbox/unchecked

how can i retrieve the checkboxes value to update the database table on pressing the update button...
TeacherLoggedInClass
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The isChecked() method return a boolean value with the information you need.
I would also suggest you use another way with implementing the Listener. This is what Google recommends:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click... for example your checkBox.isChecked()
}
});
}
If you want to get the single checkbox value and set database, you can follow below code,
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cbBox.isChecked()) {
// do your operation
} else {
// do your operation
}
}
}
if you need to use a list of checkbox status to update databases, you'd better to create a collection to cache every checkbox status , and with bulkInsert or transaction to commit.

Force close error

What is the force close problem of this program?
public class MyActivity extends Activity {
TextView t=(TextView)findViewById(R.id.textView1);
Button r=(Button)findViewById(R.id.button2);
private OnClickListener i=new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("fghffghfhgf");
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
r.setOnClickListener(i);
}
}
You need to get your TextView and Button after inflating the layout.
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here inflate the layout
setContentView(R.layout.main);
//now you can get your widgets
final TextView t= (TextView)findViewById(R.id.textView1);
Button r=(Button)findViewById(R.id.button2);
r.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("fghffghfhgf");
}
};
);
}
}
I really recommend you to check this to build your first app.

Showing duration time using stopwatch in android?

I need to toast the stopwatch's value
ie.,time taken between start and stop
If i click the stop button it should toast that time duration.
How to do this?
Here i have tried some code
chrono_meter.java
public class Chrono_meter extends Activity {
Chronometer chr;
Button btn_stop_travel;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chronometer_layout);
chr = (Chronometer)findViewById(R.id.chronometer1);
chr.start();
btn_stop_travel = (Button)findViewById(R.id.btn_stop_inspection);
btn_stop_travel.setOnClickListener(mStopListener);
}
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
chr.stop();
}
};
}
Try this
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
chr.stop();
Toast.makeText(Chrono_meter.this, chr.getText(), Toast.LENGTH_LONG).show() ;
}
};

Android Automatic Refresh Button

I'm having trouble with something that seems like it should be very simple. I have a frame layout with 2 Buttons (one on top of the other naturally). When I click on the top button, it automatically takes me to a website, and the button beneath it replaces it to be the visible one. I want to set up an automatic refresh so that a few seconds later, the button that was originally on top becomes on top again. Thank you for any help you can give! Here is the Java, and with my attempt at creating an automatic refresh:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton bJava1= (ImageButton)findViewById(R.id.button1);
final ImageButton bJava2 = (ImageButton)findViewById(R.id.button2);
final WebView webview1= (WebView)this.findViewById(R.id.webView1);
final MediaPlayer sound= MediaPlayer.create(Youtube.this, R.raw.soundclip1);
final Handler handler = new Handler();
Refresh = new Runnable() {
public void run() {
bJava1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
webview1.loadUrl("http://www.google.com");
if(sound.isPlaying()){
bJava1.setVisibility(ImageButton.VISIBLE);
bJava2.setVisibility(ImageButton.GONE);
}else {
sound.start();
bJava1.setVisibility(ImageButton.GONE);
bJava2.setVisibility(ImageButton.VISIBLE);
}
}
});
handler.postDelayed(Refresh, 10000);
}
};
handler.post(Refresh);
Problem is, that your Refresh runnable only registers a onClickListener on the first button and calls itself every 10 seconds, you should register the onClickListener only once outside the Runnable and only call the if block within your Refresh.run() Method:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton bJava1= (ImageButton)findViewById(R.id.button1);
final ImageButton bJava2 = (ImageButton)findViewById(R.id.button2);
final WebView webview1= (WebView)this.findViewById(R.id.webView1);
final MediaPlayer sound= MediaPlayer.create(Youtube.this, R.raw.soundclip1);
final Handler handler = new Handler();
Refresh = new Runnable() {
public void run() {
if(sound.isPlaying()){
bJava1.setVisibility(ImageButton.VISIBLE);
bJava2.setVisibility(ImageButton.GONE);
}else {
sound.start();
bJava1.setVisibility(ImageButton.GONE);
bJava2.setVisibility(ImageButton.VISIBLE);
}
}
};
bJava1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
webview1.loadUrl("http://www.google.com");
handler.postDelayed(Refresh, 10000);
}
});

Categories

Resources