I want sent the content of the variable indicatorpositionstart of main2Activity to mainActivity. I used the solution "extra", but does not work.
What's the problem?
public class Main2Activity extends AppCompatActivity {
private Button imageValidate;
int indicatorPositionStart = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageValidate = findViewById(R.id.buttonValidate);
imageValidate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2 = new Intent(Main2Activity.this, MainActivity.class);
intent2.putExtra("indicatorPositionStartt", indicatorPositionStart );
startActivity(intent2);
}
});
}
public class MainActivity extends AppCompatActivity{
int id = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView3);
if (id != 0) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);
textView.setText(indicatorPositionStart);
}
}
}
Thank you in advance.
The problem is that you are checking the value before you get them from the extras, see this lines on the second activity:
// you are using it here, before getting the value
if (id != 0) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);
textView.setText(indicatorPositionStart);
}
}
Another problem on the MainActivity is that you are not filling in the id field.
I fixed it, just replace this one with your second activity:
public class MainActivity extends AppCompatActivity{
int id = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView3);
// get the value first
id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
// then use it
if (id != 0) {
String indicatorPositionStart = String.valueOf(id);
textView.setText(indicatorPositionStart);
}
}
}
In your MainActivity, remove this check if (id !=0 ) because activity started you set the value of id =0 so if (id !=0 ) this if is never true.
For Sending Data:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("arg", YourData);
startActivity(intent);
For Receiving Data:
String data = getIntent().getExtras().getString("arg");
Update
remove if( id != 0) and use if(getIntent().hasExtra("indicatorPositionStartt"))
like this:
if (getIntent().hasExtra("indicatorPositionStartt")) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);
textView.setText(indicatorPositionStart);
}
Related
I'm kind of new to android. I want to get an int value id(id of dynamically generated buttons) from class MainPage into class Note when a button is clicked. but the value of id always turns to zero in Note class.
here's the summerized code:
MainPage class:
public class MainPage extends AppCompatActivity {
public int id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int j=0; j<allnotes.size(); j++) {
//generating buutons
final Button preview_text = new Button(this);
preview_text.setId(j)
preview_text.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//getting id of the clicked button
id=v.getId();
startActivity(new Intent(MainPage.this, Note.class));
}
});
}
}
}
Notes class:
public class Note extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
MainPage obj=new MainPage();
int id=obj.id
View parentLayout = findViewById(android.R.id.content);
Snackbar mySnackbar = Snackbar.make(parentLayout,Integer.toString(id) , 10000);
mySnackbar.show();
}
in snackbar message, id is always zero.
You need to add the id to the Intent you use to start the Note activity. You do this by using Intent.putExtra(...) and in Note you retrieve it via getIntent().getIntExtra(...)
Here I implemented #Riccully Answer in your code.
MainPage.java
preview_text.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//getting id of the clicked button
id = v.getId();
Intent intent = new Intent(MainPage.this, Note.class);
intent.putExtra("id_value", id);
startActivity(intent);
}
});
Note.java
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
int id = getIntent().getIntExtra("id_value", 0);
View parentLayout = findViewById(android.R.id.content);
Snackbar mySnackbar = Snackbar.make(parentLayout, Integer.toString(id), 10000);
mySnackbar.show();
}
we can do this via intent Because Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services, etc.
In Our case, we can send the v.getId() to Note.java, viaputExtra
intent.putExtra("view_id",v.getId());
and receive the value in Note.java by using
getIntent().getIntExtra("view_id", 0);
I am creating a simple tasklist app in Android Studios.
I have two activities as following (removed imports and package specifications):
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
EditText taskAdd;
Button add;
public ListView tasks;
public Integer pos = 0;
public ArrayList<String> taskArray;
public ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
taskAdd = findViewById(R.id.taskAdd);
add = findViewById(R.id.add);
tasks = findViewById(R.id.tasks);
taskArray = FileHelper.readData(this);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, taskArray);
tasks.setAdapter(adapter);
add.setOnClickListener(this);
tasks.setOnItemClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.add:
String entered = taskAdd.getText().toString();
if (entered != "" || entered != null || entered != "null") {
adapter.add(entered);
taskAdd.setText("");
FileHelper.writeData(taskArray, this);
Toast.makeText(this, "Task Added!", Toast.LENGTH_SHORT).show();
}
break;
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pos = position;
Intent intent = new Intent(MainActivity.this, Pop.class);
startActivity(intent);
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
}
private void processExtraData(){
Bundle extras = getIntent().getExtras();
if (extras != null) {
int value = extras.getInt("Value");
if (value == 1) {
taskArray.remove(pos);
adapter.notifyDataSetChanged();
Toast.makeText(this, "Task Removed!", Toast.LENGTH_SHORT).show();
}
}
}
}
Pop.java (a popup)
public class Pop extends Activity implements View.OnClickListener {
Button deleteButton;
Button finishedButton;
Button timerButton;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow);
deleteButton = findViewById(R.id.deleteButton);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*0.5),(int)(height*0.5));
deleteButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent i = new Intent(this, MainActivity.class);
i.putExtra("Value", 1);
startActivity(i);
}
}
After I click deleteButton in Pop.java, processExtraData in MainActivity.java is supposed to run. The Toast does appear, but the selected object in the ListView is not deleted. No errors are thrown either. In addition, using Log.d to check the size of taskArray confirmed that this is not just a graphical issue. Why is this the case, and how should I go about fixing it?
Thank you for replying in advance.
The issue is that you are using an object reference instead of a primitive data type, and so when you are calling taskArray.remove(pos), it is looking for pos the object rather than its denoted integer value.
Instead of:
taskArray.remove(pos);
try:
taskArray.remove(pos.intValue());
I wanna recreate activity with saving one int value. I don't know why onSavedInstance always has value null:
public class ActionActivity extends AppCompatActivity implements View.OnClickListener {
//...
int p = 0;
String newString;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action);
progress = (TextView) findViewById(R.id.Progress);
if (savedInstanceState != null) {
p = savedInstanceState.getInt("PROGRESS");
}
p++;
newString = String.valueOf(p);
progress.setText(newString);
#Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(), ActionActivity.class);
startActivity(i);
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("PROGRESS", p);
super.onSaveInstanceState(savedInstanceState); }
}
So... My problem is that when I try to send Intent putExtra , get it in the new class and then put it into a TextView it returns null...
Here is my code:
final String phonenum = text.getText().toString();
Intent i = new Intent(sms_verification.this, sms_verification_two.class);
i.putExtra("num", num);
i.putExtra("phonenum" , phonenum);
startActivity(i);
And then the "sms_verification_two"
Button next;
EditText code;
TextView phone;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_verification2);
next = (Button)findViewById(R.id.button);
code = (EditText)findViewById(R.id.editText2);
phone = (TextView)findViewById(R.id.textView);
String phonenum = getIntent().getStringExtra("phonenum");
String num = getIntent().getStringExtra("num");
phone.setText(phonenum);
Try using this approach on your sms_verification_two activity
String phonenum = "";
String num = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
phonenum = extras.getString("phonenum");
num = extras.getString("num");
}
phone.setText(phonenum + num);
Make sure the data you put in the intent isn't null. The code itself looks fine to me.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button goToNextActivity=(Button) findViewById(R.id.bSendID);
final EditText etNum=(EditText) findViewById(R.id.etNumID);
final EditText etPhoneum=(EditText) findViewById(R.id.etPhoneumID);
goToNextActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String textFromEtNum=etNum.getText().toString();
String textFromEtPhoneum=etPhoneum.getText().toString();
Intent i = new Intent(getApplicationContext(), secondActivity.class);
i.putExtra("num", textFromEtNum);
i.putExtra("phonenum" , textFromEtPhoneum);
startActivity(i);
}
});
}
}
second activity:
public class secondActivity extends AppCompatActivity {
Button next;
EditText code;
TextView phone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
next = (Button)findViewById(R.id.button);
code = (EditText)findViewById(R.id.editText2);
phone = (TextView)findViewById(R.id.textView);
String phonenum = getIntent().getStringExtra("phonenum");
String num = getIntent().getStringExtra("num");
phone.setText(phonenum + " " + num);
}
}
First Activity
**if your text variable TextView or EditText then write**
String phonenum = text.getText().toString();
Intent i = new Intent(sms_verification.this, sms_verification_two.class);
i.putExtra("nums", num);
i.putExtra("phonenums" , phonenum);
startActivity(i);
Second Activity
public class sms_verification_two extends AppCompatActivity {
Button next;
EditText code;
TextView phone;
String phonenumss = "",numss = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
next = (Button)findViewById(R.id.button);
code = (EditText)findViewById(R.id.editText2);
phone = (TextView)findViewById(R.id.textView);
if(getIntent().getStringExtra("phonenums") != null){
phonenumss = getIntent().getStringExtra("phonenums");
}
if(getIntent().getStringExtra("nums") != null){
numss = getIntent().getStringExtra("nums");
}
phone.setText(" " +phonenumss + " " + numss);
}
}
I tried looking on other questions but nothing seems to work. Trying to pass the value stored in sUsername from activity Username.java to MainActivity.java (username).
This is Username.java:
public class Username extends ActionBarActivity implements View.OnClickListener {
EditText eUsername;
Button login;
String sUsername;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_username);
eUsername = (EditText) findViewById (R.id.username);
sUsername = eUsername.getText().toString();
login = (Button)findViewById(R.id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername); //first argument is the name of the string being passed
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.id.login:
loginClick();
break;
}
}
and this is MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String username = bundle.getString("containsUsername");
WebView listOfSongs = (WebView) findViewById (R.id.webview);
String url = "http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user="+username+"&api_key=68f82cd7b37e7b23800c2025066531c9&format=json";
listOfSongs.loadUrl(url);
}
your loginCheck Method should be like this:
private void loginClick() {
sUsername = eUsername.getText().toString();
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername); //first argument is the name of the string being
startActivity(intent);
}
As in on create your edittext might be empty.
Please check your edittext is empty or not. Chnage your method longClick.
private void loginClick(){
if(!sUsername.isEmpty()) {
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername);
startActivity(intent);
} else {
Toast.makeText(Username.this, "Please enter username.", Toast.LENGTH_LONG).show();
}
}