Android Button Problem - android

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.

Related

1 Button 3 Command onclick android

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.

How to know which child view is clicked in a relativelayout

I have a relative layout to which child views are added and removed dynamically(any number can be added or removed)
My question is how to know which view was clicked so that i can add different onclicklisteners depending on the type of child views
Adding and retrieving the tag while click event can help. Here is the code.
For adding tags:
customView1.setTag(someTag);
customView1.setOnClickListener(myClickListner);
For retrieiving:
OnClickListener myClickListener = new onClickListener(){
#Override
public void onClick(View v) {
if(v.getTag() == someTag){
//do stuff
}else if(v.getTag() == otherTag){
//do something else
}
}
in your adapter class you need to write like this am sharing the sample code snippet
public static class ChatListItemsViewHolder extends
RecyclerView.ViewHolder {
public ChatListItemsViewHolder(View v) {
super(v);
// TODO Auto-generated constructor stub
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// write your code here
}
});
}
let me know if you need more clarity.

i want to get position in a list data how to get the position of click of button in android

how to get the position from list on click of data.
i want access button in android.on the click of button of the adapter class it should give the position.
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
you can set the position as the tag
holder.beginDate.setTag(position); // set every time getting the View
and retrieve it using
// put this code where creating a new instance of holder
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int pos = (Integer) view.getTag();
// do anything using the position
}
});
After initialization of your button in getView() method write:
holder.beginDate.setTag(position);
and in your onClick() use:
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int position=Integer.parseInt(((Button)v).getTag().toString());
}
});

Text to speech unusual behaviour in android

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.

Any listener is possible in if condition

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();
}
});
}

Categories

Resources