Android programming: Adding TextView above pane - android

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.

Related

Displaying a TextView immediately after another

I am a first time developer for Android, so you can say I've been learning as I was developing. For most of my code that doesn't have to do with the XML layout, I had no problem patching my rookie mistakes. With that said, my rookie mistakes has caught up to me in regards to two TextViews when I initially designed them with the GUI interface designer (my major rookie mistake).
My display_city tv and display_today_date tv seem to have a symbiotic relationship with each other. Removal of either one would crash the app. They seem so dependent on each other that changing each other's positioning is impossible (at least from the myriad of things I have tried such as setting layout margins).
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip"
android:layout_above="#+id/display_today_date"
android:layout_below="#+id/get_data"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/display_today_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/display_pollen_type"/>
My question is - how do I simply position display_today_date immediately after my display_city? When I first started this Android app, I relied a lot on the GUI builder. That was my first rookie mistake, which resulted in this symbiotic relationship I explained.
Currently this is what my app looks like:
I have tried changing display_today_date's layout to android:layout_below="#+id/display_city. This results in a crash. I checked logcat, but it did not give me relevant information to the reason of the crash within the XML file.
P.S. get_data is my TextEdit box.
You already have the city to show above the date with the line android:layout_above="#+id/display_today_date". You can't have 2 views in a relative layout each reference the other, or it won't be able to figure out what to do. If you don't want to put the city above the date, delete that line then add the code to place it where you want.
You could use a LinearLayout with the orientation set to horizontal. That way there is no reference to another view. So if you delete one the other one won't cause the app to crash.

When and Why an xml layout tag should instantial an object of a class?

I do not know when to let an xml layout tag instantiate an object of a class and why it is sometime essential to do so. I just need explanation or any guide or tutorial please.
Example:
<com.example.camerasurface.CustomCameraView
android:id="#+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"/>
Lets just keep it simple and say that whatever you do in the XML file is HARD CODED.
It wont change throughout the entire application.
So whatever you want hardcoded constant in your site you declare in the XML.
The rest of dynamic stuff can happen in java.
Hope this helps.

List event handlers for an Android control

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,

android changing text view (is this code ok?)

Can some one help me, I have heard alot of things and I dont know what to believe. I am making an app that is a counter. In my xml layout i have a TextView acting as a counter and the text is set by a string in strings.xml and i am controlling what the text view says from my java file. here is some code snip its. all I want to know is this ok?, it works fine but I want to know is it a bad or good way.
"counter" equals a variable.
"display" is referencing the ID of the textview"
what i am using to control the text view.
display.setText(String.valueOf(counter));
here is my text view in my xml layout
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tvDisplay"
android:gravity="center"
android:text="#string/counter"
android:textSize="20dp" />
here is the string in strings.xml
<string name="counter">0</string>
It's fine, that's how you change text dynamically.
Yes, All things are right and good. Yo should have to give the String Value as like that.
If there is a Small Value of TextView then you can directly give as android:text="abcd"
And If you want to give any reference of that then your code is also correct and works as well.
For the Best use of coading your should have to try as like you have done rightnow. as Because it helps you a lot if there are number of TextView and you want to manage or change the Value of it quickly.
Enjoy. :)
Thanks.

Custom listview with two buttons in each row

I have spent literally two days trying to sort this issue. If anyone could help I would be massively appreciative.
What I'm trying to achieve:
Have a ListView, whereby the player can add new entries (players), through a text field (for the player name), and then a submit button. In each field of the ListView, I display the player name, and then two ImageButtons. One with a male symbol, and one with a female symbol. The male symbol is toggled by default, and the user can set the player as being male or female by toggling either the male button or the female button. Finally, once the user moves onto the next screen (a new activity), the application will save the player names and the attached sex to some form of storage and proceed to the next activity.
What I have achieved:
I have a simple array adapter, which upon the player adding a new player name to the list, I run the notifyDataSetChanged() on it. The adapter also is set to use a custom row layout file. Inside the layout file, it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_marginTop="5dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/playerName"
android:layout_centerVertical="true" android:text="Derp" android:textStyle="bold" android:layout_marginLeft="5dp" android:textSize="22dp" android:textColor="#color/male_blue"></TextView>
<ImageButton android:layout_height="60dp"
android:layout_width="60dp" android:onClick="maleClickHandler"
android:src="#drawable/male_icon_settings" android:id="#+id/buttonA" android:layout_alignParentRight="true" android:layout_marginRight="65dp"></ImageButton>
<ImageButton android:onClick="femaleClickHandler"
android:layout_height="60dp" android:layout_width="60dp" android:id="#+id/buttonB"
android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:src="#drawable/female_icon_settings"></ImageButton>
</RelativeLayout>
</LinearLayout>
The two buttons on each row reference to methods in the class file. Here is my code for this:
public void maleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton maleButton = (ImageButton) vwParentRow.getChildAt(1);
maleButton.setImageDrawable(getResources().getDrawable(
R.drawable.male_icon_selected));
vwParentRow.refreshDrawableState();
}
public void femaleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton femaleButton = (ImageButton) vwParentRow.getChildAt(2);
femaleButton.setImageDrawable(getResources().getDrawable(
R.drawable.female_icon_selected));
vwParentRow.refreshDrawableState();
}
I haven't yet implemented any inter-connectivity between these two buttons, to allow only one to be active at a time, or to even untoggle one, since I think I might be taking the wrong approach entirely.
The problem:
Upon adding new entries to the list, AFTER toggling one and/or the other male/female buttons, things get really buggy, and the male/female toggled icon might move as it should, along with the attached player string, or more likely, those toggled will stay on that first row (array position 0 of the list), or even move into the second list position, AND copy themselves as being toggled onto the row above.
How you can help...?
I have attached an image below of my screen, from the emulator, to help illustrate my points
Screenshot!
I think that I might need to use some form of custom adapter; I have done so much reading around on the subject, but I can't find anything relevant to what I am trying to achieve, so if you could point me in the right direction, or even try and put together the most basic solution to this type of problem, I would be very grateful.
Finally, when I get this working, which form of storage would be best for storing player names, and their sex? I would like the user to be able to keep the player list after they quit the application and restarted it.
Thanks for any help! :)
You will need to use a Custom Adapter, which in itself should be able to track the male/female flag for each of it's entries.
Your method will not work since the state of the buttons are managed by the getView method of the adapter. Even if you change them by digging through the children, the next time when the getView method is called, it's going to mess up things.
A lot of this depends on how many players you expect to have in your game. If it's a number that would likely fit on one screen (or very close to it), the ListView is actually unnecessary. ListViews and adapters aren't really a convenience method as much as they are a tool to improve performance. They only keep in memory what is on the screen and recycle old, already-displayed Views for new rows when you scroll--this is why some of your button states are being copied to different rows.
There are a couple of ways you could fix this:
You could write a custom adapter yourself as Kumar Bibek suggests. In this adapter, you would want to override the getView() method to make sure each button has the correct state each time the method is called.
You could also simply use a ScrollView populated with a few of your rows manually if you don't have enough data to warrant using a ListView. This way you wouldn't need to worry about your rows being recycled and button states being out of wack.
In addition, you might want to look into using a RadioGroup for the gender selector (I can't think of a much better use for radio buttons since they are made to be mutually exclusive).
Also, the outer LinearLayout in your row XML file looks unnecessary.
As far as storage, you could either use an SQLite database or SharedPreferences. SharedPreferences requires no setup, but I feel like an SQLite database is more suited to your needs.

Categories

Resources