Align ImageView and TextView in a Linear Layout - android

I try to align my ImageView and my TextView but the imageView is higher than my textview.
Xml :
<RelativeLayout
android:layout_marginTop="8dp"
android:id="#+id/secondRelative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/firstRelative"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageNote"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/rank"
android:layout_marginEnd="2dp"
/>
<TextView
android:id="#+id/note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Test"
android:layout_alignBaseline="#id/imageNote"
/>
</RelativeLayout>
What I get on preview :
And what I get on my device :
I want to align ImageView and TextView but I don't know how to do.
Edit :
Thanks to #Bob Malooga and #Rethinavel Pillai.
I used setCompoundDrawables of my TextView and that work perfectly ! (And it's better for perf).
Here is my new code :
<RelativeLayout
android:id="#+id/relativeDetails"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
/>
<TextView
android:id="#+id/note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_below="#+id/time"
/>
<TextView
android:id="#+id/internetNeeded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_below="#+id/note"
/>
</RelativeLayout>
To set the drawable in TextView :
TextView duree = (TextView) convertView.findViewById(R.id.time);
int colorG = Color.parseColor("#FFA500");
Float taille = Utils.convertDpInPx(16, activity);
/*Convert DP in Pixel
public static float convertDpInPx(int dp, Activity activity) {
Resources r = activity.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
return px;
}*/
Drawable imgChrono = getContext().getResources().getDrawable( R.drawable.chrono );
imgChrono.setBounds( 0, 0, Math.round(taille), Math.round(taille)); //To change width and height of drawable
imgChrono.setColorFilter(colorG, PorterDuff.Mode.SRC_ATOP); //To change color of element
duree.setCompoundDrawables(imgChrono, null, null, null );
duree.setText(duration);

try this way:
<LinearLayout
android:layout_marginTop="8dp"
android:id="#+id/secondRelative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageNote"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/rank"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"/>
<TextView
android:id="#+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:layout_alignBaseline="#id/imageNote"
/>
</LinearLayout>

Related

Dynamic list not showing all elements in a linear layout

I have a horizontal linear layout that is filled with TextViews from a dynamic list, but it doesn't show all elements since it seems to be out of the parent's bounds. What I need is to fill the space below if needed.
Here's my code:
The xml:
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView_serie" android:contentDescription="imatge_serie"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom_serie"
android:textSize="20dp"
android:layout_marginLeft="8dp"
android:id="#+id/nom_serie" android:contentDescription="nom_serie" tools:text="Nom_serie"
android:layout_toRightOf="#id/imageView_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "16dp"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:id="#+id/any_serie" android:contentDescription="any_serie" tools:text="any_serie"
android:layout_toRightOf="#id/nom_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sinopsis"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/sinopsi_serie" android:contentDescription="sinopsis_serie" tools:text="sinopsis_serie"
android:layout_toRightOf="#id/imageView_serie"
android:layout_below = "#id/nom_serie"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:id="#+id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
android:layout_centerVertical="true"
style="?android:attr/ratingBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Puntuacio"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/puntuacio" android:contentDescription="puntuacio" tools:text="puntuacio"
android:layout_toRightOf="#id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Casting:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/casting" android:contentDescription="casting" tools:text="casting"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/cast" android:contentDescription="cast" tools:text="cast"
android:layout_toRightOf="#id/casting"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Direccio:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/direccio" android:contentDescription="direccio" tools:text="direccio"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/dir" android:contentDescription="dir" tools:text="dir"
android:layout_toRightOf="#id/direccio"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temporades:"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/nTemporades" android:contentDescription="nTemporades" tools:text="nTemporades"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comentaris"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/titol_comentaris_serie" android:contentDescription="titol_comentari_serie" tools:text="titol_comentari_serie"
/>
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/panell"
android:orientation="horizontal">
</LinearLayout>
And this how I generate the text views from the list:
LinearLayout panell = (LinearLayout) findViewById(R.id.panell);
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView btn = new TextView(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
//btn.setBackgroundColor(Color.rgb(70, 80, 90));
panell.addView(btn, params);
btn = ((TextView) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
}
Here's my :
output
I recommend Google's flexbox-layout project: https://github.com/google/flexbox-layout
A FlexboxLayout (when configured correctly) has the ability to add any number of child TextViews in a line (like LinearLayout would), but will automatically "wrap" to the next line if a child wouldn't fit.
Why not try to use a ListView , if u add the same propreties every time

Add labels above Seekbars steps with different font size, vertically aligned to seekbars thumb

I would like to create a Seekbar, above which there will be text label on each Seekbar step, what it should look like is shown in below image
this is the expected result, for which what i have done,
<RelativeLayout
android:id="#+id/layout_font_size"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtFont_size_hint"
style="#style/textStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Aa" />
<LinearLayout
android:id="#+id/layout_seekbar_interval_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtFont_size_hint"
android:layout_marginLeft="16dp"
android:layout_marginRight="#dimen/margin_16dp"
android:orientation="horizontal"
android:weightSum="5">
<TextView
android:id="#+id/txtSize_14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_14" />
<TextView
android:id="#+id/txtSize_18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_18sp" />
<TextView
android:id="#+id/txtSize_24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_24sp" />
<TextView
android:id="#+id/textSize_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_30sp" />
<TextView
android:id="#+id/txtSize_36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_36sp" />
</LinearLayout>
<SeekBar
android:id="#+id/seekBarSetting_font_size"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_below="#id/layout_seekbar_interval_holder"
android:layout_marginLeft="#dimen/margin_16dp"
android:layout_marginRight="#dimen/margin_16dp"
android:max="4"
android:theme="#style/seekBarYelloStyle" />
</RelativeLayout>
Output of this is
the Layout looks similar to what is expected but, while moving the seekbar thumb, the thumb is not vertically aligned with the TextView so the problem here is the seekbar thumb is not vertically aligend with the text indicating the steps of seekBar.
I had also tried calculating the exact position of TextView by dividing the seekbars width by steps (Here we have 5 steps but first one is on 0th position so i divided the total seekbar Width by 4 and then placed each text accordingly) but didn't got the expected solution.
I am bit confused here, and want a simple solution as soon as possible.
P.S: ScreenShot of output of view created dynamically is also attached for reference
layout for this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="android.sample.MainActivity">
<TextView
android:id="#+id/txtFont_size_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sizes"/>
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:weightSum="5">
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<TextView
android:id="#+id/txtSize1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="15sp"/>
<TextView
android:id="#+id/txtSize_18"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="20sp"/>
<TextView
android:id="#+id/txtSize_24"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="25sp"/>
<TextView
android:id="#+id/textSize_30"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="30sp"/>
<TextView
android:id="#+id/txtSize_36"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="35sp"/>
</LinearLayout>
<SeekBar
android:id="#+id/seekBarSetting_font_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="4"/>
Now what i have done is aligned the width of Seekbar till the center of the last TextView which in this case is txtSize_36
and have set the ems as android:max="4" so there are five possible values(you can change this to as much as you want)
Code for Activity is :
public class MainActivity extends AppCompatActivity {
SeekBar seekBar;
private LinearLayout bar, ll;
TextView txtSize_14, txtSize_36;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBarSetting_font_size);
txtSize_14 = (TextView) findViewById(R.id.txtSize1);
txtSize_36 = (TextView) findViewById(R.id.txtSize_36);
ll = (LinearLayout) findViewById(R.id.ll);
}
float density;
#Override
protected void onResume() {
super.onResume();
ViewTreeObserver vto = ll.getViewTreeObserver();
//****old code (may not work on all orientations)****
/*vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
int width = ll.getMeasuredWidth();
int height = ll.getMeasuredHeight();
ViewGroup.LayoutParams params = seekBar.getLayoutParams();
density = getResources().getDisplayMetrics().density;
int newwidth = (int) (txtSize_14.getLeft() / density);
newwidth = newwidth+ (txtSize_36.getLeft() + txtSize_36.getRight()) / 2;
params.width = newwidth;
seekBar.setLayoutParams(params);
}
});*/
//****new code (should work on all orientations)****
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
seekBar.setPadding(15, 0, 15, 0);
seekBar.setX(((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) - 15);
ViewGroup.LayoutParams params = seekBar.getLayoutParams();
int centerwidth = ((txtSize_36.getLeft() + txtSize_36.getRight()) / 2) - ((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) + 15;
params.width = centerwidth;
seekBar.setLayoutParams(params);
}
});
}
}
Here is screenshot for reference (I have clubbed for all positions of seekbar):
Frankly speaking it took me a while to sort this out using XML only.
The first and last label are differently positioned than the others....
In case you will change the size of the thumb from 15dp - you must also change the margin of first and last TextView and size of Spaces (margin + Space = thumb size)
preview
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="0dp"
android:paddingLeft="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="0dp">
<TextView
android:text="7 days"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="left|center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="0.5"
android:layout_marginLeft="5dp"/>
<Space
android:layout_width="10dp"
android:layout_height="match_parent"/>
<TextView
android:text="1 month"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="1"
android:layout_marginRight="0dp"
android:id="#+id/textView" />
<TextView
android:text="3 months"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="1"
android:layout_marginRight="0dp"/>
<TextView
android:text="1 year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="1"
android:layout_marginLeft="0dp"/>
<TextView
android:text="3 years"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="1"
android:layout_marginLeft="0dp"/>
<Space
android:layout_width="10dp"
android:layout_height="match_parent"/>
<TextView
android:text="5 years"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="right|center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="0.5"
android:layout_marginRight="5dp"/>
</LinearLayout>
</RelativeLayout>

