[Q] Easy string initialize question - Android Software Development

Hi,
I am reading a book on Android development. In one of the source code examples the author uses the construct:
String tt = **;
I assume he is initializing his new string to empty, but I have never seen this before and just want to be sure. There is probably a more detailed meaning.
Google search doesn't find anything like this. Can someone confirm what this means?
Thanks for any info or pointers to the definition.
Barry.

Uh, that's an error.
// Initialize String, 2 ways
String tt = null; // I always use this
String tt = ""; // empty string can be unpredictable

Related

Simulate Button Click

Hi,
I am trying to simulate a PictureBox.Click event. I have searched these forums and the ones on MSDN with many different combinations of search terms.
However I cannot find anything!
Basically what I am trying to achieve is to fire the picturebox's click event from within my code. What I have read so far seems to indicate that I need to make a call to SendMessage (COM interop?) to actually make windows perform the click.
This is for the compact framework version 1.0.
Any help you can give would be great because this is all very new to me, I'm a web application developer by trade so i'm a fish out of water on this one!
OK, the other method that I am investigating is the use of the mouse_event as demonstrated in this article by Daniel Moth.
However I am struggling to find the namespace Win32Api anywhere in the framework so I'm struggling with that also.
*Update*
I have been able to simulate a click using mouse_event in a call to the coredll using the following code:
Code:
[DllImport("coredll")]
static extern bool SetCursorPos(int X, int Y);
[DllImport("coredll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, int dwExtraInfo);
[Flags]
public enum MouseEventFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
bool tempVal = SetCursorPos(x, y);
mouse_event((uint)MouseEventFlags.LEFTDOWN, 0, 0, 0, 0);
mouse_event((uint)MouseEventFlags.LEFTUP, 0, 0, 0, 0);
however I have one outstanding problem (that I know of!).
I am struggling to capture the corrext X and Y co-ordinates of the control. I have tried many different methods and they all return values of 0 for both axis.
How do you guys do it?
Many Thanks
I haven't answered till now since I don't know .NET
But since you found your way to using native APIs I think I can help you.
This is how I would do it in C/C++:
Code:
RECT wndRect; //this is a structure that contains window top, left, bottom and right coordinates.
GetWindowRect(FindWind(L"[I]window class[/I]", L"[I]window name[/I]"), &wndRect);
x = wndRect.left + 1; //add 1 to window position to make sure the click is inside
y = wndRect.top + 1;
GetWindowRect returns the window position in screen coordinates for top left and bottom right corners.
For FindWindow to work you need to know the class and name of the window you want to find. You can find them using a utility called SPY++ which comes with any Microsoft C++ compiler.
The class is window type (so it will probably be something like 'PictureBox' or 'Image') and window name is most likely blank.
Hope this helps.
Thanks fo your help
I don't know if your code would have done the trick or not but I managed to work my way through the different class definitions to find what i needed.
Code:
int x = selectedButton.PointToScreen(selectedButton.Bounds.Location).X + 2;
int y = selectedButton.PointToScreen(selectedButton.Bounds.Location).Y + 2;
This still doesn't work but I really don't have time to work out why, all I know is that the call to SetCursorPos() returns false. I know the mouse_event code works because if I position the cursor over the Start button it opens the menu.
Time is of an essence so for now I'm just going to have to drop this and pray that I have time to finish this when towards the end of my project.
/me thinks writing my first Mobile 5.0 application in 4 days (I'm a web application developer by trade) was bad planning on the management's fault anyway
Thanks for your input though
use this method signature:
[DllImport("coredll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
int instead of uint or long.
For me it worked.

Capturing the Talk Key?

I am pasting this from my post on the MS .NETCF Dev Forums:
========================8< snip ======================
Hello.
I cannot seem to find a complete example for this, and as I am new to using unmanaged code, I need a COMPLETE one. If there is a MANAGED way to do this - great, but I certainly can't find one. Even the SHCMBM_OVERRIDEKEY constant was buried so far into the docs I needed spelunking equipment to find it.
I am writing a Dialer App, mostly as proof of concept. I cannot figure out how to capture the Talk key in order to actually place the call.
I pulled some constants from winuser.h and aygshell.h:
Code:
public const int WM_USER = 0x0400;
public const int SHCMBM_OVERRIDEKEY = (WM_USER + 403);
public const int VK_F3 = 0x72;
public const int VK_TTALK = VK_F3;
public const int SHMBOF_NODEFAULT = 0x00000001; // do not do default handling of this key
public const int SHMBOF_NOTIFY = 0x00000002; // send us the WM_* messages for this key
and imported some funcs from coredll and aygshell:
Code:
[DllImport("coredll.dll", SetLastError=true)]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("aygshell", SetLastError=true)]
public static extern IntPtr SHFindMenuBar(IntPtr hwnd);
And then I have this call in InitializeComponent():
Code:
SendMessage((IntPtr)SHFindMenuBar(this.Handle), SHCMBM_OVERRIDEKEY, VK_TTALK, SHMBOF_NODEFAULT | SHMBOF_NOTIFY);
Now I have a couple of questions.
A - is there anything wrong with all of that?
and B - in other examples, I see people somehow getting ahold of a .Message property of the Form, but I don't seem to have System.Windows.Forms.Message anywhere... Not in the namespace or off the window object itself. I assume it's being passed back in some sort of event related to the form, but what?
I hope these aren't total n00b questions, but as I asid I am really unfamiliar with how unmanaged code really works, and have never done any Win32 C++ only vanilla C++ under unix.
Thanks in advance.
====================>8 unsnip =================
In case that's not clear: I want to make the green talk button NOT launch the phone's default dialer, but instead initiate the call from within MY dialer. The actual hardware button, not a softkey and not an on-screen button.
I am using an HTC Mogul.
.NETCF 2.0
VS2005
C#
and a partridge in a pear tree!
Any help would be MUCH appreciated. I am a little surprised that there doesn't seem to be an obvious way of doing it. Seems like a rather glaring omission to the managed SDK. Perhaps there is a way and I have just missed it.
Thanks!
Bump for the daytime crew.

[Q] Converting an editable text box to be used as a integer

I have a edit able text box that is next to a button lets say onclick of the button it will take the text from the editable text box and converted to an integer so it can be subracted by.. oh lets say 32 so my first method was this.
number = EText.getText();
textOutcome.setText(number - 32);
this gave me an error of
The operator - is undefined for the argument type(s) Editable, int
my second attempt was
textOutcome.setText((Integer.parseInt(EText.getText().toString()) - 32));
This gave me no errors and it ran fine untill i clicked the button which causes the command above to run. and it force closed my application.
Does any one have any ideas i can do, or any sample code?
Thanks,
Blue.
Bumping the topic.
Please can anyone help me with this..!
Have you tried stepping through the code with the debugger? Otherwise, the code you posted looks fine. There is probably something else at play here.
Basicall it force closes whea subtraction is made..
Sent from my Samsung Epic 4g.
Ahh the debugger found something early in the code thanks for the idea !
You have to do any mathematical arithmetic with the integer type, not with string, So when you extract the number from the EditText you have to convert the string into a integer, or whatever type of number it may be.
WHen you have the integer then you can apply the arithmetic.
when this is done you then have to convert the total into a string type again.
Hope this helps.
So basically im going to write a little of code off the top of my head which obviously can have errors. Since im not testing it.
EditText txbox = (EditText)findViewById(R.id.EditText01);
String txdata = txbox.getText().toString();
int txval = Integer.ParseInt(txdata);
//you should make sure that the value is an integer, if not i think //Integer.ParseInt() will just cut off the rest if its irrational.
//Now you can do arithmetic//
int b = 2;
int t;
t = txval - b;
//The total value is in variable t.
So lets convert this into a string and put it back into an object like edittext or textview
txbox.setText(String.valueOf(t));
//Thats all, hope that helps.

Send array to javascript

Hey guys,
I would like to get some information from a content provider and send it to my javascript code with by using the addJavascriptInterface method. From javascript, I can call a java function that returns a string and I have no issues. However, if I try to call a function that returns an array of strings, the function doesn't even get called. I could understand if the data just didn't get passed properly but android seems to be doing something to stop this function from even being called. Does anyone know how I can do this?
here's my current, broken code(with most of it cut out for obvious reasons)
Code:
webView.addJavascriptInterface(new Object(){
public String[] getSources(){
Log.i(TAG,"getting sources"); //this log never shows up
Cursor c = managedQuery //you get the idea
sources = new String[c.getCount()]
//fill up the array
return sources;
}
}, "Android");
then in javascript, I have
Code:
var sources = Android.getSources() //this ends up being null
Any help would be greatly appreciated
Thanks,
Sam
so.. probably not possible?

[Q] Problem with strings

Last day I opened my Strings.xml file with Notepad and added all my needed texts to it. Today I opened eclipse and wrote this code :
TextView title1 = (TextView) findViewById (R.string.myAddedTextName);
But the problem is that eclipse cannot find that. I restarted it but it didn't work again. I checked the strings.xml file by eclipse. Everything was corect but the R.String (gen) hasn't saved them. Now I can't accses the texts. Is there anyway I can access them?
Thanks in advance.
You can clean the project. That way it will build again. Just select Project > Clean... and then select your project.
I may be wrong but I don't think you can do this: TextView title1 = (TextView) findViewById (R.string.myAddedTextName);
TextView is a widget object. You are telling eclipse to find a TextView (by id) but are telling it to look for a stored string.
zalez said:
I may be wrong but I don't think you can do this: TextView title1 = (TextView) findViewById (R.string.myAddedTextName);
TextView is a widget object. You are telling eclipse to find a TextView (by id) but are telling it to look for a stored string.
Click to expand...
Click to collapse
That is right. Ids are integer values. I did not see that. :laugh:
Change it to
Code:
TextView title1 = (TextView) findViewById (R.id.myAddedId);
You set the id that way:
Code:
<Button ...
android:id="@+id/myAddedId"
... />
This is what I was thinking:
If title1 is indeed a TextView in your layout you would do this:
Code:
TextView title1 = (TextView) findViewById(R.id.myTextView);
title1.setText(R.string.myAddedTextName);
Thank you.
The strings are now OK.
Also the famous TextView = String which I wrote at the first post has no problem in eclipse.
torpedo mohammadi said:
Thank you.
The strings are now OK.
Also the famous TextView = String which I wrote at the first post has no problem in eclipse.
Click to expand...
Click to collapse
This is because R.id......
And R.string........ Point to int data type so compiler won't mind
Not to mention it will crash at runtime
Sent from my GT-S5302 using Tapatalk 2
So, suppose I have written a text in the strings.xml like :
<String name ="text">Hi there.</string>
Using which code can retreive the "Hi there."? I have something in my mind:
String s = getString(R.string.text);
Or
String s = getResources().getString(R.string.text);
Which one can give me the "Hi there."?
The first one. Not sure about the second one, but just use the first
Both of them will do the same job
Sent from my GT-S5302 using Tapatalk 2

Categories

Resources