Accessing a TextView from a resource file - android

Let me first explain what my Androïd application is made of :
- a class extended from an Activity. In the OnCreate member of my class, I try to access a TextView described in my main.xml file by using
"MyTextView=(TextView)findViewById(R.id.myTextView);".
- an xml file where the TextView is described as follows :
<TextView
android:name="#+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/another TextView"/>
In the R.java file, I can see that my TextView is registered.
My problem is that, when I try to get a handle on the TextView with the findViewById function, I get a null pointer.
It seems a mystery to me because I wrote another application where I was able to access TextViews. And I can't see any difference beetween both applications!!!
Hello,
Here is my complete layout file :
<TextView
android:name="#+id/Titre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Application GPS :"/>
<TextView
android:name="#+id/NombreMaxSatellites"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:name="#+id/NombreSatellites"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Android from NetBeans"/>
<TextView
android:name="#+id/TempsAcquisition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Android from NetBeans"/>
<EditText
android:name="#+id/Texte"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Le texte"/>
<Button android:id="#+id/BoutonTexte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dip"
android:layout_marginTop="10dip"
android:text="Terminer"/>
I can access the Button but neither the TextView nor the EditText.
Any idea?

I am sure there are some errors in the layout, due to which your changes for layout were not saved. And then it is not reflecting in R.Java. Look for any red marks in your layout. Resolve the errors before your proceed, then it should work fine. Post your complete layout file.

Make sure that any calls to findViewById() occur after setting the layout with setContentView().
A good practice is to call setContentView() as the first line in your onCreate() method.

Make sure about 2 things
the ID of your textview is not repeated in the same layout
make sure that you are setting the content view (setContentView()) to the correct layout.

The problem is id is assigned to name android:name="#+id/Titre", it should be android:id="#+id/Titre".

Related

What is right and less error-prone approach that the community follows for the use of IDs in Android's Activity xml

<RelativeLayout ..>
...
...
<Space
android:id="#+id/space4"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/numberOfPersons" />
<TextView
android:id="#+id/txtCostPerPerson"
android:labelFor="#+id/costPerPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/space4"
android:layout_alignBottom="#+id/costPerPerson"
android:layout_alignTop="#+id/costPerPerson"
android:gravity="center_vertical"
android:text="#string/costPerPerson"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#id/costPerPerson"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/txtCostPerPerson"
android:layout_below="#+id/space4"
android:inputType="number" />
</RelativeLayout>
The above xml code snippet has TextView having attribute labelFor="#+id/costPerPerson" whose value is the id of the EditText that follows below.
I'm confused on the convention of weather I should use the way it is now, or should the #+id be used in EditText and it is referenced other way here, if so what is it? What is right and less error-prone approach that the community follows? Thanks in advance!
If you are using an IDE and its graphical layout editor, you're probably letting it handle these things.
In cases where you are doing this work more by hand, the long-standing guidance has been to put the + on the first occurrence of the ID, top-down, in the layout file. In your case, costPerPerson appears first in android:labelFor of the txtCostPerPerson TextView, and so your code is following this convention.

Error when relate 2 widget in relativelayout

Im trying to relate widgets , I write the code as bellow:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#drawable/ad1"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/ad1"
android:orientation="horizontal"
android:layout_toStartOf="#id/bt_menu" > // here there is error !!!!
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="Menu "
android:inputType="textVisiblePassword"/>
</RelativeLayout>
<ImageButton
android:id="#+id/bt_menu"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/menu5"
android:onClick="menu_onclick"/>
</RelativeLayout>
I have an error in this line:
android:layout_toStartOf="#id/bt_menu"
The error is : no resource found that matches the given name (at 'layout_toStartOf' with value '#id/bt_menu')
Can anyone help me please !!!!
Thank you in advance .. Fadel.
Change it to
android:layout_toStartOf="#+id/bt_menu" >
Add the "+"
If it is before the layout you are referencing then Eclipse doesn't seem to know about it until it has been saved. You should be able to change it back to how you have it after you have run it once. But I don't know that it will hurt anything to leave it. I do this if I use a property such as layout_below but put it before the View I want it to be relative to or something similar but I just leave it.
There may be a better way to handle this witin Eclipse, or whatever editor you are using, but AFAIK, this is the simplest and I don't know of any undesirable effects from it.
In my opition, is not the best way to use "#+id/" twice. You can receive errors of your R.java file. The problem you got is, that you use the id before you set it.
Solution:
Define id first by
android:layout_toStartOf="#+id/bt_menu"
Use id
android:id="#id/bt_menu"