Unable to add margin right to relative layout

I trying to add margin right to my relative layout. I need to add it in to HorizontalScrollView and bind data through java code. I have created layout file and add that file using layout inflator.
Below is my code:
XML Code:
<HorizontalScrollView
android:id="#+id/propertyListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:background="#color/white"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none">
<LinearLayout
android:id="#+id/propertyListViewContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
Below is layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp">
<ImageView
android:id="#+id/propertyImage"
android:layout_width="280dp"
android:layout_height="190dp"
android:scaleType="fitXY"
android:src="#drawable/image_ad_property" />
<RelativeLayout
android:id="#+id/propertyPriceContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/propertyImage"
android:background="#f2f2f2"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="₹ 1.25 cr onwards"
android:textColor="#6a6a6a"
android:textSize="12sp" />
</RelativeLayout>
<TextView
android:id="#+id/propertyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/propertyPriceContainer"
android:layout_marginTop="10dp"
android:text="Park Royale"
android:textColor="#353535"
android:textSize="14sp" />
<TextView
android:id="#+id/propertyPlace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/propertyName"
android:layout_marginTop="4dp"
android:text="Gokhale Marg"
android:textColor="#6b6b6b"
android:textSize="13sp" />
<LinearLayout
android:id="#+id/rateContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/propertyPriceContainer"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/ratingImage1"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/icon_rating_selected" />
<ImageView
android:id="#+id/ratingImage2"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/icon_rating_selected" />
<ImageView
android:id="#+id/ratingImage3"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/icon_rating_selected" />
<ImageView
android:id="#+id/ratingImage4"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/icon_rating_normal" />
<ImageView
android:id="#+id/ratingImage5"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/icon_rating_normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/varifiedContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/rateContainer"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<ImageView
android:layout_width="12dp"
android:layout_height="10dp"
android:layout_gravity="center"
android:src="#drawable/icon_verified" />
<TextView
android:id="#+id/homeVerified"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:text="Verified"
android:textColor="#6b6b6b"
android:textSize="13sp" />
</LinearLayout>
</RelativeLayout>
Below is java file:
LinearLayout propertyListViewContainer = (LinearLayout) findViewById(R.id.propertyListViewContainer);;
for (int i = 0; i < 10; i++) {
RelativeLayout child = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_project_details_property_item, null);
final float scale = getResources().getDisplayMetrics().density; //set height and width
int dpWidthInPx = (int) (280 * scale); //set width
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(dpWidthInPx, RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsBuyRent.setMargins(0, 0, 20, 0);
child.setLayoutParams(paramsBuyRent);
propertyListViewContainer.addView(child);
}
Change :
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(dpWidthInPx, RelativeLayout.LayoutParams.WRAP_CONTENT);
to:
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
OR
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Layout inflater's inflate method has a second parameter that takes root view as a parameter. If you don't provide it the margins will be ignored. So provide container view as the second parameter there. Something like this:
for (int i = 0; i < 10; i++) {
RelativeLayout child = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_project_details_property_item, propertyListViewContainer);
}
This way there's no need to add the inflated view to the parent layout at the end as it will automatically be attached to the provided layout keeping it's margins intact.

