Android - How to generate onClick code for ScrollView items? - android

I have a scrollable layout that shows images vertically :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".chatActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp">
<LinearLayout
android:id="#+id/imageGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</RelativeLayout>
This is how I generate the view in my code :
LinearLayout imageGallery;
File[] fList = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
imageGallery = (LinearLayout) findViewById(R.id.imageGallery);
addImagesToTheGallery();
}
private void addImagesToTheGallery() {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
fList = path.listFiles();
if(fList!=null)
{
int len = fList.length;
for(int i=0; i<len; i++)
{
imageGallery.addView(getImageView(i));
}
}
}
private View getImageView(int image) {
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
imageView.setImageBitmap(bitmap);
return imageView;
}
Now, I want to add an onClick event for each of those images. When someone clicks on an image, I want to display some message specific to that image. So I should either be able to get the image or its position. How can I do that?

Set "imageView" onCLickListener.
private View getImageView(int image) {
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
imageView.setImageBitmap(bitmap);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO do something
Toast.makeText(this, "item num" +
image, Toast.LENGTH_LONG).show();
}
});
return imageView;
}

Related

add a imageView below existing textview PROGRAMMATICALLY

How to add view programmatically to existing view, for example I have textview in XML and now I need to add imageview below that textview programmatically, here is what I tried,
HorizontalScrollView scroll = new HorizontalScrollView(getApplicationContext());
LinearLayout imageLayout = new LinearLayout(getApplicationContext());
scroll.setLayoutParams(new ViewGroup.LayoutParams(imageLayout.getWidth(),
imageLayout.getHeight()));
scroll.addView(imageLayout);
for (int i = 0; i < 15; i++) {
final ImageView imageView = new ImageView(this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.logo);
imageLayout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Tag", "" + imageView.getTag());
}
});
}but its not adding any imageview, I'm trying to do this in **AlertDialog**
ScrollView cannot have more than one child. You should wrap your layout inside of a LinearLayout and then after you can add any number of views into that LinearLayout. cheers :)
You are doing with wrong way.
Once check with below code snippet.
LinearLayout yourlayout = (LinearLayout)findViewById(R.id.layoutid);//Layout which is inside scrollview
LinearLayout imageLayout = new LinearLayout(getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your LinearLayout below your TextView object
params.addRule(RelativeLayout.BELOW, TextView.getId());
// set the layoutParams on the LinearLayout
imageLayout.setLayoutParams(params);
yourlayout.addView(imageLayout);
for (int i = 0; i < 15; i++) {
final ImageView imageView = new ImageView(this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.logo);
imageLayout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Tag", "" + imageView.getTag());
}
});
}
I solved it by adding
<HorizontalScrollView
android:id="#+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
in XML
&
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
for (int i = 0; i < 10; i++) {
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
imageView.setImageBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher));
imageView.setScaleType(ScaleType.FIT_XY);
layout.addView(imageView);
}
in MainActivity.java
ImageView imageView = new ImageView(this.getActivity());
imageView.setId(View.generateViewId());
params.addRule(RelativeLayout.BELOW, imageView.getId());

Resizing images into LinearLayout and HorizontalScrollView

how can i set the images which are in the HorizontalScrollView
As you can see the images are so much wide, i need them to be of normal size.
This is my code for the HorizontalScrollView xml file:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="5dp"
android:id="#+id/hscrollview"
android:layout_weight="0.2">
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
This is what i have tried programmatically.
private ImageView getImageView(final Integer image, final int index) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 0);
imageView.setLayoutParams(lp);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(index);
}
});
imageView.setImageResource(image);
return imageView;
}
Change
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
to
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

GridView Dynamically 2:3 layout data set

I am using one one layout and include 5 times on other layout. but i want to user Grid View instance of incluide layout. Here I am sharing Screen shot. please help me to make this layout in android
This is Flipboard layout. and second one is my layout.
GridView does not support this. One has to use a custom linearlayout and add views to it dynamically by setting the layout parameters.
Use the class as layout back bone
public class CompositeView extends LinearLayout {
private ImageView imageView;
private TextView textView;
public CompositeView(Context context) {
super(context);
imageView = new ImageView(context);
imageView.setClickable(true);
imageView.setPadding(3, 3, 3, 3);
LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
imageView.setScaleType(ScaleType.FIT_XY);
textView = new TextView(context);
textView.setTextColor(getResources().getColor(android.R.color.white));
textView.setClickable(true);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
textView.setSingleLine(true);
textView.setEllipsize(TruncateAt.END);
textView.setPadding(3, 3, 3, 3);
textView.setBackgroundResource(R.drawable.strip_bg);
this.setGravity(Gravity.CENTER);
// this.setClickable(true);
this.addView(imageView);
this.addView(textView);
}
public void setText(int resid) {
textView.setText(resid);
}
public void setText(CharSequence text) {
textView.setText(text);
}
public void setIcon(int resid) {
imageView.setImageDrawable(getResources().getDrawable(resid));
}
public ImageView getImageView() {
return imageView;
}
public TextView getTextView() {
return textView;
}
}
In activity or fragment
names = new String[] { "Cloud Bingo", "New Bingo Games", "Politricks",
"Seam Group", "Regular Cloud Bingo" };
urls = new String[] { "http://www.cloudbingo.co.uk/",
"http://www.newbingogames.co.uk/",
"http://www.politricks.co.in", "http://veenagupta.in/",
"http://www.regularbingo.co.uk/" };
for (int i = 0; i < images.length; i++) {
view = new CompositeView(getActivity());
view.setText(names[i]);
// view.setOnClickListener(this);
view.setOrientation(LinearLayout.VERTICAL);
view.setIcon(images[i]);
view.setId(i); // id of whole linear view
view.getTextView().setGravity(Gravity.CENTER);
view.getImageView().setId(i); // id of just this imageview
view.getTextView().setId(i);
}
Use following logic for your purpose
for (int i = 0; i < images.length; i++) {
flag++;
if (flag % 2 == 0) {
// number is even
mainLayout.addView(compositeList.get(i));
}
else {
Log.v("in", "else");
secondaryLayout = new LinearLayout(getActivity());
LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 15, 0, 15);
secondaryLayout.setLayoutParams(params);
secondaryLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout leftLayout = new LinearLayout(getActivity());
leftLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
leftLayout.addView(compositeList.get(i));
secondaryLayout.addView(leftLayout);
i++;
if (i < images.length) {
LinearLayout rightLayout = new LinearLayout(getActivity());
LayoutParams rParams = new LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
rParams.setMargins(10, 0, 0, 0);
rightLayout.setLayoutParams(rParams);
rightLayout.addView(compositeList.get(i));
secondaryLayout.addView(rightLayout);
}
else {
LinearLayout rightLayout = new LinearLayout(getActivity());
LayoutParams rParams = new LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
rParams.setMargins(10, 0, 0, 0);
rightLayout.setLayoutParams(rParams);
secondaryLayout.addView(rightLayout);
}
mainLayout.addView(secondaryLayout);
}
}
flag is equal to -1 initially , I show one image (with text at bottom) for even rows, two images for odd rows
Here is the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="5"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<LinearLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

