Show different TextView's depending on the time of the day - android

I'm new to develop Android Apps and writing java. But, I only want to make a simple app that will give a different output on TextView depending on what time of the day it is.
F.ex. If the time is between 06:45 and 08:45 I want it to say "Good Morning", and so forth.
I have been playing with Eclipse and created a simple WebView-app but I can't seem to find any information to either choose which TextView I want shown on a specific time or show a different layout.
Do you have any tips?
This is my .java file in src for now.
package com.wao.texttime;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TextTime extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("Good Morning");
}
}

There is no need to switch between different TextViews. If you just want to display different messages in your TextView I would simply call the setText function to update the TextView's text attribute.

Related

Making a simple email application

So the story is that basically, I'm making a very simple app so that my place of employment can send the closing logs every night with ease. I have all of the text boxes to enter the information, and then at the bottom there is a checkbox and submit like so..
There is a template for the email that we edit manually every night, and basically I want to take all of the information received in the text boxes (all have their own ID, etc.) and plug it into the email template that I currently have - and send it to the emails of the employees (6 people).
Can someone lead me in the right direction, or show me how I would go about this problem?
MainActivity.java
package savo.nicholas.bestb;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I've constructed a bunch of these to get the values from the form. I'm looking to plug these values into a pre-formatted email and send them.
public EditText getGm() //$ of GM
{
return (EditText)findViewById(R.id.gm);
}
Thanks!
Nick

how to fill string arraylist with userinput EditText android eclipse

i am beginner in android programming and English language :) !!
i want to get names from user and show the names that user inputs randomly by pressing a button.
how can i store a user input names in a array list?
and As regards I don't know how many names he wants to enter and don't show a name twice and after showing the name erase that name in array list.
tanks a LOT.
package farshid.mk.teststringarraylist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StringArraylistActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<String> inputs = new ArrayList<String>();
Well, there are many ways to do this but one would be:
Create an EditText, a view that allows user input
Create a Button that the user would click to indicate that he/she is done entering the name in the EditText
Create an OnClickHanlder for the button so that you can get the text from the EditText, convert it to a String (EditText returns an Editable object), and add it to your ArrayList<String>
// add the string
myArrayList.add(myEditText.getText().toString().trim());
To remove duplicates from the List you will need to convert it to a Set... google "java arraylist remove duplicates (First Link)
Also, in Java, it's best practice to use Generics:
// psuedocode
List inputs = new ArrayList
There are many ways you could display this list of text to the user in Android, you will need to research that on your own.
You simply clear the ArrayList when you are done with the names.
This should get you going =)
There's a lot of things you'd need to do to achieve this:
Define the EditText in your layout's XML.
Get a reference to it from the Activity by calling findViewById(R.id.your_edittext_id).
From that reference, call getText().toString() to get the value from the EditText.
Add that String to your ArrayList.
That's basically the main idea behind what you're trying to achieve. I could give you the code but then perhaps its best for you to do it yourself (so that the next time you need to do this, you'd understand the concept).
Good luck! :)

getText() Not Responding

I'm trying to get the text from one XML to another but it just crashes when its supposed to happen so any help will be welcomed!
Here is the code by the way
package com.android.test1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class xmltwo extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xmltwo);
TextView one = (TextView) findViewById(R.id.textView1);
EditText two = (EditText) findViewById(R.id.editText1);
one.setText(two.getText());
}
}
That's from the java file that opens the XML where the textView is.
My guess?
The logcat will be showing a NullPointerException for the line one.setText(two.getText()); because the layout file referenced by R.layout.xmltwo doesn't actually contain a TextView with an id of R.id.textView1 so the TextView called one is null.
Either that or that layout file doesn't contain an EditText with an id of R.id.editText1 which will make that null and cause an NPE when trying to call two.getText().
Just as an extra bit of information, if you want to get the text from an EditText you need to use getText().toString()
In this article Android: edit textview defined in xml one of the members has a similar issue. They theorize that possibly setContentView hasn't finished running yet and that may be the issue. However, you're saying that it is just completely crashing so I'm not sure if that could be the problem.
Could there be another part of your in your super class that could be hanging up? Could you use the getString() function instead perhaps?
I hope some of this might be helpful! I am interested in finding a solution so I intend to keep on reading to try and help!
try
one.setText(two.getText().toString().trim()+"");
hope this help..

Android Textview spitting out something I never wrote

package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Why did it say hello world?");
setContentView(R.layout.main);
}
}
I'm following the Hello, World tutorial on the android SDK site. When I set the text for tv, it never shows. Instead, the output is:
Hello World, HelloAndroid!
Where in the world is that coming from? I never wrote that text, ever, anywhere...freaky.
TextView tv = new TextView(this); << create TextView object
tv.setText("Why did it say hello world?"); << set text to it
setContentView(R.layout.main); << display the view defined in main.xml
as scriptocalypse said you can simply set the content view to tv, alternatively, and I think a better approach is to use the main.xml:
in main.xml there is a TextView and it has a property called id, it looks something like this: <TextView android:id="#+id/text" you can use this id to get the TextView object of this xml like this:
setContentView(R.id.xml);
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("Your text");
There are 3 things to remember:
R.id.text is corresponding to android:id="#+id/text".
R.id.text is an integer, generated during the build of application, it's not a string, and cannot be manipulated as a string.
setContentView has to be called before calling findViewById, otherwise, you will get null as return value of findViewById.
Check your main.xml file. It's likely being printed from there.
To get your own test there, add your new TextView to the main.xml
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Your text here" />
(Note: the "Your text here" should really be brought out to the strings.xml file.)
To put in an editable text field, use an EditText field.
<EditText android:id="#+id/TextId" android:hint="background text"
android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
You should use setContentView before creating a new TextView object. Also, did you remember to add the TextView to the display?
setContentView(tv)
I see you adding the xml layout, but you never add the newly created TextView
You are calling setContentView() and passing in a reference to a layout, specifically main.xml as noted by Haphazard. If you want to see your TextView that you just created, you should be able to pass the variable tv into the setContentView() method.
The default main.xml has a reference to a String in strings.xml in your res/ folder. In there is the definition of a String "Hello World", which is why you are seeing "Hello World".

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

Categories

Resources