ListView cannot show after being invsibile during onCreate - android

I have problem I want to hide the listVeiw during onCreate,but the problem is that when I hide my listview to invisible, the setOnTouchListener will not work.but if I remove lstv.setVisibility(View.INVISIBLE); before the setOnTouchListener, the setOnTouchListener is working,but I need to hide first my listview during start up or onCreate.
Thank you in advance.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lstv.setVisibility(View.INVISIBLE);
vdo.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
lstv.setVisibility(View.VISIBLE);
lstv.postDelayed(new Runnable() {
#Override
public void run() {
lstv.setVisibility(View.INVISIBLE);
}
}, 3000);
return true;
}
});
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
tools:context=".MainActivity" >
<VideoView
android:id="#+id/vdo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="229dp"
android:layout_height="fill_parent"
android:smoothScrollbar="true"
android:soundEffectsEnabled="true"
/>
</FrameLayout >

For hide ListView and receive onTouchEvent on vdo
You should change
lstv.setVisibility(View.INVISIBLE);
to
lstv.setVisibility(View.GONE);
Hope this help

You can hide the listview in the layout(xml) itself. Like this
<ListView
....
....
android:visibility="gone"
/>

Related

Clicking a scroll view

I have a scroll view that has a Linearlayout child and this linearlayout has a drawingview.
the problem : I tried almost all the available answers on this topic and none of them worked, I cant understand why click is not fired for (scrollview, linear layout and even drawingview).
note : I tried setting on click for each of the three and none worked.
the xml code:
<ScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#id/scrollview"
android:clickable="true"
>
<LinearLayout
android:setorientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/linear"
android:clickable="false">
<Customview
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/draw"
android:clickable="false"/>
</LinearLayout>
</ScrollView>
First your ScrollView ,layout_height is wrapped! and for children you gave match_parent i doubt your view won't even display. Isn't it?
Then no idea why you use setorientation and android:id="#id/draw" instead of android:orientation and android:id="#+id
If you want to check scrolling works use
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
Log.d("TAG", "onScrollChanged: ");
// DO SOMETHING WITH THE SCROLL COORDINATES
}
});
You are using unnecessary attributes check this example :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8768"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollIndicatorDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#823">
<LinearLayout
android:id="#+id/first_child"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#900"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#987"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
If you write an onClickListner to the id linear_layout it just work normally!
Answer :The child Views of your ScrollView are consuming the click events you do on the ScrollView.
The solution to this problem is to set the onClickLIstener to the immediate child of the ScrollView(It's child).
So according to my example if i want to find out the ScrollView onClick i write my listener to its child.
scrolChild =(LinearLayout) findViewById(R.id.first_child) ;
scrolChild .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivityName.this, "You Clicked Me OH yeaaaaa", Toast.LENGTH_SHORT).show();
}
});
Read here when you need androidclickable-true android:clickable="true" mean's that it's not clickable?
This is my activity
public class MainActivity extends AppCompatActivity {
private ScrollView scrollView;
private LinearLayout scroolChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
scrollView = (ScrollView) findViewById(R.id.scrollIndicatorDown);
scroolChild =(LinearLayout) findViewById(R.id.first_child) ;
scroolChild.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You Clicked Me OH yeaaaaa", Toast.LENGTH_SHORT).show();
}
});
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
Log.d("TAG", "GotAScrollChanged: ");
// DO SOMETHING WITH THE SCROLL COORDINATES
}
});
}
}
You have set android:clickable="false" for both make it true it will work for you
<ScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#id/scrollview"
android:clickable="true"
>
<LinearLayout
android:setorientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/linear"
android:clickable="true">
<Customview
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/draw"
android:clickable="true"/>
</LinearLayout>
</ScrollView>

Scrolling is not working when I set GestureListener to ScrollView

I have a ScrollView and a nested TextView. When I set setOnTouchListener to it,the gestures are recognized but the scrolling not working. And If I set setOnTouchListener to nested TextView,its working fine. I have tried by googling but could not solve the problem.
But my need is to set the setOnTouchListener to ScrollView.
Please help.
layout.xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:background="#00FF00"
android:padding="15dp"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
MainActivity.java
mGestureDetector = new GestureDetectorCompat(this,this);
findViewById(R.id.scrollView).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, final MotionEvent event) {
Log.e("Stark", "setOnTouchListener");
mGestureDetector.onTouchEvent(event);
return true;
}
});
Why you need to set onTouch. If only scroll up and down. Try below
ScrollView sv;
sv.post(new Runnable() {
#Override
public void run() {
sv.fullScroll(View.FOCUS_DOWN);
}
});
make sure you have this in scrollview xml. hope it helps.
android:fillViewport="true"

Touch not working on list view

