How to go another Activity using function instead of Button widget - java

I hope if someone help me in this ,
in my projet I am trying to open another avtivity used voice command Ex, say "one " it should compare if the string is equal to what I? have it should switch to another activity or external activity such as Phone call, Camera. What I did here, I store the recognized ward in Edittext and store it in string and compare it . my 2 projects counted on how to do this
Inside onCreate:
final Button btntx = (Button)findViewById(R.id.btn1);
final EditText edittxt1 = (EditText)findViewById(R.id.edttxt);
btntx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent (Intent.ACTION_DIAL); // sow the Dial window
intent.setData(Uri.parse("tel:"));
startActivity(intent);
);
}
////////////////////////// outside the onCreate //////// Function after I get the result from the Speech recognizer and call this function to see if the what it said is equal to my keyword . SO i need to call the button to open that activity
String gwdata= edtxt.getText().toString(); // data from EditText
if (gwdata.equals("One"))
{
Toast.makeText(getBaseContext(),"They are equal", Toast.LENGTH_SHORT).show();
// Here Do someting to go to another activity
}
Thnks guys in advance .

Related

TextView from SecondActivity become visible after a button from MainActivity is pressed?

I'm very new to Android Studio Development and I was wondering how to do this, when I click a button on MainActivity, it will direct me to secondActivity where the text become visible (Originally TextView will not be visible until the button from MainActivity is pressed)
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String status = "Success!";
intent2.putExtra("Status",status);
startActivity(intent);
}
});
I want to make an if-else statement for this (on SecondActivity page) where if user straight away go to SecondActivity, it will not display any text there. But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.
Thanks!
Basically, there are several approaches you can do that.
Use intents pass data
Sure you pass a boolean type or whatever you want into this intent, I think this is the approach you are trying to make here. So I can give you an example:
In your first activity you can do something like this,
button.setOnClickListener {
val intent = Intent(this#MainActivity, SecondActivity::class.java).apply {
val status = true
putExtra("Status", status)
}
startActivity(intent)
}
And in your second activity, in your need to override onCreate to parse your intents to decide your text want to display or not.
val status = intent.extras?.getBoolean("Status")
if(status) {
hideText()
} else {
showText()
}
the other approach you can deal with it is try to create singleton class to keep the status in this class, and based this singleton class status, you may choose to hide/show your text. However this solution isn't the recommended way to do it. Because global state is bad for testing and just pollute the code.

Save settings button and back to previous activity

I have an activity that has edit text and you can type your name in it, and when you click Save button you will be redirected to MainActivity, but I don't want to open MainActivity by Intent, I make that save button save your name with shared prefs and everything works fine but I don't want to open my Main in Intent I want that when I clicked on save button the current activity that saves data close and previous activity open.
sorry for my bad English.
this is my code for save button
submitButt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == submitButt.getId()) {
String name = inputName.getText().toString().trim();
if (isValidInput(name)) {
Intent setint = new Intent(SettingsActivity.this, MainActivity.class);
setint.putExtra("name", name);
saveData();
startActivity(setint);
Toast.makeText(SettingsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show();
}
}
}
Then you just open the PreviousActivity instead of the MainActivity. Add a checker inside the PreviousActivity if "name" has data or not. You can also use finish(); this will kill the activity.
** Update **
In your MainActivity, let's say you have TextView name. Add:
#Override
public void onResume(){
super.onResume();
// Check your variable if it has value or none
textViewName.setText(variableForSharedPref)
}
Then you can proceed with usual process to go in Settings and Go Back to MainActivity.

Transfer data from one screen to another [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
How to pass an object from one activity to another on Android
(35 answers)
Closed 4 years ago.
I am currently working on a project for my programming class, it is a d&d character creator I've decided to do for my final project. I am nearly finished with it but have run into one problem. I want to take the user's data from a screen where they create a character and store it into a screen where they can view their created characters, but I am not sure how to do this. This is the code from the create a character screen:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_char_screen);
Button btnSave = (Button)findViewById(R.id.btnSaveChar);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkData())
startActivity(new Intent(NewCharScreen.this, HomeScreen.class));
}
});
Button btnEx = (Button)findViewById(R.id.btnExplanation);
btnEx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(NewCharScreen.this, ExplanationScreen.class));
}
});
}
private boolean checkData(){
final EditText charName = (EditText) findViewById(R.id.txtNameInput);
if (charName.getText().toString().isEmpty()){
charName.setError("Enter character name");
charName.requestFocus();
return false;
}
return true;
}
}
If there is a need for the rest of my code or the project itself here is a link to a repository on GitHub:
https://github.com/cbetlinski98/MAD105-final_project
If you want to pass data from one activity to another one, you should pass it inside the Intent object, so intead of creating it inside StartActivity you can create it outside as a normal object, and use putExtra() to put data inside it.
Intent intent = new Intent(getBaseContext(), DestinationActivity.class);
intent.putExtra("OBJECT_PARAM_NAME", your_object);
startActivity(intent);
To recover data on the other activity you can use
String sessionId= getIntent().getStringExtra("OBJECT_PARAM_NAME");
Pass basic types
If you need to pass basic objects like int, float, strings, you can always pass them with putExtra() but to take them in the destination activity there are relative methods like getStringExtra() or getIntExtra() and many others.
Pass your class
If your user is a class defined by you, you can implement Serializable in that class, put it inside the intent with putExtra() and recover it from the other activity with getIntent().getSerializableExtra("OBJECT_PARAM_NAME") and then just cast it to your class before putting it inside an object
In order to pass information across intents:
Intent i = new Intent(this, Character.class);
myIntent.putExtra("paramKey","paramValue1");
startActivity(i);
Then in your activity to obtain the parameters:
Intent i2 = getIntent();
i2.getStringExtra("paramKey");
startActivity(new Intent(CurrentActivity.this, AnotherActivity.class).putExtra(KEY, DATA));

How to write a phone number for calling in android but not make the call

I want to have an action onClick on a button in some activity that just writes a phone number in the agenda screen, but not call it. I have to let the user decide if he wants to call or not.
What I have found by now is this code, that works perfectly, but it's not really what I need. With this code, when I click the button, it calls the selected
private OnClickListener callToAction = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick sticky from homepage.");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:*123"));
startActivity(callIntent);
}
};
Use Intent.ACTION_DIAL to open the dialer instead of connecting the call.
You need to use other Intent Action for that.
Try Intent.ACTION_DIAL
http://developer.android.com/reference/android/content/Intent.html#ACTION_DIAL
or Intent.ACTION_VIEW
http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW
Example from Intent documentation:
ACTION_VIEW tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI.
ACTION_DIAL tel:123 -- Display the phone dialer with the given number filled in.

Passing clipboard text to other activity

My app targets API level 11 (3.0) or above. I want to utilise the Copy button from the Action Bar so that when TEXT is copied to Clipboard it will be sent to EditText of another activity and starts this activity.
Below is what I have done:
wvContent = (WebView) findViewById(R.id.wvContent);
wvContent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
final String text = clipboardManager.getText().toString();
if(text!=null)
{
Intent intent=new Intent(CurrentActivity.this,NewActivity.class);
intent.putExtra(have_word, text);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
finish();
CurrentActivity.this.startActivity(intent);
}
}
});
I place this code under onCreate, but it does not work. The text is copied but the NewActivity is not started.
I wonder whether you guys can provide me a little help to solve this problem. Many thanks.
Use ClipboardManager.getPrimaryClip() instead as getText() is depecreated. here you can find a nice example to use this to get clibpoard data.

Categories

Resources