Hide and show linearlayout - android

I have one linearlayout and have also a few button inside it.I want make it visible when touch and invisible when touch it again.
How can i make it??

LinearLayout one = (LinearLayout) findViewById(R.id.one);
one.setVisibility(View.GONE);
I suggest that you use GONE insteady of INVISIBLE in the onclick event because with
View.GONE the place for the layout will not be visible and the application will not appear to have unused space in it unlike the View.INVISIBLE that will leave the gap that is intended for the the layout

Add a boolean on your code
boolean flag = false;
then add android:clickable = true on your linear layout on xml
then use this code for reference
your_linear_layout = new OnClickListener(){
#Override
public void onClick(View v) {
if (flag){
// means true
your_linear_layout.setVisibility(View.INVISIBLE);
flag = false;
}
else{
your_linear_layout.setVisibility(View.VISIBLE)
flag = true;
}
}
};
Havent tried this yet but this should work..
Cheers

add setOnTouchListener to linearLayout get touch events as :
linearLayout.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// show-hide view here
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
// show-hide view here
return true;
}
return false;
}
});
for making View visible use yourview.setVisibility(View.VISIBLE) and for Invisible use yourview.setVisibility(View.INVISIBLE)

You should user
Invisible -: mButton.setVisibility(View.INVISIBLE);
Vsible -: mButton.setVisibility(View.VISIBLE);
Put this code in onclick listner of button With checking if condition.

Related

how can visible and invisible a textView permanently on android?

I am working on an android app and I want to appear a textview as winker. Simplest way may be that visible and invisible permanently textView.I unfortunately am weak at most of technique.
What should I do for having such graphics working.
yourtextview.setVisibility(View.VISIBLE); //for visible
yourtextview.setVisibility(View.INVISIBLE); //for invisible
yourtextview.setVisibility(View.GONE); //for remove textview from Layout Spaces
If you want to Hide/Show your TextView then refer below part of code.
There are 3 methods to Hide/Show as below:
View.VISIBLE : This method will make your View Visible.
View.INVISIBLE : This method will make your view Invisible, but space will be occupied of that view, space will not be gone.
View.GONE : This method also make your View Invisible but space of that View will also be Invisible.
You can use that 3 methods like below:
//Instead of textView you can use any view like ListView, GridView, ImageView etc.
textview.setVisibility(View.VISIBLE);
textview.setVisibility(View.INVISIBLE);
textview.setVisibility(View.GONE);
If you want to dynamically do the following
pass the visibility flag to isTextVisble(flag) method //you need to pass the flag
private void isTextVisble(boolean isVisible) {
if(isVisible)
txtView.setVisibility(View.VISIBLE);
else
txtView.setVisibility(View.GONE);
}
here you can use one button link on touch it will disapper like that you can create how manny buttons you want extra
/handle write click/
Button.setOnClickListener(new View.OnClickListener() {
//#Override
public void onClick(View v) {
Text1.setVisibility(View.INVISIBLE);
Text2.setVisibility(View.INVISIBLE);
Text3.setVisibility(View.INVISIBLE);
Text4.setVisibility(View.INVISIBLE);
if(Text.length() != 0)
deviceAddress = (byte) Integer.parseInt(Text.getText().toString());
else
deviceAddress = 00; /*default*/
Text.setText(Integer.toString(deviceAddress));
}
});
/*select the frequency*/
freqText.setOnTouchListener(new View.OnTouchListener() {
//#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Text1.setVisibility(View.VISIBLE);
Text2.setVisibility(View.VISIBLE);
Text3.setVisibility(View.VISIBLE);
Text4.setVisibility(View.VISIBLE);
return false;
}
});
/*set the selected value*/
Text1.setOnTouchListener(new View.OnTouchListener() {
//#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Text.setText(freqText1.getText().toString());
Text1.setVisibility(View.INVISIBLE);
Text2.setVisibility(View.INVISIBLE);
Text3.setVisibility(View.INVISIBLE);
Text4.setVisibility(View.INVISIBLE);
return false;
}
});

