I've had this problem for a week now and since it doesn't have any actual errors in java file i couldn't search for an answer.when i try to run the app it forcibly closes.by the way i am really new to programming so excuse me if it's an stupid question. any way lets get to the app, i want it to show the time on a text-field when the button is pressed.and here is my code
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Date;
public class MainActivity extends Activity implements View.OnClickListener {
Button btn;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
EditText txt=(EditText)findViewById(R.id.text);
txt.setText("welcome,press the button to show the time");
btn.setOnClickListener(this);
}
public void onClick(View view) {
updateTime();
}
private void updateTime() {
EditText txt=(EditText)findViewById(R.id.text);
txt.setText(new Date().toString());
}
}
and here is the layout if you need
<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"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="Press" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="88dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Your are using in your layout a Textview not an EditextView
<TextView
android:id="#+id/text"
This line is your problem:
EditText txt=(EditText)findViewById(R.id.text);
you will change this line to:
TextView txt=(TextView)findViewById(R.id.text);
but what do you need a EditextView or a TextView?
Your code must be something like:
public class MainActivity extends Activity implements View.OnClickListener {
Button btn;
TextView txt;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
txt=(TextView)findViewById(R.id.text);
txt.setText("welcome,press the button to show the time");
btn.setOnClickListener(this);
}
public void onClick(View view) {
updateTime();
}
private void updateTime() {
txt.setText(new Date().toString());
}
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_test);
btn=(Button)findViewById(R.id.button1);
EditText txt=(EditText)findViewById(R.id.text);
txt.setText("welcome,press the button to show the time");
// btn.setOnClickListener(this);
}
In your xml:
<Button
android:id="#+id/button1"
....
android:onClick="onClick"/>
Setting the onClick in the xml will call that method when the button is clicked.
Also match up the xml and java element to be either both an EditText or TextView.
Also you don't need to create two EditText txt. You are creating two local variables, when you can create one.
Button btn;
EditText txt;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_test);
btn=(Button)findViewById(R.id.button1);
txt=(EditText)findViewById(R.id.text);
txt.setText("welcome,press the button to show the time");
// btn.setOnClickListener(this);
}
public void onClick(View view) {
updateTime();
}
private void updateTime() {
txt.setText(new Date().toString());
}
Related
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);
}
...
}
I have one activity with two layouts one is main layout and the other layout name is layout2. Both layout files contain one button each. it works good if i press button on main layout it take me to layout 2 but the problem is when i click button on layout 2 to take me back to main layout i does not work.
MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button1,button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout2);
}
});
LayoutInflater inflater=this.getLayoutInflater();
View view=inflater.inflate(R.layout.layout2,null);
button2=(Button) view.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main);
}
});
}
}
These are the layout files to display
Mainlayout
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.lidiawood.myapplication.MainActivity">
<Button
android:text="Go to layout 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:id="#+id/button1" />
</RelativeLayout>
Layout 2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="go to main layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="40dp"
android:id="#+id/button2" />
</RelativeLayout>
Can any one please help me how can i get display for the mian layout from the componnent of layout 2
The onClick event listener is attached to the button in the inflated view, not to the button in the activity view. It doesn't matter that you're using the same layout, it's a different button.
This situation should never happen, because you, generally speaking, need different fragments or activities, when changing the layout. Changing the content view this way is not a good idea. If you still what to use your code, then you need the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onContentViewOneInflated();
}
private void onContentViewOneInflated() {
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout2);
onContentViewTwoInflated();
}
}
}
private void onContentViewTwoInflated() {
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main);
onContentViewOneInflated();
}
}
}
I haven't tried the code. If you have any issues, comment.
I created a button in my Android project, but it's not working.
Here is my application (based on 'hello world'):
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="#+id/button_roy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
and this is my java roy.java
package aa.aa.aaaaaaa.royb;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/**
* Created by royb on 05/11/13.
*/
public class roy extends Activity {
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createBtn = (Button) findViewById(R.id.button_roy);
createBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick (View v)
{
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
}
}
When I run it on my android device and click the button it doesn't do anything.
How can I make the toast display?
public class Roy extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createBtn = (Button) findViewById(R.id.button_roy);
createBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick (View v)
{
Toast.makeText(getApplicationContext,"HelloToast",Toast.LENGTH_SHORT).show();
}
}
}
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