How to include 3 Commands into one button?
I want insert open another activity, when button click.
This my code:
btnUpload = (Button) findViewById(R.id.btn_submit);
btnUpload.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG, "submit clicked");
if (!Ultils.isConnectingToInternet(SubmitPropertiesActivity.this)) {
showMsg(getResources().getString(R.string.open_network));
} else {
doUpload();
}
}
});
Just add line in onClick method
startActivity(new Intent(getApplicationContext(),anotherActivity.class));
like
btnUpload.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG, "submit clicked");
//other command
startActivity(new Intent(getApplicationContext(),anotherActivity.class));
}
});
Add start activity code in runOnUiThread after dismiss dialog and clear all data then add your start activity code.
runOnUiThread(new Runnable() {
public void run() {
try {
prgDialog.dismiss();
title.setText("");
price.setText("");
content.setText("");
address.setText("");
area.setText("");
lantai.setText("");
luasbangunan.setText("");
bathroom.setText("");
bedroom.setText("");
selected_amenities_list.clear();
marker_selected = 0;
//Here You add your start new activity code.
} catch (Exception e) {
e.printStackTrace();
}
}
});
Something like this could help you;
Write 3 Methods
1) Network check method
2) Upload image method
3) Start an Activity method
When the button is click;
Call Method (1)
if Network connection is there - then
Call Method (2) from Method (1)
Check if uploading method finishs, if it does -
Call Method (3) from there
This is a simple procedure, just giving you the idea I would use If I'm in the situation.
Related
I have a button (named button1) with an OnClickListener and after button is clicked, a task should be execute, and button should become unclickable until task is finished. This is my code:
#Override
public void onClick(View v) {
removeListeners();
executeMyTask();
addListeners();
}
private void disableListeners() {
button1.setOnTouchListener(null);
button2.setOnClickListener(null);
}
private void enableListeners() {
button1.setOnTouchListener(this);
button2.setOnClickListener(this);
}
Now it happen that, if I click button while executeMyTask is running (so when listener is disabled), when task finish, onClick is called again.
I would that all clicks performed while executeMyTask is running will be ignored.
how can I do ?
Why don't you just disable the whole button with:
button1.setEnabled(false);
button2.setEnabled(false);
And later just re-enable them:
button1.setEnabled(true);
button2.setEnabled(true);
It will be easier for the users to understand that the button is un-clickable at the moment.
There are 2 ways:
1) With disable all other buttons,
#Override
public void onClick(View v) {
setButtonEnabled(false);
executeMyTask();
setButtonEnabled(true);
}
private void setButtonEnabled(boolean enable) {
// TODO Auto-generated method stub
btn_1.setEnabled(enable);
btn_2.setEnabled(enable);
btn_3.setEnabled(enable);
}
2) Use a boolean to keep record of async task running or not:
Like:
#Override
public void onClick(View v) {
removeListeners();
executeMyTask();
addListeners();
}
// And in executeMyTask
onPreExecute(){
....
..
isAsyncRunning = true;
..
....
}
onPostExecute(){
....
..
isAsyncRunning = false;
..
....
}
//And in Button Click Listeners
{
if(!isAsyncRunning){
// Do something...
}
}
I have one Linearlayout - totalincome and another TableLayout normalincometable, which should appear just below the totalincome. normalincometable will be invisible when the program runs. When the user clicks on "totalincome" the table should display. If the user clicks on "totalincome again", the table should disappear. I have tried this code, but It didnt work.
totalincome.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int x =0;
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
From this code, I can make the table visible in first click but It doesnt disappear in next click. Are there any options ?
Try this:
#Override
public void onClick(View v) {
if(normalincometable.getVisibility() == View.VISIBLE) {
normalincometable.setVisibility(View.GONE);
} else {
normalincometable.setVisibility(View.VISIBLE);
}
}
You have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
because you have define x in the button click code so whenever button click it set to 0. define x outside the button click scope.
try this way:put x variable outside the button onclick() or defined x globally
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Use like this:
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Try this
Boolean isFirstTimeClicked=true;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isFirstTimeClicked)
{
normalincometable.setVisibility(View.VISIBLE);
}
else
{
normalincometable.setVisibility(View.GONE);
}
isFirstTimeClicked=!isFirstTimeClicked;
});
}
and in your code you have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
Easiest Method is
button.setVisibility(View.VISIBLE == button.getVisibility() ? View.GONE:View.VISIBLE);
I am having a strange experience with text to speech.
see my code:
Button b;
TextView title, body;
TextToSpeech tts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popups);
b = (Button) findViewById(R.id.buttonok);
title = (TextView) findViewById(R.id.textViewtitle);
body = (TextView) findViewById(R.id.textViewbody);
b.setText("ok");
title.setText(Receive.address);
body.setText(Receive.body);
tts = new TextToSpeech(Popup.this, new TextToSpeech.OnInitListener() {
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});
play();//this is not working??!!i don't know why
b.performClick();//even this is not working
/* but when i click this button it works??? how and why?*/
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
play(); // Popup.this.finish();
}
});
}
private void play() {
// TODO Auto-generated method stub
tts.speak(Receive.body, TextToSpeech.QUEUE_FLUSH, null);
}
My text to speech works fine only when i click the button but whenever i don't click this button and write the tts.speak() inside normal code it do not works...why?
regards charlie
You have to wait for onInit being called before you can start speak. In your code you call play() right after the declaration. onInit is a callback and it takes some time before it being called. If you click your button as soon as the button appears, sometime it will fail as onInit has not been called. You should have a class member boolean mIsReady and set it to true in onInit. And in your play() method
private void play() {
// TODO Auto-generated method stub
if (mIsReady) {
tts.speak(Receive.body, TextToSpeech.QUEUE_FLUSH, null);
}
}
b.performClick();//even this is not working
/* but when i click this button it works??? how and why?*/
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
play(); // Popup.this.finish();
}
});
You are performing a click with b.performClick() before setting its setOnClickListener. Also, you are better off making such calls on the onResume() method. OnCreate is meant to be used to bind views and prepare the activity. The onResume() method will be called before showing the view to the user on the foreground so that would be the best place to put this code.
Take a look at the activity lifecycle.
i do for this code but not supported for ths issue and what i do for solution this like as if codition inside any listener.
if (btn.isEnabled()) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
But why???
any button's onClickListener will only be called if its "enabled".
You dont need to bother about onClickListeners which are assigned to disabledButtons.
suppose your button is disabled at the launch of activity then this listener wont be applied to your button.
Now after some time if you enable this button (may be after some event etc.)
"THEN ALSO THIS LISTENER WONT WORK" as you did not set the listener at the first place...
so IMO dont put that in if...
this code successfully.
if (button.isEnabled()) {
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Test", 10).show();
}
});
}
i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.