invisible button that works android xml - android

I want to set a button to be invisible because there will be a picture in front of it to act like the button. Do you guys know how to set this up in the xml?
thanks in advance.

Maybe the best thing you can do is to use directly an ImageView instead of a Button, setting always a OnClickListener on it in order to catch click action. Maybe you can also add a proper selector in order to get a visual feedback on the pressed/unpressed state.

Related

setEnabled(false) not hiding TextView

So I'm following an Android tutorial and came across an issue. The video maker uses setEnabled(false) to hide a TextView until the user clicks a certain button. However, when I tried the same code, the TextView was on the screen before the user clicked the button. I've been trying to work out why for an hour, but to no avail. Below is a link to the video and a picture of my relevant code, XML code, and screen display.
Video: https://www.youtube.com/watch?v=NGRV2qY9ZiU Talks about setEnabled at 16:35
However, when I tried the same code, the TextView was on the screen before the user clicked the button.
setEnabled(false) won't actually hide the TextView. To do that you need to do
result.setVisibility(View.GONE);
When you are ready to make it visible (instead of setEnabled(true)):
result.setVisibility(View.VISIBLE);
try
result.setVisibility(View.GONE);
instead.
Edit:
note that:
result.setVisibility(View.INVISIBLE);
would also hide the view but it would still be clickable.
Use the below method. It will hide the element from the view.
result.setVisibility(View.GONE);

Add views to existing layout

I have created an xml layout and all is fine. Upon a button click I want to show more buttons on the layout. What is the right way of doing this? Right now I create the buttons on the same xml and set their visibility to GONE. Once button is clicked and set their visibility to VISIBLE.
Is this a correct way of doing things? The layout is getting a bit complicated with other image views following the same pattern
Thank you so much
To start off: I am not sure why someone gave you a down to the question, as it is pretty straight forward and clear.
Now the answer.
So, the whole thing, in order to be properly "made" ,should be done programmatically.
Before I will give you some written code (as example), I will explain you a bit how you should look at this.
You have your main XML file in which you have that button which you want to click and upon clicking, make more buttons appear on the screen.Well, in order to achieve this, creating buttons and make them INVISIBLE or VISIBLE depending on the need, is NOT really a good way to deal with it.You might wounder why? Well, it's clearly not a good way because even though your buttons are invisible ,when the application starts, the buttons ,even though being invisible ,they are being created (drawn).And this will take space and slow the application.
Say you want to be able to create an indeterminate number of buttons, upon click your first button.Well, you cannot even achieve this by the way you described in your question.You really limit yourself by using the XML so much.
The SOLUTION:
So, you have your XML file, in which you have your layout (relative or linear,does not matter for now) and your button which when is pressed creates a button.
In order to be able to get reference to your XML Layout and your Button ,you need to give them an ID.And you do this inside the XML (I am pretty sure you know that,but I prefer writing full explications).
Giving ID to the layout:
android:id="#+id/thelayout"
Giving ID to the layout:
android:id="#+id/button"
(If you don't know where to add those IDs, comment it,I will help further)
Now that you can refer to the layout and the button from java, this is where it gets fun.
You define a layout and a button.NOTE: Check your XML file!!! If you have a RelativeLayout ,you need to define a RelativeLayout ,if you have a LinearLayout...well is clear.
I am going to assume we got a LinearLayout.
LinearLayout ll;
Button btn;
Button thenewbutton;
ll= (LinearLayout)rootView.findViewById(R.id.thelayout); //The name we gave in XML
btn = (Button)rootView.findViewById(R.id.button);
What we need to do now, is to add a method which will do something when we click the buttom.
btn.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//Here we will handle the creation of the button;
thenewbutton = new Button(getActivity()); //Created the new button
thenewbutton.setText("One of the new buttons"); //Setted it the text in between the ""
setVisibility(View.VISIBLE); //Making it visible -like you were doing prolly.
//You can customize your button via methods.Write "thenewbutton." and eclipse will show you all the methods you can use in order to "play" with the new created button.
//Now, the button is created.All we need to do is to add it to the layout!Easy job.
ll.addView(thenewbutton);
return true;
}
});
And this is it ,pretty much.
I have explained it as detailed as I could.I know is a lot to read, but if you wanna truly understand, than take the 3-5 minutes to read and really go through everything I wrote and you will have another level of understanding the problem.
If you need further help, leave a comment!
Cheers!

Android: How to make sliding buttons like phone unlock button on screensaver?

I don't know how exactly this type of buttons is called that's why I can't even google about it.
You use that button to unlock phone - tap on it and move from left to right.
I'd like to add same button in my app. How to do it?
You should use SeekBar to achieve what you want. http://developer.android.com/reference/android/widget/SeekBar.html
Here is a good example
http://www.techrepublic.com/blog/app-builder/androids-seekbar-your-way/943
Try this for a library
https://github.com/sitepoint-editors/SwipeButtonExample
and this for a DIY
https://rightclicksolutions.wordpress.com/2014/04/09/android-slide-to-unlock-like-ios-mb-slider-slider/
You can overide onTouch() and change the location of the button acording to the x position. and when you lift the finger you can overide onUp() (im not sure thats the name of the method) and create an animation that will lead the button back to its place.
From your question I get that you might think its a button you can simply add from a list of buttons, well, its not. you need to manualy create it.

Custom buttons or TextView onClick or other method to solve android layout issue?

I need to create a list of buttons like these, but I don't know how to implement this, there are few options make buttons, but I don't think I can make buttons like this, if I use textview will it work? Is there any other option?
Is there any gui editor for android? Or can i make rectangle class and inside that class someone click to fire the event?
You can make a button appear anyway you want. See these examples:
http://inphamousdevelopment.wordpress.com/2010/10/05/creating-easy-custom-buttons-in-android/
http://androidcookbook.oreilly.com/Recipe.seam;jsessionid=0A9B6292F90D72D302B58DD4680BC0BC?recipeId=3307&recipeFrom=ViewTOC
Custom buttons in a LinearLayout would certainly do the trick. But when I see that screen shot, I also think ListView. That might be an interesting and scalable way to lay these out.

How to make a button appear pressed when it is not?

I have a button that plays a sound clip when it is pressed. I would like the button to appear pressed during the duration of the sound clip. By that, I mean that I would like the button to take on the default pressed-button appearance while the sound is playing. How can I implement this? I have tried using a number of things in the onClickListener (such as setSelected, requestFocus, etc), but none of those do the trick. I have also tried changing the onClickListener to an onTouchListener, again with no dice. Am I wrong in assuming that there must be a way to simply set the button image to appear pressed? (BTW, the button object is of type Button, not ImageButton).
Thanks for any advice!
Please see this question. It details a couple of different ways this can be done.
Lookup 'selector' drawables and let android take care of this for you.

Categories

Resources