I am new to Android development and Eclipse. I have been coding on ASP.Net and MS Visual Web Developer for years. In VWD, when you add a control to the design view, double clicking on it will automatically bring you to code view for the OnClick function of the control you have just created. You can also see the list of possible event handlers for a control from the design view.
But I can't seem to find this feature in Eclipse. Is there such a thing? I did a search on Google and the best I found is this (same question but without an answer).
http://www.techrepublic.com/forum/questions/101-341077/event-handlers-in-eclipse
Anyone to advice please?
Thanks!
What you're talking doesn't quite exist in Eclipse. You'll have to manually open your java class and add the method to the corresponding java activity there.
For example, if you set the android:onClick XML attribute to "myAwesomeMethod" in your layout XML file, in the corresponding Activity that uses that layout, you'll need to make sure you have a "myAwesomeMethod" method defined.
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myAwesomeMethod" />
<!-- even more layout elements -->
In your java Activity class:
public void myAwesomeMethod(View v) {
// does something very awesome
}
Note: you can also do this programmatically, which is what I generally do. However, defining the android:onClick method will save you a few lines of code.
For more information, check out this post. It gives a lot more detail on how to assign onClick handlers to a button and the two ways you can do so.
No, that is not how Eclipse works. You add the control in the xml file, then in the activity that you are going to load that layout in you add the onClickListener on the element you want to respond to clicks for,
Related
Up until 12 hours ago I had never touched Android programming or even XML. As such my question may appear incredibly stupid.
Basically my "app" (if you can call it that) consists of a tabbed fragments with placed immediately below the "app_name" of my app.
What I would like to do is to add some STATIC text immediately below the app_name and (at the same time) immediately above the tabbed fragments -- we are talking about a SINGLE text "element" just be clear. What I have tried so far has been.... unsuccesful. Basically, I thought it would be as follows:
My main.xml currently looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/TextView01"
android:layout_below="#+id/AnalogClock01"
android:layout_width="wrap_content"
android:text="#+id/TextView01" android:layout_height="wrap_content">
</TextView>
</LinearLayout>
where I believe the TextView component would be all the XML required to add the static text-field I want. Subsequently I add:
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("Additional information");
to my MainActivity.java file immediately after calling super.onCreate(savedInstanceState) and my naivity begs me to belive that it should work, but clearly it doesnt (in fact the app crashes when loading it into the emulator).
As this does not work the purpose of my post is to hopefully get one or more of you to offer a solution to "fix" this.
Thanks
Subsequently I add: ... MainActivity.java file immediately after calling super.onCreate(savedInstanceState)
If you're doing that, you're going to have a problem.
You need add
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("Additional information");
after setContentView(R.layout.main);
First you need to give your Activity something to display (a UI) then you can modify the components that make up that UI.
Otherwise findViewById() will return null and you will end up with NullPointerExceptions
Some other things to note:
android:layout_below="#+id/AnalogClock01"
You don't have a AnalogClock01 defined in that layout. If it is defined somewhere else though, you might not crash your app, but things probably won't work as expected.
Even if you did have AnalogClock01 in that layout, you do not access it with #+id/ since you're not assigning an id, but accessing it. Use #id/ instead.
And
android:text="#+id/TextView01"
is also an issue.
To reference Strings from your res/values/strings.xml file, use #string/string_name No + either, and definitely not an id.
You should try to make the activity have multiple layouts in the one activity.
This may help: http://developer.android.com/guide/topics/ui/declaring-layout.html#Position
Also try the .onStart() method instead, although I don't exactly know if it will change anything.
I would like to know if this kind of layout is supported under Android and what is its name.
The description:
the application is revealed by default in a main view and a menu will appear when making a "swipe gesture" from the margins to the center of the screen; usually this layout has to offer some kind of callback or manage to stop the underlaying activity for the application so the user can use the menu without interfering with what he is doing with the application itself.
Thanks.
If you are looking for a sliding layout, here is an example:
you can call it with:
<com.slidingmenu.lib.SlidingMenu
xmlns:sliding="http://schemas.android.com/apk/res-auto"
android:id="#+id/slidingmenulayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
sliding:viewAbove="#layout/YOUR_ABOVE_VIEW"
sliding:viewBehind="#layout/YOUR_BEHIND_BEHIND"
sliding:touchModeAbove="margin|fullscreen"
sliding:touchModeBehind="margin|fullscreen"
sliding:behindOffset="#dimen/YOUR_OFFSET"
sliding:behindWidth="#dimen/YOUR_WIDTH"
sliding:behindScrollScale="#dimen/YOUR_SCALE"
sliding:shadowDrawable="#drawable/YOUR_SHADOW"
sliding:shadowWidth="#dimen/YOUR_SHADOW_WIDTH" />
There is no built in function like that, so you have to use third party libraries
There is no built in functionality for this behavior. You'll need to make it yourself.
For clarification: I want all the custom dialogs to look like system default dialogs: for example, when user is using htc sense, the dialog should match htc sense dialog style, when user is using samsung with official customasation, the dialog should look like it. I want to go as native as possible. I am familiar that there are possibilities to modify the dialog, but I'm looking for a way to reference to the style the system uses and create a dialog with that.
What I have:
What I would like to achieve:
Code for my dialog layout which I invoke with dialog.setContentView(...):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/Theme.Dialog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip" >
</ListView>
</LinearLayout>
As you can see setting the style="#android:style/Theme.Dialog isn't helping. Any ideas what might be helping?
(I am familiar that there is already such a question: https://stackoverflow.com/questions/6746535/android-custom-dialog-but-with-look-and-feel-of-default-one but there isn't an accepted answer)
If you want the dialog that appear just for your application purpose then follow this:
See this image i have create this one to save Image or Post it to different way.
I have created the xml layout as per my requirement. If you want to add the list view as you have explained in to the question then you have to manualy implement that behaviour in the content of the dialog. Means for such layour You have to implement the Custom ListView that have that radio button and text. And also have some Java code to do action according to that selection.
With that you can acheive as you want.
Enjoy. :)
You can build a native-look like dialog for one UI, but when another user has some other ROM/modified UI, then the dialog doesn't look like a native one to hem/her. There isn't a built in functionality, to access system dialog resources and populate all dialogs from native layouts only.
I`m wondering about one thing. If I decide to have a part of my View same in every Intent. For example 2 buttons at the bottom of screen and for example I have 3 diffrent views, List, Detail and a third one :) Do I need to put the buttons on every xml schem for each view or can I create other xml and attach it in each activity with all listener etc.
If I can attach in activity how can I do that ?
Create a XML buttons.xml with the common elements
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/myButton"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
<ImageButton
android:id="#+id/myButton2"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
</merge>
Include it in another xml:
<include layout="#layout/buttons"/>
where buttons is the name of the xml file to be included
You can use the <include /> tag in your XML files.
See Layout Tricks for an example.
Create a separate layout for your buttons then in the layout you wish to display them use the include tag.
This will allow you to reuse the same layout in multiple parent layouts.
The include works as stated above you could also consider using fragments
Fragments
You can use these in older versions of Android by including the compatibility library in your application. It's definitely more work than a simple include but if you need some reusable UI for more sophisticated features than simple buttons you might want to look at that as well
How to use compatibility API
When doing Android Development where can I find a list all the valid XML attributes for the Button element?
I have tried looking at the schema http://schemas.android.com/apk/res/android, but that is just a place holder of some kind because nothing exists at the URL.
The documentation for Button lists the documented XML attributes. There are not specifically for Button, just those inherited from TextView (and View). If you go by the sources, you may find an attribute that appears to be supported, but if it's undocumented, the support could go away in a future version of the OS.
You can find them here. Expand 'Inherited XML Attributes'.
If using eclipse, one simple approach is to start creating a button in the xml file:
<Button
android:id="myButton" >
</Button>
Next, you can start typing another attribute. First type "android", then wait for a couple of seconds:
<Button
android:id="myButton"
android >
</Button>
After you wait, type a colon:
<Button
android:id="myButton"
android: >
</Button>
After this point, the IDE should popup a window displaying a list of all the options, which you can select to read the javadocs for the selected item.