How to open new Activity on image click in Android Studio


How to Open New Activity on Image click in Android Studio
Step 1:- Create new Activity on Image click or button in android studio is very important topic of Android Studio. In this tutorial i will tell you how to open new Activity in android studio on Image click. Tomorrow i will tell you how how to open new activity on click button and open new activity
 Firstly Create New project------------ Click app-----------res-------Right click on Drawable paste the image in drawable folder see in this image
Click app-------------res-----------layout--------------content.xml and add image View in layout
<ImageView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:id="@+id/imageView" 
 android:src="@drawable/fashion" 
 android:layout_centerVertical="true" 
 android:layout_centerHorizontal="true" />
 See this image for example 
 fashion is image name
Step 2:-click app---------res---------layout------------right click on layout-----------new-----------activity------------select blank_activity. and write different name and different package name see this image for example


Step 3:- Go to MainActivity.java and write these lines
 private static ImageView img;
click();
public void click()
{
    img = (ImageView)findViewById(R.id.imageView);
    img.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) {
 Intent intent = new Intent("fgedfydfcgu.imagesethh.Main2Activity");
     startActivity(intent);
    }
    });
}




then your Main Activity show see it example 
 package karantips.imageset;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private static ImageView img;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click();
    }
    public void click()
    {
        img = (ImageView)findViewById(R.id.imageView);
        img.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
   Intent intent = new Intent("image.karantips.com.Main2Activity");
                startActivity(intent);
            }
        });
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
 // Handle action bar item clicks here. The action bar will 
 // automatically handle clicks on the Home/Up button, so long 
 // as you specify a parent activity in AndroidManifest.xml. 
 int id = item.getItemId();

        //noinspection SimplifiableIfStatement 
 if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Note:- Yellow color is your secondactivity package name you see 
in your second
content.xml 
Step 4:-Go to androidmanifest.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="fgedfydfcgu.imageset" >

    <application 
 android:allowBackup="true" 
 android:icon="@mipmap/ic_launcher" 
 android:label="@string/app_name" 
 android:supportsRtl="true" 
 android:theme="@style/AppTheme" >
        <activity 
        android:name=".MainActivity" 
        android:label="@string/app_name" 
         android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
           <action android:name="android.intent.action.MAIN" />

           <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
 android:name="fgedfydfcgu.imagesethh.Main2Activity" 
 android:label="@string/title_activity_main2" 
 android:theme="@style/AppTheme.NoActionBar" >
      <intent-filter>
     <action android:name="fgedfydfcgu.imagesethh.Main2Activity" />

  <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
        </activity>
    </application>

</manifest>
Your project is complete then this output is show
You click this image then open your new Activity

You Like my Post Please share with friends.

You see my other post please click this link
 
 


No comments:

Post a Comment