i need some help with switch/case statement syntax
im trying to use onClick to have different buttons do different things
i have the first one working
it just uses an intent to start a new activity.
for the next button, i want it to open a specific url
looks like this so far
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
case R.id.engadget_button:
Intent a = new Intent(this, )
how do i get the engadget_button to open engadget.com?
Is it just something like this:
case R.id.engadget_button:
String url ="http://www.engadget.com/";
Intent a = new Intent(Intent.ACTION_VIEW);
a.setData(Uri.parse(url));
startActivity(a);
break;
??
http://www.androidsoftwaredeveloper.com/2009/04/15/how-to-open-a-url-from-code/
Related
Immediate Disclaimer: I am not a programmer, I've been dumped with this as part of a group project, so apologies if the code is shabby.
I've got a main activity as the start-up page with several buttons that should open different activities, three of these buttons work perfectly with opening up their specific activities (Main2Activity, MOT and Garage), but the others, with the same structure being used, just close the app instead of opening the next screen.
public void defineButtons() {
findViewById(R.id.mot_button).setOnClickListener(buttonClickListener);
findViewById(R.id.enter_button).setOnClickListener(buttonClickListener);
findViewById(R.id.garage_button).setOnClickListener(buttonClickListener);
findViewById(R.id.profile_button).setOnClickListener(buttonClickListener);
findViewById(R.id.contact_button).setOnClickListener(buttonClickListener);
findViewById(R.id.settings_button).setOnClickListener(buttonClickListener);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mot_button:
Intent intent = new Intent(MainActivity.this, MOT.class);
startActivity(intent);
break;
case R.id.garage_button:
Intent x = new Intent(MainActivity.this, garage.class);
startActivity(x);
break;
case R.id.profile_button:
Intent a = new Intent(MainActivity.this, Profile.class);
startActivity(a);
break;
case R.id.contact_button:
Intent b = new Intent(MainActivity.this, Contact.class);
startActivity(b);
break;
case R.id.settings_button:
Intent c = new Intent(MainActivity.this, Activity_Settings.class);
startActivity(c);
break;
case R.id.enter_button:
reg_input=findViewById(R.id.reg_input);
Intent i = new Intent(MainActivity.this, Main2Activity.class);
regNo = reg_input.getText().toString();
i.putExtra("Value", regNo);
startActivity(i);
finish();
break;
This is the relevant code for it, let me know if you want to see anything else.
I'm probably being really stupid, but I'd appreciate the help.
Your app is crashing, probably because the activities your want to start aren't in the manifest. Check your manifest and make sure that all your activities are declared there.
I am trying this intent so I can navigate from one activity to another activity but I'm getting this error `
final Button btnAdd = findViewById(R.id.addEmp);
btnAdd.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(v.getId() == R.id.addEmp){
Intent intent = new
Intent(getCallingActivity(),AddEmployee.class);
startActivity(intent);
}
}
});`
there is a red line under
(getCallingActivity(),AddEmployee.class);
there error says
cannot resolve constructor
Is anything wrong with this
(getCallingActivity(),AddEmployee.class);
statement?
You need to change your line from
Intent intent = new Intent(getCallingActivity(),AddEmployee.class);
to this:
Intent intent = new Intent(YourActivityName.this,AddEmployee.class);
OR
Intent intent = new Intent(getApplicationContext(),AddEmployee.class);
Edit
Whats wrong with getCallingActivty()?
getCallingActivity() returns ComponentName while intent constructor requires Context as a first argument.
Hope this will work
The problem I am having is that it prints out Null on the second activity and not the actual username that is entered. Is the data being passed to the second activity correctly? Does the second activity need more code? Sorry but not the best at programming.
I have this code in my main class
if (username.getText().toString().equals("batman") &&
password.getText().toString().equals("Joker")) {
Toast.makeText(MainActivity.this, "Username and
password is correct", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.example.*******.loginpage.User");
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class));
This is the code inside my second class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Intent intent = getIntent();
String username = getIntent().getStringExtra("username");
TextView textView = (TextView) findViewById(R.id.textView4);
textView.setText("Welcome" + " " + username );
The problem is your intent in your first class
Intent intent = new Intent("com.example.*******.loginpage.User"); <-- have created an intent
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class)); <-- but using new Intent
You have created an intent but you passing new intent. Use your created Intent instead of passing new Intent.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",String.valueOf(username));
startActivity(intent);
EDIT
Instead using String.valueOf(username) you must use username.getText(), because String.valueOf(username) is method to translate your object to String.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText());
startActivity(intent);
Two problems here.
First one is that you have to pass the intent where you put your extra instead of creating new one to startActivity, like
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText().toString());
startActivity(intent);
Second problem is that username looks like editText, String.valueOf won't pass actual value, use username.getText().toString() like i mentioned in code.
I want to make the ImageView work as button, because I want it be able to click and go to another activity. Each imageView(button) should contain its own value. The problem is, I don't know how to pass the value in the imageView(button) to another activity. This is what I have tried so far:
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
String value = " ";
switch(view.getId())
{
case R.id.imageView2:
value = "5";
break;
case R.id.imageView6:
value = "10";
break;
case R.id.imageView3:
value = "30";
break;
case R.id.imageView02:
value = "50";
break;
case R.id.imageView06:
value = "100";
break;
default:
break;
}
if(view.getId()==R.id.imageView2){
//get the value from switch case and send to other activity
}
Try this way,hope this will help you to solve your problem.
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
switch (view.getId()) {
case R.id.imageView2:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "5");
startActivity(intent);
break;
case R.id.imageView6:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "10");
startActivity(intent);
break;
case R.id.imageView3:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "30");
startActivity(intent);
break;
case R.id.imageView02:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "50");
startActivity(intent);
break;
case R.id.imageView06:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "100");
startActivity(intent);
break;
default:
break;
}
}
};
String valueFromIntent = getIntent().getStringExtra("YourKeyName");
Intent intent = new Intent(YourSecondActivity.this, YourThirdActivity.class);
intent.putExtra("YourKeyName", valueFromIntent);
startActivity(intent);
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", value);
startActivity(i);
here
if(view.getId()==R.id.imageView2){
//here
}
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
You can use Bundle to do the same in Android
//Create the intent
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“imagebuttonValue”, getrec);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
Now in your second activity retrieve your data from the bundle:
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String imagebuttonValue = bundle.getString(“imagebuttonValue”);
Using Intent you can call another activity and even send data with the help of putExtra from one activity to another activity, simply check below piece of code for understanding:
Intent intent= new Intent(currentActivity.this,nextActivity.class);
intent.putExtra("Key",yourvalue);
startActivity(intent);
On next acitivty to retrieve data:
Intent intent = getIntent();
String yourvalue= intent.getExtras().getString("Key");
Pass data trough Intent to your next activity and get your data in that activity like this
Intent intent = new Intent(Activity.this,SecondActity.class);
intent.putExtra("key",value);
startActivity(intent);
Get like this :
Intent intent = getIntent();
String value = intent.getStringExtra("key");
u want to launch a new activity and pass the label value to that ?
if yes then just create an Intent and add the value to it , and use this intent to launch other acitivty.
for example if your value is "5" then :-
Intent intent = new Intent(context) ;
intent.putExtra("key","5");
(refer here)
startActivity(intent);
and at onCreate() method of newly launched activity :-
String value= getIntent.getStringExtra("key"); (Refer here )
Get the values from the Image view. Use Extras and send it to the other Activity.
Lets Say first Activity is X and Next Activity is Y :-
//Include this in your code in the first activity inside your if condition
if(view.getId()==R.id.imageView2){
Intent main= new Intent(X.this, Y.class);
main.putExtra("key", value);
X.this.startActivity(main);
}
At Y Activity onCreate
Intent intent = getIntent();
String value= intent.getStringExtra("key");
Hope it helps.
Thanks!
I am new to android development so there is probably something simple that is wrong. If you need any more info I will be glad to give that to you. Thanks in advance.
I am trying to add a button in my navdrawer.class. This is what I have.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.new_account:
Intent intent = new Intent(this, AddAccountActivity.class);
this.startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
I get an error.
since you´re into a fragment you must use:
Intent intent = new Intent(getActivity(), AddAccountActivity.class);
or
Intent intent = new Intent(getActivity().getApplicationContext(), AddAccountActivity.class);
for example see the context used in your Toast (getActivity())
Toast.makeText(getActivity(), "This Will Create A New Account.", Toast.LENGTH_SHORT).show();
You should write
Intent intent = new Intent(AddAccountActivity.this, AddAccountActivity.class);
instead of
Intent intent = new Intent(this, AddAccountActivity.class);
Am I right that this is the fragment instance? If this is the case, thats your problem. The intent constructor needs a context and an activity class to work.
Fragment does not inherit from context. You can get the underlying activity with the getActivity() method.
try this:
Intent intent = new Intent(getActivity(), AddAccountActivity.class);