How to get programmatically package Name in Android

In many cases you have to need package name programmatically so, here is the
some method name for getting package name.

You will have to initialize it in the main activity's onCreate() method:

Global to the class:

public class MainActivity extends Activity {
public static String PACKAGE_NAME;
private TextView tvPackageName;
private Button btnNext;
protected void onCreate(Bundle savedInstanceState) {
PACKAGE_NAME = getApplicationContext().getPackageName();
tvPackageName.setText("Package Name "+"\""+PACKAGE_NAME+"\"");
btnNext.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent inext= new Intent(getApplicationContext(), GetPackageName.class);



In Next Class you can access this package Name, Like this:
public class GetPackageName extends Activity {

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), MainActivity.PACKAGE_NAME, Toast.LENGTH_SHORT).show();


You can then access it via MainActivity.PACKAGE_NAME.

Output snapshots:

No comments:

Post a Comment


Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace