Display image and text on runtime, android? - android

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);

Related

To add textview dynamic at run time Android

I want to add Text-view at run time on Android to display the contents of my database on a activity,which can change every time.
Please Help
You could create a TextView dynamically like this for example:
//create a TextView with Layout parameters according to your needs
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//if your parent Layout is relativeLayout, just change the word LinearLayout with RelativeLayout
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
//get the parent layout for your new TextView and add the new TextView to it
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
linearLayout.addView(tv);
And in the tv.setText(...); line you could set the text, that your got from your database.

How to automatically create imageview in android

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.

Adding image to LinearLayout each time AlertDialog item is selected

I am trying to add an image to a LinearLayout each time the user selects an item from an AlertDialog modal but once the first item is selected, no other images will appear and the first image doesn't even change to the newly selected item. Ideally, i would like the first image to appear in the center of the layout as it does and when a second choice is selected, I would like it to appear to the right of the first image and then both are centered.
Thanks to anyone looking at this.
Below is the ImageView and LinearLayout portion of my code. This code is in the onClick() method for the AlertDialog
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout l = (LinearLayout) findViewById(R.id.picLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
imageView.setId(which);
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
int pic = Pic.getPicID(which);
imageView.setImageResource(pic);
l.addView(imageView, items.size()-1, params);

how to align text view to the right of Image view within Linearlayout Dynamically in android

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout3);
TextView tv = new TextView(this);
ImageButton imb=new ImageButton(this);
ll.addView(tv);
tv.setSingleLine();
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setHorizontallyScrolling(true);
tv.setFocusableInTouchMode(true);
tv.setText(myString1);
This is my code and I'm able to display data in textView now. But I want to put one imageButton to the left of textView so that will work as cancel strip. but I'm having a problem. How can I set imageButton left of text view programatically?
You have two solutions for your question. You can use the below code if you just want to show image on the right of textview.
TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon,0, 0,0);
or else
set orientation of your linearlayout as Horizontal and add the imageview first and then textview second. So they will be aligned as your requirement.
Below is the example.
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton imb=new ImageButton(this);
imb.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ll.addView(imb);
ll.addView(tv);

How to center a textView with java code inside a linear layout

So I want to center a text View inside a linear layout where I have 2 other objects that populate the linear layout. I have a imageView and another textView. How would I go about centering the textView so the text is in the middle of the screen? This is what I have so far.
View linearLayout = findViewById(R.id.rockLayout);
ImageView mineralPicture = new ImageView(this);
TextView mineralName = new TextView(this);
TextView mineralProperties = new TextView(this);
mineralProperties.setText("The properties are: " + Statics.rockTable.get(rockName).getDistinctProp());
mineralProperties.setId(2);
mineralName.setText("This mineral is: " + rockName);
mineralName.setGravity(Gravity.CENTER);
mineralName.setId(1);
mineralName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralPicture.setId(3);
mineralPicture.setImageResource(R.drawable.rocks);
mineralPicture.setAdjustViewBounds(true);
mineralPicture.setMaxHeight(100);
mineralPicture.setMaxWidth(200);
mineralPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
((LinearLayout)linearLayout).addView(mineralName);
((LinearLayout)linearLayout).addView(mineralProperties);
((LinearLayout)linearLayout).addView(mineralPicture);
I should mention I've tried doing such things as mineralName.setGravtiy(Gravity.CENTER); and it hasn't worked.
a) let the textview match_parent on the width and use gravity center
b) on the textview layout params set the layout_gravity to center (sorry for the terms I normally do this in XML)!

Categories

Resources