get my image same height as the rest of my edittext controls

I have an image that is taking up all of the screen. I have tried increasing and decreasing the controls weight, adjust min and max heights and widths but i cant get the image height the same as the other controls.
<TextView
android:id="#+id/header_screenName_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/appBodyDefault"
android:textSize="#dimen/titleFontSize"
android:textColor="#color/white"
android:gravity="left"
android:text="#string/activity_newuser_header_string" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:background="#color/yellow"
android:layout_margin="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/firstName_editText"
android:layout_weight="1"
android:hint="First Name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/lastName_editText"
android:layout_weight="1"
android:hint="Last Name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/passwordOne_editText"
android:layout_weight="1"
android:hint="Password" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordTwo_editText"
android:layout_weight="1"
android:hint="Re-type Password" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/email_editText"
android:layout_weight="1"
android:hint="Email" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/btn_profile"
android:clickable="false"
android:adjustViewBounds="true" />
</LinearLayout>
</LinearLayout>
In android scale the image in a good way is hard, I'm using this method for that the image use the full space in the view without lose resolution.
(is only use this in the ImageView)
public static void ajustarImageViewToImage(final ImageView iv) {
ViewTreeObserver observer = iv.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Fazendo o redimensionamento das imagens para de acordo com o VIEW
Drawable drawable = iv.getDrawable();
int ih = drawable.getIntrinsicHeight();
int iw = drawable.getIntrinsicWidth();
int x = ((iv.getWidth() * ih) / iw) + 1;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, x);
lp.weight = 1;
lp.setMargins(0, 0, 0, 10);
iv.setLayoutParams(lp);
}
});
}
if you don't care about the resolution and the scale, try use android:scaleType="fitXY" in your ImageView on the XML.

