Textview image in android - android

In my application i have to show an image in my text view. Also When i click 1st time 1 image should draw and clicking next time another image should be drawn.Is it possible to draw an image in textview?Please help me..

You can use
setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
for the textView

You can set a background image using the android:background attribute
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image" />
Or programatically with the setBackgroundDrawable()or setBackgroundResource() methods:
textView.setBackgroundResource(R.drawable.image);

You can take an array of Drawables, an an index for that:
TextView tv;
Drawable[] drbl = new Drawable[4];
int drblIndex = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
drbl[0] = getResources().getDrawable(R.drawable.ic_launcher);
drbl[1] = getResources().getDrawable(R.drawable.img2);
drbl[2] = getResources().getDrawable(R.drawable.img3);
drbl[3] = getResources().getDrawable(R.drawable.right_arrow);
tv = (TextView) findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tv.setBackgroundDrawable(drbl[drblIndex++]);
if(drblIndex == drbl.length)
drblIndex = 0;
}
});
}
Then you can set values of that array as I have done.
Then onClick you can move to next Index and can set new Drawable-Image to TextView.
When Index reaches to last value, set it to zero, simply.

Set OnClickListener on the TextView and in its onClick() method, use the following methods to set the images :
textview.setBackgroundDrawable();
or
textview.setBackgroundResource();

try this code, textview with image.
CharSequence dogstate = null ;
Html.fromHtml("" + " "
+ "your text" + "", featuregetter, null);
dogatstatus.append(dogstate);
private ImageGetter featuregetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Log.i(" get drawable mathod ", "");
Bitmap B = BitmapFactory.decodeResource(getResources(),
R.drawable.bone);
BitmapDrawable BD = new BitmapDrawable(B);
BD.setBounds(0, 0, B.getWidth(), B.getHeight());
return BD;
}
};

Related

How to get all images as array of bitmap from drawable folder using Drawable?

I am using the below code to get 9 (bird1 to bird9) images from drawable folder by manually giving each name.
How can i use an array or replace R.drawable.bird1 with variable ( as the below statement accept only actual value) to get all the 9 images in an array?
Drawable myDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), R.drawable.bird1, null);
bitmap1 = ((BitmapDrawable) myDrawable).getBitmap();
myDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), R.drawable.bird2, null);
bitmap2 = ((BitmapDrawable) myDrawable).getBitmap();
I got it working like this. I'm sure someone can do it much nicer... but this works.
Caveats:
1) Remember to replace "com.example" with your real package name.
2) Replace "numberOfBirdsBitmaps = 3" with however many you have.
3) The "derp" ImageViews are just for my testing purposes.
public class MainActivity extends AppCompatActivity {
int numberOfBirdsBitmaps = 3;
ArrayList<Bitmap> birdBitmapArray = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView derp1 = (ImageView) findViewById(R.id.imageView);
ImageView derp2 = (ImageView) findViewById(R.id.imageView2);
ImageView derp3 = (ImageView) findViewById(R.id.imageView3);
for (int i = 0; i<numberOfBirdsBitmaps; i++) {
String temporaryString = "bird" + ((Integer) (i+1)).toString();
int temporaryIdentifier = getResources().getIdentifier(temporaryString, "drawable","com.example");
Drawable temporaryDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), temporaryIdentifier, null);
Bitmap temporaryBitmap = ((BitmapDrawable) temporaryDrawable).getBitmap();
birdBitmapArray.add(temporaryBitmap);
}
derp1.setImageBitmap(birdBitmapArray.get(0));
derp2.setImageBitmap(birdBitmapArray.get(1));
derp3.setImageBitmap(birdBitmapArray.get(2));
}
}
Love,
Boober.
You can try and get the drawable id using a loop:
for(int=1; i<=9 ; i++)
{
getResources().getIdentifier("bird"+i,"drawable",getPackageName());
}

setTextcolor by using getCurrentTextColor()

I need to get current text color form TextView and then assign this value to TextView.setTextColor(). But I get a large int -1979711488138, how can I get a color from it?
Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);
or
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
Suppose you want to set color from textView1 to textView then you can do like this:
textView.setTextColor(textView1.getCurrentTextColor());
public class MainActivity extends Activity
{
private TextView txtViewIpLable,textView1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
textView1.setTextColor(txtViewIpLable.getTextColors());
}
private void init()
{
// TODO Auto-generated method stub
txtViewIpLable = (TextView) findViewById(R.id.txtViewIpLable);
textView1 = (TextView) findViewById(R.id.textView1);
}
}
You cannot fit the number into an int, I get -1979711488 which is #8A000000 i.e. black with 138 alpha. You can get all parts of the color like this:
int color = getCurrentTextColor();
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
It's really strange why the default text color is not a solid color but rather black with an alpha value as that is more expensive for the system.

