I have an activity that gets touch events (int x,int y, int type of event) and manage a mapview(osmdroid) or buttons with the given information. I have to cover the mapview so I placed it on a framelayout and place and upper image. If the upper image is visible, I am not able to pan the mapview underneath (but I am able to zoomIn and zoomOut).
this is my activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.client.Client"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/Disconnect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Disconnect/ Reconnect"
android:onClick="disconnect"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/frameLayout"
android:layout_below="#+id/disconnect">
<LinearLayout
android:id="#+id/memeContent"
android:layout_width="535px"
android:layout_height="350px"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical"
>
<org.osmdroid.views.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="300px"
>
</org.osmdroid.views.MapView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50px"
>
<Button
android:id="#+id/ZoomIn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="5"
android:onClick="ZoomIn"
android:text="Zoom In"
android:textSize="10dp" />
<Button
android:id="#+id/ZoomOut"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="5"
android:text="Zoom out"
android:onClick="ZoomOut"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imageViewFront"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/image"
android:scaleType="centerCrop"
android:focusable="false"
android:focusableInTouchMode="false"/>
</FrameLayout>
</RelativeLayout>
and this is my MainActivity.java
public class MainActivity extends Activity {
LinearLayout memecontentView;
FrameLayout frameLayout;
ImageView imageViewFront;
View v;
MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_late);
imageViewFront= (ImageView) findViewById(R.id.imageViewFront);
//imageViewFront.setVisibility(View.GONE);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
mapView.setMultiTouchControls(true);
mapView.setUseDataConnection(true);
memecontentView=(LinearLayout) findViewById(R.id.memeContent);
mapView.setBuiltInZoomControls(false);
}
//look for items in given coordinates
public void lookForButton(String msg){
String[] strArray = msg.split(" ");
final MotionEvent m;
final int x=Integer.valueOf(strArray[1]);
final int y=Integer.valueOf(strArray[2]);
int type=Integer.valueOf(strArray[3]);
switch (type){
case 2:
m = MotionEvent.obtain(226707,226707,0/*ACTION_DOWN*/,x,y,0);
runOnUiThread(new Runnable() {
public void run() {
memecontentView.dispatchTouchEvent(m);
}
});
break;
case 3:
m = MotionEvent.obtain(226707,226707,1/*ACTION_DOWN*/,x,y,0);
runOnUiThread(new Runnable() {
public void run() {
memecontentView.dispatchTouchEvent(m);
}
});
break;
case 5:
m = MotionEvent.obtain(226707,226707,2/*ACTION_MOVE*/,x,y,0);
runOnUiThread(new Runnable() {
public void run() {
memecontentView.dispatchTouchEvent(m);
}
});
break;
}
}
public void ZoomIn(View v){
mapView.getController().zoomIn();
}
public void ZoomOut(View v){
mapView.getController().zoomOut();
}
}
If the upper image is not visible (imageViewFront.setVisibility(View.INVISIBLE);) the code above works great, but if I comment that line(I need to do it), I am not able to pan the mapview. I do not know if the upper image is stealing its touchevents. How can I prevent that? or how can I make MapView's touch events work even if mapView is under?
Try setting an onTouchEventListener that returns false for all touches. Apply that to any view that may be above the mapview, including layouts. That should make the touch go to the view underneath it, which will eventually hit the mapview.
frameLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
return false;
}
});
Related
I have two views in my activity in my Android app. One View (we'll call this viewOne) has a bunch of SeekBars while the other view (we'll call this viewTwo) contains a Drawable that is redrawn whenever a user touches it.
viewTwo has an OnTouchListener. The issue is that when I touch viewOne to move a SeekBar, viewTwo's touch listener is getting called and this creates a huge amount of lag due to it needing to redraw every time the listener is called.
Why is this happening and how can I stop it? Thanks!
EDIT:
Main Activity Layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<com.rfoo.viewOne
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:id="#+id/vOne"/>
<com.rfoo.viewTwo
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:id="#+id/vTwo"/>
</LinearLayout>
View One Layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:id="#+id/sb1"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:id="#+id/sb2"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:id="#+id/sb3"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:id="#+id/sb4"/>
</LinearLayout>
View Two Layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myView"/>
</LinearLayout>
Main Activity Code:
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initializations here...
// And then:
View viewTwo = (View)findViewById(R.id.vTwo);
viewTwo.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.vTwo) {
vTwo.invalidate();
}
}
}
You implemented View.OnTouchListener but you haven't used it. I think your purpose is using that to trigger onTouch()
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initializations here...
// And then:
View viewTwo = (View)findViewById(R.id.vTwo);
viewTwo.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.vTwo) { // you can use viewTwo instead of v as well
viewTwo.invalidate();
}
}
}
Well i am almost sure the problem is because the unused nested layouts i tried this and worked fine for me :
public class exampleActivity extends AppCompatActivity {
private static final String DEBUG_TAG = "Motion event";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_layout);
findViewById(R.id.myView).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return (v.getId() == R.id.myView) &&
handleGesture(MotionEventCompat.getActionMasked(event));
}
});
}
private boolean handleGesture(int action) {
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d(DEBUG_TAG, "Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,"Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
Log.d(DEBUG_TAG,"Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d(DEBUG_TAG,"Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return false;
}
}
}
And this is my xml layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".25"
android:id="#+id/sb1"/>
<SeekBar
android:layout_width="match_parent"
android:layout_weight=".25"
android:layout_height="0dp"
android:id="#+id/sb2"/>
<SeekBar
android:layout_width="match_parent"
android:layout_weight=".25"
android:layout_height="0dp"
android:id="#+id/sb3"/>
<SeekBar
android:layout_width="match_parent"
android:layout_weight=".25"
android:layout_height="0dp"
android:id="#+id/sb4"/>
<View
android:layout_weight="1"
android:background="#color/appframe__colorAccent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/myView"/>
</LinearLayout>
I'm having an issue with button click events and seekbar events not firing. When I drag on my seekbar, the slider changes position visually but there's no event being fired in my OnSeekBarChangedListener. I also have an ImageButton that should receive click events but it does not fire either. The MyView at the bottom here handles touch events properly. It holds a GLSurfaceView that I can pan around and pinch-zoom on. What could be causing the events to not fire?
Here's my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tec_root">
<ImageButton
android:id="#+id/button_star"
android:contentDescription="#string/star_description"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/star_empty"
android:background="#00000000"
android:scaleType="fitXY"/>
<LinearLayout
android:id="#+id/view_plot"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/button_star"/>
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/button_star"
android:layout_toStartOf="#id/button_star"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:progress="50"
android:layout_alignBaseline="#id/button_star"
android:id="#+id/seekbar" />
</RelativeLayout>
And here are the listeners:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
starred = prefs.getBoolean("starred", false);
final ImageButton starButton = (ImageButton) findViewById(R.id.button_star);
starButton.setImageDrawable(ContextCompat.getDrawable(this, (starred ? R.drawable.star_filled :
R.drawable.star_empty)));
starButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
starred = !starred;
prefsEditor = prefs.edit();
prefsEditor.putBoolean("starred", starred);
prefsEditor.apply();
starButton.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
(starred ? R.drawable.star_filled : R.drawable.star_empty)));
}
});
((SeekBar) findViewById(R.id.seekbar)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.e("TEST", "Seekbar onProgressChanged called");
Toast.makeText(getApplicationContext(), "Seekbar fired", Toast.LENGTH_SHORT).show();
// respond to update
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
MyView myView = new MyView(this, points, false);
((LinearLayout) findViewById(R.id.view_plot)).addView(myView);
}
try this, replace LinearLayout with FrameLayout, it's not best at perfomance but works well
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tec_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<SeekBar
android:id="#+id/seekbar"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="#ABABAB"
android:layout_weight="1"
android:progress="50" />
<ImageButton
android:id="#+id/button_star"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#00000000"
android:contentDescription="#string/star_description"
android:scaleType="fitXY"
android:src="#drawable/star_empty" />
</LinearLayout>
<FrameLayout
android:id="#+id/view_plot"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
Try changing your code to this:
MyView myView = new MyView(this, points, false);
myView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
((LinearLayout) findViewById(R.id.view_plot)).addView(myView);
Could this be reproduced by removing the GLSurfaceView?
If so, changing SurfaceView.setZOrderOnTop might help.
http://developer.android.com/reference/android/view/SurfaceView.html#setZOrderOnTop(boolean)
there may be issue of layout design so please rearrange the components of ui.
seekbar is over imagebutton so android platform is not able to detect touch event.
set layout_below in seekbar
One possible reason for this could be touch handling in custom MyView. We have to be very careful about return value of onTouchEvent() function. Try returning "true" from your onTouchEvent() if you are consuming event else return "false".
I am looking to slide/up down a nested layout on its parent layout click.
Ie the parent layout will have a hidden child. On click I would like the parents height to animate down (slide down) to fit the child layout. On click again I would like the child to animate up (slide up). Basically just animating the parents height to show/hide the child.
I have found this which looks to work but seems like a lot of code:
http://gmariotti.blogspot.com/2013/09/expand-and-collapse-animation.html
I have seen a lot of things using 'animateLayoutChanges' to animate things however I cannot get that to work.
I have tried this:
<LinearLayout android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout android:id="#+id/child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:animateLayoutChanges="true">
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text to show/hide"/>
</LinearLayout>
</LinearLayout>
Then in code:
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout child = (LinearLayout)findViewById(R.id.child);
child.setVisibility(child.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
That sets the visibility of the child view correctly but there is absolutely no animation.
Well, first of all android:animateLayoutChanges effects the child elements. So, obviously, if you are changing the properties of the element itself, it will not be animated.
I think you can accomplish what you are trying to in API 16 by enabling the LayoutTransition.CHANGING animation.
<LinearLayout android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Some text to show/hide"/>
</LinearLayout>
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
View text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View message = findViewById(R.id.message);
ViewGroup.LayoutParams params = message.getLayoutParams();
params.height = params.height == 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
message.setLayoutParams(params);
}
});
Try to use SlidingDrawer
use this code
xml layout sideupdown.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical" >
<Button
android:id="#+id/bopen"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="open" />
<Button
android:id="#+id/btogg"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="change" />
<Button
android:id="#+id/bclose"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
<SlidingDrawer
android:id="#+id/slidingDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="#+id/chkbok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="On/Off" />
</LinearLayout>
</SlidingDrawer>
</FrameLayout>
java class SlideUpDown.java
public class SlideUpDown extends Activity implements OnClickListener,
OnCheckedChangeListener, OnDrawerOpenListener, OnDrawerCloseListener {
Button opn, cloz, chng, hndl;
CheckBox chkbox;
SlidingDrawer sd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sideupdown);
refference();
opn.setOnClickListener(this);
cloz.setOnClickListener(this);
chng.setOnClickListener(this);
chkbox.setOnCheckedChangeListener(this);
sd.setOnDrawerOpenListener(this);
sd.setOnDrawerCloseListener(this);
}
private void refference() {
opn = (Button) findViewById(R.id.bopen);
cloz = (Button) findViewById(R.id.bclose);
chng = (Button) findViewById(R.id.btogg);
chkbox = (CheckBox) findViewById(R.id.chkbok);
sd = (SlidingDrawer) findViewById(R.id.slidingDrawer);
hndl = (Button) findViewById(R.id.handle);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bopen:
sd.open();
break;
case R.id.bclose:
sd.close();
break;
case R.id.btogg:
sd.toggle();
break;
}
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkbox.isChecked()) {
sd.lock();
hndl.setEnabled(false);
} else {
sd.unlock();
hndl.setEnabled(true);
}
}
#Override
public void onDrawerOpened() {
}
#Override
public void onDrawerClosed() {
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
In one of our applications we hide and show a header and footer. We do this by changing the height and visibility of the view.
It looks something like this
ValueAnimator va;
if (show)
va = ValueAnimator.ofInt(0, px);
else
va = ValueAnimator.ofInt(px, 0);
va.setDuration(400);
va.setInterpolator(new DecelerateInterpolator(2));
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
topicNavBar.getLayoutParams().height = value;
topicNavBar.requestLayout();
if (value == 0) {
if (show)
topicNavBar.setVisibility(View.VISIBLE);
else
topicNavBar.setVisibility(View.GONE);
}
if (show && value == px) {
animationInProgress = false;
}
if (!show && value == 0) {
animationInProgress = false;
}
}
});
va.start();
Hope this helps
I am trying to implement a virtual joystick that is sending data through a socket. I want data to be sent only when user is pressing (touching) the joystick. Everything was working fine with the code, but I decided that joystick view should be bigger. To achieve it i changed the android:layout_weight in the leftSide RelativeLayout from 4 to 3. After this adjustment on touch events are never registered. Joystick animation is working perfectly though. Reverting changes make the program work again.
What is happening here? What are the other ways to make view bigger on the screen?
I have the following layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
tools:context="com.my.package.DisplayControlActivity">
<RelativeLayout
android:id="#+id/leftSide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" >
<!-- working fine with android:layout_weight="4" -->
<SurfaceView
android:id="#+id/videoStream"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</SurfaceView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rightSide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:id="#+id/testImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:contentDescription="#string/test_image"
android:scaleType="fitStart"
android:src="#drawable/test_image" >
</ImageView>
<com.my.package.SquareJoystickView
android:id="#+id/virtual_joystick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</com.my.package.SquareJoystickView>
</RelativeLayout>
And here is OnTouchListerer part of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* some irrelevant code */
joystickView = (SquareJoystickView) findViewById(R.id.virtual_joystick);
isSending = false;
joystickView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
isSending = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isSending = true;
}
return true;
}
});
}
This seems to be a very silly problem but its just not working for me.
I have 2 ImageButtons & a HorizontalScrollView(HSV) on a page. Strangely one of the Image Button(1st one) is not clicking. Also I wanted to make the HSV clickable so I used:
android:clickable="true", which does not work
You can use the xml & activity exactly as I have posted.
This is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/topRL"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/free_msgs_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="free msgs left" />
<ImageButton
android:id="#+id/buy_imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher"
/>
</RelativeLayout>
<ListView
android:id="#+id/chat_page_listView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<HorizontalScrollView
android:id="#+id/chat_message_HSV"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#+id/send_image_button"
android:clickable="true" />
<ImageButton
android:id="#+id/send_image_button"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
This is the activity file:
public class MainActivity extends Activity {
ImageButton buyIB;
ImageButton sendIB;
HorizontalScrollView chatMessageHSV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
sendIB = (ImageButton) findViewById(
R.id.send_image_button);
chatMessageHSV = (HorizontalScrollView) findViewById(
R.id.chat_message_HSV);
buyIB = (ImageButton) findViewById(R.id.buy_imageButton);
buyIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "buy", Toast.LENGTH_SHORT)
.show();
}
});
chatMessageHSV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hsv", Toast.LENGTH_SHORT).show();
}
});
sendIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "send", Toast.LENGTH_SHORT).show();
}
});
}// end of onCreate
}
And I cant use button click like
public void onClick(View view)
{}
because the actual problem is containing a drawer layout where I'm using fragments & there this method doesn't work.
The problem in the ImageButton doesn't respond for you click because the ListView will respond first for the click because it above the ImageButton try to re order it , when i remove it the ImageButton respond for the click
about the HorizontallScrollView i found the solution to use the Touch Event instead of Click try this code
chatMessageHSV.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(ImageButtonActivity.this, "hsv",
Toast.LENGTH_SHORT).show();
}else if (event.getAction() == MotionEvent.ACTION_UP){
// here code if the touch up
}
return false;
}
});
feed me back
The ImageButton not clicking problem can be solved by adding to ListView these 2 lines:
android:layout_below="#+id/topRL"
android:layout_above="#+id/bottomRL"