Zooming an image of a ScrollView

I´m trying to include zooming in a Horizontal ScrollView. The idea is that the user touches the screen in the image he wants to highlight and that image get a little bit bigger than the others. I have make the Horizontall ScrollView but I´m having problems with the OnClick event. What is the better way to do it?
This is what I have in the MainActivity.java
public class Carrusel extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.iniciar);
{int[] images = new int[] { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4,
R.drawable.image5 };
LinearLayout sv = (LinearLayout) findViewById (R.id.carrusel);
for (int i=0 ; i<5; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (images[i]);
sv.addView(iv);
}
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.drawable.image1:
//I don´t know what to put here
break;
}
}
}
And here is what I have in te main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/black" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<LinearLayout
android:id="#+id/carrusel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Here's how I'd do it:
MainActivity.java :
public class MainActivity extends Activity implements OnClickListener {
ImageView lastClicked = null;
int width = 400;
int height = 320;
int padding = 30;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout l;
l = (LinearLayout) findViewById(R.id.linear);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
width, height);
int[] images = new int[] { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5 };
for (int i = 0; i < 5; i++) {
ImageView iv = new ImageView(this);
iv.setLayoutParams(layoutParams);
iv.setImageResource(images[i]);
iv.setPadding(padding, padding, padding, padding);
iv.setOnClickListener(this);
l.addView(iv);
}
}
#Override
public void onClick(View v) {
if (v instanceof ImageView) {
if (lastClicked != null) {
lastClicked.setPadding(padding, padding, padding, padding);
lastClicked.invalidate();
}
v.setPadding(0, 0, 0, 0);
v.invalidate();
lastClicked = (ImageView) v;
}
}
}

How can I add images to a LinearLayout programmatically?

There is a LinearLayout and some Buttons in my project. I want when each Button clicked add a small image to LinearLayout from drawable. How can I add a image to a LinearLayout programmatically?
This is my LinearLayout in a HorizontalScrollView:
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
and here is my activity:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout notes = (LinearLayout) findViewById(R.id.LinearLayout_notes);
final ImageView notes_do = new ImageView(this);
notes_do.setBackgroundResource(R.drawable.notes_do);
new Thread(new Runnable() {
#Override
public void run() {
final ImageButton img_1 = (ImageButton) findViewById(R.id.img_1_);
img_1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if( event.getAction() == MotionEvent.ACTION_DOWN ) {
notes.addView(notes_do);
}
return true;
}
}); // end of ontouch
}
}).start(); // end of thread
} // end of oncreate
}// end of activity
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout1);
for(int i=0;i<5;i++)
{
ImageView ii= new ImageView(this);
ii.setBackgroundResource(R.drawable.ic_action_search);
ll.addView(ii);
}
You can do a findViewById to the linearlayout and add an imageview to it dynamically in the onclicklistener of button
LinearLayout linLay = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = new ImageView(this);
//add image to imageview
...
linLay.addView(image);
ImageView img = new ImageView( this);
LinearLayout rl = (LinearLayout) findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams viewParamsCenter = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
img.setImageResource(R.drawable.ic_launcher);
img.setLayoutParams(viewParamsCenter);
rl.add(img);
As you want to set the image dynamically, on the button's click. you can add the image view to the linear layout
View LinearLayout1 = findViewById(R.id.Layout1);
ImageView image1 = new ImageView(getApplicationContext());
String uri = "#drawable/myresource.png"; // Here you can set the name of
// the image dynamically
int imageResource = getResources().getIdentifier(uri, null,
getPackageName());
Drawable res = getResources().getDrawable(imageResource);
image1.setImageDrawable(res);
((ViewGroup) LinearLayout1).addView(image1);

Categories

Resources