2

I'm making an app that will allow the user to add a photo to an imageview from the gallery of their phone which I have already been able to code successfully. I now want to be able to allow the user to add text on top of the photo and then save the text and image as one back into the gallery. Is there any way to do this?

XML:

  <LinearLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView android:id="@+id/image" android:layout_height="400dp" android:layout_width="fill_parent"/>
    <LinearLayout 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal">
        <Button 
            android:id="@+id/chooseimage" 
            android:layout_height="fill_parent" 
            android:layout_weight="1" 
            android:layout_width="fill_parent" 
            android:text="Choose Image"/>
        <Button 
            android:id="@+id/addtext" 
            android:layout_height="fill_parent" 
            android:layout_weight="1" 
            android:layout_width="fill_parent" 
            android:text="Add Text"/>
        <Button 
            android:id="@+id/save" 
            android:layout_height="fill_parent" 
            android:layout_weight="1" 
            android:layout_width="fill_parent" 
            android:text="Save"/>
    </LinearLayout>
</LinearLayout>  

Member.java

public class MemberActivity extends Activity implements OnClickListener {
    /**
     * Called when the activity is first created.
     */

    private static final int SELECT_IMAGE = 1;
    Button openGallery;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.design);
        openGallery = (Button) findViewById(R.id.chooseimage);
        openGallery.setOnClickListener(this);

    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.chooseimage:
                Intent gallery = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivityForResult(gallery, SELECT_IMAGE);
                break;
            default:
                break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == SELECT_IMAGE) {
            Uri selectedImage = data.getData();
            String path = getPath(selectedImage);
            Bitmap bitmapImage = BitmapFactory.decodeFile(path);
            ImageView image = (ImageView) findViewById(R.id.image);
            image.setImageBitmap(bitmapImage);
        }
    }

    public String getPath(Uri uri) {
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

        return cursor.getString(columnIndex);
    }
}
beresfordt
  • 5,088
  • 10
  • 35
  • 43
user1358678
  • 31
  • 1
  • 3
  • Umm i clearly said i had coded the first part but I need help understanding what to use for the next bit. I never asked for you to code anything. – user1358678 Apr 26 '12 at 12:48

4 Answers4

3

Yes there is a way to do this,

You can create an Bitmap of any view using buildDrawingCache() and getDrawingCache()

TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());

You can also check this answer for further reference.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1
zapl
  • 63,179
  • 10
  • 123
  • 154
0

You can also use TextView instead of ImageView and set image as background to TextView.And after you can also add text to TextView. Save the all the texts to somewhere and on showing again Gallery again use the texts you save with the images.

Arun Badole
  • 10,977
  • 19
  • 67
  • 96
0

try this:

 TextView tv1 = new TextView(this);
 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100);
    tv1.setLayoutParams(layoutParams);
    tv1.setText("testing 1 2 3");
    tv1.setTextColor(Color.BLACK);
    tv1.setBackgroundColor(Color.TRANSPARENT);

add to it on button click with your layout-->your layout reference and then

.addview(tv1);

Drim
  • 514
  • 5
  • 18