Command button to import text to textview from edittext using Scanner? - java

ok so i what to develop an application that will take the text from edittext to textview with the click of button this is what i have so far kind of a noob to java
package one.two;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Button;
import java.util.Scanner;
public class one extends Activity {
public static void main(String args[]){
Scanner what = new Scanner(System.in);
what.toString();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Button btn = findViewById(yourButtonId);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText txt = findViewById(yourEditTextId);
TextView txt1 = findViewById(yourTextViewId);
txt.setText( txt.getText().toString() );
}
});

Related

How to solve the '.MainActivity.<init>(MainActivity.java:16)' error?

EditText object is not created, why?
EditText editText=(EditText)findViewById(R.id.edittext);
This is for an App that extends AsyncTask class. fetchingData is a class that extens AsyncTask class.But it gives error on creating object of EditText.
package com.example.zafar.omdb_7;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String string;
Button button;
public static TextView data;
//
private EditText editText = findViewById ( R.id.edittext );
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=findViewById(R.id.fetched_data);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
//
#Override
public void onClick(View v) {
// string = editText.getText ().toString ();
fetchingData process= new fetchingData();
process.execute();
}
});
}
}
Your code should be something like this:
package com.example.zafar.omdb_7;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String string;
Button button;
public static TextView data;
//
private EditText editText;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=findViewById(R.id.fetched_data);
editText = findViewById ( R.id.edittext );
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
//
#Override
public void onClick(View v) {
// string = editText.getText ().toString ();
fetchingData process= new fetchingData();
process.execute();
}
});
}
}
You won't find the view before you set it on the activity. I hope this helps.
You can't use the following:
public class MainActivity extends AppCompatActivity {
// This won't work!
private EditText editText = findViewById ( R.id.edittext );
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
}
Because you can't call the findViewById before you set the Activity layout with setContentView(). It means that you can't find a view if you didn't have the layout yet.
So, edit your code to something like this:
public class MainActivity extends AppCompatActivity {
...
private EditText mEdtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Now you have the layout, you can find the view
mEdtText = findViewById(R.id.edittext);
...
}
}

Perserve value in text field in screen 1 while pressing back from the screen 2 using variables and intents

I have 2 screen on first i have Text field and when i pressed the button it takes me to second screen with data from the Text Field but while pressing the back button from screen 2 to screen 1.
I want that the text field(screen 1) will show the previously added data through variables.
////////////////////////////////////Screen 1 code://///////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.os.PersistableBundle;
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 {
Button button;
EditText name;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.buttonNext1);
name=(EditText)findViewById(R.id.editTextName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String namevalue= name.getText().toString();
savedInstanceState.putString("MyString", "Welcome back to Android");
Intent intent=new Intent(MainActivity.this,Main3Activity.class);
intent.putExtra("Name",namevalue);
startActivity(intent);
}
});
}
}
////////////////**Screen 2 code:**////////////////////////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent= new Intent(Main3Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
Screen 2
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
String valueOfName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
valueOfName = getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
screen2Done();
}
});
}
public void screen2Done() {
Intent intent=new Intent();
intent.putExtra("RESULT_STRING", valueOfName);
setResult(RESULT_OK, intent);
finish();
}
#Override
public void onBackPressed() {
screen2Done();
}
On screen 1, capture the value in onActivityResult() method, similarly as in screen 2.

Cannot Resolve Symbol 'setOnClickListener'

I am fairly new to Android/Java developing and I have run into this error. I cannot figure out what is wrong after doing extensive research and playing with my methods. Here is my code:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener()
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
}
If anyone knows what I could've done wrong, please let me know! Thanks!
Put the code inSide onCreate()
public class mainactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Button Clicked
}
});
}
}
put your code inside onCreate() method
see the android lifeCycle to understand why :https://developer.android.com/guide/components/activities/activity-lifecycle.html
Your code is out of your method, Change it to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener()
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
}
Do Button initialization and adding listener to Button inside onCreate() method.
Try this:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
}
}

Button and click display

I have tried Everything to my knowledge and nothing seems to work. Any help would be appreciated.
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class activity_main extends Activity {
TextView txtCount;
Button btnCount;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount= (TextView) findViewById(R.id.textView);
txtCount.setText(String.valueOf(count));
btnCount= (Button)findViewById(R.id.button);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
count++;
txtCount.setText(String.valueOf(count));
}
});
}
}
}
No errors appear at any point and the program works without errors; the button clicks and does not crash, but no click is counted numerically in the textView.
you onClick() listener is wrong.
try this one
btnCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
txtCount.setText(String.valueOf(count));
}
});

Cannot find symbol class onClickListener

I'm new to android development. Here's the problem i ran into.
I'm using Android Studio.
I looked up on many sites, they said to import the related class. Having done
that, the problem remains.
Any help is appreciated.
Can anyone please help me with this, i have been searching for a while now for the solution.
Here's the code:
package com.example.veeresh.myapplication;
//import statements
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(
//error: cannot find symbol class onClickListener
new Button.onClickListener()
{
public void onClick(View v)
{
TextView text1 = (TextView)findViewById(R.id.text1);
text1.setText("Veeresh Here");
}
}
);
}
}
Error:
Error:(24, 27) error: cannot find symbol class onClickListener
Error:Execution failed for task ':app:compileDebugJava'.
Compilation failed; see the compiler error output for details.
It should be new View.OnClickListener() instead of new Button.onClickListener()
OnClickListener with a capital O.
Edit your code like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
TextView text1 = (TextView)findViewById(R.id.text1);
button1.setOnClickListener(new View.onClickListener()
{
public void onClick(View v)
{
text1.setText("Veeresh Here");
}
}
);
update your code with the below code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView text1 = (TextView)findViewById(R.id.text1);
text1.setText("Veeresh Here");
}
}
Both Button.OnClickListener() and View.OnClickListener() works fine it is just the capital O that you are missing.

Categories

Resources