1

In this activity I have an implementaion with AppCompatCallback and a delegate who set my toolbar , I have this thing for other 2 activitys and works fine , but here i get an error to the line delegate1.setSupportActionBar(toolbar).. I don't understand why...

import android.app.Activity;
import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
   import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
   import android.net.Uri;
 import android.os.Bundle;
     import android.os.Environment;
  import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatCallback;
 import android.support.v7.app.AppCompatDelegate;
 import android.support.v7.view.ActionMode;
  import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
  import android.view.View;
import android.widget.AdapterView;
 import android.widget.Button;
  import android.widget.EditText;
  import android.widget.GridView;
     import android.widget.TextView;
  import android.widget.Toast;

 import java.io.File;
  import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
   import java.util.ArrayList;


 public class AlbumActivity extends Activity implements AppCompatCallback {


private final int REQUEST_CODE_CAMERA_IMAGE = 1000;
private final int REQUEST_CODE_EXTERNAL_IMAGE = 2000;
private AppCompatDelegate delegate1;
String nameAlbum;
// Declare variables
private String[] FilePathStrings;
private String[] FileNameStrings;
private File[] listFile;
GridView grid;
GridViewAdapter adapter;
File file;
boolean deleted;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    delegate1 = AppCompatDelegate.create(this, this);

    //call the onCreate() of the AppCompatDelegate
    delegate1.onCreate(savedInstanceState);

    //use the delegate to inflate the layout
    delegate1.setContentView(R.layout.album_activity);


    Toolbar toolbar = (Toolbar) findViewById(R.id.mytoolbarr);

    delegate1.setSupportActionBar(toolbar);
    delegate1.setTitle("Your Pictures");

    Button btnChoosePicture = (Button) findViewById(R.id.addimage);
    Intent intent = getIntent();
    nameAlbum = intent.getStringExtra("nameAlbum");
    // Check for SD Card
    if (!Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED)) {
        Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG)
                .show();
    } else {
        // Locate the image folder in your SD Card
        file = new File(Environment.getExternalStorageDirectory()
                + File.separator + nameAlbum);
        if (file.isDirectory()) {
            listFile = file.listFiles();
            // Create a String array for FilePathStrings
            FilePathStrings = new String[listFile.length];
            // Create a String array for FileNameStrings
            FileNameStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++) {
                // Get the path of the image file
                FilePathStrings[i] = listFile[i].getAbsolutePath();
                // Get the name image file
                FileNameStrings[i] = listFile[i].getName();
            }
        }

        // Locate the GridView in gridview_main.xml
        grid = (GridView) findViewById(R.id.gridview);
        // Pass String arrays to LazyAdapter Class
        adapter = new GridViewAdapter(this, FilePathStrings, FileNameStrings);
        // Set the LazyAdapter to the GridView
        grid.setAdapter(adapter);

        // Capture gridview item click
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                Intent i = new Intent(AlbumActivity.this, ViewImage.class);
                // Pass String arrays FilePathStrings
                i.putExtra("filepath", FilePathStrings);
                // Pass String arrays FileNameStrings
                i.putExtra("filename", FileNameStrings);
                // Pass click position
                i.putExtra("position", position);
                startActivity(i);
            }

        });
        grid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, final long id) {

                AlertDialog.Builder builder = new AlertDialog.Builder(AlbumActivity.this);


                builder.setCancelable(true);
                builder.setMessage("Are you sure you want to delete this picture ?");
                builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }

                });
                builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        File filepath = Environment.getExternalStorageDirectory();
                        File dir5 = new File(filepath.getAbsolutePath()
                                + nameAlbum+FileNameStrings[position]);

                        File file3 = new File(String.valueOf(dir5));
                       deleted = file3.delete();
adapter.notifyDataSetChanged();
                        finish();
                        startActivity(getIntent());

                        dialog.dismiss();



                    }
                });

                builder.setTitle("Delete Picture");
                AlertDialog dialog = builder.create();
                dialog.show();
                return true;
            }
        });

            }


//select picture from external storage
        btnChoosePicture.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // choose picture from gallery
                Intent PhotoPickerIntent = new Intent(
                        Intent.ACTION_PICK);
                File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                String pictureDirectoryPath = pictureDirectory.getPath();
                Uri data = Uri.parse(pictureDirectoryPath);
                PhotoPickerIntent.setDataAndType(data, "image/*");


                startActivityForResult(PhotoPickerIntent,
                        REQUEST_CODE_EXTERNAL_IMAGE);


            }
        });
    }
@Override

3 Answers3

0

but here i get an error to the line delegate1.setSupportActionBar(toolbar)

It can be two differents things :

You've imported android.widget.Toolbar instead of android.support.v7.widget.Toolbar

Your Activity has to extends AppCompatActivity if you want to use setSupportActionBar(toolbar)

If you extends AppCompatActivity you have to use Theme.AppCompat.Light.NoActionBar

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

@Skizo here everything works fine and extends Activity...

public class EnterDataActivity extends Activity implements AppCompatCallback {
private AppCompatDelegate delegate;
EditText editTextPersonName;


/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    delegate = AppCompatDelegate.create(this, this);

    //call the onCreate() of the AppCompatDelegate
    delegate.onCreate(savedInstanceState);

    //use the delegate to inflate the layout
    delegate.setContentView(R.layout.enter_data);

    editTextPersonName = (EditText) findViewById(R.id.et_person_name);
    Toolbar toolbar= (Toolbar) findViewById(R.id.mytoolbar2);

    delegate.setSupportActionBar(toolbar);
    delegate.setTitle("Enter Album Name");

}
@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;
    }else if(id==android.R.id.home){
       finish();
    }

    return super.onOptionsItemSelected(item);
}

public void onClickAdd (View btnAdd) {

    String personName = editTextPersonName.getText().toString();


    if ( personName.length() != 0 ) {

        Intent newIntent = getIntent();
        newIntent.putExtra("tag_person_name", personName);


        this.setResult(RESULT_OK, newIntent);

        finish();

    }
}

@Override
public void onSupportActionModeStarted(ActionMode mode) {

}

@Override
public void onSupportActionModeFinished(ActionMode mode) {

}

@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
    return null;
}

}

  • Let me know what you've imported please – Skizo-ozᴉʞS ツ Sep 25 '15 at 09:46
  • @Skizo , it works , but I want to know why the other activity works just fine with extends Activity.... I'm so confused –  Sep 25 '15 at 09:54
  • That's weird though.... Are you sure that in your EnterDataActivity you have imported support library? – Skizo-ozᴉʞS ツ Sep 25 '15 at 09:56
  • By the way if I helped to solve you your issue feel free to mark my answer as a correct :D – Skizo-ozᴉʞS ツ Sep 25 '15 at 09:57
  • @Skizo look at this [link](https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3) , is the same i had done to my activity , but I don't know why is not working with extends Activity , if I'll change my activity extends AppCompatActivity I have to make some changes to my project and I'am a little lazy :)) –  Sep 25 '15 at 10:08
  • What you have to change? – Skizo-ozᴉʞS ツ Sep 25 '15 at 10:11
0

I did solved my issue , I had to include my toolbar layout in my activity layout... Thank you everyone for interest in solving my issue!!