problem i'm getting (random order) after select images from gallery - android

after select multi images from gallery on order selection the images goes to the recycle view on random order not as I select on specific order or sort. for example if I select B then A then C but the code always bring them with random sort or order like A C B and sometimes C A B. . the order seems undefined at first I thought android sort them based on date and time added to the gallery or what their name i mean alphabet letter but then I realize the order seems undefined. i'm not sure if I need to add listener with array track the user selections. any suggestion
ArrayList imagesUriArrayList;
public static final int slect_photo = 100;
Bitmap bitmap1 = null;
RecyclerView rec;
LinearLayoutManager lnm;
Adopter_Browse list;
ArrayList<MyCard> mylist;
public void Onclick_View(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, slect_photo);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (data.getData() != null) {//data.getData() != null) {
imagesUriArrayList = null;
imagesUriArrayList = new ArrayList();
try{
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
imagesUriArrayList.add(data.getClipData().getItemAt(i).getUri());
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
try {
for (int i = 0; i < imagesUriArrayList.size(); i++) {
bitmap1 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), (Uri) imagesUriArrayList.get(i));
mylist.add(new MyCard(count_s, bitmap1));
}
} catch (IOException e) {
e.printStackTrace();
}
lnm = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
list = new Adopter_Browse(mylist);
rec.setLayoutManager(lnm);
list.notifyDataSetChanged();
rec.setAdapter(list);
}

I found out that using onActivityResult that provided by google will always return the order of your selection on miss , so maybe a bug or need improve .
I got two option :
1- using ItemTouchHelper library ... helped me to solve my problem !
2- gradle source form githup !
I share this maybe some one can get relief !

Related

Picking a number from a contact but removing the country code

I am able to get the contact phone number on button click then transfer it to an EditText, but I want only to get the result without the country code. Exemple : +33 xx xx xx xxx becomes xx xx xx xxx
Is there a way I can achieve this?
The following code I use to get the number and put it in an EditText:
Button click:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == 1) && (resultCode == RESULT_OK)) {
Cursor cursor = null;
try {
Uri uri = data.getData();
cursor = getContentResolver().query(uri, new String[] { CommonDataKinds.Phone.NUMBER }, null, null, null);
if (cursor != null && cursor.moveToNext()) {
String phone = cursor.getString(0);
editText.setText(phone);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Use the libphonenumber library from Google. Don't try to create your own logic, as format varies from location to location.
Try to use replaceall. For example:
String phone = cursor.getString(0);
phone = phone.replaceAll("+33", "");
editText.setText(phone);

Call Image from Gallery/ImageButton to Canvas (DrawingView)

can anyone help me to solve this problem, how to call image from gallery/imagebutton to canvas for drawing?
this is my snippet code :
buah1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), BelajarMewarnai.class);
startActivity(intent);
}
});
If anyone need a project I will send my project to email.
Actually i wanted to post the link in the comment for u but u seemed to bit less fimiliar with android(i thinking so), So i am posting my code to do that. First choose on which button click u want the user to be guided to gallery(only default gallery and not anything else, u may get a null pointer if u choose pic from any other).
On that button click u do this:
private void onClickOfButton(View v){
Intent galleryIntent=new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(Intent.createChooser(galleryIntent, "pic-select"), 10);//(10 its just a request code, u can give ur own, but same should there at receiving end )
}
Since u have started an activity for result, so u should be awaiting for a result from that started activity, so that activity will bring u the image and in ur activity u just collect it and put it into ur image view like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==10 && resultCode==Activity.RESULT_OK){
try{
Uri selectImageUri=data.getData();
String selectedImagePath=getPath(selectImageUri);
Bitmap pic=BitmapFactory.decodeFile(selectedImagePath);
if(pic!=null){
yourImageView.setImageBitmap(pic);
}
}catch(NullPointerException ex){
Toast.makeText(getActivity().getBaseContext(), "Go to default gallery area and choose a pic", Toast.LENGTH_LONG).show();
}
}
else
super.onActivityResult(requestCode, resultCode, data);
}
The getPath method:
private String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
So this will do ur job...
try this:
buah1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), BelajarMewarnai.class);
Bundle bundle = new Bundle();
//Get the image name you clicked on
String imageName = getResources().getResourceName(imageId);;
//Send the image name as a string, which you want to load in the other activity
bundle.putString("image_to_load", imageName.split("/")[1].toString());
intent.putExtras(bundle);
startActivity(intent);
}
});
then you can get your image name in the next activity in onCreate like this:
//Here you can get the image name you already clicked in the previous activity to load in your canvas
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String image_name = bundle.getString("image_to_load")
}
then you can set the image name to your imageView like this:
//Here you can get all resources in res folder
Resources res = getResources();
//define a new string which takes the image name you get from extras in the above code
String mDrawableName = image_name;
//Now you have the image name you clicked in theprevious activity, and ResID is to get the resource id from its name
int resID = res.getIdentifier(mDrawableName, "drawable", getPackageName());
//Here you can get the drawable from the resourceID you obtained in the above line
Drawable drawable = res.getDrawable(resID );
YOUR_IMAGE is the ImageView of the canvas you want to load the image inside in order to draw or whatever you want to do
drawView.setBackgroundDrawable(drawable);
I hope this will help..

