How can i set the same image resource for a series of imagebutton using a for loop ?
ImageButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14,
b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27,
b28, b29, b30;
public void setresource() {
for (int i = 0 ; i <= 15; i++){
b[i].setImageResource(R.drawable.playzz);
}
}
The above code give error on b[i]
The type of the expression must be an array type but it resolved to int
Try the below. You have not initialized ImageButton
ImageButton b[];
In your onnCreate(param)
b = new ImageButton[15];
for (int i = 0 ; i <15; i++){
b[i] = new ImageButton(ActiivtyName.this);
b[i].setImageResource(R.drawable.playzz);
}
Also remember to add the button to you root view of the layout.
Example:
You can also set the layout programatically. You can use use linear layout or relative layout set the layout parameters. Add the Imagebuttons to the layout and set the content to the activity.
Modify the below according to your requirements.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ll"
tools:context=".MainActivity" >
</LinearLayout>
</ScrollView>
MainActivity.java
public class MainActivity extends Activity {
ImageButton b[];
LinearLayout ll;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.ll);// initialize linearlayout
setresource();
}
public void setresource()
{
b = new ImageButton[15];
for (int i = 0 ; i < 15; i++){
b[i]= new ImageButton(MainActivity.this); // initilize
b[i].setMaxWidth(40); // set the maxwidth
b[i].setImageResource(R.drawable.ic_launcher); // set background image
ll.addView(b[i]); // add the imagebutton to the linearlayout
}
}
}
Related
I have seen some explanations on how to add two views next to each other in LinearLayout by using XML. How can this be done programatically?
I have the following two methods:
private void createListOfInstalledApplications() {
installedApplicationNames = this.getApplicationNames();
installedPackages = this.getPackageNames();
for(int i = 0; i < installedApplicationNames.size(); i++){
TextView scrollAppsView = new TextView(this);
scrollAppsView.setId(i);
scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
layout.addView(scrollAppsView);
}
}
This method adds the names of installed applications on the Android phone to the LinearLayout. On the right side of these applications, I would like to have a drop down menu where some settings can be chosen. For now the following methods adds the Spinners to the LinearLayout:
private void createDropDownMenuesForApplications() {
List<String> vibPatternNames = new ArrayList<String>();
VibrationPatternManager vibPatManager = VibrationPatternManager.getInstance();
List<VibrationPattern> vibrationPatterns = vibPatManager.getAllAvailablePatterns();
for(VibrationPattern vibrationPattern : vibrationPatterns){
vibPatternNames.add(vibrationPattern.getName());
}
for(int i = 0; i < installedApplicationNames.size(); i++){
Spinner dropDownMenu = new Spinner(this);
dropDownMenu.setId(i);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, vibPatternNames);
dropDownMenu.setAdapter(dataAdapter);
layout.addView(dropDownMenu);
}
}
The drop-down menus now appear below the application names. However, I would like them to appear to the right. So
"ApplicationName1 drop-down-menu1"
instead of
ApplicationName1
ApplicationName2
Drop-down-menu1
Drop-down-menu2
How do I do this?
Just do
layout.setOrientation(Horizontal)
I have done similar task as required by you, please have a look at my answer:
https://stackoverflow.com/a/33339420/5479863
in JSON response image URLs were provided so, at runtime I have to add views dynamically adjacent to each other.
Look at the answer to know more...
I hope that will help you...
Happy coding :)
Try the following:
1) MActivity.class--------------
public class MActivity extends AppCompatActivity {
private LinearLayout layout_horizontal;
private LinearLayout layout_vertical;
private LayoutInflater layoutInflater;
private FrameLayout fl;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Frame layout generic container --- root.
// Frame layout has as a direct child layout_vertical (Linear layout with orientation vertical).
// for each item inside installedApplicationNames, re-create a linear layout with orientation horizontal (layout_horizontal),
// with a TextView and Spinner and then add it as a direct child of the linear layout with orientation vertical.
fl = (FrameLayout) findViewById(R.id.fl);
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout_vertical , null);
fl.addView(view);
layout_vertical = (LinearLayout) view.findViewById(R.id.ll_v);
populate();
}
private void populate() {
layout_vertical.removeAllViews();
List<String> installedApplicationNames = new ArrayList<String>();
for (int k = 0; k < 30; k++) {
installedApplicationNames.add("N" + k);
}
for (int i = 0; i < installedApplicationNames.size(); i++) {
View view1 = layoutInflater.inflate(R.layout.layout_horizontal , null);
layout_horizontal = (LinearLayout) view1.findViewById(R.id.ll_h);
TextView scrollAppsView = new TextView(this);
scrollAppsView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
scrollAppsView.setId(i);
scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
layout_horizontal.addView(scrollAppsView);
// TODO: use a certain method to get vibPatternNames for the current installedApplicationName
List<String> vibPatternNames = new ArrayList<String>();
for (int j = 0; j < 4; j++) {
vibPatternNames.add("S" + j);
}
Spinner dropDownMenu = new Spinner(this);
dropDownMenu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
dropDownMenu.setId(i);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, vibPatternNames);
dropDownMenu.setAdapter(dataAdapter);
layout_horizontal.addView(dropDownMenu);
layout_vertical.addView(layout_horizontal);
}
}
}
2) layout.xml-----------
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="5dp"
android:id="#+id/fl">
</FrameLayout>
</ScrollView>
3) layout_vertical.xml-----------
<?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:id="#+id/ll_v"
android:layout_gravity="center"
android:orientation="vertical">
</LinearLayout>
4) layout_horizontal.xml---------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/ll_h"
android:layout_gravity="center"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
I want to add more LinearLayout in a empty one when I press "add button".
When I press the first time "it works", the empty LinearLayout show that I want but if I press the button again don't work ( LogCat show me that occurs a NullPointerException). I see that its not create a new one LinearLayout below the previous one and no idea how solve it.
Let me show you part of my code:
First, I created
static int ID=0;
...
LinearLayout mLayout []= new LinearLayout[10];
...
OnCreate:
...
mLayout[ID] = (LinearLayout) findViewById(R.id.linearLayout_expandir);
...
Then:
private OnClickListener onClick() {
// TODO Auto-generated method stub
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout[ID].addView(createNewTextView("Contain: ", ID));
mLayout[ID].addView(createNewEditText(ID));
mLayout[ID].addView(createNewTextView("Nº Liter: ",ID));
mLayout[ID].addView(createNewEditText(ID));
ID++;
}
};
}
XML: (This LinearLayout is empty: no EditText, Buttons etc and is inside another LinearLayout)
(More code from other LinearLayouts)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.66"
android:orientation="horizontal"
android:id="#+id/linearLayout_expandir">
</LinearLayout>
(More code from other LinearLayouts)
Thank you for your time!
One question: you only have 1 LinearLayout, why do you create an array of LinearLayout while you're not adding another LinearLayout to the LinearLayout?
The error probably occurred when the ID become 1, mLayout[1] has not been initialized and you're trying to add another view to it.
LinearLayout expandir = (LinearLayout) findViewById(R.id.linearLayout_expandir);
in the onClick() method:
public void onClick(View v) {
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.addView(createNewTextView("Contain: ", ID));
linearLayout.addView(createNewEditText(ID));
linearLayout.addView(createNewTextView("Nº Liter: ",ID));
linearLayout.addView(createNewEditText(ID));
expandir.addView(linearLayout);
ID++;
}
I've created a solution for you. This activity has a button that will generate layouts for you. I use an arraylist instead of an array but the idea is the same.
This is a picture of many nested layouts that have been created using this demo.
Once you get this code in your project, you will just have to add the TextView/Edit Text code that you mentioned. Let me know if you have any questions. :)
public class LinearLayoutActivity extends Activity {
/*This linear layout is the first container*/
LinearLayout outerMostLayout;
/*This array holds the linear layouts that you will create dynamically*/
List<LinearLayout> subLayouts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_layout);
// Initialize your views
outerMostLayout = (LinearLayout) findViewById(R.id.container_layout);
subLayouts = new ArrayList<LinearLayout>();
Button button = (Button) findViewById(R.id.button);
// Set the button onclick
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get a reference to the container layout
LinearLayout container = outerMostLayout;
if (!subLayouts.isEmpty()) {
// Check to see if we have created any yet new containers yet
//-- If we have, then use the newest container
container = subLayouts.get(subLayouts.size() - 1);
}
// Initialize the new layout with padding to show the change
LinearLayout linearLayout = new LinearLayout(LinearLayoutActivity.this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
android.widget.LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setPadding(16, 16, 16, 16);
// Add textview with linear layout name
TextView textView1 = new TextView(LinearLayoutActivity.this);
textView1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
textView1.setText("Linear Layout: " + (subLayouts.size() + 1));
textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
linearLayout.addView(textView1);
// Add the layout to the array
subLayouts.add(linearLayout);
// Set the color of the new layout so that we can see the change
int size = subLayouts.size();
if (size % 2 == 0) {
// Every even layout will be blue
linearLayout.setBackgroundColor(getResources().getColor(R.color.blue));
} else {
// every odd layout will be red
linearLayout.setBackgroundColor(getResources().getColor(R.color.red));
}
// Finally, add the linear layout to the container layout
container.addView(linearLayout);
}
});
}
}
Here is the Layout xml
<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="match_parent"
android:background="#color/red"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"/>
<LinearLayout
android:id="#+id/container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
</LinearLayout>
</LinearLayout>
I am creating n number of images with wordImage and assign a differnt Id.This happens on a button click add. There is another button remove when I click that all the images that I have created should be removed from the view(Head is the view). Plz help how to do that.
onclick of add button
for (int getwordcount = 0; getwordcount <5; getwordcount++) {
int i=0;
WordImage = new ImageView(this);
WordImage.setId(getwordcount);
WordImage.setBackgroundResource(alphabetsimages[getwordcount]);
RelativeLayout.LayoutParams para=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
para.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
para.leftMargin=maragin+=54;
para.bottomMargin=25;
para.width=BtnNext.getWidth()/3;
para.height=BtnNext.getHeight();
WordImage.setLayoutParams(para);
WordImage.setTag(getSplitString);
Head.addView(WordImage);
}
onclick of remove button
only the last -------------> Head.removeView(WordImage);
create image is removed from view.
How do I remove all images created using wordimage.Any help would be greatly appreciated.
WordImage saves link just for the last ImageView. Previous link lost in this line:
WordImage = new ImageView(this);
This is why Head.removeView(WordImage) removes only the last image.
If you want delete all added image elements, the easiest way do this is to add RelativeLayout container like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout android:id="#+id/Head" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:onClick="onClickAdd"
android:id="#+id/button" android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove"
android:onClick="onClickRemove"
android:layout_alignBaseline="#id/button"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Then you can delete all images only by one command:
Head.removeAllViews();
In other case, if you need to remove separate images, you have to delete your items in cycle similar to your "add" block:
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
Head.removeView(findViewById(getwordcount));
}
Activity class will be like this:
public class MyActivity extends Activity {
RelativeLayout Head;
int alphabetsimages[] = {android.R.drawable.arrow_down_float, android.R.drawable.btn_dropdown, android.R.drawable.btn_minus, android.R.drawable.ic_dialog_map};
ImageView WordImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickAdd(View v){
int margin = 0;
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
int i=0;
WordImage = new ImageView(this);
WordImage.setId(getwordcount);
WordImage.setBackgroundResource(alphabetsimages[getwordcount]);
RelativeLayout.LayoutParams para=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
para.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
para.leftMargin=margin+=54;
para.bottomMargin=25;
para.width = 40;
para.height = 40;
WordImage.setLayoutParams(para);
Head= (RelativeLayout)findViewById(R.id.Head);
Head.addView(WordImage);
}
}
public void onClickRemove(View v){
//Head.removeAllViews();
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
Head.removeView(findViewById(getwordcount));
}
}
}
I hope that's what you need!
I need to detect which is the clicked image in my application. I try a lot of ways of to do that. I generate Imageview objects dinamically from a Integer Array and i detect that one image is clicked but not which.
I need to put two rows of images but i need that the movement of this images be booth at the same time, not individually. I made a horizontalScrollView with two layouts, one vertical a one horizontal. I put two imageviews in the vertical and send this layout to the horizontal layout through a loop for.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutContenedor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linearLayoutHorizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
My Activity:
public class MainActivity extends Activity implements OnClickListener{
private LinearLayout layoutDinamicoHorizontal;
private LinearLayout layoutGlobal;
private Integer[] listaPeliculas = { R.drawable.android1, R.drawable.android2,
R.drawable.android3, R.drawable.android4, R.drawable.android5, R.drawable.android6,
R.drawable.android7, R.drawable.android8, R.drawable.android9, R.drawable.android10,
R.drawable.android11, R.drawable.android12, R.drawable.android13, R.drawable.android14,
R.drawable.android15, R.drawable.android16, R.drawable.android17, R.drawable.android18,
R.drawable.android19, R.drawable.android20, R.drawable.android21, R.drawable.android22,
R.drawable.android23, R.drawable.android24, R.drawable.android25, R.drawable.android26,
R.drawable.android27, R.drawable.android28, R.drawable.android29, R.drawable.android30};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// layout in variable of LinearLayout type
layoutDinamicoHorizontal = (LinearLayout) findViewById(R.id.linearLayoutHorizontal);
layoutGlobal = (LinearLayout) findViewById(R.id.layoutContenedor);
layoutGlobal.setBackgroundResource(R.drawable.fondodepantallacarrusel);
// create ImageView
ImageView imageViewDinamicoArriba;
ImageView imageViewDinamicoAbajo;
for (int i = 0; i < 30; i = i+2){
// Creamos a new object from ImageView class
imageViewDinamicoArriba = new ImageView(this);
imageViewDinamicoAbajo = new ImageView(this);
// padding to objects of ImageView class
imageViewDinamicoArriba.setPadding(10, 15, 10, 15);
imageViewDinamicoAbajo.setPadding(10, 15, 10, 15);
Drawable imagenArriba = getResources().getDrawable(listaPeliculas[i]);
Drawable imagenAbajo = getResources().getDrawable(listaPeliculas[i+1]);
// Se los pasamos a los objetos
imageViewDinamicoArriba.setImageDrawable(imagenArriba);
imageViewDinamicoAbajo.setImageDrawable(imagenAbajo);
LinearLayout vertical = new LinearLayout(this);
vertical.setOrientation(LinearLayout.VERTICAL);
// Añadimos las vistas al objeto vertical
vertical.addView(imageViewDinamicoArriba);
//vertical.setOnClickListener(this);
vertical.addView(imageViewDinamicoAbajo);
vertical.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// I Tryed with every metods of View
Log.i("galeria", "Imagen: " + v.getId());
}
});
// Add vertical Layout to horizontal Layout
layoutDinamicoHorizontal.addView(vertical);
} // End for
} // End onCreate
#Override
public void onClick(View v) {
}
} // End Activity
imageViewDinamicoArriba.setTag(new Integer(i)) or
imageViewDinamicoAbajo.setTag(new Integer(i))
Then you will get your index in your Log.i("galeria", "Imagen: " + v.getTag());
int index = (Integer) v.getTag();
When you create a View dynamically, the id isn't added by default. If you want to use the ids to filter you should add the id manually:
imageViewDinamicoArriba.setId(i);
For more info View setId method documentation.
Hope it helps.
I am a android beginner, I have added images dynamically inside the linear layout, which will scroll from right to left. The problem is that the images are moving / scrolling properly but when I click on it the on-click listener is not called.
Actually when moving the images the image button is moving but the onclick position is not changed according to the image scrolling.
public class ImageLayoutsActivity extends Activity {
int ImageInt, fromXDelta, toXDelta;
int PosInt, myHarizantalLayoutInt, aDisplayInt, aDisplayDiv = 0;
AnimationSet myAnimation = new AnimationSet(true);
LinearLayout myHarizantalLayout;
HorizontalScrollView myHorizontalScroll;
View myView;
Intent myIntent;
private Button clickedButton = null;
public int currentimageindex = 0;
TranslateAnimation myTranslateAnimation = null;
private String[] imgArr = { "icn01", "icn02" };
private String[] URLArray = { "http://www.google.co.in/",
"http://in.yahoo.com/?p=us" };
LinearLayout.LayoutParams Parems;
Display aDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View aView = LayoutInflater.from(GroupActivity.myGroup).inflate(
R.layout.horizantal, null);
setContentView(aView);
GroupActivity.myGroup.setTitle("---CII---");
aDisplay = getWindowManager().getDefaultDisplay();
aDisplayInt = aDisplay.getWidth();
aDisplayDiv = aDisplayInt / 5;
Log.i("value##########################", String.valueOf(aDisplayDiv));
Log.i("aDisplayInt##########################",
String.valueOf(aDisplayInt));
// define xml layout here
myHarizantalLayout = (LinearLayout) findViewById(R.id.horiztonal_outer_layout_id);
myHorizontalScroll = (HorizontalScrollView) findViewById(R.id.horiztonal_scrollview_id);
// Button aBtnOne = (Button) findViewById(R.id.BtnOneId);
// Hide HorizantalLayout scroll bar
myHorizontalScroll.setHorizontalScrollBarEnabled(false);
myHorizontalScroll.isSmoothScrollingEnabled();
myHarizantalLayout.measure(aDisplay.getWidth(), aDisplay.getHeight());
Log.v("EmailActivity#########", myHarizantalLayout.getMeasuredHeight()
+ ", " + myHarizantalLayout.getMeasuredWidth());
int aLayoutInt = myHarizantalLayout.getMeasuredWidth();
Log.i("aLayoutInt###########", String.valueOf(aLayoutInt));
// Add images in for loop
for (int i = 0; i < imgArr.length; i++) {
// int aVal=myHarizantalLayout.getChildCount();
Log.i("ImageInt############################", "=====Inside the loop======");
// create button instance
final Button aImgBtn = new Button(this);
// myButton.setOnClickListener(null);
// add background image resource files
ImageInt = getResources().getIdentifier(imgArr[i], "drawable",
getPackageName());
Log.i("ImageInt############################", "ImageInt");
// add integer image values to drawable control
Drawable aDrawable = this.getResources().getDrawable(ImageInt);
Log.i("aDrawable$$$$$$$$$$$$$$$$$$$$$$$$$", "aDrawable");
// add drawable file to button instance
aImgBtn.setBackgroundDrawable(aDrawable);
aImgBtn.measure(aDisplay.getHeight(), aDisplay.getWidth());
Log.i("aButton#############################", "aButton");
Parems = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Parems.setMargins(10, 0, 5, 0);
Log.i("Parems%%%%%%%%%%%%%%%%%%%%%%%%%%", "Parems");
// int aParemIn = Parems.width;
// Log.i("aparemIn$$$$$$$$$$$$", String.valueOf(aParemIn));
myHarizantalLayout.measure(aDisplay.getHeight(),
aDisplay.getWidth());
int myHarizantalLayoutInt = myHarizantalLayout.getMeasuredWidth();
Log.i("myHarizantalLayoutInt$$$$$$$$$$$$",
String.valueOf(myHarizantalLayoutInt));
myTranslateAnimation = new TranslateAnimation(aDisplayInt - 300,
-myHarizantalLayoutInt - aDisplayDiv, 0, 0);
myTranslateAnimation.setDuration(20000);
myTranslateAnimation.setRepeatCount(-1);
myTranslateAnimation.setRepeatMode(1);
myAnimation.addAnimation(myTranslateAnimation);
Log.i("myAnimation###########################", "myAnimation");
aImgBtn.setLayoutParams(Parems);
myHarizantalLayout.addView(aImgBtn);
Log.i("myHarizantalLayout&&&&&&&&&&&&&&&&&&&&&&&&&",
"myHarizantalLayout");
// add animation to HarizantalLayout
myHarizantalLayout.startAnimation(myTranslateAnimation);
// Call webview based on image button click event
aImgBtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Log.i("view$$$$$$$$$$$$#############", "---btn clicked -----");
int aVal = myHarizantalLayout.indexOfChild(view);
Log.i("indexOfChild*************************",
"indexOfChild");
if (view == aImgBtn) {
Log.i("aButton%%%%%%%%%%%%%%%%%%%%%%%%", "aButton");
aImgBtn.setId(aVal);
String aUrlStr = URLArray[aVal];
Log.i("aUrlStr$$$$$$$$$$$$#############", aUrlStr);
}
// Log.i("aUrlStr$$$$$$$$$$$$#############", aUrlStr);
// Pass values to webview activity
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="bottom" android:background="#00f">
<LinearLayout android:layout_below="#+id/RelativeLayout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" android:gravity="bottom">
<ImageView android:id="#+id/barImageId"
android:layout_width="150dp" android:layout_height="58dp"
android:background="#drawable/cii" android:layout_alignParentRight="true" />
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="58dp" android:id="#+id/horiztonal_scrollview_id"
android:fadingEdge="none" android:background="#000" >
<LinearLayout android:id="#+id/horiztonal_outer_layout_id"
android:fadingEdge="none" android:layout_height="fill_parent" android:gravity="center_vertical"
android:layout_width="fill_parent" android:background="#f00">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
Try to Use Pager instead of HorizontalScrollView,
This link may be helpful https://github.com/nostra13/Android-Universal-Image-Loader