My android project connected to server.And i retrieve data from server if it's 5 then create 5 imageview in linearlayout in other words automatically create imageview, depends on retrieved data.
This code Loops over number of images and creates new ImageView for each time, and then it adds them to the Parent Linear Layout. You can also set the LayoutParams of ImageView dynamically as well.
LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);
for(int i=0;i<number_of_images;i++) {
ImageView image = new ImageView(context);
layout.addView(image);
}
For Linear Layout:
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
for(int i=0;i<Number_Images;i++){
//ImageView Setup
ImageView i mageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
}
//make visible to program
setContentView(linearLayout);
Hope this Help You.
Related
I'm creating the above popup, the content of which consists of rows of horizontal LinearLayout views within a main vertical LinearLayout. Each horizontal LinearLayout contains one ImageView and one TextView.
I'm creating this within a PopupWindow, and doing so programmatically so that I can change the ImageView source as required.
As you can see the first icon seems to take up a lot of space, despite having the same code generating it as the other icons.
Below is the code:
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams mainLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
verticalLayout.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams iconParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams textParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//History row
LinearLayout historyLayout = new LinearLayout(context);
historyLayout.setLayoutParams(layoutParams);
historyLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView historyIcon = new ImageView(context);
historyIcon.setImageResource(R.drawable.small_book_grey);
historyIcon.setAdjustViewBounds(true);
historyIcon.setLayoutParams(iconParams);
historyLayout.addView(historyIcon);
TextView historyText = new TextView(context);
historyText.setLayoutParams(textParams);
historyText.setText("History");
historyLayout.addView(historyText);
verticalLayout.addView(historyLayout);
//Exam row...
//... (duplicate of history row)
I've tried playing around with the layout parameters, even creating a mock xml layout that displays the content as I'd like, to match the parameters to.
If anyone can give some advice on making that book icon the same size as the others, I'd be grateful.
Add a scaleType to ImageView of fitCenter
Write this code under historyIcon.setAdjustViewBounds(true);
historyIcon.setWidth()`
And put width according to your layout.
Although I didn't figure out why the first image was scaling differently to the other images, I did find another solution: Using compound left drawables.
historyText.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Drawable img = m_context.getResources().getDrawable(
R.drawable.small_book_grey);
img.setBounds(0, 0, img.getIntrinsicWidth() * historyText.getMeasuredHeight() / img.getIntrinsicHeight(), historyText.getMeasuredHeight());
historyText.setCompoundDrawables(img, null, null, null);
historyText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Manually setting the bounds to match the TextView worked. Seems clunky, but it was the only way I could get it to do what I was aiming for.
I want to display image and text at runtime of the activity.
Similarly just like chat happening in WHATSAPP.
I displaying the text in textview and image in imageview.
The problem i am facing is I want to show both of them on the same activity with scroll
For eg:
image
text
text
image
text
image
image
Which layout or control should i use to display like this?
add both like this..
LinearLayout linearLayout=new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams vp=new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.settext("test");
linearLayout.addView(imageView,tv);
ImageView imageView= new ImageView(this);
imageView.setImageBitmap(bitMap);
imageView.setVisibility(View.VISIBLE);
imageView.setBackgroundColor(0xFFFF00FF);
//linearLayout=LinearLayout+imageView;
LinearLayout.LayoutParams Iv=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
linearLayout.addView(imageView,Iv);
setContentView(linearLayout,vp);
I have to develop five to six horizontal scrolling view,in this scrolling image add dynamic using adapter.
Feature:
1) Image can be drag and drop one scroll view to another
2) image can be move one horizontal scrolling to another view
3) image can be selected/unselected
4) with with all version
i am using this library, but sometime scrolling is not that much smooth(chopping)
Check this links may be it's useful to you.
1) Insert a view dynamically in a HorizontalScrollView in Android
2) http://android-er.blogspot.in/2012/07/implement-gallery-like.html
3) Horizontal ListView like Google Catalogs
Using this code you can add android control programatically to linear layout and just add Horizontal Scrollview to Linear Layout through xml
You will get horizontally listview.
//My coding here.
String[] name={"PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT"} ;
myLInearLayoutmain =(LinearLayout) findViewById(R.id.linearLayoutmain);
for(int i =0;i<6;i++)
{
LinearLayout li=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
li.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams paramsnew = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params1.setMargins(30, 20, 30, 0);
//add textView
valueTV = new TextView(this);
valueTV.setText(""+name[i]);
valueTV.setId(5);
valueTV.setLayoutParams(paramsnew);
valueTV.setGravity(Gravity.CENTER);
// adding Button to linear
valueB = new Button(this);
valueB.setText(""+name[i]);
valueB.setId(i);
valueB.setLayoutParams(params);
valueB.setOnClickListener(this);
valueB.setGravity(Gravity.CENTER);
// adding Imageto linear
img = new ImageView(this);
img.setImageResource(R.drawable.ic_launcher);
img.setLayoutParams(paramsnew);
//add the textView and the Button to LinearLayout
li.addView(valueTV);
li.addView(valueB);
li.addView(img);
li.setLayoutParams(params1);
myLInearLayoutmain.addView(li);
}
i am adding an imageView to a Relative layout.It shows up at the beginning of layout
I have two buttons in the layout.
I button to the layout left and one to layout right. Now i want to place these under the button to right.
how can i do that. My code is
ImageView image = new ImageView(this);
image.setImageBitmap(imageBitmap);
layout.addView(image);
Try using the
android:Layout_below="#id/yourButtonId" or
android:LayoutBelow="#id/yourButtonId"
Programmatically it means :
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, yourButtonVariable.getId());
yourRelativeLayout.addView(yourImageView, rlp);
How can I make an ImageView appear in the middle of the screen when the user clicks a button? I have placed the image in the res/drawable-folder.
I've been trying with the code below but I don't know how to make the ImageView appear:
View v = new ImageView(getBaseContext());
ImageView image;
image = new ImageView(v.getContext());
image.setImageDrawable(v.getResources().getDrawable(R.drawable.gameover));
Thanks!
//LinearLayOut Setup
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
int id = getResources().getIdentifier("gameover", "drawable", getPackageName());
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(id);
someLinearLayout.addView(imageView);
If you add to RelativeLayout, don't forget to set imageView's position. For instance:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200, 200);
lp.addRule(RelativeLayout.CENTER_IN_PARENT); // A position in layout.
ImageView imageView = new ImageView(this); // initialize ImageView
imageView.setLayoutParams(lp);
// imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setImageResource(R.drawable.photo);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.addView(imageView);
Create the ImageView
Use an OnClickListener in the button
Add the ImageView to the layout or set the visibility of the ImageView to VISIBLE