Adding the contents from XML to Java - java

I have been reading this tutorial -
http://developer.android.com/training/basics/firstapp/building-ui.html
and I got a problem... In the tutorial they explain the XML elements (TextField and Button) and in the end they say how to tun the program to see the button + the text field.
The problem is, that the emulator doesn't show them.. just a black screen.
I guess it's because I haven't added anything to the .java main file, but in the tutorial nothing was mentioned about it.
What should I do?
Thanks a lot for everyone who will help!

If your XML file name is main.xml then to display it content you need this method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

Related

Can I make clickable text in dynamic text files on android studio java?

I am trying to make a application that will allow people to tag more people. I want to make such that when a person clicks / taps in the name of another person in the TextView, it should go to the person's profile in the application (but not in the web). How can I make it?
Any insight of how to do would be respected!
A short code of how to do would be much respected!
TextView Content Example: I am here in the city with Manoj and he is enjoying this place.
I want to make this such that when a person clicks Manoj it should be gone to the next fragment or next activity. And the data should is to sent to the server, so it is something We cant just use only onClick Listener for the text, as the text is determined and the link is created by public user not me?
The image would help you understand!
First in your java file find your TextView by xml id
TextView tv = (TextView)findViewById(R.Id.textView);
then, you have to set click listner on the textView
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here you can use intent to naviaget to another activity
}
});

Renaming Toolbar based on the bottom navigator

How do I change Toolbar Titles in different bottom navigation activities, I currently have one displayed in all which is one my manifest
android:label="#string/home"
but since I have four activities on my bottom navigator how can I rename them all on the activities, I tried adding a toolbar it added below the original, home. Tried renaming manifest particular activities doesn't work. Thanks in advance.
I tried setting titles on the activities like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collections);
setTitle(R.string.collections);
}
I want to have something like this, Image One Image two
You can use setTitle in your activity.

Error in Second Activity "cannot be resolved or not a field"

I would like to create a simple android app which will have two screens
one is MainActivity
other is SecondActivity
After creation of second activity I am getting an error activity_second cannot be resolved or not a field even though activity_second XML exist don't how to resolve.
setContentView(R.layout.activity_second);
Above line showing error
please go through the images to get a clear picture of what the errors i am facing
SecondActivity.java Image
Second Activity XML Image
How would i solve the above errors
Thanks in Advance
Not a big problem..you have given names of your drawable images wrong so your R.java not buliding successfully.
ie android only allows a-z,0-9 in identifiers for drawable images so make names of drawable in lowecase
for example: IMAGE1.png is wrong
change to image1.png and your R.java will build successfully and your layout will be recognize.
setContentView(R.layout.activity_second);
Hope this helps.
Try to import com.example1.hello1.R in SecondActivity.class

How to add a scrollbar in a EditText dynamically/programmatically in Android?

I know how to adda scrollbar through the xml properties such that there is a bar in the right side of the edittext field once we write stuff in it by making setSingleline(false).
My question is :-
is there a way to do the same through the activity , that is, dynamically?
for eg:-
Code is :-
public class abc extends Activity
{
EditText edit = (EditText)findViewById(R.id.EditText1);
edit.setVerticalScrollBarEnabled(true);
edit.isVerticalScrollBarEnabled();
}
These two does not work. When i add these , i still am not able to see the scrollbar on the rightside of the Edittext when i scroll..
Through XML, when we add :- android:scrollbars="vertical" , then we are able to see the scrollbar..
I am asking for a WORKING way to do it through the activity dynamically or in other words programatically
Please its urgent. I would sincerely be thankful.
In XML
android:fadeScrollbars="false"
Dynamically/programatically:
edit.setScrollbarFadingEnabled(false);

Web Page Not Available - Loading local HTML on Android

Yo guys, this is bugging me and I can't find a fix for it,
I have a simple activity to launch a WebView and display a HTML file
public class HelpViewer extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page_layout);
WebView browser = (WebView) findViewById(R.id.help_page_webview);
browser.loadUrl("file:///android_asset/help.html/");
}}
The help_page_layout is your standard WebView resting in a LinearLayout - Not problems there.
The help.html is sitting under the /res/raw/ directory. From what I have see online, there shouldn't be a problem here either.
But when the WebView goes to load, it just tells me that the WebPage was not available because help.html couldn't be found.
What am I doing wrong?
This question suggests the file should be in /assets rather than /res/raw. You also have a trailing slash on the end of your URL.

Categories

Resources