in my android project ,as show in picture, how to event click on every added button.
on click button add new view, on same as new view add same.
this is how i add new views..
if(myLayout.getParent() != null) {
((ViewGroup)myLayout.getParent()).removeView(myLayout);
}
final EditText editTextWayPoints = (EditText) myLayout.findViewById(R.id.add_item_name);
final ImageButton btnone = (ImageButton) myLayout.findViewById(R.id.add_btn);
btnone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(AddRecipeActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
binding.linlay1.addView(myLayout);
now i want button click on every new views.
Related
First of all, I have searched in forum for "How to get text from edittext in other layout", and tried many other ways, then I decided to use LayoutInflater but it did not help.
The implementation of LayoutInflater (I follow this guide)
LayoutInflater factory = getLayoutInflater();
View regisText = factory.inflate(R.layout.regis, null);
EditText user = (EditText) regisText.findViewById(R.id.edt1);
String usr = user.getText().toString();
My parent layout is named activity_input.xml having 3 buttons (I do not set text button). I put only button Id for easy view.
<Button
android:id="#+id/btn1">
</Button>
<Button
android:id="#+id/btn2">
</Button>
<Button
android:id="#+id/btn3">
</Button>
After I click a button, one dialog appears to select value (1, 2 or 3) to set the button display text (1, 2 or 3) in activity_input.xml.
final Dialog dialog1 = new Dialog(user_input.this);
dialog1.setTitle("Select a Number");
dialog1.setContentView(R.layout.game_dialog);
dialog1.show();
button1 = (Button) dialog1.findViewById(R.id.btn1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
p1_btn.setText("1");
dialog1.dismiss();
}
});
The onClick works perfect.
But my problem is that I want to get the text of the button in parent layout again, I try to implement Layoutinflater but it does not work, here is the code.
LayoutInflater f1 = getLayoutInflater();
View v1 = f1.inflate(R.layout.activity_input, null);
Button btn1 = (Button) v1.findViewById(R.id.b1);
String t1 = btn1.getText().toString();
I check to make sure that the String t1 is retrieve successfully by print the string but it shows nothing
Hope you guide help me as soon as possible!
Thanks you very much.
You can make a static variable and store the result there, then access it from your dialog. So in your ActivityInput.java
public class ActivityInput extends Activity {
public static String input = "";
//code
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
input = "1";
p1_btn.setText(input);
dialog1.dismiss();
}
});
}
Now in your other java class ActivityOutput.java:
public class ActivityOutput extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
//code
String result = ActivityInput.input;
//use the result string somewhere to display it in your app
}
}
I need to be able to add buttons to a layout using an "add" button. The problem is that I need each button to have an OnClickListener()/onClick method. I was thinking every time the "add" button is pressed then i would add a new button to an array but im not sure add the listener and implement an onClick method for each button I create.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SmartChronometer chrono = (SmartChronometer) findViewById(R.id.chrono);
final Button start = (Button) findViewById(R.id.button2);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (chrono.isRunning())
chrono.pause();
else {
chrono.chronoStart();
}
}
});
}
I need to add chronomoter,button and listeners every time I click an "Add" button.
set all the clicklisteners as you wich!
call findViewById(R.id.btnSecond).setVisibility(View.GONE); on creat, then when clickin the first button
Button btnSecond;
...
public void onClick(View v) {
findViewById(R.id.btnSecond).setVisibility(View.VISIBLE);
if (btnSecond.getVisibility() == View.VISIBLE); {
findViewById(R.id.btnThird).setVisibility(View.VISIBLE);}
}
this way you can put all your information in the java file and all buttons in xml, but they will be hidden until click.
This is one way, other answer my come. Good Luck :)
implements OnClickListener
Button add = (Button) findViewById (R.id.addButton);
add.setOnClickListener (this);
List<Button> buttons = new ArrayList <Button>();
for (int i = 0; i < buttons.size (); i++){
buttons.get (i).setOnClickListener (this);
}
#Override
public void onClick (View v){
for (int i = 0; i < buttons.size (); i++){
if (v.getId () == buttons.get (i).getId ()){
// do stuff you want
}else if (v.getId() == R.id.addButton){
//add button
}
}
}
Hope this will work, didnt test it.
I am using this code to create buttons and added this buttons in layout. When I click one of this button selected changed successfully. But what should I do here for clicking in button the previous button's selected state will be unselected.
In one word only one button has to be selected.
private void createButtons(String categoryTitle){
final Button myButton = new Button(mActivity);
myButton.setBackgroundResource(R.drawable.btn_default_holo_dark_trans1);
myButton.setText(categoryTitle);
if (mFirstButtonSeleted){ //this is for first run only first button will be selected
myButton.setSelected(true);
mFirstButtonSeleted = false;
}
mButtonsLayout.addView(myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myButton.setSelected(true);
}
});
}
Create a reference to the previously selected button. Then in your on click method you could do something like this :
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(previousButton !=null)
previousButton.setSelected(false);
myButton.setSelected(true);
previousButton = myButton;
}
});
I'm new in android and i have created editText dynamically with the following code while clicking add new button.Is it possible to add a delete button near editText so that each while clicking the delete respective editText will be removed?
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
EditText newPass = new EditText(getApplicationContext());
allEds.add(newPass);
newPass.setHint("Name of Label");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newPass.setWidth(318);
newPass.setTextColor(Color.parseColor("#333333"));
newPass.setId(textb);
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner();
}
});
Yes, create your remove button at the same time as your EditText and use the removeView() method just like the addView() method, maybe like this:
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
...
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner();
Button btnRemoveOld = new Button(this);
btnRemoveOld.setId(32); // arbitrary number
btnRemoveOld.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
rAlign.removeView(findViewById(textb));
}
});
// You will need to set parameters to define how the button looks
// and where it is in relation to the EditText here
rAlign.addView(btnRemoveOld);
}
});
I have lots of image buttons. User can press any of those button and i need to know which button is pressed. These buttons appears dynamically so i don't know how much of them will be.
For one image button i would write this listener:
ImageButton ib = new ImageButton(this);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(TravelBite.this, "test", Toast.LENGTH_SHORT).show();
}
});
But how to do one listener for all my Images Buttons? Can i recognize which button was pressed by it's tag ? Like tag would be ID from sqlite.
Also i put image to button with this code:
button.setImageDrawable( testPic );
button is ImageButton and testPict is drawable (image)
But when i press this button it don't show that it is pressed if i do this:
button.setBackgroundColor(R.color.transparent_background);
I had to do this because i just want to see Buuton image which i could press and recognize what i pressed.
Thanks.
ok what you can do is that you can write a single callback function and then set it to each and every button it will allow you to handle each button with a sing function like :
View.OnClickListener btn_listener = View.OnClickListener() {
#Override
public void onClick(View v) {
// Do whatever work you want.
int id = v.getid();
// check for id and do you task.
}
Arraylist<Button> btn_group = new Arraylist<Button>;
or
Arraylist<int> btn_id_group = new ArrayList<int>;
for (int i =0; i < 10; i++) {
Button btn = new Button(getApplicationContext());
btn.setId(i);
btn_id_group.add(i) or btn_group.add(btn);
btn.SetOnClickListener(btn_listener);
}
I think it will work for you.
You can use View.setTag(object) and View.getTag() and store in it sqlite id. Something like this:
View.OnClickListener listener = View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(TravelBite.this, (String)v.getTag(), Toast.LENGTH_SHORT).show();
//or some function that do somthing useful
//if( ((String)v.getTag).equals("image1") ){} or anything else
}
And in for loop:
String tagFromSqlite = "image1";
ImageButton ib = new ImageButton(this);
ImageButton.setTag(tagFromSqlite);
ib.setOnClickListener(listener);
final OnClickLisener listener = new OnClickListener() {
public void onClick(View v){
switch((Integer)v.getTag()){
case R.id.zero:
break;
case R.id.one:
break;
case R.id.two:
break;
}
}
}
//when init the Buttom
ImageButton btn = new ImageButton(Context);
btn.setTag(NUMBER);
you'll have to manually assign ID's to keep them separated - I had to do something similar (1000 was the base ID I chose to add upon as well)
Although View v in the listener refers to the button pressed, when you programmatically create buttons the id's are not unique and caused me issues, so that's why I set them specifically
View.OnClickListener btn_listener = View.OnClickListener()
{
#Override
public void onClick(View v)
{
//you can use v.getID() here
}
}
for (int i =0; i < 10; i++)
{
Button btn = new Button(getApplicationContext());
btn.setID( 1000 + i );
btn.SetOnClickListener(btn_listener);
}