i'm new to Android programming, i want to creating an android apps that need user input in MainActivity then display the username in another activity. i planned create 5 more activity to display the username.
this is the MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.start);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);
getMenuInflater().toString();
sendName();
startActivity(intent);
}});
}
public void sendName(){
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);
EditText inputnama = (EditText) findViewById(R.id.inputnama);
intent.putExtra("username", message);
}
and this is new1Activity
Intent username = getIntent();
String username1 = getIntent().getStringExtra("username1");
i used the same code to another new Activity but it don't display anything.
can anyone explain to me what's wrong?
EditText inputName = (EditText) findViewById(R.id.inputnama);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(inputName != null){
String message = inputName.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("prefName", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", message);
editor.apply();
editor.commit();
startActivity(new Intent(MainActivity.this, Quiz1Activity.class);
}
}});
In QuizActivity and other activities
SharedPreferences sharedPreferences = getSharedPreferences("prefName", 0);
String userName = sharedPreferences.getString("username", "");
store values in Intent;
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);
EditText inputnama = (EditText) findViewById(R.id.inputnama);
intent.putExtra("username", inputnama.getText().toString() );
get values using Intent;
String username1 = getIntent().getStringExtra("username");
If i understood the question correctly you need this when starting your activity:
Intent intent = new Intent();
intent.putExtra("userName", userName);
startActivity(intent);
and to get the name in the new activity:
getIntent().getExtras().getString("userName");
In your onClick method, you declare your :
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);.
Then in sendName(), you have :
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);.
You should understand that these two intents are not the same, they are two differents objects. The modifications on one won't affect the other.
So your intent in onClick wasn't modified. What you want to do is to return the modified intent from sendName, something like startIntent(sendName());.
Additionally, you could just pass intent as parameter in sendName.
Use this clicklistener.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new
Intent(MainActivity.this,Quiz1Activity.class);
getMenuInflater().toString();
EditText inputnama = (EditText) findViewById(R.id.inputnama);
intent.putExtra("username", inputnama.getText().toString());
startActivity(intent);
}});
Related
I am new in the android dev and java
My idea is to make BMI that will have history.
So I am at the point where I struggle with the intent for the second activity.
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openActivity2();
}
});
}
private void openActivity2() {
Intent intent = new intent(this, Activity2.class);
startActivity(intent);
}
I am getting:
can not resolve symbol intent
'i' should capital when you create new Intent() object The openActivity2() should look like this
private void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
String[] charc={"FLash","ARROW","SUPERGIRL","BATMAN","SUPERMAN","ATOM","AQUAMAN","CYBORG","JOKER","BANE","GREENLANTERN","ZOOM","REVERSEFLASH","FIRESTORM"};
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"onCreate was Called",Toast.LENGTH_SHORT).show();
lv = (ListView) findViewById(R.id.idList);
b = (Button) findViewById(R.id.Btn);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,charc);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
s=charc[i];
b.setText(s);
Toast.makeText(getApplicationContext(),"Item Clicked: "+i,Toast.LENGTH_SHORT).show();
}
});
}
public void btnclick(View v){
startActivity(new Intent(MainActivity.this, approutes.list.Display.class)
.putExtra("Flash",s));
}
Intent accept String array
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("string", array);
startActivity(intent);
In your receiving intent you need to do:
Intent i = getIntent();
string_array = i.getStringArrayListExtra("string");
Try this.
From Your first Activity
Intent intent = new Intent(context, FirstActivity.class);
intent.putExtra("string-array", charc);
context.startActivity(intent);
Then In Next Activity Get it Like this in on Create()
Intent intent = getIntent();
String [] stringArray = intent.getStringArrayExtra("string-array");
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("list_as_string", charc);
startActivity(intent);
along with these you should add your SecondActivity in Manifest as Activity and make sure you have added btnclick() method to button in xml layout or you can set button click with following
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call intent here
}
});
To pass data between activities there is Intent object. You should do:
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("charc", charc);
context.startActivity(intent);
And to get charc in NextActivity do:
Intent intent = getIntent();
char[] charc = intent.getStringExtra("charc");
When i click onClick button change imageButton in MainActivity.class
This is Activity with onClick button:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
This is code in MainActivity with imageButton:
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton2);
imageButton.setImageResource(R.drawable.psik);
if I understand well, you should pass some indicator to change image resource to the MainActivity and set it in onCreate(). Try to change your code like this:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
myIntent.putExtra("shouldChangeButton", true);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
and then in MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean defaultValue = false; //or true as you need
if(getIntent().getBooleanExtra("shouldChangeButton", defaultValue)) {
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
ib.setImageResource(R.drawable.img);
}
}
or in case you want permanent change, you should keep it in SharedPreferences instead of passing it through intent:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
prefs.edit().putBoolean("shouldChangeButton", true).apply();
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
and
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean defaultValue = false; //or true as you need
SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
boolean shouldChangeButton = prefs.getBoolean("shouldChangeButton", defaultValue);
if(shouldChangeButton) {
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
ib.setImageResource(R.drawable.img);
}
}
You need to add a onClickListener to the view which would trigger onClick. And then you need override the onClick where you finish the operations to change imageButton.
Just Look at this:
Button btn=new Button(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//to change you imageButton here.
imageButton.setBackgroundResource(0);
}
});
This is the write activity. In here I want to write a text and after clicking a button I want this text to appear in a new activity called read activity.
public class Write extends Activity implements OnClickListener
{
EditText text; TextView retrive1;
public static String filename="Mysharedstring" ;
SharedPreferences someData;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.write);
setupVariables();
someData = getSharedPreferences(filename, 0);
}
private void setupVariables()
{
Button sav= (Button) findViewById(R.id.save);
Button ret= (Button) findViewById(R.id.retrive);
text= (EditText) findViewById(R.id.txtText);
retrive1= (TextView) findViewById(R.id.textview);
ret.setOnClickListener(this);
sav.setOnClickListener(this);
}
public void onClick(View v)
{
String stringdata= text.getText().toString();
SharedPreferences.Editor editor = someData.edit();
editor.putString("sharedString", stringdata);
editor.commit();
}
}
I don't know what to write in the read activity.
public void onClick(View v)
{
//???
}
The most simple way that I can think of is this first Activity
Intent intent= new Intent(this, theOtherActivity.class);
intent.putExtra(Key, "Value");
startActivity(intent);
And on theOtherActivity
String receivedData=getIntent().getExtras().getKey(Key);
public void onClick(View v)
{
// get the shared string from the SharedPreference
SharedPreferences sp = getSharedPreferences("Mysharedstring",0);
String s = sp.getString("sharedString","Nothing found.");
// display the shared string
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
Sources for SharedPreferences:
How to save app settings?
How to keep information about an app in Android?
Shared Preferences | Android Developers
I am trying to get the data from an EditText in my first screen and then be able to use this in my second screen. I am using putExtra method but it keeps giving me the error message
Cannot make a static reference to the non-static method putExtra(String, String) from the type Intent
Does anyone know how to solve this? Thanks!!!
final EditText et = (EditText) findViewById(R.id.word);
Button btn1 = (Button) findViewById(R.id.go_btn2);
btn1.setOnClickListener(new OnClickListener(){
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_click);
#Override
public void onClick(View v){
buttonSound.start();
startActivity(new Intent(MainActivity.this, Second.class));
Intent.putExtra("name", et.getText().toString());
}});
You need to create an object of Intent first:
final EditText et = (EditText) findViewById(R.id.word);
Button btn1 = (Button) findViewById(R.id.go_btn2);
btn1.setOnClickListener(new OnClickListener(){
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_click);
#Override
public void onClick(View v){
buttonSound.start();
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("name", et.getText().toString());
startActivity(intent);
}});
You have to create instance of Intent class then to use
final EditText et = (EditText) findViewById(R.id.word);
Button btn1 = (Button) findViewById(R.id.go_btn2);
btn1.setOnClickListener(new OnClickListener(){
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_click);
#Override
public void onClick(View v){
buttonSound.start();
Intent intent = new Intent(MainActivity.this, Second.class) ;
intent.putExtra("name", et.getText().toString());
startActivity(intent);
}});
final EditText et = (EditText) findViewById(R.id.word);
Button btn1 = (Button) findViewById(R.id.go_btn2);
btn1.setOnClickListener(new OnClickListener(){
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_click);
#Override
public void onClick(View v){
buttonSound.start();
Intent intent = new Intent(MainActivity.this, Second.class)
startActivity(intent);
intent..putExtra("name", et.getText().toString());
//Intent.putExtra("name", et.getText().toString());//**This cause problem in your code**
}});