OnClickListener on scrollView

I have a scrollView with lot of elements
ScrollView scroller = (ScrollView)findViewById(R.id.scrollView);
I need to attach an onClickListener to the scrollview so I do
scroller.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// This is all you need to do to 3D flip
AnimationFactory.flipTransition(viewAnimator, FlipDirection.LEFT_RIGHT);
}
});
But this is not getting triggered when I touch. Any Ideas?
The best solution seem to put LinearLayout into ScrollView and set the setOnClickListener on it.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myLayout"
android:clickable="true"
android:orientation="vertical">
<!-- content here -->
</LinearLayout>
</ScrollView>
in the Activity :
LinearLayout lin = (LinearLayout) fragment.rootView.findViewById(R.id.myLayout);
lin.setOnTouchListener(new setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Whatever
}
});
You need to set the setOnClickListener directly on the ScrollView's child.
Since a ScrollView can have only one child, you can simply use this approach:
ScrollView scrollView = //...
View.OnClickListener mOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// ...
}
//Set the onClickListener directly on the ScrollView's child
scrollView.getChildAt(0).setOnClickListener(mOnClickListener);
It is because the child of the ScrollView is getting the touch event of the user and not the ScrollView. You must set android:clickable="false" attribute to each and every child of the ScrollView for the onClickListener to work on ScrollView.
Or else the alternate could be to set the onClickListener on each of the ScrollView's children and handle it.
UPDATE 22/12/2020
sadly this also triggers after each scroll event.
This the actually the answer to the question without any odd cases by using View.OnTouchListener instead of View.OnClickListener on the ScrollView and detecting the MotionEvent.ACTION_UP where the finger is left off the screen.
To make sure that it's not a scroll, then save previous touched screen x, y values of the MotionEvent.ACTION_DOWN and compare it to those of MotionEvent.ACTION_UP. If they are not equal then certainly the user is moving their finger (i.e. scrolling) before they left it off the screen.
int mXOld, mYOld; // field values to save the tap down on the ScrollView
scrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mXOld = x;
mYOld = y;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (x == mXOld || y == mYOld) { // detecting it's not a horizontal/vertical scrolling in ScrollView
// HERE add the code you need when the ScrollView is clicked
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
return false;
}
}
return false;
}
});
Original Answer: Odd case that is different than the question
My problem was somehow different, so I wanted to share it..
I have a ScrollView that I have to use match_parent in its width & height; and I have an internal TextView that is centered in the ScrollView.
The text of the TextView can be long so it occupies the full height of the ScrollView, and sometimes it can be short, so there will be blank areas on the top and bottom., So setting the OnClickListener on the TextView didn't help me whenever the text is short as I want the blank areas detects the click event as well; and also the OnClickListener on the ScrollView doesn't work..
So, I solved this by setting OnTouchListener on the ScrollView and put code into MotionEvent.ACTION_UP So it can kind of simulating complete tap by lefting off the finger off the screen.
private View.OnTouchListener mScrollViewTouchListener = new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// DO Something HERE...
}
return false;
}
};
As #Zain pointed out, sometimes it is necessary to capture OnClicks for the whole area of the Scrollview, while the childs may be smaller or invisible.
To circumvent scrolling detected as an onClick, we used a GestureDetector:
final protected GestureDetector gestureDetector = new GestureDetector(getActivity(),
new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
in onCreateView
scrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(gestureDetector.onTouchEvent(event)){
[do stuff]
}
return false;
}
});
I think you can custom a ScrollView, override the dispatchTouchEvent method, add add the custom onClick callback.

Android On Focus Listener and On Click Listener on ImageView

