I have Horizontalscrollview and i added Imageviews dynamically but onLongPress I want checkboxes to come on each image inside scroll view at the top right corner of each image,and delete Image views that are checked.
And,also the new imageview created is shown last inside scrollview.How to show the latest image view created first inside HorizontalScrollView.
My Output and I want something like this
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:orientation="vertical"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<ImageView
android:id="@+id/bigimageview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="0.40"
android:cropToPadding="false"
android:soundEffectsEnabled="false"
android:scaleType="fitXY"
android:layout_marginTop="20dp" />
<HorizontalScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFFFFF"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none"
>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/insidescroll">
<Button
android:id="@+id/cApartment"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="15dp"
android:layout_gravity="center"
android:textColor="#EEEEBB"
android:text="Add Image"
android:textSize="8dp"/>
<View
android:id="@+id/horizontalview"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#EEEEEE" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
MainActivity:
package com.example.asad.sample;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.ViewGroup.LayoutParams;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import static com.example.asad.sample.R.drawable.img1;
public class MainActivity extends AppCompatActivity {
private int i=1,j=1;
private static int RESULT_LOAD_IMAGE = 1;
private LinearLayout linearLayout2;
private String[] arr = new String[10];
int[] ids=new int[15];
private int k=1;
private int g=1;
private View view;
private ImageView img1;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
Button CApartment = (Button) findViewById(R.id.cApartment);
img1 = (ImageView) findViewById(R.id.bigimageview);
view=(View)findViewById(R.id.horizontalview);
linearLayout2 = (LinearLayout) findViewById(R.id.insidescroll);
CApartment.setOnClickListener((new View.OnClickListener() {
public void onClick(View v) {
//Calling Validate Function
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
}
));
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
// Now we need to set the GUI ImageView data with data read from the picked file.
setimage(imagePath);
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
}
public void setimage(String imagePath)
{
final ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
// imageView.setMaxHeight(100);
// imageView.setMaxWidth(60);
imageView.setLayoutParams(new LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
ids[i] = g;
imageView.setId(ids[i]);
arr[j]=imageView.getDrawable().toString();
linearLayout2.addView(imageView);
img1.setScaleType(ImageView.ScaleType.CENTER_CROP);
img1.setImageDrawable(imageView.getDrawable());
// Toast.makeText(MainActivity.this,"string"+ ids[i],Toast.LENGTH_SHORT).show();
k=i;
i++;
j++;
g++;
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int l=1 ; l<=k ;l++) {
if (v.getId() == ids[l]) {
ImageView imag = (ImageView) findViewById(ids[l]);
imag.getDrawable();
// Toast.makeText(MainActivity.this, imag.getDrawable().toString(), Toast.LENGTH_SHORT).show();
if (imag.getDrawable().toString() == (arr[l])) ;
{
// Toast.makeText(MainActivity.this, "string" + arr[l], Toast.LENGTH_SHORT).show();
// Toast.makeText(MainActivity.this, "string" + arr[l], Toast.LENGTH_SHORT).show();
img1.setImageDrawable(imag.getDrawable());
// Toast.makeText(MainActivity.this, "ids" + ids[l], Toast.LENGTH_SHORT).show();
// Toast.makeText(MainActivity.this, "string" + arr[l], Toast.LENGTH_SHORT).show();
}
}
}
}
});
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//your stuff
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
return true;
}
});
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}