findviewbyid returns null (Textview) - android

when i try to set the text of an textview i get a nullpointerexception. somehow the findviewbyid method return null but i dont know why.
this is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/rl_ros_layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#+id/tv_ros_view1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ProgressBar
android:id="#+id/pb_ros_bar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:visibility="invisible" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rl_ros_layout1" >
<TableLayout
android:id="#+id/tl_rostertable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="2" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="#string/team" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="#string/pos" />
<TextView
android:layout_height="wrap_content"
android:padding="3dp"
android:text="#string/name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="3dp"
android:text="#string/score" />
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
and my oncreate method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.roster);
final Bundle extra = getIntent().getExtras();
position = extra.getInt("pos");
int id = de.damps.fantasy.HomeActivity.ID[position];
url = de.damps.fantasy.HomeActivity.URL + "/roster/2011/" + id;
tbl = (TableLayout) findViewById(R.id.tl_rostertable);
team = (TextView) findViewById(R.id.tv_ros_view1);
String s= de.damps.fantasy.HomeActivity.TEAMS[position];
team.setText(s);
new GetRoster().execute(url);
}
the tablelayout is found without any problmes. but the textview is null.
i already cleaned the project but it didnt solve the problem
would be nice if someone can help me

You have a typo:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="#+id/tv_ros_view1"
android:textAppearance="?android:attr/textAppearanceMedium" />
(android:text instead of android:id)

You wrote
android:text="#+id/tv_ros_view1"
instead of
android:id

you create ID your text view like this android:id="#+id/tv_ros_view1" in place of android:text="#+id/tv_ros_view1" this is use full to you.

In you xml layout:
android:text="#+id/tv_ros_view1"
should be:
android:id="#+id/tv_ros_view1"
the text attribute is used to tell textview what text to show.

In your faulty/first text view you have written like this:
android:text="#+id/tv_ros_view1"
Just replace this with this:
android:id="#+id/tv_ros_view1"
This is how you have to assign id to a view(TextView).

Related

Android TextView inside RadioGroup Not Exactly Fitting

I do try to get text datas from JSON, i do. But when it is time to write they are not exactly fitting. My XML file is here...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_questionAdjective"
android:layout_width="fill_parent"
android:layout_height="139dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="15dp" />
<RadioGroup
android:id="#+id/grp_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rdb_optionA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/rdb_optionB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionB"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/rdb_optionC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/rdb_optionD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionD"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
And what it looks like after that.
How can i set C and D options next to radiobuttons ? Thanks in advance.
EDIT:SOLVED I deleted the textView parts in XML and used the for loop which #Gabriella Angelova provided me. That really helped. Thanks.
You could use the following code snipet to set the text of your radio buttons dynamically:
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.grp_options);
for (int i = 0; i < radioGroup .getChildCount(); i++) {
((RadioButton) radioGroup.getChildAt(i)).setText(*your value from JSON*);
}
radioButton.setText("your text");

How to show text with central align

