Android Beginner: How can I build a dynamic Hello World app? - android

First things, first, I'm just starting with Android Application Development and would like to make a basic starter app. I have a Hello World app which will display the text Hello World when a button is pressed, but I would like to know how I can include a text box for a name, so the user can enter their name and then have the app say hello to them when they press the button.
Any help would be appriciated because I haven't a clue if I'm honest, I'm still figuring out what most of the code does but creating simple apps like this help me further my understanding.
Thank you,
Max

For the text box you'll probably want to use a EditText. Make sure to insert it into your layout xml and give it a name you can reference, like nameTextBox or something. It should look something like
<EditText android:layout_height="wrap_content" android:id="#+id/nameTextBox" android:layout_width="match_parent"></EditText>
Then when your activity runs, make sure to put it into a variable like you probably did your HelloButton. So
setContentView(R.layout.layout);
nameBox = (EditText) findViewById(R.id.nameTextBox);
Then when the user clicks the button go ahead and grab whatever is in the nameBox. If you're using a ClickListener for your button this would go inside the OnClick method, just before you print out the text.
String name = nameBox.getText().toString(); // Inherits toString from CharSequence
// code to display concatenate and display your hello string goes here

I advise you to look at the samples provided by google on the android developer page.
http://developer.android.com/resources/browser.html?tag=sample
Look how things are done there and use that to make your own

Related

How to modify a text view in app on user request

I'm trying to make a app with some nice features. One of them is :
I want to modify the text that you see in my app on user request. By this I mean if you see the MENU name in my app and want to display it as MAIN MENU or MY MENU etc you press a button, open an edit interface, write the name,click ok and that's it, text changed.
Here is little part of my aplication, it's shows up Slide to change the brightness, but user want to change it to “Brighness changer” . Any ideeas how to do that ?
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Slide to change the brightness"
android:id="#+id/textView"
I know that all my names are saved in a strings.xml file , problems is that i don't want to recompile my app everytime user will make a change..that's the main problem. I need a way to do it to avoid this .
Thanks in advance !
You store the string they want to use in a shared preference. When you display this layout, read the value from the shared preference and call setText on the TextView to set the text. Use the existing string as the default, of course.

Create a textfield which only supports copy-option

Is there a way to make a textfield (EditText, TextView) on Android which displays copyable text (no edit options)? Something like Swing's JTextField().setEditable(false) on Windows?
Edit: It seems that my question was a little bit unclear, so I'm going to give it some real-world context. I am working on a checksum calculator which lets you pick a file and prints the results of the calculation into 4 different textfields. I want the user to be able to copy those hashes from the textfield, but to disable all edit options.
I believe what you are asking is very straightforward and simple, if I am reading your question wrong, please correct me!
Are the four text fields that hold the results required to be EditText 's?
otherwise - >
Question:
How to make TextView only copyable, not editable
Answer:
Well using a TextView solves the not editable part
and if you take a look
at my android studio. I have a simple TextView with an attribute of
textIsSelectable="true"
allowing users to copy and paste!
Hope this helped!
-Stephen

Problems with layout

I am trying to create my own Phone Dialer from scratch in Eclipse Android, I did a simple phone layout in the XML with numbers 0-9 and some Buttons/ImageButtons.
Basically I put some Buttons and ImageButtons and I understand that I need call setOnClickListener() on them in the Java code. Something like:
Button no1 = (Button) findViewById(R.id.button1);
no1.setOnClickListener(new View.OnClickListener()
I did an <EditText on top of the numbers (Buttons and ImageButtons) to display the numbers. But nothing come out when I click. I am not sure what I miss out which cause this problem.
I also did a "settings" button in the same XML file, which upon clicking on the button, a toast will pop out which have a couple of clickable icons. I am not too sure on how to do it. I did read some tutorials but mine seems to be not quite right.
hopefully if the above-mentioned points can be rectified, I hope I can try to make the SMS characters to be Unicode, which can limit to lets say 50 characters etc.. but that's still far-fetched for me though. I'll do this in the later part.
I did a Google research but I don't quite get the correct info as its just bits and pieces here and there...
Actually I want to create this customized phone dialer as I intent to install it in my Mum's 2.1 Froyo phone.
Can anyone advise me please? Is there a sample code where I can check what I went wrong?
Have your class implement OnClickListener and make a switch(arg0.getId()) and a case for each number pressed.
Every time you press a number have an EditText receive that number and then when you press the call button have it call the numbers in that EditText.
In that amount of time, the application may still have some flaws in it. But you are on the right track. I would have your class xxx.java extend View.OnClickListener, then you can add your class as the listener for each button, e.g. no1.setOnClickListener(this);. Use a switch statement to figure out the phone number and make the call.

popup windows in android?

Pretty new to android so excuse me if this is a really obvious question.
Say my application has a bunch of TextViews, each one showing the attributes of a certain product (name, price, etc). I have a button next to each of these TextViews labeled "modify".
How do I make it so that when I press the modify button next to a certain attribute, a popup window with a space to enter text into comes up so that the user can enter text into this box and then have the actual attribute listing on the original page change? Actually I just need a push in the right direction with creating this popup text field... not sure if there is already some built in functionality for this or if not, what would be the best way to create this kind of thing.
Thanks.
Why not have the modify button set TextEdit.setEnabled(true); and then change focus with TextEdit.setFocus? Note that both of these are inherited from view
If you really want a dialog you might want to looking into the AlertDialog.Builder. I know you can use it with buttons and radio buttons, but I'm not sure you can get it to work with a TextView.
Use a code like this for the input popup: Android dialog input text
In the positive button handler, set your edittext content programmatically like this:
myEditText.setText(value).
As simple as that. The only difference with a standard GUI framework is that you don't retrieve the value as a result of the popup function. Instead, you must provide an action handler.

highlight the word on longpress android

Hello
In my android application i am using a webview to display apage of contents .
What i require is like if the user longpress on a word then that particular word should be highlighted and should be highlighted even if the user reloads the app again.
Is there any way that i can get this done in android.
Please forward your valuable suggestions.
Thanks in advance:)
Putting it in a WebView is what will make this tricky, if it were just a regular view I would say extend TextView and implement onLongClickListener for it as well. Not sure how to squeeze this into an existing webview though :/

Categories

Resources