ListView Items get reset onActivityResult from 2 different activities

I have a list view which contains a TextView for each list item. I need to take a picture onItemClick of the listView. onActivityResult of the ACTION_IMAGE_CAPTURE intent and updating the listview , I start another activity for result.
The problem I am facing is that all the textviews in my list view are getting reset when I come back to the activity onActivityResult from the second activity. Can you please help me with this.
This is my onItemClick
public void onItemClick(AdapterView<?> arg0, View v, int index, long arg3) {
selectedIndex = index; //selectedIndex is a class level variable
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = createImageFile();
if (f != null) {
imageFileName = f.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
}
startActivityForResult(takePictureIntent, 1);
}
}
This is my onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
View v = listView.getChildAt(selectedIndex
- listView.getFirstVisiblePosition());
TextView textView = (TextView) v.findViewById(R.id.textView1);
int quantity = Integer
.parseInt(textView.getText().toString().trim().length() > 0 ? quantityTV
.getText().toString() : getString(R.string._0));
quantity++;
textView.setText(String.valueOf(quantity));
listViewAdapter.notifyDataSetChanged();
Intent intent = new Intent();
intent.setClass(getApplicationContext(), NotificationActivity.class);
intent.putExtra("Value1", "0");
startActivityForResult(intent, 100);
}
else if (requestCode == 100) {
// do nothing
}
}
While you're updating the content of the text view here, you're not updating the data that backs the adapter for your list view. This means when your activity comes back into view (after the second startActivityForResult) it's redrawing itself with the old data.
Instead of updating the view directly, you should update the data that backs the adapter. Something like this; you'll have to modify it to suit your code.
if (requestCode == 1 && resultCode == RESULT_OK) {
List<Integer> adapterData = listViewAdapter.getQuantities();
int quantity = adapterData.get(selectedIndex) + 1;
adapterData.set(selectedIndex, quantity);
listViewAdapter.setQuantities(adapterData);
Intent intent = new Intent();
intent.setClass(getApplicationContext(), NotificationActivity.class);
intent.putExtra("Value1", "0");
startActivityForResult(intent, 100);
}
And in your adapter, you'd have something like this:
public List<Integer> getQuantities() {
return mQuantities;
}
public void setQuantities(List<Integer> quantities) {
mQuantities = quantities;
notifyDataSetChanged();
}

Android - Passing database and id through activities