ImageView event when in view

I have a layout like so.
HorizontalScrollView
LinearLayout (Horizontal)
ImageView (A number of times)
Is there any event/way I can tell which ImageView is visible? I'd like an event if possible so I can store the index of the current Image that's visible.
Something like
class MyViewClass
private int index;
....
imageView.onEventViewListener(...
index = currentImageIndex;
);
EDIT: My current code loading the ImageViews
OnClickListener click = new OnClickListener() {
#Override
public void onClick(View v) {
Object i = v.getTag();
smoothScrollTo((width*((Integer) i+1)), 0);
}
};
for(int i = 0;i < mImages.size();i++) {
Gallery.Image image = mImages.get(i);
ImageView iv = new ImageView(getContext());
ViewGroup.LayoutParams par = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(par);
iv.setTag(i);
iv.setOnClickListener(click);
Picasso.with(getContext())
.load("http://www.example.com/files/galleries/" + image.getGalleryId() + "/" + image.getSrc() + "_full.jpg")
.into(iv);
layout.addView(iv);
}
You can use viewpager; it will provide all the events that's required by you
You can try this widget:HorizontalListView.
Its attribute mLeftViewAdapterIndex means first Visiable item's index.
Here is the source code in github.
https://github.com/MeetMe/Android-HorizontalListView/blob/master/AndroidHorizontalListView/src/com/meetme/android/horizontallistview/HorizontalListView.java

Android - Bitmap Height = Textview Text Height?

Adding an image in a text view works. Changing the image height and width to text view text height not works. (Original Image "red circle" is: 32px x 32px)
Output looks like this:
test img http://img850.imageshack.us/img850/9500/m1f.png
XML:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
Code:
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
String getText = getResources().getString(R.string.getText);
textView.setText(getText);
SpannableStringBuilder ssb = new SpannableStringBuilder(getText);
Bitmap redcircle= BitmapFactory.decodeResource( getResources(), R.drawable.redcircle );
Bitmap resizedbitmap = Bitmap.createScaledBitmap(redcircle, (int)textView.getMeasuredHeight(), (int)textView.getMeasuredHeight(), true);
ssb.setSpan( new ImageSpan( resizedbitmap ), 3, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
textView.setText( ssb, BufferType.SPANNABLE );
}
EDIT:
I want, that the image height and width is equal text view text height. For example like this:
test img 2 http://img849.imageshack.us/img849/3163/r9x.png
Try using
ImageSpan(resizedbitmap, ImageSpan.ALIGN_BASELINE)
instead of
ImageSpan(resizedbitmap)
The default alignment of an ImageSpan is ALIGN_BOTTOM.

Add images to a HorizontalScrollView

I want to create a HorizontalScrollView which reads the images from drawable folder. The name of the images are "image1" "image2" ... "image20". I donĀ“t know how I can use the numbers to read them. Here is what I have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=1 ; i<20; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (R.drawable.image1);
sv.addView(iv);
}
}
You can do that in two ways.
First one is to create array with the id's of images you want to use and in your for cycle, just add the images to your layout:
int[] images = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image20};
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=0 ; i<20; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (images[i]);
sv.addView(iv);
}
Or the second way, you can create something similar to this:
for (int i=1 ; i<=20; i++){
String uri = "drawable/image"+i;
// int imageResource = R.drawable.image1;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView iv = new ImageView (this);
iv.setBackgroundResource (imageResource);
sv.addView(iv);
}
I didn't tested the codes, but I think they should work.
If you wish to use drawables without an array list, you can do this:
getResources().getIdentifier("Name of the Drawable", "drawable", "Your Package Name");
So your code will be:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=1 ; i<20; i++){
ImageView iv = new ImageView (this);
int myImage = getResources().getIdentifier("image"+i, "drawable", "Your Package Name");
iv.setBackgroundResource(myImage);
sv.addView(iv);
}
}
A lot of examples like this show building a list of your images first. And then you can use your code and iterate through the list.
So something like
List<Drawable> imagesToAdd = Arrays.asList(R.drawable.image1,R.drawable.image2, .... R.drawable.image20);
Then you can even use the foreach loop to iterate through this.
for (Drawable image in imageToAdd) {
etc...
}

Categories

Resources