I'm trying to create a custom view component which looks like this:
a component will work as a menu - it has 4 imageviews inside which react to onClick event.
I made a separate .xml with them and a class for it. Than i set the parent node class from .xml "class = 'myclass'".
In my .xml an imageview has a onClick declared, but when i try to handle it using myclass - application crashes. What is wrong? Or I suppose the main question is: what is a right way to set event handlers for a custom view?Handlers which are in this views class definition. Should I write in .xml of a view something like
Addition: how do I, then, can fire events from this custom view for activity which will hold it to handle?
Here is my menu .xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
class="com.example.mtgesturestest.MTMenuViewController"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main_layout" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imageChannels"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/channels"
android:layout_marginBottom="10dp" />
<ImageView
android:id="#+id/imageMessages"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/messages"
android:layout_marginBottom="10dp" />
<ImageView
android:id="#+id/imageTimeline"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/timeline"
android:onClick="yell"
android:layout_marginBottom="10dp" />
</RelativeLayout>
<ImageView
android:id="#+id/imageMenu"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/menu"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-15dp"
android:onClick="maximizeMenu"/>
</RelativeLayout>
Application says it cant find onClick handler (maximizeMenu), but its searching it in the activity where I add this view instead of com.mtgesturestest.MTMenuViewController class. I want this menu to be independent. It should handle click, play some animation and then fire event for parent activity. So you can say that this onClick here is for inner use of my custom view.
Why not try to add the onClick listener from inside the MTMenuViewController class like
ImageView menu = (ImageView)this.findViewById(R.id.imageMenu);
menu.setOnClickListener(PUT THE LISTENER HERE);
Related
More specifically, I'm using SherlockListFragment inside a ViewPager and the following layout
(full source):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/filter_bar"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
...
/>
<ListView
android:id="#android:id/list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="true"
android:layout_above="#+id/filter_bar"
... />
<TextView
android:id="#android:id/empty"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="true"
android:layout_above="#+id/filter_bar"
android:text="..."
android:visibility="invisible"
.../>
</RelativeLayout>
When I open the application I see this:
That looks fine. Then I connect and open a filter, it's empty for now
Note that ListView gets resized just fine. Now I type in the filter to filter out everything:
Whoops! Action bar disappears from the display! However, if swipe back and forth through the ViewPager and do it again, everything becomes fine (and stays fine):
What could I do to make the empty view behave normally, just like ListView does?
set windowSoftInputMode to the activity declaration in AndroidManifest.xml . Something like this
<activity
android:name=".controll.MyActivity"
android:label="#string/title_activity_my"
android:windowSoftInputMode="adjustResize"/>
Have a look on following screenshot.
In this layout, I'm displaying overlay view (the one with pink color) to disable the log in button click until loading is complete. But the problem is that, though overlay view is appearing above login button but still login button click is called. Any idea whats happening here?
Following is my overlay view xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88ff0000">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/loader_bg">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Just a moment..."
android:textSize="12dp"
android:textColor="#000000"
android:layout_margin="10dp"
android:layout_gravity="center_horizontal"/>
<ProgressBar android:id="#android:id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:progressBarStyleSmall"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center_horizontal"
android:indeterminateDrawable="#drawable/loading_wheel"/>
</LinearLayout>
</RelativeLayout>
Solved it my self.
Android dispatches events to background widgets if there are not implemented for the overlay events. So simply add on click listener on the overlay view to avoid click on Login button.
Hi everyone I ve been searching this a lot but I couldn't find an answer that would work in my case. In my layout i have a listview and outside the list and more specific above i have an imageview an editText which work as a search and a custom Button!The problem is that when i press the button does not start the Activity which i have put in th Intent.Why is this happening?I cannot find the error and so far i had never problem with button events.
Here is my xml:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/imageView1"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="false"
android:layout_below="#+id/autoCompleteTextView1" >
</ListView>
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="LIST OF PATIENTS"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#33B5E5" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="90dp"
android:layout_height="85dp"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_below="#+id/textView1"
android:padding="2dp"
android:src="#drawable/search_user" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignBottom="#+id/autoCompleteTextView1"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="#+id/add"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_above="#+id/linearLayout1"
android:layout_alignParentRight="true"
android:background="#drawable/buttonadd"
android:onClick="button_click"
android:src="#drawable/add" />
</LinearLayout>
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_toLeftOf="#+id/linear"
android:ems="10"
android:hint="Search patient" />
the button is the right on the top next to autocomplete.In the .java doc i have right this code as far as concerned the button:
In the onCreate
addp=(Button) findViewById(R.id.add);
addp.setOnClickListener(this);
In th onClick
case R.id.add:
Intent add= new Intent(sqlviewpatients.this,afprognosis.class);
startActivity(add);
break;
I ve been doing the same thing to others button and they working only this one does not respond to onClick events.Please tell me what I am doing wrong?
thanks in advance
This is because your ids do not match:
addp=(Button) findViewById(R.id.add);
and:
<Button
android:id="#+id/addpatient"
Change the java to:
addp=(Button) findViewById(R.id.addpatient);
EDIT: If you are defining your onClick within the XML then you should not need to set an onClickListener within the java code. Just make the code:
Intent add= new Intent(sqlviewpatients.this,afprognosis.class);
startActivity(add);
within a method that is called button_click.
EDIT after Code update
Okay, after reading the code you just posted: sqlviewpatients:
what does customwindow3 look like? Does it inherit from ListActivity or what?
This class only implements onClickListener and so a bunch of code won't get executed, including onCreate unless of course, customwindow3 does
Original
1) your button's id is addpatient not add which is the clause if you switch statement
2) have you looked into doing this:
public void button_click(View v)
{
Intent .....
}
Also, from the snippet you posted this:
addp=(Button) findViewById(R.id.add);
addp.setOnClickListener(this);
shouldn't actually run (you'll get a null pointer exception) since there is no element with that id (unless of course you've defined that somewhere else and didn't show us).
First of all thanks everyone for your answers and your time!I appreciate it
Second i found where the error was. the onClick method should have an #overide above
I want to create and overlay view when a button is clicked. Something like clicking smileys button in Whatsapp.
I have created a framelayout with default visibility as GONE and added an onclick listener on smiley button to toggle its visibility.
My layout XML:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<ImageView
android:id="#+id/smileyButton"
android:layout_width="24dip"
android:layout_height="48dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/contactIconContentDescription"
android:src="#drawable/emo_im_happy" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/smileyButton" >
<TextView
android:id="#+id/chat_smiley_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white" />
</FrameLayout>
</RelativeLayout>
I have added onClickListener to smileyButton which toggles visibility of chat_smiley_list.
Other than some positioning problems, I am not sure whether this is the best way to do it.
I think you could use a Context Menu: documentation
I'm used to the fact that items of listview without any problem (even with complicated layout) handle onpress events (some actions that makes it pressed and unpressed). But now I want the same from LinearLayout - if you press anyway on layout, all descendants should get onpress events.
This is the part of my fragment layout, which should be highlighted onpress event.
<LinearLayout
android:id="#+id/ll_about_fragment_feedback"
android:gravity="center_vertical"
android:layout_height="54dp"
android:background="#drawable/about_item_bkg"
android:layout_width="match_parent">
<ImageView
android:layout_height="wrap_content"
android:background="#drawable/about_item_feedback_icon"
android:layout_width="wrap_content"
android:layout_marginLeft="11dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/about_fragment_feedback"
android:layout_marginLeft="14dp"
android:textSize="16dp"
android:textColor="#drawable/about_item_textcolor"/>
</LinearLayout>
Background of root layout changes onpress, but textcolor of descendants don't. Looks like they don't get event from root.
So, can you help?
UPDATE: LinearLayout ll_about_fragment_feedback has bound OnClickListener and there is everything fine with selecrors
If you want a Selector to apply to all views under your LinearLayout then you just need to add android:duplicateParentState="true" to all of the child views.
<LinearLayout
android:id="#+id/ll_about_fragment_feedback"
android:layout_width="match_parent"
android:layout_height="54dp"
android:background="#drawable/about_item_bkg"
android:gravity="center_vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:background="#drawable/about_item_feedback_icon"
android:duplicateParentState="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:duplicateParentState="true"
android:text="#string/about_fragment_feedback"
android:textColor="#drawable/about_item_textcolor"
android:textSize="16dp" />
</LinearLayout>
Just bind your onClickListener to root object of your layout (ll_about_fragment_feedback) and that's it.