Android TextView doesn't do linebreak in ListView (multiline text)

I have created a 3-level ExpandableListView and have the problem that the TextViews which are used for the 2nd and 3rd level do not support line-breaks if the content is too long. It should be dynamically over more than one line, if needed. The 1st level TextView does it well (automatically) and I actually had the same settings in the xml for all three TextViews. Followed are the layout xmls, the one TextView with the id groupname is for the 2nd level (e.g. the first red X in the picture below) and the one with id childname is for the 3rd level (e.g. the second and third red X in the picture below). It should all be like at the green hook in the picture.
"singleLine=false" seems not to work. Also tried some different options found in other SO posts, but what I've testet haven't worked for me. Like ellipsize, scroll horizontale, different layout_width and so on. The only thing worked is to set a fixed layout_width on x hundred dp, but this is not dynamically, I'm right?
Would be great if anybody could help me with this. Lot of thanks!
Here's a screenshot:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/childname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"
android:textColor="#AAAAAA"
android:singleLine="false"
android:text=""
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginRight="60dp"
android:textColor="#555555"
android:singleLine="false"
android:gravity="center_vertical"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
Add this line in your xml
android:inputType="textMultiLine"
or
Add text using coding like this, where you can add line break using '\n'(But here you have to manually add breaks where you want them)
TextView txt1 = (TextView) findViewById(R.id.childname);
txt1.setText("Hi \nHello \nHow are You");
Results will be
Hi
Hello
How are You
Edit
Accepted Answer - removing the line 'android:layout_alignParentLeft="true"
try using LinearLayout instead of RelativeLayout as parent for the TextView
I had add this attribute to my TextView inside ListView, and makes it do line break correct.
android:maxWidth="xxxdp"
F.Y.R.

Android bug with buttons and editText

I have some android code (textView, editText, button) and I add all strings in string.xml. I call these data in this way:
Button vibro;
setVibro((Button) findViewById(R.id.vibro));
vibro.setText(getString(R.string.vibro_button));
vibro = (Button)findViewById(R.id.vibro);
and also I create set and get method for it. This is one part of my main.xml:
<EditText
android:id="#+id/editme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/username"
/>
<Button
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/bfirst"
/>
and this is one part of my string.xml:
<string name="bfirst">Register</string>
The problem is that I see the buttons and edit text and everything in all my pages. I am sure that I call them in a correct way. But what can cause a problem?
Is setVibro a method?
Also, try to set this line:
vibro = (Button)findViewById(R.id.vibro);
before this line:
vibro.setText(getString(R.string.vibro_button));

LinearLayout in App Widget

I am working with modified version of sample WeatherListWidget to get a better understanding of App Widgets. Things are fine - except when I try to replace the dark_widget_item and light_widget_item layout files with slightly more complex layout files. Here is original layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_item"
android:layout_width="match_parent"
android:layout_height="46dp"
android:paddingLeft="25dp"
android:gravity="center_vertical"
android:background="#drawable/item_bg_light"
android:textColor="#e5e5e1"
android:textSize="24sp" />
I would like to be able to have multiple text lines. But:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_item"
android:background="#drawable/item_bg"
android:layout_width="match_parent"
android:layout_height="46dp"
android:paddingLeft="25dp">
<TextView android:id="#+id/type_string"
android:textColor="#666666"
android:textSize="20sp" />
<TextView android:id="#+id/title_string"
android:textColor="#666666"
android:textSize="18sp" />
</LinearLayout>
fails.
In fact, it results in "Sorry! The application Launcher (process com.android.launcher) has stopped unexpectedly. Please try again. Force close".
Reinstating TextView widget_item.xml fixes this. I suspect that part of the problem is how I reference RemoteViews in WeatherWidgetService.getViewAt() - but I am getting very little help from DDMS or LogCat or anything else.
Thanks guys, I got the notification. (SO requires a username with length > 2, hence the dot)
Answer as per comment:
I don't see any layout_width and layout_height attributes for both of the TextViews in your LinearLayout - they are mandatory. Also, if you want the two TextViews to be above eachother, add android:orientation="vertical" to the LinearLayout. And just to the record, you can break a CharSequence to multiple lines in a single TextView by adding "\n" inbetween the different elements.
If you're going to include an image as well, then you're probably better off with a LinearLayout than a single TextView indeed, although you could potentially use the intrinsic drawable option of the latter. That could get a little messy though, especially if you're planning on using different styles for the different lines of text... Not impossible, but I'd stick with the LinearLayout. ;)

Categories

Resources