I have 2 android activity's
One where I have a room list, i.e. list of rooms.
The other the room contents. Where all the content of a selected room is displayed.
All I want is for the room id(r_id) to be passed through the room list class into the room overview class where I can retreive all its contents from the database.
I've been trying to figure out where i'm going wrong, I think its around line 94 - 101:
selectedRoom = (String) ((TextView) view).getText();
Cursor c2 = sampleDB.rawQuery("SELECT * FROM tbl_roomDesc WHERE roomName =?", new String[] {selectedRoom});
if (c2 != null ) {
if (c2.moveToFirst()) {
do {
r_id = c2.getString(c2.getColumnIndex("r_id"));
}while (c2.moveToNext());
}
}
c2.close();
Can anyone help me figure out what i'm doing wrong here?
Here is the source for the room list
Here is the source for the room overview
Thanks in advance!
To pass data between to Intents, use the putExtras() method.
Example:
Intent i = new Intent(...);
i.putExtras("roomid", "10");
startActivity(i);
final int roomId = r_id;
next = (ImageButton) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
CreateBuilding.val = "Retrieve";
sampleDB.close();
Intent intent2 = new Intent(RoomList.this, TabLayoutActivity.class);
intent2.putExtra("roomId", roomId);
setResult(RESULT_OK, intent2);
finish();
startActivityForResult(intent2, 0);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
int roomId = data. getIntExtra("roomId", 0);
}
}
}
If I have not misunderstood you put the roomId inside the Intent and retrieve it inside the onActivityResult

Encode in QRCode using ZXING

I'm trying to encode a String in QR Code wusing ZXING library. this is the lines of code corresponding to this :
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("ENCODE_TYPE", "TEXT_TYPE");
intent.putExtra("ENCODE_DATA","HELLO WORLD");
startActivityForResult(intent, 0);
}
});
}
After clicking on the button i have a "force close"
After looking in some websites, we say that it works just with these lines. unfortunately, it isnt for me.
PLEASE Can you give some advices to make it working. OR if you have other way to integrate a QRCode generator to my ANDROID App it will be great too.
Enzo, this is another way to get it working, try this:
private void encode(String uniqueID) {
// TODO Auto-generated method stub
BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
int width0 = 500;
int height0 = 500;
int colorBack = 0xFF000000;
int colorFront = 0xFFFFFFFF;
QRCodeWriter writer = new QRCodeWriter();
try
{
EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = writer.encode(uniqueID, barcodeFormat, width0, height0, hint);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++)
{
int offset = y * width;
for (int x = 0; x < width; x++)
{
pixels[offset + x] = bitMatrix.get(x, y) ? colorBack : colorFront;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
ImageView imageview = (ImageView)findViewById(R.id.qrCode);
imageview.setImageBitmap(bitmap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This line:
Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
identifies the Activity you are calling with the Intent. In order for it to work that Activity must exist on the device. If you haven't implemented the Activity inside your project (that is, there is no class ENCODE inside your com.google.zxing.client.android package) then you will be calling an external application from yours. If there are no applications on the device/emulator that respond to the broadcast for com.google.zxing.client.android.ENCODE then you are not going to get anywhere with this solution.
You either need to install an application that will respond to com.google.zxing.client.android.ENCODE or find another way to do it.
It is possible to generate the barcode yourself using zxing libraries within your application. Have a look at the project here at Google Code for some downloads. This will remove your dependancy on an external application existing, providing a more thorough solution.
Rather than Zxing library you are also able to get the string from QRCode by using intents as below :
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for bar codes
startActivityForResult(intent, 0);
} catch (Exception e) {
Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW,marketUri);
startActivity(marketIntent);
}
And on activity result as below :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
Log.v("MESSAGE: ", contents);
Intent in = new Intent(MainActivity2.this,MainActivity3.class);
Bundle b3= new Bundle();
b3.putString("content",contents);
in.putExtras(b3);
startActivity(in);
}
}
}
It may be helpful for you.
Thank You.

Categories

Resources