I have an ImageView (original size: 500 x 80) . I am changing its width parameter like this:
imageView1.getLayoutParams().width=(int) (500.0*width_proc);
When I use ImageView.getWidth() the result is 555, which is perfect.
When I use ImageView.getHeight() the result is still 80. It doesn't update to the new value, even though the image is automatically rescaled.
I tried to add a Sleep(100), to use ImageView.getViewTreeObserver().addOnGlobalLayoutListener, or to add measure() functions, nothing works.
Code looks like this:
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View myView=this.findViewById(android.R.id.content);
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
BuildGraphInterface();
}
});
}
........
public void BuildGraphInterface()
{
.......
//================= search 1 ===========================//
ImageView imageView1 = (ImageView) findViewById(R.id.imageview1);
imageView1.getLayoutParams().width=(int) (500.0*width_proc);
imageView1.requestLayout();
imageView1.invalidate();
TextView textView1 = (TextView) findViewById(R.id.textview1);
textView1.setText( " " + imageView1.getHeight() + " " + imageView1.getWidth() );
.......
}
Related
I have single line textview. I am setting string in it. Sometimes full string not possible to display. So I just want that string which is displayed.
TextView valueTV = new TextView(context);
// valueTV.setEllipsize(TextUtils.TruncateAt.MIDDLE);
valueTV.setText(list.get(i).getTitle());
valueTV.setTag(i);
valueTV.setBackgroundColor(Color.parseColor(clr));
valueTV.setTextColor(Color.parseColor(txtclr));
if(dt1.equals(startdt))
{
valueTV.setTextColor(Color.parseColor(txtclr));
}else {
valueTV.setTextColor(Color.TRANSPARENT);
}
valueTV.setTypeface(type_thin);
valueTV.setId(list.get(i).getId());
valueTV.setMaxLines(1);
LinearLayout.LayoutParams ll=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setMargins(0,1,0,0);
valueTV.setLayoutParams(ll);
valueTV.setOnClickListener(this);
lladd.addView(valueTV);
For Example
Full String : I am Android Developer
RIght now it displays:
I am And
So How can I get
I am And?
Thanks
I thanks both #Andriy and Sujith for helping. Mix of both answers worked for me.
ViewTreeObserver vto = valueTV.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int end = valueTV.getOffsetForPosition(valueTV.getWidth(), 0) + 1;
String displayed = valueTV.getText().toString().substring(0, end);
System.out.println("i---displayed--" + displayed);
}
});
Try to use getOffsetForPosition(). For visible text from start something like that:
int end = textView.getOffsetForPosition(textView.getWidth(), 0) + 1;
String displayed = textView.getText().toString().substring(0, end);
ViewTreeObserver vto = textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
String text = (String) textView.getText().subSequence(0, textView.getLayout().getEllipsisStart(0));
Log.i(TAG, "Txt: " + text);
}
});
this gives you the text visible in textview (without three dots)
I have an empty RelativeLayout. I must get height and width of layout (set to match the parent). I have tried to search online but, to no avail. This is my code, can you help me? When I launch the application it crashes in the getCoordinate method. Sorry for my English.
public class MainActivity extends AppCompatActivity {
Random rand = new Random();
int[] coordinate = new int[2];
public void getCordinate(final RelativeLayout layout){
layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// Ensure you call it only once :
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
coordinate[0] = layout.getMeasuredWidth();// width must be declare as a field
coordinate[1] = layout.getMeasuredHeight();// height must be declare as a fiel
}
});
}
public void makeButton(RelativeLayout play_area){
int button_x = rand.nextInt(coordinate[0]);
int button_y = rand.nextInt(coordinate[1]);
Button bomb = new Button(this);
bomb.setX((float)button_x);
bomb.setY((float)button_y);
play_area.addView(bomb);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout play_area = (RelativeLayout) findViewById(R.id.play_area);
play_area.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
getCordinate(play_area);
Log.d("Coordinate", "Xmax: " + coordinate[0] + " Ymax: " + coordinate[1]);
//makeButton(play_area);
}
}
you shouldn't call measure(). you could maybe just call .setLayoutParams(width, height) to set the view to match parent's width/height.
and simply calling .getMeasuredWidth() (or height) on the view should return their width/height values
My usecase is to load images from a URL, and I am using picasso, in the following way to acheive that.
ll = (LinearLayout) findViewById(R.id.tasklayout);
ImageView imageView = new ImageView(this);
Picasso.with(this).load(taskFieldDao.data).into(imageView);
ll.addView(imageView, new LinearLayout.LayoutParams(800, 800));
What I see is a 800 * 800 whitespace with no image in it. Looking at the logs, there doesn't seem to any problem. Atleast picasso doesn't record anything.
I replicate your problem with button click and it is working. You can as well check the your path is correct
downloadButton = (Button)findViewById(R.id.download_button);
downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
ImageView imageView = new ImageView(PicassoDownloadActivity.this);
Picasso.with(PicassoDownloadActivity.this).load(Helper.imageDownloadPath).into(imageView);
relativeLayout.addView(imageView, new LinearLayout.LayoutParams(800, 800));
}
});
Picasso won't typically give errors when something goes wrong, i suggest doing something like this:
ll = (LinearLayout) findViewById(R.id.tasklayout);
final ImageView imageView = new ImageView(this);
Picasso.with(this).load(taskFieldDao.data).into(imageView, new Callback() {
#Override
public void onSuccess() {
ll.addView(imageView, new LinearLayout.LayoutParams(800, 800));
}
#Override
public void onError() {
Log.e("Picasso","Image load failed);
}
});
i want to increase my integer. but i don't know to fix it
Even i am using this code, my picture more brightness with int +10 from before.
try {
ImageView image = (ImageView) findViewById(R.id.imgView);
Bitmap bMap = mPhoto;
int a=0;
image.setImageBitmap(SetBrightness(bMap,20+a));
a = a + 10;
} catch (Exception e) { }
Can you help me?
solution
try {
ImageView image = (ImageView) findViewById(R.id.imgView);
Bitmap bMap = mPhoto;
mPhoto = Bitmap.createBitmap(SetBrightness(bMap,20));
image.setImageBitmap(mPhoto);
} catch (Exception e) { }
Just with code above....
When i click, this code increase by itself :)
please notice that your variable a should be a global variable. so that it doesn't set to zero every time when you try to increase brightness.
And for the way you can increase brightness by value 10, on clicking a button is:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//your code//
a = a + 10;
}
in your button in click set this
a += 10;
You can set onClickListener for imageView. And in onClick method you can increase your integer value.
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//increase integer value
a = a + 10;
}
I want to put elements in my view depending on the position of the other ones. Let me explain : First, I have two textviews and I've put successfully the second (villeOffre) one under the first one (titreOffre) :
titreOffre = (TextView) findViewById(R.id.offreTitre);
titreOffre.setText(capitalizedString(offre.getTitle()));
villeOffre = (TextView) findViewById(R.id.offreVille);
villeOffre.setText(offre.getLocality());
infos = (TextView) findViewById(R.id.infos);
ViewTreeObserver vto = titreOffre.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(
{
#Override
public void onGlobalLayout()
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) villeOffre.getLayoutParams();
params.setMargins(15,titreOffre.getBottom()+12,15,0);
villeOffre.setLayoutParams(params);
titreOffre.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
This works perfectly, but now I want to put a third textview (infos) which is depending on villeOffre position. How to do that ?
Thanks a lot.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rr = new RelativeLayout(this);
b1 = new Button(this);
b1.setId(R.id.Button01);
recent = b1;
b1.setText("Click me");
rr.addView(b1);
setContentView(rr);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1 = new TextView(can.this);
tv1.setId((int)System.currentTimeMillis());
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
recent = tv1;
}
});
}