Ok So i want to pass values from One activity to another using getextrand putextra method.
In Second Activity in which I want to receive data is full of contents like Buttons and Text view. and i want to set that certain value which i have received from MainActivity to a particular text box.
setContenView(R.id.intent)
is the easiest one method to show a string but what if I want to Set this value to one or more textview. My code is here
MainActivity
package com.prashant.cookbook;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
static String Message_send="Prashant";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et=(EditText)findViewById(R.id.editText1);
Button send=(Button)findViewById(R.id.send);
final Intent msg_send= new Intent(this,Second.class);
String MSG= et.getText().toString();
msg_send.putExtra(Message_send, MSG);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(msg_send);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
SecondAvtivity
package com.prashant.cookbook;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class Second extends Activity {
private TextView tv;
private Intent rcv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv = (TextView) findViewById(R.id.msg_show);
rcv = getIntent();
String Show_msg;
Show_msg=rcv.getStringExtra(MainActivity.Message_send);
tv.setText(Show_msg);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
but when I run this code I got nothing but a blank second Activity Not even a default text
get Value entered by user in EditText on Button click and then use msg_send.putExtra for placing value in Intent as:
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String MSG= et.getText().toString(); //<< get value from EditText here
msg_send.putExtra(Message_send, MSG);
startActivity(msg_send);
}
});
Related
This is my Input Edit Text class
package com.example.websocketclient;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText name;
Button enter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
name = (EditText) findViewById(R.id.editTextdname);
String dname = name.getText().toString();
sendname(dname);
}
});
}
public void sendname(String dname) {
Intent i = new Intent(this,Chatroom.class);
Bundle bundle = new Bundle();
bundle.putString("myname", dname);
i.putExtras(bundle);
startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is my display the EditText to TextView class
package com.example.websocketclient;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class Chatroom extends Activity {
TextView uname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatroom);
uname = (TextView) findViewById(R.id.textViewmynam);
//kuha
Bundle bundle = getIntent().getExtras();
String urname = bundle.getString("myname");
uname.setText(urname);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chatroom, menu);
return true;
}
}
When i run it.. it says Unfortunately, then it will close..
I would like to pass a data from -> EditText to my other class to my display class -> TextView.
Along with ρяσѕρєя K answer, for passing the value you, since you are passing only one String, you can just use:
intent.putExtra("value","key");
and while getting,
String value= getIntent.getStringExtra("key","default_value");
it says Unfortunately, then it will close..
Because you are not initializing enter Button object before calling setOnClickListener. do it as:
setContentView(R.layout.activity_main);
// initilize Button here
enter= (Button) findViewById(R.id.BUTTON_ID_IN_XML);
Hi I have an android app created which is a basic login screen which I want to take you to a new activity screen if logged in correctly. However I am having trouble with the redirect. Here is my code:
package com.example.loginscreen;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view){
if(username.getText().toString().equals("mara") &&
password.getText().toString().equals("mara")){
Toast.makeText(getApplicationContext(), "Login Successful!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,HomePage.class));
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(false);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
At the moment if you login with the wrong credentials everything works as it should but if you login correctly the app just exits and says that it has stopped working. I know the problem it with my startActivity intent because if I remove
startActivity(new Intent(MainActivity.this,HomePage.class));
it at least puts up the toast to notify you that you have logged in correctly. Thanks!
Here is the code for the HomePage:
package com.example.loginscreen;
import android.widget.ImageView;
public class HomePage extends MainActivity{
ImageView image;
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
}
}
Firstly,
register HomePage activity in manifest.
And
change
public class HomePage extends MainActivity{
}
to
public class HomePage extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
Hope this helps.
I found this code to create a share window:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
public class AndroidShareActionProviderActivity extends Activity {
private ShareActionProvider myShareActionProvider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.completed);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
myShareActionProvider = (ShareActionProvider)item.getActionProvider();
myShareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
myShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"http://www.example.com/");
return shareIntent;
}
}
My question is, is it possible to make this share window open when an ImageButton outside the actionbar is clicked instead and if possible how to do that?
yes and not so difficult either. what you need to do is lauch the intent in your onclick. since that method returns the view u want it will be something like this
this.YOURIMAGEBUTTON.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(createShareIntent()));
}
});
I'm starting learning android development but im facing a an error which says :
Multiple markers at this line
- view cannot be resolved to a type
- The method setOnClickListener(View.OnClickListener) in the type View is not applicable
for the arguments (new
OnClickListener(){})
my program is :
package com.sc.uploader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add;
Button sub;
TextView disply;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add =(Button) findViewById(R.id.badd);
sub=(Button) findViewById(R.id.bsub);
disply= (TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new view.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
so please if you could help me resolving the error i will be grateful
thanks
Change this line
add.setOnClickListener(new view.OnClickListener() {
to
add.setOnClickListener(new View.OnClickListener() {
capital "V". It is looking for a variable view when it should be set on the View Class.
OnClickListener Docs
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button abButton = (Button) findViewById(R.id.button1);
final TextView changelingtext = (TextView) findViewById(R.id.changeling);
abButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Buttons are working baby", Toast.LENGTH_LONG).show();
count++;
String a = Integer.toString(count);
changelingtext.setText(a);
gotonextpage(v);
}
});
}
public void gotonextpage(View view){
Intent intent = new Intent(this, SecondpageActivity.class);
startActivity(intent);
intent.putExtra("count", count);
//finish(); if you want to end this page
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
First class is above, second class is below
package com.example.collegematch;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SecondpageActivity extends Activity {
int values;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondpage);
Intent intent = getIntent();
values = intent.getExtras().getInt("count");
Button exitButton = (Button) findViewById(R.id.exit);
Button textbutton = (Button) findViewById(R.id.coolbutton);
TextView texty = (TextView) findViewById(R.id.cooltext);
textbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), Integer.toString(values), Toast.LENGTH_LONG).show();
System.out.println(values);
}
});
exitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Seeya", Toast.LENGTH_LONG).show();
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.secondpage, menu);
return true;
}
}
In the mainActivity, everytime button abButton is pressed, it increases the count variable by 1. It also creates a new intent and sends that variable via extra to that intent.
In the second activity, the "values" variable getting the data from the intent is giving me a null pointer exception. Why?
Intent intent = new Intent(this, SecondpageActivity.class);
startActivity(intent);
intent.putExtra("count", count);
Change to
Intent intent = new Intent(this, SecondpageActivity.class);
intent.putExtra("count", count);
startActivity(intent);
You are setting the extra after you've already started the 2nd activity
just alter these two code lines,
Intent intent = new Intent(this, SecondpageActivity.class);
intent.putExtra("count", count);
startActivity(intent);
Your SecondActivity Intent start before setting the extra count to it.