I am working on a project that has 5 buttons when I click on any of them it should show another 3 buttons. When I click on any of the 3 buttons it should open an activity.
I have tried working with MaterialArcMenu it doesn't give me what I need it gave me a circle button .
Can I receive any advice on this?
Button 1 ---> Nested Button 1 --> Nested Button 1
---> Nested Button 2
Button 2
Button 3
Button 4
Button 5
Have 8 buttons in your xml file. 5 of which are visible, 3 of which are hidden & disabled
Have the 5 implement the same onClickListener (using xml onClick attribute is easiest for this)
Have the 3 implement the same onClickListener as well (using xml onClick attribute is easiest for this)
In the onClickListener for the 5 buttons, write code that makes the other 3 buttons visible & enabled
In the onClickListener for the 3 buttons, write code that opens the new activity
If you are creating the button from a layout, then what I would suggest to do is add the activity or fragment as the click event listener. Secondly, set a tag for the button. Then when the button is clicked, get the tag for the view and handle accordingly.
Here is an example:
protected CustomImageButton getSmartLinkImageButton(int buttonImageResource, int colorToChangeTo, boolean isLargerImage, ActionType buttonAction) {
LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
SmartLinkImageButton button = (SmartLinkImageButton) inflater.inflate(R.layout.footer_smartlink_image_button, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mFooterButtonWidth,
isLargerImage == true ? mFooterButtonHeight / 2 : mFooterButtonHeight);
params.gravity = Gravity.CENTER;
button.setLayoutParams(params);
if(isLargerImage)
button.setScaleType(ScaleType.FIT_CENTER);
button.setImageResource(buttonImageResource);
if(colorToChangeTo > 0) {
changeImageViewImageColor(button, null, colorToChangeTo);
button.setDefaultColorFilter(colorToChangeTo);
}
button.setTag(buttonAction); //HERE IS THE TAG CREATION
button.setOnClickListener(this);
//optional method for mapping image buttons
mapImageButton(button, buttonAction);
inflater = null;
return button;
}
Then the onClick:
#Override
public void onClick(View view) {
view.getTag(); //DO ACTION BASED ON VIEWS TAG
}
Related
I am new to android development.
I want to add EditText and a remove button ( X ) when they click on a button (called “contact”). So that there will be multiple contacts. On click of the remove button the corresponding added EditText with remove button should be disappeared.
It will be something like this,
————————— X
————————— X
————————— X
————————— X
———————
|contact|
———————
If I use any Adapter, how to add the empty EditText and a remove button ( X ). If not any other better options?
Finally I need to get all the values entered in the EditText.
NOTE: On click of “contact” it should be adding EditText and a remove button ( X ).
Kindly give me a clear and simple way to do this.
Thanks!
Create a LinearLayout and button. On the button click call the following method. Also, create a List<EditText> in your onCreate Method to track all added edittexts & retrieve texts later.
private void createNewEditText() {
EditText editText = new EditText(this);
editText.setMaxLines(1);
editText.setSingleLine(true);
editText.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, VectorDrawableCompat.create(resources, R.drawable.ic_clear_black_24dp, null), null);
editText.setOnTouchListener(new View.OnTouchListener {
#Override
Boolean onTouch(View view,MotionEvent event) {
val DRAWABLE_BOTTOM = 3
val DRAWABLE_RIGHT = 2
val DRAWABLE_LEFT = 0
val DRAWABLE_TOP = 1
if (event.getRawX() >= (editText.right - editText.getCompoundDrawables()[DRAWABLE_RIGHT].bounds.width())) {
editTextList.remove(editText);
parentLinearLayout.removeView(editText);
return true;
}
return false;
}
})
editText.requestFocus();
editText.setText(voicetext);
editTextList.add(editText);
parentLinearLayout.addView(editText);
}
You can use a LinearLayoutwith orientation set to vertical. you can then create another layout with yourEditTextand Button and add them dynamically to the LinearLayout within the onClick method for contact button.
Do not forget to generate different IDs for your newly added views to be able to find them later
The X button will then just remove its parent from the layout
I am duplicating my linear layout dynamically and I have to set onClickListeners for buttons inside the linear layout.
for(int i = 0; i <10 ; i++){
// other code here
Button approve_btn = (Button) findViewById(R.id.rent_number_up_btn);
approve_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
approve_btn.setText(String.valueOf(i));
}
});
}
Everything works fine except that my button's text is always set to 9. I think that's because when the listener is called the value of i is 9 at that time. What I want the value of i at the time the button's listener is set and I am not sure how to do that.
How can I solve this problem? Any help is appreciated.
The issue is that you are setting click listener to the same button (by calling findViewById()) 10 times in a row. You get the value 9 because thats the last click listener which you added to the button.
for(int i = 0; i <10 ; i++){
// other code here
Button button = new Button(<Activity Instance>);
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
approve_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
approve_btn.setText(String.valueOf(i));
}
});
}
In above code you need to add those buttons in your linearlayout.
Hope this will help you,
Thanks
I am not sure what you want to do but:
Like #Shaishav said your are using same button (R.id.rent_number_up_btn) and your are replacing the click listeners on top of each other. The last value (of the your counter "i") before your loop finish is 9 , that's why it show 9 all the time. If you want to add 10 buttons inside your Linear layout just create new Button(context) every time when your loop starts and add this button to your layout via
yourLinearlayout.addView(yourNewButton);
Then if you set click listener to your new button maybe it will show different values :)
I have one activity and here i have 100 buttons, i want that when i press Button 1 then press another Button the Button 1 should get unpressed.
i know i can make this with
if(Button1.isPressed()) {
Button2.setPressed(false);
Button3.setPressed(false);
Button4.setPressed(false);
Button5.setPressed(false);
Button6.setPressed(false);
Button7.setPressed(false);
Button8.setPressed(false);
......................... }
else { do nothing }
.... BUT!
it's too much code
Coders will kill me or will just laugh on me.
any ideas?
maybe there is a way to unpress the all buttons from the activity?
Not the prettiest solution ever, but you could make an OnClickListener like this:
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View current = parent.getChildAt(i);
if (current != v && current instanceof Button) {
((Button) current).setPressed(false);
}
}
((Button) v).setPressed(true);
}
}
and attach it to all of your buttons.
Then, whenever a button is clicked, it will iterate over all views that are in the same layout (or actually, view group) as the clicked button, and, for any of those views that are buttons except for the clicked button, it will call setPressed(false).
Note that this only works out of the box if all the buttons are in the same layout. If they are in nested layouts, you will have to adapt it a little.
Off topic: What do you need 100 buttons for? That's a lot of buttons. You may want to redesign your user interface
Ok so instead of looping through all the buttons on over and over again when one button is pressed, you can just store a variable which stores the button number of the button that was last pressed. Now, when the second button is pressed, disable the button that was pressed earlier, you get its index from the saved variable, enable the button that was pressed and store its index in the variable.
Heres an example pseudo code to give you and idea:
int buttonLastPressed = 0;
void onButtonClick(Button buttonPressed){
if(buttonLastPressed != 0){
disableButton(buttonLastPressed);
enableButton(buttonPressed);
buttonLastPressed = buttonPressed.getIndex()
}
}
Saves you from looping through each and every button to disable it.
Define id of button 1 to 100
When press button occurs save it's id in some member variable like previous_pressed
Before updating a previous_pressed value find and unpress previous pressed button like this
Button previous_pressed_button = (Button) findViewById(previous_pressed);
Now you have the previous pressed button, So upress it or whatever.
I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});
I create buttons dynamically based on the size of an array list that i get from another object. For each entry of the arraylist i need to create three buttons each one with different action.
Like this i need to create 3 times the sizes of arraylist number of buttons. If i had only one set of buttons I can write onClick()-method which takes the id of the button but here i have 3 buttons for each entry and need to write 3 different actions for those three buttons.
How could this be done?
Similar thing I have done when i needed a textview for each of my array item.it was like-
String[] arrayName={"abc","def","ghi"};
for(int i=0;i<arrayName.length;i++)
{
TextView tv=new TextView(context);
tv.setPadding(20, 5, 40, 5);
tv.setText(arrayName[i]);
tv.setTextSize(1, 12);
tv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.parseColor("#2554C7"));
tv.setClickable(true);
tv.setId(i);
layout.addView(tv);
}
In same way,you can add button in similar way. and in click event of each of them,you can code for their actions separately.(I have not tried this).So in each iteration,you will have 3 buttons for each of the array item.
Edit - 1:
You can differentiate ids like:
mButton1.setId(Integer.parseInt(i+"1"));
mButton2.setId(Integer.parseInt(i+"2"));
mButton3.setId(Integer.parseInt(i+"3"));
Then you can set click listener on each of the button like mButton1.setOnClickListener... and so on.
Declare a list of buttons:
private List<Button> buttons = new ArrayList<Button>();
Then add each button to this list:
buttons.add(0, (Button) findViewById(id in ur layout));
Inside a for loop give click listener:
Button element = buttons.get(i);
element.setOnClickListener(dc);
where dc is ur object name for your inner class that implements OnClickListener.
To access each button you can give:
Button myBtn;
#Override
public void onClick(View v) {
myBtn = (Button) v;
// do operations related to the button
}
Create the Common Listener class for your button action event and set the used into the setOnClickListener() method
Now set the unique id for the buttons.
Now suppose your class look like this :
class MyAction implements onClickListener{
public void onClick(View view){
// get the id from this view and set into the if...else or in switch
int id = view.getId();
switch(id){
case 1:
case 2:
/// and so on...
}
//// do operation here ...
}
}
set this listener in button like this way.
Button b1 = new Button(context);
b1.setId(1);
b1.setOnClickListenr(new MyAction());
Button b2 = new Button(context);
b2.setId(2);
b2.setOnClickListener(new MyAction());