I'm attempting to make a simple click action which calls a certain number, I'm on the last stage of the code and I cannot see what I'm doing wrong. Currently its the startActivity action which seems to be presenting the error but I don't know why I have watched multiple tutorials and I can see any difference. When above startActivity it informs me that a call permission is required?
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_home);
//On load the program automatically hides the taskbar.
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
Button b = (Button) this.findViewById(R.id.BTNCall);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent PhoneCall = new Intent(Intent.ACTION_CALL);
PhoneCall.setData(Uri.parse("tel:123"));
startActivity(PhoneCall);
}
});
}
I have also added a permission into the android manifest
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
You should set your target SDK version to lvl 22 or under, because for lvl 23 you need to ask user at run time for permission, look there for a better explanation.
Since you are doing it from a OnClickListener, the "this" pointer references to the clickListener class you are implementing, you need to get the reference to your activity just use:
MyActivityClassName.this.startActivity() //Dont know your class name
That should get rid of the red highlight.
If you are still getting a crash we will need the logcat output to solve it.
Hope this helps.
Related
In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
startActivity(i);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.
Fragment fragment = Fragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
.content_frame);
This tip features how to change Android’s default animation when switching between Activities.
The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity.
Here’s an example:
//Calls a new Activity
startActivity(new Intent(this, NewActivity.class));
//Set the transition -> method available from Android 2.0 and beyond
overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.
for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483
Change like this code you must be passing activity as context in adapter
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
Activity activity = (Activity) context;
activity.startActivity(i);
activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
Note : Context is the Base Object of Activity
Update :
I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .
Alternative :
Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.
There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
I'm using Android Studio.
In the MainActivity inside the onCreate I did:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTime = SystemClock.uptimeMillis();
serverChecksThread.start();
status1 = (TextView) findViewById(R.id.textView3);
timerValue = (TextView) findViewById(R.id.timerValue);
uploadedfilescount = (TextView) findViewById(R.id.numberofuploadedFiles);
uploadedfilescount.setText("Uploaded Files: 0");
addListenerOnButton();
initTTS();
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
}
And in the activity_main.xml I added:
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/btn1"
android:text="Close App" />
I have two questions.
The main is that if it's logical and good thing to do to add a button that will close the application so it will not leave anything behind? This way when i'm running the application over again on my android device it's starting clean reseted.
The sub question is when I click the button and exit the app then when I'm running it again for a millisecond the app blink. And it happen after I added this button code it wasn't before. I'm not getting error or exception but it's blinking for a very short time.
"Running" and "closed" are fuzzy concepts in Android. When an app is in the background, it may or may not actually be running. When an activity is in the backstack, an in-memory instance of it may or may not exist. When your last activity finishes, the framework may or may not kill the process. And when you start the app again, the framework may or may not create a new instance of your Application class.
Calling System.exit(0) is a bad idea because it short-circuits the Android framework. It may result in unspecified behavior (read: really strange bugs.) Better to just finish your last activity and let the framework do as it likes.
Whether it's good UX to show a close button is a matter of opinion. Google recommends against it. The preferred way to "close" an activity is by pressing the back button.
It is not necessary that you add a close button to an application on android because, there is usually a, either software button, or b, a hardware button on the device to close (minimize) applications. so it wouldn't be a good thing to add a button, and it would also be illogical
And for your second question, I did not quite understand your point.
Even if you close your app with a button, it won't be closed permanently, it still will be shown on the users device as a 'running on background app'. Because android is not working like windows, so its not so useful to add such a button.
As far as i know the only apps that using this button is using it to be sure in 100% that the user left the app and that the connection that he had will be closed, so no one else will be able to use his login or password...
When on Android there is not Facebook native app, then in order to share a post from another Android app a web dialog is being opened. Here on step 4 there is a description how to open that dialog (see publishFeedDialog function).
My problems is that all the other code described in this tutorial I have wrote in AppActivity which is a subclass of Activity. Hence, getActivity() method is not defined as far as it is defined for Fragments. To solve this problem I have defined a private variable like this:
private Activity activity;
Assigned a value in onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
// .......
}
and used in all places where there was a call of getActivity(). As I not a Java neither an Android expert, I would like to understand whether I have done it correctly.
I have an android app that simply just toggles a value in the settings. For this app when clicked I DON'T want to show a layout, just a Toast and then kill itself. I have this already:
public class Test extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Toast.makeText(this, "MESSAGE", Toast.LENGTH_LONG).show();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
Is there a possibility to disable the layout at all?
It sounds like a Service will be much more appropriate in this case.
Services run in the background, but can still perform some limited UI tasks such as showing Toasts.
Activities are not designed to be used without a UI.
Simple, just comment out one line looks like:
//setContentView(R.layout.activity_main);
oh, to kill self, you may simply call finish by the end of onCreate.
I have an android app that has various activities.
for example there is a home screen and an update screen.
The home screen is a list activity.
A button on the home screen brings you to the update screen that updates the database from a server. In theory when Returning to teh homescreen after an update, the list should be changed to reflect the update just done.
the code for going to the update screen is as follows:
Button synch = (Button) findViewById(R.id.synchButton);
synch.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
Intent intent = new Intent(HomeScreen.this, SynchScreen.class);
startActivity(intent);
}
});
and the code for returning back to the homescreen after the updates is:
main_menu.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
finish();
}
});
the list is compiled from an async task that runs in onStart, so my understanding is that onStart should run when I return to the homescreen, thus always displaying the most up to date version of the list.
On my Emulator I always get teh updated list, but when I run it on my phone the list is never updated, it just returns to the state of teh screen before I did the update.
any ideas?
thanks
Kevin
Check the Activity lifecycle section of the Android documentation. The code updating the view should probably be moved to onResume, since the Activity might not get killed when launching a new one.
Put the code for starting the Asynctask in onResume. Read the documentation related to activity life cycle.
#Override
public void onResume() {
super.onResume();
Apaptor.refreshListView(Vector);
}