how to change the string values of sting.xml file - android

i hava a layout file like
test.xml
<?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="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/test_string" />
</RelativeLayout>
and java file like this.
test1.java
public class test1 extends Activity {
String temp="This is String"; //which change dynamically
TextView tempview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tempview=(TextView)findViewById(R.id.test_string);
tempview.setText(temp);
setContentView(R.layout.test);
}
}
what i am trying to do is show the value of temp in activity as above code. but it throws an NullPointerException. so how to do. how to set the values of sting.xml???
thanks in advance..

setContentView(R.layout.test); should come before (TextView)findViewById(R.id.test_string);

Related

TextView settext no effect in Activity

Here's the problem with setting text in TextView in Activity. I guess the problem was that I used the same id for each fragment XMLin another activity. I used the same XML code for relative layout, but all worked well in fragments. The problem with TextView is in another activity. Please have a look at my code :
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
}
and xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/action_bar_title_map"
android:layout_width="match_parent"
android:layout_height="#dimen/actionbar_height"
android:background="#null"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/town_name_txt"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:ellipsize="end"
android:textSize="#dimen/actionbar_title2"
android:paddingLeft="#dimen/side_padding"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trade_txt"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:ellipsize="end"
android:textSize="#dimen/actionbar_title2"
android:paddingLeft="#dimen/side_padding"/>
</RelativeLayout>
<org.osmdroid.bonuspack.mapsforge.GenericMapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/action_bar_title_map"/>
</RelativeLayout>
first what I do to fix this: I have change id of TextView. But it hasn't effect.
I tried to delete relative layout in XML, and it deletes from screen. No contact to XML file from Java file.
Sorry for bothering and thanks to all answers. The problem was a dupplicate setcontentView() method:
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
setContentView(R.layout.map_main);
}
p.s espacially Thanks to Sabya
It is weird that you don't get an exception in this code.
Because there is no method settext(). There is method called setText().
Change the method name from settext() to setText()..
Case Sensitive problem with method settext() to setText().
Use your onCreate method from this one:
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
TextView townNameTxt = (TextView)findViewById(R.id.town_name_txt);
TextView tradeTxt = (TextView)findViewById(R.id.trade_txt);
String townName1 = "London";
String trade1 = "buy sum";
townNameTxt.setText(townName1);
tradeTxt.setText(trade1);
}
Clean and Build project and run it again.
Hope, it will help you
If you are running it on Android Lollipop version, Uninstall and re-install the application and check.

Declare EditText as TextView in Android

i have a xml layout main.xml like this
<?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="fill_parent"
android:gravity="center" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text"
android:textColor="#000"
android:textSize="16dp"
android:id="#+id/edtxt"
android:gravity="center|top|left"/>
</RelativeLayout>
and i declare this in my activity like this
public class MyActivity extends Activity {
private TextView txt = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt= (TextView)findViewById(R.id.edtxt);
}
}
but i have no problem in my activity. why?
see this link http://developer.android.com/reference/android/widget/EditText.html
and look here
you will find your answer
That would be because EditText is a subclass of TextView. You're casting an EditText to a TextView, which works because an EditText IS a TextView.
Check out the EditText's docs: http://developer.android.com/reference/android/widget/EditText.html
And here's a little something so that you can learn about inheritance, which is what is happening here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Answer is simple, it is so because EditText extends TextView.

Unexpected interruption of application

Why, in this very simple example, the last statement (setBackgroundColor)
produces a crash of the application ?
public class Macumba extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView vista = (ImageView)findViewById(R.id.vista);
vista.setBackgroundColor(Color.YELLOW);
}
}
main.xml is very simple, too:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="#+id/vista"
android:layout_width="300px"
android:layout_height="330px"
android:layout_x="9px"
android:layout_y="8px"
/>
</AbsoluteLayout>
What kind of exception do you get? NullPointerException? If so, it's most likely because vista is null, which is because R.id.vista is not in your layout.
Are you sure it's the right main.xml file? And not one from eg. another project?

Dynamic Strings and Xml

I'm new to Android (and surely I'm not an expert Java developer), so I'm sorry for the dumb question
I would like to "capture" the id of a textView and to set its text value dynamically,
here is how I'm trying to do this:
public class AndStringTestActivity extends Activity {
/** Called when the activity is first created. */
String abc = "abcabc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(abc);
setContentView(R.layout.main);
}
}
and here is the Xml from which i'm reading the id of the textview i would like to write inside
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
the error i receive is the usual Application stopped unexpectedly
Thanks.
Call setContentView before trying to find the TextView. The TextView does not exist before you have called setContentView (which will inflate your XML layout)
Call setContentView(R.layout.main) before you call findViewById().

Activity of android

I am working with the xml file ,through which i am accessing a string through a variable passkey.later i am assigning it to strPassword. i am getting error for setListAdapter.Here is my code..
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="passkey">
<item>1234</item>
</string-array>
</resources>
and
public class AdminLogin extends Activity
{
public void onCreate(Bundle bdlActivity)
{
String[] strPassword = getResources().getStringArray(R.array.passkey);
setListAdapter(new ArrayAdapter<String>(this,R.layout.adminlogin,
R.id.Editpassword, strPassword))
//button events
}
You need to extend your class with ListActivty. You cant instantiate ListView in simple activity.
You can do it from a simple Activity too:
say you have a main layout, like:
mylayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="#+id/btn_ok" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="OK" android:layout_alignParentBottom="true" />
<ListView android:id="#+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_above="#id/btn_ok" />
</RelativeLayout>
This would be the layout of your activity, with a list view inside of it (#id/listview)
Your Activity's onCreate method should look like this:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
final ListView listView = (ListView) findViewById(R.id.listview);
listView.setListAdapter(new ArrayAdapter<String>(this, R.layout.adminlogin,
R.id.Editpassword, strPassword))
}

Categories

Resources