I have a weather app that requires you to take the text from a edit text field and display it in a text view, I'm trying to make it so when I enter a place it will generate random weather for them along with displaying their input.
I haven't tried much apart from examples online as I recently started learning android development.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
/**
* Implementation for the main activity
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// member variable for the user provided location
private String location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the Get Forecast button
Button btnGetForecast = (Button) findViewById(R.id.btnGetForecast);
// set the click listener to the btnGetForecast Button
btnGetForecast.setOnClickListener(this);
EditText loc = findViewById(R.id.etLocationInput);
location = loc.getText().toString();
}
#Override
public void onClick(View view) {
// view is the View (Button, ExitText, TextView, etc) that was clicked
// if it was the btnGetForecast
if (view.getId() == R.id.btnGetForecast){
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvTitle" />
<TextView
android:id="#+id/tvInstructions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter a location below for the forecast" />
<EditText
android:id="#+id/etLocationInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/btnGetForecast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Forecast" />
<TextView
android:id="#+id/tvLocationDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The weather in " />
</LinearLayout>
I expect the app to show input from user and display it in text view
This line in your onCreate: 'location = loc.getText().toString()' will only get the current text in loc, which at onCreate will be an empty string.
You should look into TextWatcher: https://developer.android.com/reference/android/text/TextWatcher. This will allow you to get a callback when the text of loc changes and you can then carry out your weather generation.
Well, all you need to do is to add this code in onClick method:
#Override
public void onClick(View view) {
// view is the View (Button, ExitText, TextView, etc) that was clicked
// if it was the btnGetForecast
if (view.getId() == R.id.btnGetForecast){
String text = loc.getText().toString();
yourTextView.setText(text);
}
}
And don't forget to make view variables global, so you could access them outside onCreate method, without using findViewById() again.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView yourTextView;
private EditText loc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the Get Forecast button
Button btnGetForecast = (Button) findViewById(R.id.btnGetForecast);
// set the click listener to the btnGetForecast Button
btnGetForecast.setOnClickListener(this);
loc = findViewById(R.id.etLocationInput);
yourTextView = findViewById(R.id.tvLocationDisplay);
}
...
}
Related
So I have seen multiple examples of this done and for some reason I cannot get onSaveInstanceState to save the value of my EditText properly.
I have a very simple example that looks just like the image below.
first a TextView, then an EditText followed by a Button.
When clicking the Button it saves the value in EditText and then applies it to the TextView.
So the problem is it only works once, meaning I start in Portrait mode then turn my phone to Landscape and it saves then back to Portrait and it does not so the TextView appears blank.
My Example code is below:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="50dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="50dp"
tools:context="mydomain.com.savetest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default Text"
android:textSize="48sp"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="choose a word"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVE WORD"
android:id="#+id/bu"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
MainActivity.java
package mydomain.com.savetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private EditText mEditText;
private Button mButton;
private String editTextValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.button);
editTextValue = mEditText.getText().toString();
mTextView.setText(editTextValue);
if(savedInstanceState != null){
mTextView.setText( savedInstanceState.getString("text") ); //setting the saved value to the TextView
}
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editTextValue = mEditText.getText().toString();
mTextView.setText(editTextValue);
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("text", editTextValue); //saving EditText value
}
}
So the whole goal of this is to get the TextView to save the word that was int the EditText and maintain it switching from portrait to landscape. As of now if I type lets say "Car" it will update the TextView with "Car" and will also have the value of Car even turning to Landscape mode, BUT when turning back to portrait the TextView value is gone and has nothing. I Cant seem to figure out whats going on here because I have set the value of EditText to TextView in the beginning on onCreate() so when the app starts again it should take the saved value just fine.
You have to get the current text directly from EditText instead of relying on a String variable being initialized in OnClickListener() once. It works only once because you've pressed the button once.. After the 2nd orientation change, the button wasn't pressed and hence String editTextValue is more likely null, because you would have to press the button after each orientation..
Change somethings as follows:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//don't save EditText value in a variable, instead directly get it from the EditText.
outState.putString("text", mEditText.getText().toString());
}
also remove the variable initialization from OnClickListener() Its not needed
editTextValue = mEditText.getText().toString(); // remove this.
Hello I want that my methods work instantly when the user enters the application.
For now I have an onClick event in my xml which activates so to say my methods(this works).
the xml:
<TextView
android:id="#+id/cd_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:paddingBottom="0dp"
android:text="time"
android:textColor="#000"
android:textSize="21sp"
android:onClick="dateEnd"/>
and Main Activity.java
String dateStopKukuk = "21 Dec 2015";
private void displayDateEnd(String etime) {
TextView priceTextView = (TextView) findViewById(R.id.cd_start);
priceTextView.setText(etime);
}
public void dateEnd(View v) {
displayDateEnd(dateStopKukuk);
}
Edit: I added a comments to explain you this code.
Try this:
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener
{
// onClick method is called when one of view is clicked. (you must use setOnClickListener on this View to inform system to call this)
#Override
public void onClick(View v)
{
// check if clicked view is cd_start
if(v.getId() == R.id.cd_start)
{
// true cd_start is clicked
displayDateEnd(dateStopKukuk);
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); // REQUIRED!!! When you don't add this your app will be crash.
setContentView(R.layout.activity_main); // sets activity_main as layout (if you use different name of layout replace activity_name with your layout name)
findViewById(R.id.cd_start).setOnClickListener(this); // finds view with cd_start id and sets click listener. When this view is clicked system calls onClick method.
// and now your code what do you want to do when activity is creating.
}
String dateStopKukuk = "21 Dec 2015";
private void displayDateEnd(String etime) {
TextView priceTextView = (TextView) findViewById(R.id.cd_start);
priceTextView.setText(etime);
}
/* You don't need dateEnd method now */
}
XML:
<TextView
android:id="#+id/cd_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:paddingBottom="0dp"
android:text="time"
android:textColor="#000"
android:textSize="21sp"
/> <!-- You don't need android:onClick -->
In your activity.
I hope it helped you.
I have a simple jokes application. When you press the button you get new joke. If the previous one was bigger than the screen you can scroll it down. If you go down to the bottom and you go to the next joke, you are transfered to the bottom of the newly generated joke, but i want it to go to the top, and automatically display the start of the joke. How can i do this ?
I assume it would be done via the java code.
Thank you for your time.
Use the scrollTo(int x, int y) method, i like to have ScrollView around my TextView but i think the same thing will work for a TextView only. hope you understand!
Rolf
Example
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="130dp" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="long\n\n\n\n\n\n\n\n long\n\n\n\n\n\n\n very text here!" />
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="joke" />
</LinearLayout>
java:
package org.sample.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class AutoscrollActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button new_joke;
private TextView joke;
private ScrollView scroll;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new_joke = (Button) this.findViewById(R.id.button);
new_joke.setOnClickListener(this);
joke = (TextView) this.findViewById(R.id.text);
scroll = (ScrollView) this.findViewById(R.id.scroll);
}
#Override
public void onClick(View v) {
joke.setText("Long\n\n\n\n\n\n\n\n joke \n\n\n\n\n\n\n\nlong joke joke");
scroll.scrollTo(0, 0);
}
}
I'm working in an application that has in the same view a set of buttons and a web view(GONE), when the app starts it shows only the buttons from where at a click it shows the web view and loads a url (the layout where the buttons are is set to GONE) that contains a flash media player and a single button that when is click it makes the web view invisible again and shows the first layout that contains the buttons to be able to choose a different website, the problem is that when the web view goes invisible the flash media players stays stuck on the screen and blocks the buttons, how can I fix this? It would be ok if the media player stays behind the buttons but I cannot find an answer, any help will be highly appreciated, Thank you.
Update with Code:
<?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"
>
<LinearLayout android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="match_parent" android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Web 1" android:id="#+id/buttonWeb1"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/buttonWeb2" android:text="Web 2"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Web 3" android:id="#+id/buttonWeb3"></Button>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="#+id/linearLayout2" android:layout_width="match_parent" android:orientation="vertical">
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/buttonBack" android:text="Back to Buttons"></Button>
<WebView android:layout_height="match_parent" android:layout_width="match_parent" android:id="#+id/webView"></WebView>
</LinearLayout>
And the main Activity
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
public class FlashejemploActivity extends Activity {
WebView wv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
final LinearLayout buttons = (LinearLayout) findViewById(R.id.linearLayout1);
final LinearLayout webV = (LinearLayout) findViewById(R.id.linearLayout2);
final Button web1 = (Button) findViewById(R.id.buttonWeb1);
final Button web2 = (Button) findViewById(R.id.buttonWeb2);
final Button web3 = (Button) findViewById(R.id.buttonWeb3);
final Button back = (Button) findViewById(R.id.buttonBack);
wv = (WebView) findViewById(R.id.webView );
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.setWebViewClient(new WebViewClient());
wv.setInitialScale(1);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setUseWideViewPort(true);
final Activity PActivity = this;
wv.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
PActivity.setTitle("Cargando....");
PActivity.setProgress(progress * 100);
if(progress == 100)
PActivity.setTitle(R.string.app_name);
}
});
webV.setVisibility(View.GONE);
web1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttons.setVisibility(View.GONE);
webV.setVisibility(View.VISIBLE);
wv.loadUrl("http://los40.com.mx/Player.htm");
}});
web2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttons.setVisibility(View.GONE);
webV.setVisibility(View.VISIBLE);
wv.loadUrl("http://www.beat1009.com.mx/n2/paginas/radio.php");
}});
web3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttons.setVisibility(View.GONE);
webV.setVisibility(View.VISIBLE);
wv.loadUrl("http://besame.com.mx/Player.htm");
}});
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttons.setVisibility(View.VISIBLE);
webV.setVisibility(View.GONE);
}});
}
}
I know its been a while but I have just solved a similar problem by getting the webview to empty
wv.loadUrl("about:blank");
I'm trying to move the position of the seekbar using a button. Basically I have a seekbar from 0 to 100. and I have button presents set up at arbitrary values (40,50,60 etc). When I try to set the progress on the seekbar via button, I get a fault.. I've already initialized the seekBar in the onCreate() method.
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
currentProgress = 40;
seekBar.setMax(100);
seekBar.setProgress(currentProgress);
button40.setOnClickListener(button40Listener);
But when use the below, it crashes.
private OnClickListener button40Listener = new OnClickListener() {
public void onClick(View v) {
currentProgress = 40;
seekBar.setProgress(currentProgress);
}
}
This seems straight-forward. Any ideas?
You shouldn't need to put up another Seekbar. The initial one should be fine. Without the exception message and stack trace I'm not sure what is causing the crash. However, I just coded an example and works as you would expect. Perhaps by looking at my example you can identify your issue.
SeekbarTest.java:
package com.example.seekbartest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class SeekbarTest extends Activity {
/** Called when the activity is first created. */
private SeekBar seekBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
seekBar = (SeekBar)findViewById(R.id.seekBar1);
Button fortyPctButton = (Button)findViewById(R.id.buttonFortyPct);
fortyPctButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
seekBar.setProgress(40);
}
});
Button sixtyPctButton = (Button)findViewById(R.id.buttonSixtyPct);
sixtyPctButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
seekBar.setProgress(60);
}
});
Button eightyPctButton = (Button)findViewById(R.id.buttonEightyPct);
eightyPctButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
seekBar.setProgress(80);
}
});
}
}
And here is the main.xml it is referencing for the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:text="#string/hello"
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBar1"
android:layout_below="#+id/textView1"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"/>
<Button
android:layout_width="wrap_content"
android:text="40%"
android:id="#+id/buttonFortyPct"
android:layout_height="wrap_content"
android:layout_below="#+id/seekBar1"
android:layout_alignLeft="#+id/seekBar1"/>
<Button
android:layout_width="wrap_content"
android:text="60%"
android:id="#+id/buttonSixtyPct"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/buttonFortyPct"
android:layout_alignTop="#+id/buttonFortyPct"
android:layout_alignBottom="#+id/buttonFortyPct"/>
<Button
android:layout_width="wrap_content"
android:text="80%"
android:id="#+id/buttonEightyPct"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/buttonSixtyPct"
android:layout_alignTop="#+id/buttonSixtyPct"
android:layout_alignBottom="#+id/buttonSixtyPct"/>
</RelativeLayout>
Just create a new android app and replace the generated code + layout with the example above. It should work for you.
Good luck,
Craig