-1

this is my code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void Smart1(View view){
        Intent Smart1app =getPackageManager().getLaunchIntentForPackage("project.developers.com.demoproject");
        startActivity(Smart1app);



        }


    public void Smart2(View view) {
        Intent Smart2app =getPackageManager().getLaunchIntentForPackage("com.smart.hayat");
        startActivity(Smart2app);

    }}

when i run application using this code, i cant access both apps.What is the the logical error or my logic itself might be wrong

Ashish Kudale
  • 1,230
  • 1
  • 27
  • 51

1 Answers1

-1

ContrainLayout is bit difficult to understand. In this xml I have used LinearLayout. Inside that I have added two ImagesView On each ImagesView there is onClick property is added. Please go through the code, If you didn't understand let me know.

Here is your xml file

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <ImageView
        android:layout_weight="1"
        android:onClick="Smart1"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_weight="1"
        android:onClick="Smart1"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

And Here your java file

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void Smart1(View view) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(ComponentName.unflattenFromString("project.developers.com.demoproject"));
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public void Smart2(View view) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(ComponentName.unflattenFromString("com.smart.hayat"));
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
Ashish Kudale
  • 1,230
  • 1
  • 27
  • 51