I have an imageview - It has both the attributes -focusable and focusableintouchmode set to true
<ImageView
android:id="#+id/ivMenu01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="true"
android:focusableInTouchMode="true" >
</ImageView>
I have implemented the onFocusChangeListener in my activity-
#Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.ivMenu01:
if (hasFocus) {
ivMenu01.setImageBitmap(Utility
.getBitmap("Home_ford_focus.png")); // Focussed image
} else {
ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford.png")); // Normal image
}
break;
default:
break;
}
}
Also the onClickListener -
case R.id.ivMenu01:
ivMenu01.requestFocus();
Intent iFord = new Intent(HomeScreen.this, FordHome.class);
startActivity(iFord);
break;
Now when i click the ImageView the first click gives the focus to the ImageView and the second click performs the action.
I am not sure why this is happening .
The first click should request focus as well as perform the action.
Any help on how to do this will be highly appreciated.
It's the way the widget framework is designed.
When you look at View.onTouchEvent() code, you'll find out that the click action is performed only if the view has taken focus:
// take focus if we don't have it already and we should in
// touch mode.
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallback();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// click
}
}
So, as you noticed, the first click makes the view gain focus. The second one will trigger the click handler since the view already has focus.
If you want to alter the bitmap of the ImageView when it's pressed, you should implement an View.OnTouchListener and set it via ImageView.setOnTouchListener() method. That listener should look more or less like this:
private View.OnTouchListener imageTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// pointer goes down
ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford_focus.png"));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// pointer goes up
ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford.png"));
}
// also let the framework process the event
return false;
}
};
You can also use a Selector aka State List Drawable to achieve the same thing. See reference here: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Any way to tell when any button in a layout is UN-pressed

(Android 3.2) I have a TableLayout with 9 buttons. I want to know when any of them are un-pressed, i.e., when a press is complete, i.e., ACTION_UP. I don't care which button, I just want to know when any button which had been pressed has just been released.
I was hoping there was an Android::onTouch in the XML, like there is an Android::onClick, and I could point them all at one onTouch event handler to look for an ACTION_UP. But there isn't. I'm trying to avoid writing 9 separate OnTouchListeners.
Any suggestions?
Thanks in advance.
why don't you add onTouchListener from code and indeed, do what you want to do when you have ACTION_UP, as seen here
You can just assign each view to a single onTouch listener programmatically instead of in the XML.
Make one listener:
private View.OnTouchListener myListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Your logic
return false;
}
};
and then add that to each button.
findViewById(R.id.btn1).setOnTouchListner(myListener);
findViewById(R.id.btn2).setOnTouchListner(myListener);
Optimally you could create these buttons programmatically instead of referencing them from xml so you can do this in loops, or possibly put them in one ViewGroup and iterate though its children and add the listeners that way.
why not just create a single listener:
OnTouchListener listener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Button b = (Button) v; // the button that just gets released
if (event.getAction() == MotionEvent.ACTION_UP) {
...
}
}
});
and attach to all the buttons using a loop:
Button buttons[] = {...};
for (Button b : buttons) {
b.setOnTouchListener(listener);
}
or alternatively:
int button_ids[] = {R.id.button1, R.id.button2, R.id.button3, ...};
for (int id : button_ids) {
((Button) findViewById(id)).setOnTouchListener(listener);
}

LinearLayout OnClickListener not responding

I'm creating a custom widget by expanding LinearLayout. One of the elements in my custom widget is a linear layout, inflated from another layout. When I set the OnClickListener it is not responding. Can you please advise?
Thanks!
Instead of using setOnClickListener use setOnTouchListener
This code will work as a onclick event
YourLinearLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
boolean returnValue = true;
if(event.getAction()==MotionEvent.ACTION_UP){ //on touch release
returnValue = false; //prevent default action on release
//do something here
}
return returnValue;
}
});
And then add this to your LinearLayout class to intercept child touch events
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true; //will prevent child touch events
}
Have you declared the LinearLayout clickable?
You can do this either in the XML:
android:clickable="true"
or in Java code:
myLinearLayout.setClickable( true );
Also see the other StackOverflow thread on this question:
onClickListener on a LinearLayout
Make sure you put all childs inside LinearLayout to android:clickable="false". Than myLinearLayout.setClickable( true ) works for me.

Categories

Resources