I am trying to pass text from a custom dialog to the home activity. The bundle always comes up as null instead of the value I'm trying to pass and I can't figure out why. I've tried looking at similar questions but I have yet to find a solution.
Dialog
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_ingredients_dialog);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(AddIngredientsDialog.this,
android.R.layout.select_dialog_item, fruits);
editText.setAdapter(arrayAdapter);
text = editText.getText().toString();
Intent intent = new Intent(AddIngredientsDialog.this, AddIngredientsActivity.class);
intent.putExtra("Text", text);
startActivity(intent);
dialog.dismiss();
}
});
}
Home Activity
ingredientList = findViewById(R.id.listView);
ArrayList<String> ingredients = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_ingredients);
//bundle
Bundle extras = getIntent().getExtras();
text = extras.getString("Text");
button = findViewById(R.id.add);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
ingredients.add(text);
adapter = new ArrayAdapter<>(AddIngredientsActivity.this, android.R.layout.simple_list_item_1, ingredients);
ingredientList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
In your home activity, try using getIntent().getStringExtra("Text"), instead of extras.getString("Text");
//bundle
text = getIntent().getStringExtra("Text")
Problem could be here
`text = editText.getText().toString();`
Make sure you've written something in EditText
It is better using startActivityForResult to start your dialog Activity. Check this answer Sending data back to the Main Activity in Android
Related
I´m new to app development and I´m trying to figure out how to send an user input like name, to a list view located in a diferent activity.
Thanks in advance.
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button mbuttonNext;
private EditText meditTextName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditTextName = (EditText) findViewById(R.id.editTextName);
mbuttonNext = (Button) findViewById(R.id.buttonNext);
mbuttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ClientList.class);
startActivity(intent);
}
});
}
}
ListViewActivity:
public class ClientList extends AppCompatActivity {
private ListView listaClientes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
listaClientes = (ListView) findViewById((R.id.listView));
}
}
In the main activity you need to need intent.putExtra
mbuttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ClientList.class);
intent.putExtra("VariableName", "Fred");
startActivity(intent);
}
});
Than in listView
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
listaClientes = (ListView) findViewById((R.id.listView));
Bundle bundle = getIntent().getExtras(); //get the intent and data passed
//next check that bundle is not null
if (bundle != null) {
String name = bundle.getString("VariableName");
//try loging out the value
Log.i("Name", name);
}
}
I believe this will solve your problem
When starting the new activity:
Intent newIntent = new Intent(MainActivity.this, ClientList.class);
newIntent.putExtra("com.example.myapp.NAME", "someName");
startActivity(newIntent);
Then in ClientList:
Intent intent = getIntent();
String name = intent.getStringExtra("com.example.myapp.NAME");
Can then use name variable as source data for the ListView.
Usually you should create an Adapter that will "set" the items in the ListView. In order for you to send/transfer the user input to another activity you can use "putExtra()". You can do that the following way:
intent.putExtra("str1",item)
where str1 is the key to get your item in the activity you started. To do that you should do:
Bundle b = getIntent().getExtras();
Obj a = (TypeCast) b.get("str1");
After that you should read how to create an Adapter and set it in a ListView in order for your ListView to show the item.
I am semi-ok with the understanding of how you store and retrieve data through intents, what I am not sure about however is the code implementation. What I want to accomplish is to pass stored data (name/value pairs and a bundle object) to my ThirdActivity class from MainActivity class. Here is a snippet what i have attempted thus far (which when the user clicks on the a3button it just displays a blank page with no string):
MainActivity from which I want to store data to send to ThirdActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a2_button = (Button) findViewById(R.id.a2_button);
a2_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a2intent = new Intent("com.tester.lab.x");
startActivityForResult(a2intent, 1);
}
});
Button a3_button = (Button) findViewById(R.id.a3_button);
a3_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a3intent = new Intent(v.getContext(), ThirdActivity.class);
a3intent.putExtra("greeting","my Name");
startActivity(a3intent);
}
});
}
}
ThirdActivity:
public class ThirdActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
getIntent().getStringExtra("greeting");
}
}
How would I display the string "my Name" once the user clicks ThirdActivity button? Any help is wonderful, thanks!
Everything is good, just add String type for value that you are getting and use this String as you want (to show in TextView else..):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
Or by using Button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
Button three = (Button) findViewById(R.id.your_button_id);
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
});
}
i am new to android, i have created two activities where main activity consist one button to go to secondActivity in second activity where the user inputs name and the data is taken back to main activity using intents.The problem i am facing is i want to display the data in listview
MainActivity.java
public class MainActivity extends Activity {
Button addcontact;
ListView listview;
//TextView name;
ArrayAdapter<String> listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listView1);
TextView name = (TextView) findViewById(R.id.txtname);
addcontact = (Button) findViewById(R.id.addbtn);
addcontact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, 1);
savedata();
}
});
}
private void savedata() {
Intent intent = getIntent();
String fName = intent.getStringExtra("fname");
List<String> ListElements = new ArrayList<String>(Arrays.asList(fName));
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1, ListElements);
listview.setAdapter(adapter);
}
}
SecondActivity.java
public class SecondActivity extends Activity {
EditText edname;
Button addbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
edname =(EditText) findViewById(R.id.edname);
addbtn = (Button) findViewById(R.id.savebtn);
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("fname", edname.getText().toString());
startActivity(intent);
}
});
}
}
I would suggest you don't recreate your first Activity. Instead of that approach, use startActivityForResult().
In short - Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).
For more chek this link.
In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}
Is there a way to open the system dialog settings->location & security->Install from SD card programmatically from my application?
You launch at least the security setting by the following code.
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName(
"com.android.settings",
"com.android.settings.SecuritySettings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}