TextView settext no effect in Activity - android

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.

Related

How to set layout Background programatically

I want to set the layout background programatically, depending on the genre of the song to be played in my application.
I tried this:
public class AnswerActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY");
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.hide();
LinearLayout root = (LinearLayout) findViewById(R.id.ctr1);
Bundle data = getIntent().getExtras();
if(data.getString("genre").equals("rock")){
root.setBackgroundResource(R.drawable.wprock);
}
else if(data.getString("genre").equals("pop")){
root.setBackgroundResource(R.drawable.wppop);
}
else if(data.getString("genre").equals("hiphop")){
root.setBackgroundResource(R.drawable.wphiphop);
}
}
But it doesn't work, it's throwing a Null Pointer Exception in the root.setBackgroundResource lines, whenever anyone of these take place.
Any ideas?
Thanks in advance
EDIT: I do have R.drawable.wprock/pop/hiphop, plus I ruled out that possiblity bevause I tried to use a color instead with the setBackgroundColor mehtod and I had the same exception.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#1d1d1d" >
<ImageView
android:id="#+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="130dp"
android:onClick="go"
android:background="#drawable/playbtn" />
</LinearLayout>
You got the error because you root is a new layout that has not been in your Activity :
LinearLayout root = new LinearLayout(this);
Remove the above code and give an android:id to the outer layout in R.layout.play, and use findViewbyId instead.
First you check the root layout id of your xml if it is correct then follow the code
protected void onCreate(Bundle savedInstanceState) {
Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY");
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.hide();
LinearLayout root = (LinearLayout) findViewById(R.id.ctr1);
Bundle data = getIntent().getExtras();
if(data.getString("genre").equals("rock")){
root.setBackgroundResource(R.drawable.wprock);
}
else if(data.getString("genre").equals("pop")){
root.setBackgroundResource(R.drawable.wppop);
}
else if(data.getString("genre").equals("hiphop")){
root.setBackgroundResource(R.drawable.wphiphop);
}
then check
Bundle data = getIntent().getExtras();
data in bundle is null or some thing else
Your xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ctr1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#1d1d1d" >
<ImageView
android:id="#+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="130dp"
android:onClick="go"
android:background="#drawable/playbtn" /></LinearLayout>

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.

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().

how to change the string values of sting.xml file

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);

Android:Drawing Many Text Views

I'm new to android platform , I'd like to settext using textviews , I tried to write set text into two textviews but its just draws one textview why?
I can't draw two textviews
TextView tv1;
TextView tv2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
tv1 = new TextView(this);
tv2 = new TextView(this);
tv1.setText("Hello");
tv2.setText("How are you?");
}
On Android, the user interface normally should be created using XML-files, instead of Java code. You should read up on the tutorials on android.com, especially:
http://developer.android.com/guide/topics/ui/declaring-layout.html
An example:
In your res/layout/main.xml, you define the text TextView's:
<?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/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 1"/>
<TextView android:id="#+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 2"/>
</LinearLayout>
Then if you use setContentView in the activity to display this, the app will show to TextView's:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
If you want to programmatically set the text in the Activity, just use findViewById():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
((TextView)this.findViewById(R.id.TextView1)).setText("Setting text of TextView1");
}
I definitely second TuomasR's suggestion to use XML layouts. However, if you're wanting to add new TextViews dynamically (i.e. you don't know how many you will need until runtime), you need to do a couple of other steps to what you are doing:
First, define your LinearLayout in main.xml (it's just easier that way than LayoutParams, IMO):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
Now, you can go to your code, and try the following:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//This inflates your XML file to the view to be displayed.
//Nothing exists on-screen at this point
setContentView(R.layout.main);
//This finds the LinearLayout in main.xml that you gave an ID to
LinearLayout layout = (LinearLayout)findViewById(R.id.my_linear_layout);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
t1.setText("Hello.");
t2.setText("How are you?");
//Here, you have to add these TextViews to the LinearLayout
layout.addView(t1);
layout.addView(t2);
//Both TextViews should display at this point
}
Again, if you know ahead of time how many views that you need, USE XML.

Categories

Resources