• Latest Code...

    Featured Post

    Implementing Hilt in a Kotlin Android Jetpack Compose Project with MVVM Architecture

     In modern Android development, maintaining a scalable codebase can be challenging, especially when it comes to dependency management. Hilt,...

    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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    PACKAGE_NAME = getApplicationContext().getPackageName();
    tvPackageName=(TextView)findViewById(R.id.tv_pn);
    btnNext=(Button)findViewById(R.id.btn_next);
    tvPackageName.setText("Package Name "+"\""+PACKAGE_NAME+"\"");
    btnNext.setOnClickListener(new OnClickListener() {

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

    }
    });
    }

    }

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

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

    }

    You can then access it via MainActivity.PACKAGE_NAME.

    Output snapshots:

    Contact Form

    Name

    Email *

    Message *