I want to put an Icon inside an EditText so I did like this:
android:drawableLeft="#drawable/search_icon"
The problem is that the image is bigger than the EditText so the EditText becomes bigger in order to fit the image size. What I want is the opposite: The image should resize in order to fit the EditText height, is it possible?
I (desperately) tried with:
android:adjustViewBounds="true"
But obviously it doesn't work.
Is there any way to do so?
Try
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dd80aa"
android:orientation="vertical">
<RelativeLayout
android:layout_width="363dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text here"
android:marginLeft="50dp"/>
<ImageView
android:layout_width="wrap_contenta"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="right"
android:src="#android:drawable/ic_menu_search"/>
</RelativeLayout>
</LinearLayout>
You can wrap in RelativeLayout , on the left set an ImageView with layout_height match_parrent
You can simply do this by fixing the height of the edit text.
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:drawableLeft="#drawable/beautiful"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
you can use below code for resizing
Drawable d = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
byte[] ba;
do {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
Log.e("BEFORE REDUCING",
bitmap.getHeight() + " " + bitmap.getWidth() + " "
+ bitmap.getRowBytes() * bitmap.getHeight());
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
Log.e("After REDUCING",
bitmap.getHeight() + " " + bitmap.getWidth() + " "
+ bitmap.getRowBytes() * bitmap.getHeight());
ba = bao.toByteArray();
if ((ba.length / 1024) >= 650) {
bitmap = Bitmap.createScaledBitmap(bitmap,
(int) (bitmap.getWidth() * 0.95),
(int) (bitmap.getHeight() * 0.95), true);
}
Log.e("BYTE LENGTH", "" + ba.length / 1024);
} while ((ba.length / 1024) >= 650);
Related
I'm trying to get Width and Height of Bitmap and set them as Layout Parameter of CustomView
JAVA :
private void setBg() {
drawView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
drawView.getLayoutParams().height = orginalBitmap.getHeight();
drawView.getLayoutParams().width = orginalBitmap.getWidth();
relativeLayout.addView(drawView);
}
XML :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom"
android:layout_below="#+id/clear">
<ImageView
android:id="#+id/fullimg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal|center_vertical"
android:adjustViewBounds="true" />
<RelativeLayout
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" />
</RelativeLayout>
But the code above doesn't work, when I run the app I get this
the emojis, for example, should stop right in the corner of image view but instead of that it still appears in the two empty spaces.
Use this method instead , you have to set the layout params this way and then add them to your drawView
private void setBg(){
android.view.ViewGroup.LayoutParams params = drawView.getLayoutParams();
params.width = orginalBitmap.getWidth();
params.height = orginalBitmap.getHeight();
drawView.setLayoutParams(params);
relativeLayout.addView(drawView);
}
Here's what I've done, and it works well
mDensity = getResources().getDisplayMetrics().density;
actionBarHeight = (int) (110 * mDensity);
bottombarHeight = (int) (60 * mDensity);
viewWidth = getResources().getDisplayMetrics().widthPixels;
viewHeight = getResources().getDisplayMetrics().heightPixels - actionBarHeight - bottombarHeight;
viewRatio = (double) viewHeight / (double) viewWidth;
bmRatio = (double) orginalBitmap.getHeight() / (double) orginalBitmap.getWidth();
if (bmRatio < viewRatio) {
bmWidth = viewWidth;
bmHeight = (int) (((double) viewWidth) * ((double) (orginalBitmap.getHeight()) / (double) (orginalBitmap.getWidth())));
} else {
bmHeight = viewHeight;
bmWidth = (int) (((double) viewHeight) * ((double) (orginalBitmap.getWidth()) / (double) (orginalBitmap.getHeight())));
}
final RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lparams.addRule(RelativeLayout.CENTER_IN_PARENT);
lparams.height = bmHeight;
lparams.width = bmWidth;
drawView.setLayoutParams(lparams);
I'm pretty new to Android and I'm really struggling trying to understand what is going on in my application.
I'm trying to build a small game in which a circle appears on the screen, in a random position and it disappears as soon as the user clicks on it. Then a new circle appears in another position.
The problem is that comparing the coordinates of the user's click, and the coordinates of the center of the circle, these seem to be totally different…
The problem seems to be in the circle coordinates, because if I try to force its position to the center of the view, it appears in a totally wrong position, at the right bottom of the view.
What am I doing wrong?
This is the code of the function drawing the circles in the view (which is as big as its parent).
public void drawDots(){
Paint paint = new Paint();
paint.setColor(Color.MAGENTA);
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
View ll = (View) findViewById(R.id.circle);
ll.setBackground(new BitmapDrawable(bg));
centerX = rand.nextInt(ll.getWidth());
centerY = rand.nextInt(ll.getHeight());
Canvas canvas = new Canvas(bg);
canvas.drawCircle(centerX, centerY, radius, paint);
Log.i("Point center X :" + centerX, " Y " + centerY);
ll.setOnTouchListener(new View.OnTouchListener() {
int x = 0, y = 0;
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = (int) event.getX();
y = (int) event.getY();
Log.i("CLICK X: " + x, "click Y: " + y);
if((x < (centerX + radius) && x > (centerX - radius))) && ((y > centerY + radius) && (y < centerY - radius)) ){
Log.i("x ok: " + x + " y ok: " + y, "Counter: " + counter);
drawDots();
}else{
Log.i("x NOT ok " + x, " Circle area between: " + (centerX-radius) + " e " + (centerX + radius));
Log.i("Y NOT ok " + y, " Circle area between: " + (centerY-radius) + " e " + (centerY + radius));
}
}
return true;
}
});
}
And this is the layout. The textview and the button are needed to start a timer but they are removed from the layout as soon as the user clicks on the start button.
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="#+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="37dp"
android:textSize="40sp"
android:textColor="#ffffff"
android:text="#string/timerVal" />
<Button
android:id="#+id/startButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="38dp"
android:text="#string/startButtonLabel" />
<View android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/circle"/>
Sorry for my bad explanation but I even dunno where the problem is.
EDIT I tried this solution and it actually seems to improve the situation but if i only consider the x-axis, for the y-axis there is any correspondence but in this way it seems the coordinates of the click are totally out of range... Circle drawn on canvas doesn't match the screen
even though i actually don't understand WHY it is needed
The problem is here:
centerX = rand.nextInt(ll.getWidth());
ll.getWidth() returns you 0 and rand.nextInt() won't work
when ll.getWidth() is 0.
so cannot call nextInt(0). Check out Random#nextInt().
to fix this make a check:
if(ll.getWidth() == 0)
centerX = 0;
else
centerX = rand.nextInt(ll.getWidth());
But I'm not certain what you are doing so hopefully you will find a better solution.
P.S - same you have to do with the other case, I still doubt what is the need of rand.nextInt(ll.getWidth());. (you may edit your question and make clear what you want to achieve)
as a newbie you code is nice enough to gain upvotes :)
~happycoding
I finally figured out the problem, which was here:
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
with this call i was specifying a view of 480px of width and 800px of height, whereas my view is occupying the entire parent view (which is the screen).
This was a quite a newbie mistake, but i hope it could help someone else as i've been struggling for two days with it! :)
Here is the animation:
https://www.youtube.com/watch?v=YqFI4r4VYgg&feature=youtu.be
Right now I am doing this
<ImageView
android:id="#+id/zweet_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/description"
android:src="#drawable/zweet_logo" />
<RelativeLayout
android:id="#+id/appLoading"
style="#style/GenericProgressBackgroundAppLoading"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_below="#+id/zweet_logo"
android:layout_centerHorizontal="true" >
<ProgressBar style="#style/GenericProgressIndicatorAppLoading" />
</RelativeLayout>
logo_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator" >
<scale
android:duration="700"
android:fillBefore="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
<translate
android:duration="700"
android:fromXDelta="-200"
android:fromYDelta="-200" />
MainActivity is like
/** Define and start logo animation */
Animation logoMoveAnimation = AnimationUtils.loadAnimation(this,
R.anim.logo_animation);
ImageView zweetLogo = (ImageView) findViewById(R.id.zweet_logo);
zweetLogo.startAnimation(logoMoveAnimation);
That's so basic.
--
Right I have the iOS code:
- (void)animateZweetLogo
{
CGRect startFrame = self.zweetLogoImageView.frame;
MTTimingFunction timingFuction = kMTEaseOutBounce;
CGFloat duration = kAnimateZweetLogoAnimationDuration;
CGFloat endY = startFrame.origin.y;
CGFloat endX = startFrame.origin.x;
CGFloat endScale = 1;
CGFloat endRotation = 0;
CGFloat endAlpha = 1;
[UIView mt_animateViews:#[self.zweetLogoImageView]
duration:duration
timingFunction:timingFuction
options:MTViewAnimationOptionBeginFromCurrentState
animations:^{
//self.zweetLogoImageView.mt_animationPerspective = -1.0 / 500.0;
CGRect r = self.zweetLogoImageView.frame;
r.origin.x = endX;
r.origin.y = endY;
// scale
CGFloat h = startFrame.size.height;
CGFloat w = startFrame.size.width;
CGFloat hh = h * endScale;
CGFloat ww = w * endScale;
r.size.height = hh;
r.size.width = ww;
r.origin.y -= (hh - h) / 2.0;
r.origin.x -= (ww - w) / 2.0;
self.zweetLogoImageView.frame= r;
// alpha
self.zweetLogoImageView.alpha = endAlpha;
// rotation
CGFloat radians = mt_degreesToRadians(endRotation);
self.zweetLogoImageView.layer.transform = CATransform3DMakeRotation(radians, 0, 1, 0);
} completion:^{
[self performSelectorOnMainThread:#selector(resumeLoading) withObject:nil waitUntilDone:YES];
}];
}
Is there some library of animations that I can use to do something that look like the iOS code? Or there is no other than just code and test it in Java in a long and iterative process?
Check http://www.mybringback.com or http://thenewboston.org/list.php?cat=6.
or developer docs at developer.android.com are pretty useful.
I am creating tablerow (9 rows) dynamically. I want to equally divide the table layout height to table rows. But its not working...
I am creating 9 rows each row contains 9 TextView
My xml is
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<TableLayout
android:layout_weight="80"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#000"
android:paddingTop="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingBottom="1dp"
android:id="#+id/sudokuboard"
android:stretchColumns="*"
android:weightSum="100" >
</TableLayout>
And my code is
tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);
//1st Row
for(int i=0; i < 9; i++)
{
TableRow NewRow1 =new TableRow(this);
NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,10.0f));
NewRow1.setPadding(1, 1, 1, 1);
for(int j=0; j<9; j++)
{
TextView tv_00 = new TextView(this);
int id=10*i;
id=id+j;
String strText=""+id;
tv_00.setText(strText);
tv_00.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT, 10.0f));
tv_00.setGravity(Gravity.CENTER);
tv_00.setBackgroundColor(Color.parseColor("#FFFFFF"));
tv_00.setId(id);
NewRow1.addView(tv_00);
}
//tblsudoku.addView(NewRow1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tblsudoku.addView(NewRow1);
}
Please help me to solve this issue...
Updation on 29/12/2013
New Code
tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);
for(int i=0; i < 9; i++)
{
TableRow NewRow1 =new TableRow(this);
NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,1f));
NewRow1.setBackgroundColor(Color.RED);
LinearLayout Newrowlayout =new LinearLayout(this);
Newrowlayout.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
Newrowlayout.setOrientation(1);
NewRow1.setPadding(1, 1, 1, 1);
for(int j=0; j<9; j++)
{
TextView tv_00 = new TextView(this);
int id=10*i;
id=id+j;
String strText=""+id;
tv_00.setText(strText);
tv_00.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT, 1f));
tv_00.setGravity(Gravity.CENTER);
tv_00.setTextColor(Color.YELLOW);
tv_00.setId(id);
Newrowlayout.addView(tv_00);
}
NewRow1.addView(Newrowlayout);
tblsudoku.addView(NewRow1);
}
Design
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:paddingTop="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingBottom="1dp"
android:id="#+id/sudokuboard">
</TableLayout>
The key sentence is "A widget must have the LayoutParams of its
parent.", so you have to change the TableRow.LayoutParams to TableLayout.LayoutParams for your table rows.
Hope it works!
More details click Here
Next time use the search bar at the top right corner before posting a question pls. Have a look at the thread below, I believe that is what your looking for.
Table Layout with equal rows height
Hello I have two images and i am merging that images using canvas
here is the below code
Intent intent = getIntent();
//bm is the image get from camera
bm = BitmapFactory.decodeFile(intent.getStringExtra("getdata"));
//this is simple white text image
bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.txt_made_hawk_nelson);
imgSeletedPhoto = (ImageView) findViewById(R.id.imgSelectedPhoto);
int width = bm.getWidth();
int height = bm.getHeight();
int maxWidth = (width > bm1.getWidth() ? width : bm1.getWidth());
int maxHeight = (height > bm1.getHeight() ? height : bm1.getHeight());
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) maxWidth) / width;
float scaleHeight = ((float) maxHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
bmOverlay = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
Canvas canvas = new Canvas(bmOverlay);
//canvas.drawBitmap(bm, 0, 0, null);
canvas.drawBitmap(bm1, 0, 50, null);
imgSeletedPhoto.setImageBitmap(bmOverlay);
} catch (Exception e) {
e.printStackTrace();
}
i want to scale image and image should look like below
but instead of by doing above code it is looking like
XML code of this file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="10" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="7" > // this is covering 70% of screen
<ImageView
android:id="#+id/imgSelectedPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#baca9d"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
can any body help me how to stretch image and make it full from bottom and top side also and put text in center
int targetWidth = 70% of screen width;
int targetHeight = 70% of screen height;
int saclex = (int)((float)targetWidth / (float)bmp1.getWidth());
int sacley = (int)((float)targetHeight / (float)bmp1.getHeight());
apply scale values to bmp1 and create scaled bitmap
saclex = (int)((float)targetWidth / (float)bmp.getWidth());
sacley = (int)((float)targetHeight / (float)bmp.getHeight());
apply scale values bmp and create scaled bitmap. Hope this will help. or else you can use this method to create it.
Bitmap bitmap = Bitmap.createScaledBitmap(mTargetImage, bWidth,
bHeight, false);