hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
12/14/13 07:48 AM (11 years ago)

hard coding text in layout screen

Good morning, I was wondering if anyone can help me learn how to hardcode text in a layout screen. When I add a text field, it remains to be edible in my app. Also I am getting a error that it "should be an @string resource", but not sure how to do it. If possible to learn with the graphical layout would be great. Thanks in advance, Howard
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
12/14/13 08:19 AM (11 years ago)
I hate Android Layouts. A Text field will always be editable; it's a text field. If you don't want them to edit it, then you either disable it in code, or use a 'Label' instead. If you provide text, it wants you to add it to the 'strings.xml' file, and reference it from there. That 'can' be done, but if they're warnings and not errors, you can ignore them. The proper format for a 'hard coded' string in Android would be something like: android:text="Email" And if you want to avoid those errors altogether, remove it from the xml and do it in code. Cheers! -- Smug
 
hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
like
12/14/13 09:06 AM (11 years ago)
Hi Smug, thanks for the reply. For your first suggestions, either disable it or use a label how would I go about disabling it? Also, I thought I would be able to add a label like a text field in the graphical view but I was not able to see how I would do that. Thanks again, Howard
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
12/14/13 11:19 AM (11 years ago)
@hashner13, Code it in the xml then declare it in the corresponding java class. Make sense? LA
 
hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
like
12/14/13 11:32 AM (11 years ago)
@LA....Sadly no. I know basically no code. Sorry, H-
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
12/14/13 11:40 AM (11 years ago)
Something like this in your xml: <TextView android:id="@+id/myText" </TextView> Then on that same screen for java: Before onCreate do this: TextView myText; The after onCreate do this: //get references to TextView myText = (TextView)thisScreensView.findViewById(R.id.myText); Any better? LA
 
hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
like
12/14/13 11:44 AM (11 years ago)
A little better! And in no way would I have know to code that! :-) thanks, but I will have to try it tomorrow and will keep you posted. Just looking at it, where would I input the text that I want it to show? Thanks, H-
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
12/14/13 11:51 AM (11 years ago)
That would depend if you are connecting the text with a control panel then you would you a string but if you are just coding then I use the interface builder on the xml for that but to hard code it: <TextView android:text="Put your text here:" </TextView> Then give it an id like about in either textview or EditText in java LA
 
hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
like
12/15/13 06:18 PM (11 years ago)
Okay, so with @LA and @smug instructions along with a little searching on the web, this is what I did on the xml file and seem to work for the most part. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/qrButton" android:text="@string/Cr_text" android:textSize="30sp" /> Learned a little in the process such as reference text strings and adjusting size. Small and simple but a large step for me! :-) The only thing I can't do is to center the text. I thought I saw it prior but can't now. Any suggestions. Thanks again, Howard
 
hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
like
12/15/13 06:23 PM (11 years ago)
I thought I could add: android:layout_centerHorizontal="true" but it is not centering it.
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
12/16/13 04:35 PM (11 years ago)
Awesome @hashner! You are getting it! LA
 
hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
like
12/16/13 07:30 PM (11 years ago)
Thanks, but I still get it centered.
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
12/16/13 07:47 PM (11 years ago)
You got it centered? LA
 
hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
like
12/16/13 07:49 PM (11 years ago)
Sorry, I just saw my last response made no sense. It has been a long day. No I have not been able to get it centered yet. H-
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
12/16/13 07:52 PM (11 years ago)
This should do it: android:gravity="center" LA
 
hasher13
Android Fan
Profile
Posts: 376
Reg: Jul 21, 2012
Wethersfield
8,060
like
12/16/13 08:19 PM (11 years ago)
Yes, that did. Thank you so much, H-
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
12/16/13 08:32 PM (11 years ago)
Awesome! You are welcome friend! LA
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.