Android : how to pass the default selected thing in to a new Activity

Viewed 124

In my Activity i have a EditText, ImageView and Button.I place a ImageView inside the GridView, When My Activity was loaded.

The Gridview First Image was Selected Default and i want to Enter the EditText name and pass both the EditText value and the Default selected image( user can also Choose his Own Image if he dont like the default image ) to the New Activity

My Problem is User Enter the edittext value and he press the Button it pass only the EditText value not the default Selected Image, If user Choose the other Images in the gridview it pass both the ImageView and the EditText value also if he Choose the default selected image manually then it works Fine .

how to pass the default marked image without selecting again how to pass to another Activity [![enter image description here][1]][1]

My Code :

int[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
    int imageRes;

    public NewListCreate() {
    }
         public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        
                 View view = inflater.inflate(R.layout.new_list_create, container, false);
        
                ImageButton done = view.findViewById(R.id.done);
                final EditText listname = (EditText) view.findViewById(R.id.listname);
                final GridView gridView = (GridView) view.findViewById(R.id.gridview);
        
                final CustomAdpter customAdpter = new CustomAdpter(images,getContext());
                gridView.setAdapter(customAdpter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        customAdpter.selectedImage = i;
                        customAdpter.notifyDataSetChanged();
                        imageRes = images[i];
       
                    }
                });
     done.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    
                    String itemname = listname.getText().toString();
                    if (!TextUtils.isEmpty(listname.getText().toString())) {
    
                        startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image",imageRes));
                        dismiss();
                    } else {
                        Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
                    }
    
                }
    
           });
    
            
            return view;

CustomAdapter

 private int[] icons;
        private Context context;
        private LayoutInflater layoutInflater;
        public int selectedImage = 0;

        public CustomAdpter(int[] icons, Context context) {
            this.icons = icons;
            this.context = context;
            this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
    public int getCount() {
        return icons.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

     public int selectedImage = 0;
        @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
        
                    if (view == null)
                    {
                       view =  layoutInflater .inflate(R.layout.image_list,viewGroup,false);
        
                    }
        
                    ImageView imageicons = view.findViewById(R.id.image);
                if (i < icons.length) {
        
                    imageicons.setImageResource(icons[i]);
        
                    if (i != selectedImage) {
                       imageicons.setImageAlpha(50);
                    }
                    imageicons.setScaleType(ImageView.ScaleType.CENTER_CROP);
                   // imageicons.setLayoutParams(new GridView.LayoutParams(150, 150));
                    if (i ==  selectedImage) {
        
                        view.setBackgroundColor(Color.WHITE);
                    } else {
                        view.setBackgroundColor(Color.TRANSPARENT);
                    }
                };
        
        
                return view;
1 Answers

try this code :

activity_frist.xml

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout 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="match_parent"
    tools:context=".FirstActivity">

    <ImageView
        android:id="@+id/img_one"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="?selectableItemBackground"
        android:scaleType="fitXY"
        android:src="@drawable/img_natural" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img_one"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal"
        android:text="Go to second Activity"
        android:textSize="20dp" />
</RelativeLayout>

FirstActivity.java

public class FirstActivity extends AppCompatActivity {
    ImageView img_one;


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

        img_one = (ImageView) findViewById(R.id.img_one);

        img_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BitmapDrawable drawable = (BitmapDrawable) img_one.getDrawable();
                Bitmap bitmap = drawable.getBitmap();
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                intent.putExtra("BitmapImage", bitmap);
                startActivity(intent);
            }
        });

    }

}

activity_second.xml

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout 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="match_parent"
    tools:context=".FirstActivity">


    <ImageView
        android:id="@+id/img_two"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="?selectableItemBackground"
        android:scaleType="fitXY" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img_two"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="Second Activity"
        android:textSize="20dp" />
</RelativeLayout>

SecondActivity.java

public class SecondActivity extends AppCompatActivity {

    ImageView img_two;


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

        img_two = (ImageView) findViewById(R.id.img_two);

        Intent intent = getIntent();
        Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
        img_two.setImageBitmap(bitmap);


    }
}
Related