Stam
Stam

Reputation: 2490

Sending a View from a GridView to another Activity

My main layout has a GridView. The GridView contains some GridLayouts with an ImageView and a TextView inside. What I am trying to do, is when I select a GridLayout from the GridView, a new Activity opens with this specific GridLayout on its layout.

My idea is to transfer the id of the GridLayout via an Intent to the second Activity. This is the code I made so far.

//from MainActivity

public final static String USERDATA="com.example.myproject.MESSAGE";

protected void onCreate(Bundle savedInstanceState) {
.
.


 GridView gridview = (GridView) findViewById(R.id.gridView1);
adapter=new ArrayAdapter(this,R.layout.windows_button, R.id.textView1,names);
          gridview.setAdapter(adapter);     
    .
     gridview.setOnItemClickListener(new OnItemClickListener() {


    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                    int viewId=v.getId();
                    sendToLogin(viewId);
                }


            });
}

public void sendToLogin(int viewId){

     Intent intent = new Intent(this,LoginActivity.class);
     intent.putExtra(USERDATA, viewId);
     startActivity(intent);
}


//from second activity

View userdata;//The View that will receive the GridLayout
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);


        //recieving the intent and the information from the gridview list
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.USERDATA);
        userdata=(View)findViewById(R.id.loginView);


    }
  1. How could I do this?
  2. How can I isolate another View from the GridLayout?(for example the textView inside the GridLayout).

Upvotes: 0

Views: 1506

Answers (1)

murala
murala

Reputation: 29

did you try this

Intent i = new Intent(this, SecondActivity.class);
Bitmap b = img.getDrawingCache();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("myImage", bs.toByteArray());
startActivity(i);

In next Activity wrote like

if(getIntent().hasExtra("myImage")) {
    ImageView image = new ImageView(this);
    Bitmap b = BitmapFactory.decodeByteArray(
        getIntent().getByteArrayExtra("myImage"),0,getIntent().getByteArrayExtra("myImage").length);        
    image.setImageBitmap(b);
}

first check below link to api functionality

link

Another approach 1)

first add listener like below
selection = (TextView) findViewById(R.id.selection);

        GridView gv = (GridView) findViewById(R.id.grid);

        ArrayAdapter<String> aa = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1, 
                items );

        gv.setAdapter(aa);
        gv.setOnItemClickListener(this);

2) in item click

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        selection.setText(items[position]);
    }

Upvotes: 1

Related Questions