I have 4 imageview in one rows but i wanted to visible and gone need to do based on response. so how to remove unused space which i hardcoded layout.
<ImageView
android:id="#+id/imgFacebookUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/facebook_login_guest"
app:layout_anchor="#id/appbar"
android:visibility="gone"
app:layout_anchorGravity="bottom|center" />
<ImageView
android:id="#+id/imgLinkdinUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="200dp"
android:src="#drawable/icon_linkdin"
app:layout_anchor="#id/appbar"
android:visibility="gone"
app:layout_anchorGravity="bottom|center" />
public void setPadding (int left, int top, int right, int bottom)
Sets the padding. The view may add on the space required to display
the scrollbars, depending on the style and visibility of the
scrollbars. So the values returned from getPaddingLeft(),
getPaddingTop(), getPaddingRight() and getPaddingBottom() may be
different from the values set in this call.
Finally
ImageView ImageViewObj = (ImageView) findViewById(R.id.imgLinkdinUrl);
ImageViewObj.setPadding(0, 0, 0, 0);
This deletes all paddings (all sides) for your imgLinkdinUrl ImageView:
ImageView linkedInUrlImage = (ImageView) findViewById(R.id.imgLinkdinUrl);
linkedInUrlImage.setPadding(0, 0, 0, 0);
Related
How do I create an edit text with currency on left and value on right.
this is the image
Currency can also be part of edit text? or currency is a different edit text?
I hope it can only be 1 edit text.
Why not create a LinearLayout that is horizontally oriented
and align a TextView to the left and an Edit Text on the right?
Use EditText and set this property android:drawableLeft="#mipmap/ic_launcher"
code
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#mipmap/ic_launcher"
android:hint="Add money"
android:gravity="center"
android:padding="3dp"/>
If you need to dynamically change this image then use this code inside your java file
params :- left, top, right, bottom
editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.drawableLeft, 0, 0, 0);
Use of SpannableString you can Achieve this.
In activity_main.xml file.
<EditText
android:id="#+id/edtCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Test"
android:textColor="#000"
android:textSize="26sp" />
Add this in you MainActivity.java.
EditText edtCurrency = (EditText) findViewById(R.id.edtCurrency);
SpannableString spannableString = new SpannableString("USD 123.456");
spannableString.setSpan(new UsdSpannableSuperScript((float) 1.0), 2, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
edtCurrency.setText(spannableString);
Here is UsdSpannableSuperScript.java.
public class UsdSpannableSuperScript extends SuperscriptSpan {
//divide superscript by this number
protected int fontScale = 2;
//shift value, 0 to 1.0
protected float shiftPercentage = 0;
//doesn't shift
UsdSpannableSuperScript() {
}
//sets the shift percentage
UsdSpannableSuperScript(float shiftPercentage) {
if (shiftPercentage > 0.0 && shiftPercentage < 1.0)
this.shiftPercentage = shiftPercentage;
}
#Override
public void updateDrawState(TextPaint tp) {
//original ascent
float ascent = tp.ascent();
//scale down the font
tp.setTextSize(tp.getTextSize() / fontScale);
//get the new font ascent
float newAscent = tp.getFontMetrics().ascent;
//move baseline to top of old font, then move down size of new font
//adjust for errors with shift percentage
tp.baselineShift += (ascent - ascent * shiftPercentage)
- (newAscent - newAscent * shiftPercentage);
}
#Override
public void updateMeasureState(TextPaint tp) {
updateDrawState(tp);
}
}
Here is Screen.
With only single EditText can also work. For that you will have to set prefix and fetch value from edittext with respect to the prefixCount.
You can try this way. This is a bit better approach.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/grey"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="USD" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#null"
android:singleLine="true" />
</LinearLayout>
Output : Same as image above you mentioned.
With single EditText another approach. You can have a image of currency "USD" and set drawableLeft to edittext.
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:drawableLeft="#drawable/dollar_sign"
android:drawablePadding="5dp"
android:inputType="number"
android:padding="10dp"
android:singleLine="true" />
Currency currency = new Currency(Locale.US)
final String usd = currency.getCurrencyCode(); //returns USD
To use, either extend a TextView and manually add this logic, or do so through code
I have a tabWidget defined as:
<TabWidget
android:layout_alignParentBottom="true"
android:id="#android:id/tabs"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:weightSum="5"
android:background="#drawable/bottom_bar_without_bg"
/>
Now I am adding custom ImageViews in it:
Drawable d = getResources().getDrawable(R.drawable.tabbar_button_home);
ImageView img1 = new ImageView(this);
img1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img1.setImageDrawable(d);
TabHost.TabSpec tSpecHome = tHost.newTabSpec("timeline");
tSpecHome.setIndicator(img1);
tSpecHome.setContent(new MyTabContent(getBaseContext()));
tHost.addTab(tSpecHome);
The problem with this code is that all the images are spaced with each other in middle.
e.g abcdef
I want them to stretch to full width and have space in between them e.g a b c d e
Update the code as follows:
Drawable d = getResources().getDrawable(R.drawable.tabbar_button_home);
ImageView img1 = new ImageView(this);
img1.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f)); // this line changed
img1.setImageDrawable(d);
Explanation:
The third parameter in the LayoutParams constructor is the weight. Each image view now has a weight of 1.0f. So they will spread across the layout.
You have them with WRAP_CONTENT width.
Instead they should be set to a width of 0dp and each one have a layout gravity of 1.
I am working on an android application.
In an XML Layout I need to do the following:
I have a list view at the top (listViewProducts) , and another Relative view under it (receiptSection).
The list view should take as much space as it has items. And the rest is taken by the receiptSection.
So for example if I have 2 items in the listViewProducts:
The list view is as big as the 2 items and the rest is taken by the receiptView.
If I add another item, the list view now take more space and push the receiptView lower:
However if I add a lot more items, I want the list view height to stop growing to leave a minimum height for the receiptView that cannot go smaller:
As you see in the picture, the receiptVIew has a minimum height of 50dp. once the receipt view get to that height, it should stop shrinking and now the list view has a fixed size based on the remaining of the space. The rest will be scrollable.
What I have tried
I created a list view. I have android:layout_alignParentTop="true" and android:layout_height="wrap_content".
This will make it grow with its content and its at the top of the view
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ListView>
THen I created a RelativeLayout that will hold the checkout_receipt_view that is in a seperate xml layout file.
For this view I have android:layout_alignParentBottom="true" and android:layout_below="#id/listViewProducts" that will make it go under the list view and align with the bottom of the view.
I also used android:minHeight="50d" in order to set the minimum height of the receiptSection.
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/listViewProducts"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>
The listViewProducts is growing with the items, and the receiptView is taking the remaining space correctly.
The problem
however the minimum height did not work. The list view keeps on growing infinitely and the receiptSection will be pushed out of the view.
Is there a way I can make the listView stop growing when the receiptView reaches 50dp?
Thanks a lot for any help.
Unfortunately I think your best bet is to do this by making a custom view that extends ListView and overrides onMeasure.
public class CustomView extends ListView {
#Override
int maxHeight = 0;
View parentView = (RelativeLayout) (or whatever) getParent();
if (parentView != null){
maxHeight = parentView.getHeight() - 50;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Try swapping the 'layout_below'.
What you are actually saying is the following: please put my relativelayout BELOW the listview. If you want your listview to respect the height of the relativelayout, you'll have to say in the listview:
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/receiptSection"
android:layout_alignParentTop="true" >
</ListView>
And your relativelayout:
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
</TableLayout>
This is the original XML, i just need to add more. i have an imageView that's big , so it needs to be shrunk down and copied 16 times into a 4x4 grid. I can only get it to go 4 images in one column
I don't understand the problem with the large image, but I will tell you my suggestion:
there are multiple possible solutions:
Since you have 16 imageViews that you wish to create, you can use a GridView together with a BaseAdapter . If it's important for you to see it in the xml, use isInEditMode for a custo GridView, and set the adapter there to be your adapter with fake items. You should be aware of problems with the sizes of the columns/rows on the gridView , especially when changing orientations.
Another alternative could be the GridLayout
If you insist on using the TableLayout, you can have 4 TableRow instances, each has a weight of 1 . in each of them , add 4 imageViews and there each has a weight of 1.
Add 4 TableRow's and put your ImageView's into them.
Or you can create this grid from code. Like this:
LinearLayout container = null;
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
for (int i = 0; i < 16; ++i) {
if (i % 4 == 0) {
container = new LinearLayout(getActivity());
container.setOrientation(LinearLayout.HORIZONTAL);
mGrid.addView(container);
}
LinearLayout view = (LinearLayout) mLayoutInflater
.inflate(R.layout.view_item, container, false);
//populate the view in loop
view.setLayoutParams(params);
container.addView(view);
}
I have a relative layout with 3 ImageViews. The first one is a square image, the second is just used for spacing, and the third one is another square image.
Here is the xml code for that layout:
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/radioSV"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/radioLayout"
android:orientation="vertical"
android:gravity="center">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/sssImageView"
android:src="#drawable/radio_sss_400_r"
android:layout_width="10sp"
android:layout_height="10sp"
android:layout_gravity="center"
android:scaleType="centerCrop">
</ImageView>
<!-- spacing -->
<ImageView
android:id="#+id/spacing1"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="10dip"
android:layout_below="#id/sssImageView">
</ImageView>
<ImageView
android:id="#+id/gaImageView"
android:src="#drawable/radio_ga_400_r"
android:layout_width="10sp"
android:layout_height="10sp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:layout_below="#id/spacing1">
</ImageView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Now, I want my app to be useful for various screen densities, so therefore, in the java file, I ask for the density and use a switch-case statement afterwards. In this statement, the size (width, height) of the 2 ImageViews (sssImageView and gaImageView) have to be changed.
For instance, if the density of the screen is high, I want the width and height of the ImageViews to be 200sp. I've just put 10sp in the xml as a 'standard' value.
This is the part of the use-case statement for high screen density:
case DisplayMetrics.DENSITY_HIGH:
layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
sssImageView.setLayoutParams(layoutParams);
sssImageView.setMaxHeight(val_high);
sssImageView.setMaxWidth(val_high);
gaImageView.setLayoutParams(layoutParams);
gaImageView.setMaxHeight(val_high);
gaImageView.setMaxWidth(val_high);
break;
Also, val_high is 200sp:
int val_high = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 200, this.getResources().getDisplayMetrics());
Now, the part with the ImageView sssImageView works perfectly. It correctly enlarges the image from 10sp to 200sp (don't worry - the image is not 10sp originally!).
The problem is the fact that the other ImageView, gaImageView, puts itself on top of sssImageView and destroys the layout. If I comment out the 3 lines with gaImageView, it is on its correct place in the layout, but is still small (10sp) of course.
I have also tried the opposite: commenting out the 3 lines with sssImageView and only manipulating gaImageView. Now the top ImageView (sssImageView) is on its correct place and small as expected. However, the bottom ImageView, gaImageView, positions itself on top of sssImageView, and not below as written in the xml layout file.
What is wrong here?
I managed to solve this problem.
Here is the code for the DENSITY_HIGH case statement:
case DisplayMetrics.DENSITY_HIGH:
display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
// 0.36 is a scaling factor
int val_high = (int) (height*0.36);
sssImageView.setId(1);
gaImageView.setId(2);
spacing.setId(3);
layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
layoutParams2 = new RelativeLayout.LayoutParams(val_high, val_high);
layoutParams3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 10, this.getResources().getDisplayMetrics()));
sssImageView.setMaxHeight(val_high);
sssImageView.setMaxWidth(val_high);
relativeLayout.updateViewLayout(sssImageView, layoutParams);
layoutParams3.addRule(RelativeLayout.BELOW, sssImageView.getId());
relativeLayout.updateViewLayout(spacing, layoutParams3);
layoutParams2.addRule(RelativeLayout.BELOW, spacing.getId());
gaImageView.setMaxHeight(val_high);
gaImageView.setMaxWidth(val_high);
relativeLayout.updateViewLayout(gaImageView, layoutParams2);
break;
So generally, it looks like I had to "re-create" the xml file programmatically.