android:ellipsize in ICS text continues after ellipsis

I have a textview that I am programmatically setting maxLines with and then setting it to android:ellipsize="end". It works partially, but for some reason the text that is being truncated continues for a few characters after the ellipsis in 4.2.2. It is the actual text that is being truncated, not new characters.
It also is not working properly in 2.3.6 as it adds an ellipsis for each section with a line break or "\n".
I am not using any custom fonts, I have seen that has caused problems before.
Here is where I calculate and set the maxlines:
final TextView reviewView = (TextView) view.findViewById(R.id.textViewPosition3);
ViewTreeObserver observer = reviewView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int maxLines = (int) reviewView.getHeight() / reviewView.getLineHeight();
reviewView.setMaxLines(maxLines);
reviewView.setEllipsize(TruncateAt.END);
reviewView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//reviewView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentTestLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/restaurantImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/test"
android:layout_marginTop="-10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
/>
<ImageView
android:id="#+id/squareBlack"
android:layout_alignRight="#id/restaurantImage"
android:layout_alignTop="#id/restaurantImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/square_black_small"
/>
<TextView
android:id="#+id/textViewPosition"
android:layout_alignRight="#id/squareBlack"
android:layout_alignLeft="#id/squareBlack"
android:layout_alignBottom="#id/squareBlack"
android:layout_alignTop="#id/squareBlack"
android:layout_margin="1dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a test"
android:textSize="25sp"
android:textColor="#a6cbff"
android:textStyle="bold"
/>
<ImageView
android:id="#+id/squareBlue"
android:layout_toLeftOf="#id/squareBlack"
android:layout_alignTop="#id/squareBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/square_blue_small"
/>
<TextView
android:id="#+id/textViewPosition1"
android:layout_alignRight="#id/squareBlue"
android:layout_alignLeft="#id/squareBlue"
android:layout_alignBottom="#id/squareBlue"
android:layout_alignTop="#id/squareBlue"
android:layout_margin="1dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7.5"
android:textSize="25sp"
android:textColor="#902515"
android:textStyle="bold"
/>
<TextView
android:id="#+id/restaurantName"
android:layout_alignLeft="#id/restaurantImage"
android:layout_alignBottom="#id/restaurantImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restaurant Name"
android:textSize="25sp"
android:textColor="#fff"
android:textStyle="bold"
android:background="#drawable/greybar"
/>
<TextView
android:id="#+id/textViewPosition2"
android:layout_below="#id/restaurantImage"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="February 15, 2013"
android:textColor="#555"
android:textStyle="bold"
android:textSize="15sp"
/>
<Button android:id="#+id/readReview"
android:background="#drawable/readreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Read the Review"
android:textColor="#fff"
android:textColorHighlight="#ccc"
android:textSize="15sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/textViewPosition3"
android:layout_below="#id/textViewPosition2"
android:layout_above="#id/readReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColorLink="#C90404"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:textColor="#000"
/>
</RelativeLayout>
I solved this programatically, but I'm not sure it's the best solution as it feels like more of a hack. It seems to be working though.
ViewTreeObserver observer = reviewView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int maxLines = (int) reviewView.getHeight() / reviewView.getLineHeight();
reviewView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
if(reviewView.getLineCount() > maxLines)
{
int lineEndIndex = reviewView.getLayout().getLineEnd(maxLines - 1);
String text = reviewView.getText().subSequence(0, lineEndIndex-3) +" ...";
reviewView.setText(text);
}
}
});
put this in xml
android:singleLine="true"
android:ellipsize="end"
android:maxLines="1"

Categories

Resources