how to show textview like this image?
my textview will look like this image
below is my code, it's not showing text centrally:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/imagelogo2"
android:orientation="horizontal" >
<ImageView
android:id="#+id/test_button_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="20dp"
android:src="#drawable/back" >
</ImageView>
<ImageView
android:id="#+id/test_button_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="5dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:src="#drawable/options1" />
</RelativeLayout>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="If your school is not in the list.Please check" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="back later or contact your district&apos;s Nutrition Services" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="department for more information" />
</LinearLayout>
The problem is all your 3 TextViews are different widths, so "center" is different for each of them.
Change them all to:
android:layout_width="match_parent"
You just need to replace
android:layout_width = "wrap_content"
with
android:layout_width="fill_parent"
for all the TextView. Try the layout below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/imagelogo2"
android:orientation="horizontal" >
<ImageView
android:id="#+id/test_button_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="20dp"
android:src="#drawable/back" >
</ImageView>
<ImageView
android:id="#+id/test_button_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="5dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:src="#drawable/options1" />
</RelativeLayout>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="If your school is not in the list.Please check" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="back later or contact your district&apos;s Nutrition Services" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="department for more information" />
Try this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="italic"
android:layout_margin="5dp"
android:text="If your school is not in the list.Please check"
/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="5dp"
android:textStyle="italic"
android:text="back later or contact your district's Nutrition Services"
/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="5dp"
android:textStyle="italic"
android:text="department for more information"
/>
Instead of using 3 textviews you should use one textview.
and the text that you want to show, had to convert it to HTML.
String code = "<p><b>First, </b><br/>" +
"Please press the <img src ='addbutton.png'> button beside the to insert a new event.</p>" +
"<p><b>Second,</b><br/>" +
"Please insert the details of the event.</p>"
"<p>The icon of the is show the level of the event.<br/>" +
"eg: <img src = 'tu1.png' > is easier to do.</p></td>";
message = (TextView) findViewById (R.id.message);
Spanned spanned = Html.fromHtml(code, this, null);
message.setText(spanned);
message.setTextSize(16);
and make textview gravity to center.
For Times New Roman Fonts you need to do this:
create one folder "fonts" in assets folder
copy times new roman.ttf file in "fonts" folder. (times fonts file
should be in small caps)
then add this class to your activity class:
public class Typefaces{
private final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public Typeface get(Context c, String name){
synchronized(cache){
if(!cache.containsKey(name)){
Typeface t = Typeface.createFromAsset(
c.getAssets(),
String.format("fonts/%s.ttf", name)
);
cache.put(name, t);
}
return cache.get(name);
}
}
}
then try this code:
Typefaces tf = new Typefaces();
tvMessage.setTypeface(tf.get(context, "timesnewroman"));
Hope it Helps!!

Android layout positioning. Two rows text

I've got some problem with positioning TextViews. Currently my layout looks like:
Text1: Text2
Text3: Text4
Ok, but when Text2 has 2 rows length or more I've got:
Text1: Text2:1
Text3: Text2:2/Text4
I don't know how to set up my layout to obtain view like:
Text1: Text2:1
Text2:2
Text3: Text4
Can somebody help me solve that problem?
XML code:
(...)
//Text1:
<TextView
android:id="#+id/tv_author_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_genre_label"
android:layout_toRightOf="#+id/img_Cover"
android:text="Author: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
//Text2:
<TextView
android:id="#+id/tv_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tv_author_label"
android:layout_alignBottom="#+id/tv_author_label"
android:layout_toRightOf="#+id/tv_author_label"
android:text="Anonymous Anonymous"
android:textAppearance="?android:attr/textAppearanceMedium" />
//Text3:
<TextView
android:id="#+id/tv_availability_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_author_label"
android:layout_toRightOf="#+id/img_Cover"
android:
android:textStyle="bold"
android:text="Availability: "
android:textAppearance="?android:attr/textAppearanceMedium" />
//Text4:
<TextView
android:id="#+id/tv_availability"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tv_availability_label"
android:layout_alignBottom="#+id/tv_availability_label"
android:layout_toRightOf="#+id/tv_availability_label"
android:layout_alignParentLeft="#+id/tv_availability_label"
android:layout_below="#+id/tv_author"
android:text="b/d"
android:textAppearance="?android:attr/textAppearanceMedium" />
(...)
you can reconstruct your code something like this,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dfsdjfkd dsfsdf sdfsdf sdfsdfsd fjlsdjf sdj; j; jlksdfjl sdjflkdsjflsdk jkldsjf lsjldkfj sdl" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
I hope it will be helpful !!
You can encapsulate the row of text views inside a linear layout with orientation - horiontal

Auto Scroll Text In TextView

