Android button stay pressed / radio group - java

I am working on a simple converter between Decimal/Hex/Binary and have run into an issue I can't seem to solve. This is the layout I am more or less aiming for.
What I am here is when one of the buttons is pressed, it stays pressed to indicate which conversion you are currently working with. Now I have looked everywhere and it doesn't seem there is a good way of doing this, or I haven't found it at least.
What would be the best way to go about this? is there someway to just use buttons and have them stay in their "pressed" state when they are clicked? By this I only need the color of the pressed button to show. Or is there a way of doing this with some type of radio group, where the radio buttons have the same style as a regular button?

Try:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
... do a call to your conversion code here ....
button.setPressed(true);
return true;
}
});
For more info: Android docs and this SO question

Related

Scroll able edit text Inside scroll view

I had an edit text which I wanted to make scrollable. The problem was that this edit text is located inside a scrollable view.
I searched the internet and found a solution that works fine. Source:How to scroll the edittext inside the scrollview
youredittext.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (youredittext.hasFocus()) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_SCROLL:
v.getParent().requestDisallowInterceptTouchEvent(false);
return true;
}
}
return false;
}
});
The problem is after applying this solution the edit text would only scroll without a "velocity vector." So like you have to hold down your thumb and move it but the moment you remove your thumb it stops scrolling immediately.
While for example, the android scroll view has a velocity vector in which you scroll up fast then you remove your thumb, it would keep scrolling with decreasing speed. Similar to scrolling on Facebook and Instagram.
Any help is appreciated and thanks!

Ignore touch in overlay app

i create a system overlay app using this way
but i have a problem .... when i move my button to corner of screen, i can't touch system's view like Call button in the following image
image
how can i disable any touch in my button ? ( ignore my view's touch and touch system's view )
in somewhere i find this code but it isn't work
bl.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
Always visit developer.android.com first, it's really well documented with fundamental concepts.
The onTouch method will pass the event to the layer below it, if it returns false.
If you've extended a default touchable View class, you should use return super.onTouch()
Here's the link you're looking for:
https://developer.android.com/reference/android/view/View.OnTouchListener.html

Set JButton unclickable yet does not grey out

Java provides a simple method to disable its buttons with button.setEnabled(false);
However doing so will grey out the entire button thus affecting the visibility of the text and images on the disabled button.
Question: Are there any available methods/ways in Java to allow buttons to be disabled yet does not grey out?
Certainly, I am not expecting manual tweaking on the ActionListener of the buttons to achieve this.
Companies spend millions of dollars to develop a UI can is common and can be used by all users.
How is the user suppose to know that the button is disabled if there is no visual indication?
I am not expecting manual tweaking on the ActionListener of the buttons to achieve this.
Why? What is wrong with this approach? It is better than trying to create a custom LAF for all platforms your code might run on.
Anyway (rant finished) you could use a custom ButtonModel:
button.setModel( new DefaultButtonModel()
{
#Override
public boolean isArmed()
{
return false;
}
#Override
public boolean isPressed()
{
return false;
}
});
Should work for all LAF's and the button won't be painted as a pressed (which I suppose is better than just removing the ActionListener).
This is a little bit of work, and may not be the best coding style or solution, but it might satisfy your requirements. I'm going to assume you have an icon for when the button is enabled and another for when it is not.
Basically, create and manage your own, virtual "disabled" state for the button (but don't touch the built-in enabled or disabledIcon methods).
In the code that manages the enabling/disabling, when you disable "virtually", set the regular icon ( setIcon() ) to be your disabled Icon graphic, and set a boolean to reflect the virtual "disabled" state of the button.
When you "enable", use setIcon() to return the default Icon, and set the boolean to reflect "enabled".
Then, when the button is clicked, in the ActionListener, you can inspect the boolean and do nothing if the boolean says the virtual state is "disabled".

How to check if and which mouse button is pressed in Swing

How can I check if currently any mouse button is pressed and if so, which one it is?
The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.
The getButton() method seems to only return value if there has been change in buttons' state.
Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.
How can I check if currently any mouse button is pressed and if so, which one it is?
Presumably you want to invoke specific code depending on the button pressed so you can do something like:
if (SwingUtilities.isLeftMouseButton(...))
// do something
You could start by looking at How to write a Mouse Listener and the JavaDocs for MouseEvent in particular, the getButton method.
However, there are cross platform considerations that need to taken into consideration, which are overed by SwingUtilities.isLeftMouseButton and equivalent methods...
This will solve your problem
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.out.println(e.paramString()+"-"+e.getSource());
}
}, eventMask);
This is a Global Event Listeners.
Get the source and button from AWTEvent and do whatever you want to perform.

How to disable second click of splitpane's one touch exapandable button in Swing?

I want to disable the second click option of JSplitpane's one touch expandable button.
Is there any property to disable?
Please help me. Thanks in advance.
Edit: I tried the following method using BasicSplitPaneDivider
protected JButton createLeftOneTouchButton() {
JButton left = super.createLeftOneTouchButton();
left.setEnabled(true);
// left.doClick(1);
// left.setMinimumSize(new Dimension(300,600));
return left;
}
AFAIK, no, there's no quick & easy way to do this. The buttons are created together in BasicSplitPaneDivider's oneTouchExpandableChanged() method.
You could override this by creating your own implementation of BasicSplitPaneDivider, but then you get into the messy world of playing with the L&F.

Categories

Resources