Android findViewById() problem [duplicate] - android

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android Problem calling TextView from second layout file
Hey guys the main layout xml file for my activity is R.layout.date_list_layout as it is used as follows
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_list_layout);
However I wish to set the text in a TextView from this same activity in another layout xml file R.layout.display_item
I know I would usually use the code below but this is not working as the R.id.currency TextView is not in the main R.layout.date_list_layout....
TextView currency = (TextView) findViewById(R.id.currency);
currency.setText(cur);
I know the next line of code is incorrect but is there a way to write a similar line of code to access the R.id.currency in the R.layout.display_item xml or can this not be done?
TextView currency = (TextView) findViewById(R.layout.display_item/R.id.currency);
currency.setText(cur);
Some help would really be appreciated as this has me boggled the last two days and I can't find any solution online
Thanks
A

I'm quite new to Android so I am not sure, but I think this should work:
TextView textView = (TextView)View.inflate(this, R.layout.display_item, null);
"this" would be your Activity.

Related

How to create dial link on TextView? [duplicate]

This question already has answers here:
Dialing a phone call on click of textview in android
(6 answers)
Closed 8 years ago.
I am new to android and:
I have a TextView that can show a phone number.
I want to have that part of the text to be 'touchable' and attempt to make a phone call or something similar.
I have tried to implement implicit intent for when the user clicks the TextView but I can't make it work.
Is there any solution to this? Or a different approach?
Thanks.
First make your TextView clickable by adding below in your layout.xml
<TextView
...
...
android:clickable="true">
</TextView>
And in your java code inside OnClickListener of that particular TextView Start phone activty as
TextView tv=(TextView) findViewById(R.id.tv_contact);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel://"+ tv.getText().toString().trim())));
}
});'
And if you want link on specific text on textview then this post may help you
Android textview with clickable phone number
Edit
As #Apoorv suggested,you may also use android:autoLink = "phone" as
In the xml file, add the below lines.
<TextView
....
android:autoLink = "phone"
/>
For more info see android:autoLink - Have a Clickable Phone Number link in a TextView, in XML
You should use built-in linkify. Read this tutorial, easy to understand: http://android.okhelp.cz/linkify-text-link-url-in-textview-text-android-example/

setVisibility(View.Gone) with SwipeyTabs

I'm a beginner android programmer, and I'm trying to figure out how to work with SwipeyTabs. First of all, is this a good way to find the view:
rb2 = (RadioButton) getView().findViewById(R.id.rb2);
rb2.setOnClickListener(this);
My second question is about how to set the visibility of a TextView or an EditText with SwipeyTabs. I've made a simple application without SwipeyTabs, and there I use the code:
tvPlayer1.setVisibility(View.GONE);
Once I use this same line of code within SwipeyTabs, it crashes. Can someone explain me why I can't get this to work?
Thank you in advance!
Turned out I had a grammar mistake in my line of code.
etPlayer1 = (TextView) getView().findViewById(R.id.etPlayer1);
had to be:
etPlayer1 = (EditText) getView().findViewById(R.id.etPlayer1);

Change TextView text

I'm trying to change my TextView text from the code.
This is what my xml looks like:
XML:
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" />
And the code:
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
setContentView(tv1);
I'm getting an error on my device and the application stops.
I tried to show a TextView (not connected to an XML TextView) and it worked.
Your approach is incorrect. I think it will be Null Pointer Exception (next time post log cat)
You need to specify the layout you are using first, before finding views.
Java:
// Specify the layout you are using.
setContentView(R.layout.yourlayout);
// Load and use views afterwards
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
Kotlin:
// Specify the layout you are using.
setContentView(R.layout.yourlayout)
// Load and use views afterwards
val tv1: TextView = findViewById(R.id.textView1)
tv1.text = "Hello"
Click Here to study exactly you want to know
remove this.. setContentView(tv1);
I got the same problem. My app was stopping too.
Actually, I was writing the code outside of the function/method. So to fix this problem, these lines
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
must be inside a function/method. (can be user defined)
(I am new to android studio so I don't know the reason behind the problem but I only know how to fix this. Maybe this helps new ones despite this question being 8 years old.)

XML scope in Android

Is there anywhere that I can find documentation on the scope of the XML files? I have an app I am currently working on and have been struggling with getting a feature to work and it seems that the problem I am having is that I am trying to access an element in an XML file that must be out of scope. To simplify the layout, my project has main.xml, sub.xml, main.java, and sub.java files in it. As you can probably guess, main.java works with main.xml and sub.java is working with the elements in sub.xml. Here's where the issue comes in, I have a TextView element that is created in main.xml that I would like to modify the text in, but the action that would trigger it will occur in sub.java. I can't figure out how to change it from sub.java, and I can't figure out how to move the element into sub.xml. The code I am using is pretty simple:
TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);
I get a FC every time I run the app, but if I move the code into main.java, it runs flawlessly. If anyone can offer any ideas, or point me in the direction of some documentation that would explain what java files can access what elements in which xml files, that would be awesome! Sorry for the novel, but I'm just struggling to get the point across. Thanks.
try like this Bryan in Main.xml file it works with no issue...........Declare first & then Initialize it...
public class Main extends Activity {
static TextView tv;
static Button submit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
tv = (TextView) findViewById(R.id.header_text1);
}
}
Activity.findViewById(int) only works if that view is in the activity's layout. So no, you can't refer to a view in main.xml because that layout doesn't apply to sub.
Do you have any TextViews in sub.xml called myTitle?
You can access the the textview of main.java(main.xml) in submain.java as follows
In main.java write the following code
static TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);
and u can access this submain.java as
Main.titleText.setText(filePath);

Setting textview text from a string resource held in an array problem

This should be simple, but driving my crazy.
I have the following in my layout, not problems.
<TextView
android:id="#+id/birdinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00009c"
android:text="The Robin is a popular bird"
/>
Then I have these arrays which are set up with the list of string resources I have
private Integer[] DetailIds = {
R.string.barnaclegoose,
R.string.barnowl,
R.string.bewicksswan,
R.string.blackbird,
R.string.blackcap_male};
So I simply want to do this!
TextView detail = (TextView)findViewById(R.id.birdinfo);
detail.setText(DetailIds[0]);
setContentView(R.layout.main);
But this causes a force close error.
The string resource looks like this ( without header and footer info of course
<string name="barnaclegoose">What a wonderful goose!</string>
Added to this problem is if I use the resource directly to the resource
detail.setText(R.string.barnaclegoose);
For example, I still get a null exception! I'm sure I've done this before, but maybe I'm missing the obvious???
Any ideas appreciated.
( Eclipse, Android 1.5, Emulator with 1.5 )
I realize this is very old, but it came up in a search... Anyways, you need to call setContentView() before findViewById() etc:
setContentView(R.layout.main);
TextView detail = (TextView)findViewById(R.id.birdinfo);
detail.setText(DetailIds[0]);
your problem is for this line setContentView(R.layout.main);
at first you must define this this line then setText your textView
Thanks for the answer. but if you mean R.string.barnaclegoose for example, this is an integer value for the ID pointing to the string itself in the resource.
Anyway, I finally got it working by just creating the view inline instead of using an resource view.
For example
TextView t= new TextView(ctx);
t.setId(2);
t.setTextColor(Color.BLACK);
t.setText(DetailIds[bird]);
mLinearLayout.addView(t,params);
mLinearLayout.setBackgroundColor(Color.WHITE);
setContentView(mLinearLayout);
And that works perfectly.

Categories

Resources