I am using touch event on LinearLayout in which there is a list view(Its a child). When I touch on list view part touch event does not gets called. below is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llFavourite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/llProgressBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:gravity="center"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Loading"
android:indeterminate="true"
android:progressDrawable="#android:drawable/progress_indeterminate_horizontal"
android:visibility="gone" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:focusable="true">
</ListView>
</LinearLayout>
My animation:
final Animation slidedown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down_half);
llFavourite.setOnTouchListener(new OnSwipeTouchListener(MidnightMainActivity.this) {
#Override
public void onSwipeBottom() {
llFavourite.startAnimation(slidedown);
inflateFavouriteLayout(MODE_REFRESH);
}
});
but when I click on layout part event gets called. Is there any property which i should enable or disable?
Thanks in advance.
android:focusable="false"
android:focusableInTouchMode="false"
doesn't work for ImageButton.
In your layout xml, add this property to root layout
android:descendantFocusability="blocksDescendants"
from your calling class add
ListView lView = (ListView) findViewById(R.id.list);
lView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(position);
}
});

Sliding Drawer close on back button pressed

I have a sliding drawer in my application.
It open when I click on android:handle, which I have given to an image view.
I want to close the drawer when back button is pressed.
Code:
SlidingDrawer slider;
#Override
public void onBackPressed() {
slider.close ();
}
super.onBackPressed();
Main. Xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<SlidingDrawer
android:id="#+id/slider"
android:layout_width="150dp"
android:layout_marginRight="0dp"
android:layout_height="match_parent"
android:handle="#+id/handle"
android:content="#+id/content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/handle"
android:background="#drawable/up"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
/>
<GridView
android:id="#+id/content"
android:background="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</SlidingDrawer>
</LinearLayout>
</RelativeLayout>
Activity file
#Override
public void onCreate(Bundle savedInstanceState)
{
SlidingDrawer slide = (SlidingDrawer)this.findViewById(R.id.slider);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override public void onBackPressed() {
if(slide.isOpened()){
slide.close ();
}
else{
super.onBackPressed();
}
}
}
You only need to close it if it's actually opened:
SlidingDrawer slider;
#Override
public void onBackPressed() {
if (slider.isOpened()) {
slider.close ();
} else {
super.onBackPressed();
}
}
EDIT
your slider field is out of scope. You need to declare it as a class field in order it can be accessed by all non-static methods. Also consult this document.
You don't call the super method inside the in on back pressed method you just give this only...
#Override
public void onBackPressed() {
slider.close ();
}
Don't call Super method inside the back pressed

Locking Horizontal ScrollView - Android

I'm writting an application and it looks like this:
How you can see there is SeekBar on top and several buttons in HorizontalScrolView below. I would like to use SeekBar to scrolling buttons and this works fine now, but I can't turn off the default way to scroll View (draginng screen). I found similat topic on forum:
Similar Topic
But the problem is, that this way locking whole ScrollView and I cant use it by seekBar and default way - maybe I'm doing something wrong (I did it the same like in the link and this not wort even if I have TRUE in mScrollable parameter). The second problem is that my buttons (which have fill_parent height parameter) are really small.
Question: There is any good way to Lock Scrolling View in default way and use SeekBar to do this??
My Code (default HorizontalScrollView):
public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener {
static private String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
SeekBar seek = (SeekBar) findViewById(R.id.seekBar);
final HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.hsv);
seek.setMax(948);
seek.setProgress(474);
seek.setOnSeekBarChangeListener(this);
hsv.post(new Runnable() {
#Override
public void run() {
hsv.scrollTo(474, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.hsv);
hsv.scrollTo(progress, 0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d(TAG, "Tracking on");
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d(TAG, "Tracking off");
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0A0A0A"
android:orientation="vertical" >
<SeekBar
android:id="#+id/seekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- BUTTONS CODE - NOT IMPORTANT -->
</RelativeLayout>
</HorizontalScrollView>
</LinearLayout>
There is any good way to Lock Scrolling View in default way and use
SeekBar to do this??
Edit:
The solution seems to be much simpler, override the HorizontalScrollView class and pass the touch events through the view:
public class MyHScroll extends HorizontalScrollView {
// the constructors
#Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
The use this class in the layout instead of the default HorizontallScrollView:
<com.your.package.MyHScroll
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- BUTTONS CODE - NOT IMPORTANT -->
</RelativeLayout>
</com.your.package.MyHScroll>
Try placing an overlay view on top of the HorizontalScrollView to "absorb" touch events without messing with the HorizontalScrollView itself:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0A0A0A"
android:orientation="vertical" >
<SeekBar
android:id="#+id/seekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout android:layout_width="wrap_content"
android:layout_height="fill_parent">
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- BUTTONS CODE - NOT IMPORTANT -->
</RelativeLayout>
</HorizontalScrollView>
<View android:id="#+id/overlay" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout
</LinearLayout>
and then add a OnTouchListener for the overlay widget in the onCreate method to further block touch events:
findViewById(R.id.overlay).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
The second problem is that my buttons (which have fill_parent height
parameter) are really small.
See the layout file above.

Categories

Resources