Android Archives

Here is the example for creating custom class for location listener:


private class MyLocationListener implements LocationListener {

		public void onLocationChanged(Location location) {
			String message = String.format(
					"New Location \n Longitude: %1$s \n Latitude: %2$s",
					location.getLongitude(), location.getLatitude());

			Toast.makeText(MainActivity.this, message,
			Toast.LENGTH_LONG).show();
		}

		public void onStatusChanged(String s, int i, Bundle b) {
			Toast.makeText(MainActivity.this, "Provider status changed",
			Toast.LENGTH_LONG).show();
		}

		public void onProviderDisabled(String s) {
			
			Toast.makeText(MainActivity.this,
			"Provider disabled by the user. GPS turned off",
			Toast.LENGTH_LONG).show();
		}

		public void onProviderEnabled(String s) {
			Toast.makeText(MainActivity.this, "GPS turned on",
					Toast.LENGTH_LONG).show();
		}

	}

This code generates a random integer using Android’s Random class, and sets a randomly selected drawable as background for a View.

Place your images into /res/drawable/ and update private int pics[] with the drawables from which you want to pick one randomly.

private int pics[]=  {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5};
private Random rand = new Random();

public int set_rand_pic() {
    int pos = rand.nextInt(pics.length-1);
    mycard.setBackgroundResource(pics[pos]);
    return pos; 
}

A quick a simple Alert Dialog Popup with Title, Message, Icon and Button

Don’t forget to import add the imports

import android.app.AlertDialog; import android.content.DialogInterface;

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
      // TODO Add your code for the button here.
   }
});
// Set the Icon for the Dialog
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();

Android read phone(sim) number

TelephonyManager telephoneMgr = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String simNumber = telephoneMgr.getLine1Number();

you need to add ITelephony.aidl in your project..

 

Programmatically create a password field

EditText buddyPasswordEdtTxt = new EditText(mContext);
buddyPasswordEdtTxt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
buddyPasswordEdtTxt.setHint("password");
//buddyPasswordEdtTxt.setTransformationMethod(PasswordTransformationMethod.getInstance());
buddyPasswordEdtTxt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
emailLayout.addView(buddyPasswordEdtTxt);
Forward calls
Intent callIntent = new Intent(Intent.ACTION_CALL);
String forwardNumber = "9999999999"
String number = ("**21*"+forwardNumber+"#");
callIntent.setData(Uri.fromParts("tel", number, "#"));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);

 

Stop forwarding
Intent callIntent = new Intent(Intent.ACTION_CALL);
String number = "##21#";
callIntent.setData(Uri.fromParts("tel", number, "#"));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);

Incoming search terms:

  • how to divert call intent android

Shut down android device programmatically

Runtime.getRuntime().exec(new String[]{ "su", "-c", "reboot -p" }); // you should have root access!
    private static final String STATE_ACTIVE_POSITION = "context_position";
    private static final String STATE_CONTENT_TEXT = "context_text";

//save context
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);

        outState.putInt(STATE_ACTIVE_POSITION, mActivePosition);
        outState.putString(STATE_CONTENT_TEXT, mContentText);
    }

//recall context
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        mActivePosition = savedInstanceState.getInt(STATE_ACTIVE_POSITION);
        mContentText = savedInstanceState.getString(STATE_CONTENT_TEXT);

        //do your stuff here....
    }
getActionBar().setHomeButtonEnabled(true); // check API level

Incoming search terms:

  • actionbar title clickable
  • android actionbar title click
mList = new ListView(this);
mAdapter = new MenuAdapter(items);
mList.setAdapter(mAdapter);
mList.setOnItemClickListener(mItemClickListener);

mActivePosition = 0; //click the first item (item at 0th position)
mList.performItemClick(mList.getAdapter().getView(mActivePosition, null, null), mActivePosition, mList.getAdapter().getItemId(mActivePosition));

mMenuDrawer.setMenuView(mList); //base view, where you are attaching the list view.

Incoming search terms:

  • paint listview item programatically android
Page 18 of 68« First...10...1617181920...304050...Last »
We Can Build Your App for You. Lets Get Started !