I have textview with specific height and i have so many sentences within that textview. I have implemented scrollbar within that textview to see whole content. But i want to implement auto scroll and i am not getting the way to implement this auto scroll.
Here is my xml Layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1"
>
<TextView
android:id="#+id/lyricView"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:width="200dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:gravity="center"
android:maxLines="16"
android:scrollbars = "vertical"
android:paddingTop="8dp"
android:paddingBottom="5dp"
android:layout_marginTop="67dp"
android:background="#80000000"
/>
</LinearLayout>
Please help me to solve my problem. Thanx.
I did something like this
android:text=" Please take your belongings after check out."
android:singleLine="true"
android:textColor="#000000"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:layout_marginTop="150dp"
android:scrollHorizontally="true"
android:id="#+id/TextView03"
android:padding="5dip"
android:textSize="55dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I think the 'marquee' funda is missing........
an example :
public class TextViewMarquee extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.tv);
tv.setSelected(true); // Set focus to the textview
}
}
in the XML :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/mywidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:lines="1"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:textColor="#ff4500"
android:text="this is a very long piece of text that will move" />
</RelativeLayout>
This is working for me as you needed.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/lyricView"
android:layout_width="200dp"
android:layout_height="250dp"
android:layout_marginTop="67dp"
android:background="#80000000"
android:scrollbars="vertical"
android:text="#string/extra"
android:textColor="#ffffff"
android:textStyle="bold" />
</ScrollView>
</LinearLayout>
If you want to hide scroll then use this android:scrollbars="none".

Popup message like Dialog

I'm actually coding an app for Android and I need some help to do something.
I need to do a kind of popup message with this image: http://img716.imageshack.us/img716/9331/postitz.png. This image must have 2 TextView, one of them is a title and the other one is the message.
I have tried some options like Dialog with its own layout, own style, but the image fills all the screen instead of wrap the content of the TextViews. I have also tried to do it with a new Activity and occurs the same, the Image fills all the screen.
My actual layout is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="#android:color/white">
<LinearLayout
android:id="#+id/img_postit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/postit"
android:orientation="vertical">
<TextView
android:id="#+id/note_titulo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="30dp"/>
<TextView
android:id="#+id/note_descr"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="30dp"/>
</LinearLayout>
</LinearLayout>
But I have tried with a lot of different combinations, RelativeLayout, FrameLayout, and I cannot find the solution to do this.
Any idea of how to resolve it?.
Thank's for all.
you said you used as a new activity. Did you use android:theme="#android:style/Theme.Dialog in your manifest file?
And be sure that the image is little bit smaller one such that it will be suitable to act as a pop up..
If you used a dialog what exactly is the problem? Didnt you get the dialog as a pop up ?
(i think the image is larger so that it might fill the entire screen)
Create your own class extending dialog.And use layout like following. I use this in one of my apps and the dialog window is wrapped around the buttons :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/ll2"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_below="#+id/ll1"
android:gravity="left"
android:orientation="horizontal"
android:paddingBottom="5dp" >
<RadioButton
android:id="#+id/start"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/stop"
android:background="#drawable/start_on"
android:button="#android:color/transparent"
android:checked="true" />
<RadioButton
android:id="#+id/stop"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/stop_off"
android:button="#android:color/transparent" />
<RadioButton
android:id="#+id/restart"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/stop"
android:background="#drawable/restart_on"
android:button="#android:color/transparent" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/lltext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/detail_credit"
android:layout_below="#+id/ll2"
android:layout_marginBottom="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/startText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/stopText"
android:text="#string/start_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/stopText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:text="#string/stop_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/restartText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/stopText"
android:text="#string/restart_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="#+id/lltext"
android:layout_centerHorizontal="true"
android:text="OK" />
</RelativeLayout>
final Dialog dialog_my = new Dialog(FIRST.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog_my.setContentView(R.layout.about_us); //<PUT YOUR LAYOUT ID HERE>
TextView date = (TextView) dialog_my.findViewById(R.id.date);
TextView thought = (TextView) dialog_my.findViewById(R.id.thought);
TextView ok = (TextView) dialog_my.findViewById(R.id.ok);
TextView link = (TextView) dialog_my.findViewById(R.id.link);
TextView contact = (TextView) dialog_my.findViewById(R.id.contact);
WindowManager.LayoutParams lp = dialog_my.getWindow().getAttributes();
lp.dimAmount = 0.0f;
dialog_my.getWindow().setAttributes(lp);
dialog_my.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog_my.show();

Categories

Resources