Is it possible to program setText from another Java file in android? - java

Please understand me and my problem first.(i explain very badly so)
My files are ActivityMain.java And activity_main.xml which are connected to each other (as far as i know), means if I am calling
final TextView helloTextView = (TextView) findViewById(R.id.text_view_id);
helloTextView.setText(R.string.user_greeting);
in (R.id.text_view_id), text_view_id can only be connect to android:id="#+id/text_view_id" which only present it activity_main.xml (As i understand). And correct me if i am wrong if the text_view_id is not present in the activity_main.xml it ll throw an error "NullPointerException"
Now My Question is - there is anyway that i can interchange data without getting NullPointerException ?
What i mean is want to create new ActivityMain2.java, write the javacode in ActivityMain2.java but android:id="#+id/text_view_id" ll be present on activity_main.java.
I tried several ways searched stack overflow but failed. Either i am getting NullPointerException or the text field do not get updated from code.
My skill level : Just a beginer

you'll have to create another
activity2.xml
file in the layout folder, then you put another
android:id="#+id/text_view_id"
file in the layout.
and since you are a beginner i recommend the beginner's course by Google here

Related

How to format xml and java code as text inside TextView

I am creating an Android Tutorial Application. I am planning to use Tabs to display the java and the xml code used for that topic.
I thought of using TextView and applying the Java and XML code as text to it.
However I am failing to format the Java code, plus I am not able to add xml as text at all as it has tags which are not supported.
Below are snaps from another App and the desired output is what I am looking for.
Any help or suggestions are welcomed.
You could display your Code in your TextView with HTML and style/format it with CSS.
Try this,
Suppose you have HTML code like this
<h2>Title</h2><br>
<p>description here</p>
Then in android you can use below code
TextView tvTrial=(TextView)findViewById(R.id.tvTrial);
String html="<h2>Title</h2><br> <p>description here</p>";
tvTrial.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
Now any thing you want to display in you view you can store it the string and set it.

Intellij: How to find a method?

I need to find what method was called when I tap on something. The project is huge and find it by myself will take a good amount of time.
Is there any possible way to do that?
I'm using Android Studio and yeah I'm working with another person's project for the first time ever.
How I do this:
Let's say that the button has some text. Let's say, it is Login.
Go to the strings.xml, and find the text, matching the text on the button.
use Find Usages in Android Studio on the found string.
In layout XML file observe the button, which text is set to the Login string.
Use Find Usages on the ID of the button
Open the Java file, where the ID of the button is used (usually it is used in findViewById())
use Find Usages on the Button object which is found by id.
Find the setOnClickListener of that button.
see, what is called in that listener.
I'm not sure whether this somehow answers your questions, but at least I found "Analyzing Data Flow" very helpful in messed up projects.
Here's a nice article concerning the feature by Sebastiano Poggi.
Also a gif demostrating the feature in action:
It's also accessible through context menu:

How do you display a string on Android?

Edit: it turned out the XML Viewer wasn't the emulator, and does not actually run code.
So, I'm starting my first app in Android Studio 1.1.0, and I realized that I can't even manage to display a string (created in the java file).
I did all research I could, but no matter what snippets of code I copy/paste, it never works.
I tried copy/pasting the snippet of code from the first answer here: Android Eclipse: Change the text in the app to a string created withing the program and it doesn't work. The findViewById method is in red, and so is my_text_view :
String displaythisgoddammit = "display this goddammit";
private TextView text = (TextView) findViewById(R.id.my_text_view);
text.setText(displaythisgoddammit);
And my textview (in activity_main.xml) is:
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/displaythisgoddammit"/>
It can't find the string, no matter what I do. Apparently I must put it in the strings.xml file, but then how do I edit it...? In the question I linked, the snippet of code apparently worked for a string in the java file.
Here is my main file (I'm not sure if it's really relevant though, it's just snippets of code I copy/pasted): http://pastebin.com/7uuHyJWP I put the string at line 144, should I put it elsewhere?
Some help please? I've been researching for 5 hours but none of the solutions I tried work...
The issue with your code is on the line when you use
TextView text = (TextView) findViewById(id.my_text_view);
It is occuring because when you define a TextView or any other object by it's Id you need to use R.id.my_text_view.
So, to fix it simply change it to
TextView text = (TextView) findViewById(R.id.my_text_view);
In your code please check the reference to the TextView that you are making.
It should be findViewById(R.id.my_text_view);
A few things you should check:
Inside a method, you should not be using the word private, as you are not defining a class level variable.
You need to use R. before the id, to signify you are retrieving a resource.
You need to make sure that the Text View is inside activity_main.xml.
In addition to all of that, you have a lot of things wrong in your pastebin file, I highly recommend you avoid just copy and pasting code, because it is never that simple.
You need to remove all access modifiers that are not at the class level, and clean up snippets like this:
String contentAsString;
public void onCreate;(Bundle savedInstanceState;) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
txtView=(TextView)findViewById(R.id.my_text_view);
txtwidth = (TextView)findViewById(R.id.viewwidth);
hello="This is my first project";
mybtn.setOnClickListener(this);
txtView.setText(hello);}
There are brackets and semi colons all over the place, and not to mention this code is written after a return statement, so you won't even hit this line of code.
NOTE
Other things that I've found that are wrong:
You have an extra closing bracket after the DownloadWebpageTask which will cause some compilation errors.
You override onCreate twice in HTTPExample
It looks like the code you're having trouble with should be inside onCreate.
In other words, try something like this.
Also, inside your XML where you have the line android:text="#string/displayThis, the #string is basically saying you want to get the string 'displayThis' from strings.xml. If you don't have, or don't want to put 'displayThis' in strings.xml, you need to remove that attribute and just set the text programatically.

Can you view and edit a PDF in Java using iText at the same time?

I want to view the PDF in Java on a UI and, while viewing it, being able to add Items to it at the same time and, when these items are added, they update the PDF that I'm viewing to show the new information.
Is this possible using iText? Or any other way? If so, can someone point me in the right direction?
Thanks
I think Its possible when you are using a VO class and passing the values and generated again..it ll be updated..and new datas added. Im not sure...Try and let me know wats happen...
No , you cant view and edit a PDF in Java using iText at the same time.
When you will try to do it , it will give the following error.
The process cannot access the file 'myfile.pdf' because it is being used by another process.

How to translate ListView texts?

My android application includes ListView binded with SimpleCursorAdapter to the database. This database contains column, which contains data like
somenameofrow
anothername
thirdname
However instead of directly displaying these texts in the ListView, I would like to read according texts (actually - translations) from the Android resources.
How should I do it?
I think that I can use setViewValue for the same, name in the code below is TextView item, which displays my text:
case R.id.name:
TextView elName = (TextView) view;
elName.setText(getApplicationContext().getString(R.string.));
return true;
I am not sure how to understand what is correct id of the resource I am looking for (probably, cursor.getString(columnIndex) or elName.getText()) and how to get the value?
Upd. What if I exclude names from the database and will use ids instead. Will it help?
If you read the android guide ListView tutorial, you will find:
Note that using a hard-coded string array is not the best design practice. One is used in this tutorial for simplicity, in order to demonstrate the ListView widget. The better practice is to reference a string array defined by an external resource, such as with a resource in your project res/values/strings.xml file
Simply create different string-arrays items in res/values-XX, where XX are the locales you want to translate.
See the dev guide for more details about resources and i18n: http://developer.android.com/guide/topics/resources/localization.html
No you can't, since id's are generated in some basically sem-random way during compilation, so you can't be sure which id will have specific String resource.
I would suggest to use translation from DB or you can read translations from assets

Categories