How to get hosting Activity from a view?

I just pulled that source code from the MediaRouter in the official support library and so far it works fine: private Activity getActivity() { Context context = getContext(); while (context instanceof ContextWrapper) { if (context instanceof Activity) { return (Activity)context; } context = ((ContextWrapper)context).getBaseContext(); } return null; }

getActivity() returns null in Fragment function

commit schedules the transaction, i.e. it doesn’t happen straightaway but is scheduled as work on the main thread the next time the main thread is ready. I’d suggest adding an onAttach(Activity activity) method to your Fragment and putting a break point on it and seeing when it is called relative to your call to asd(). … Read more

Same Navigation Drawer in different Activities

If you want a navigation drawer, you should use fragments. I followed this tutorial last week and it works great: http://developer.android.com/training/implementing-navigation/nav-drawer.html You can also download sample code from this tutorial, to see how you can do this. Without fragments: This is your BaseActivity Code: public class BaseActivity extends Activity { public DrawerLayout drawerLayout; public ListView … Read more

Android – How To Override the “Back” button so it doesn’t Finish() my Activity?

Remove your key listener or return true when you have KEY_BACK. You just need the following to catch the back key (Make sure not to call super in onBackPressed()). Also, if you plan on having a service run in the background, make sure to look at startForeground() and make sure to have an ongoing notification … Read more

Android Center text on canvas

Try the following: Paint textPaint = new Paint(); textPaint.setTextAlign(Paint.Align.CENTER); int xPos = (canvas.getWidth() / 2); int yPos = (int) ((canvas.getHeight() / 2) – ((textPaint.descent() + textPaint.ascent()) / 2)) ; //((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center. canvas.drawText(“Hello”, xPos, yPos, textPaint);

How to have Android Service communicate with Activity

The asker has probably long since moved past this, but in case someone else searches for this… There’s another way to handle this, which I think might be the simplest. Add a BroadcastReceiver to your activity. Register it to receive some custom intent in onResume and unregister it in onPause. Then send out that intent … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)