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.
Related
I have a layout XML and when I click on a floatbutton, I create a layout programmatically, so that the data of this layout is passed to this new one.
Here's my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundnewlayout"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#drawable/img_interna" />
<TextView
android:id="#+id/txtTitle_avaliacao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Medicamento"
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/txtTitle_avaliacao"
android:layout_marginBottom="40dp"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/linearCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_margin="5dp"
android:elevation="7dp"
android:minHeight="200dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtNome_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Nome:"
android:textSize="10sp" />
<AutoCompleteTextView
android:id="#+id/edtNome_medicamento"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtNome_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="Paracetamol"
android:maxLines="1"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/txtQtd_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtNome_medicamento"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Quantidade:"
android:textSize="10sp" />
<EditText
android:id="#+id/edtQtd_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtQtd_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="3"
android:maxLines="1"
android:minWidth="50dp"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/txtPosologia_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/edtQtd_medicamento"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Posologia:"
android:textSize="10sp" />
<EditText
android:id="#+id/edtPosologia_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPosologia_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="2 vezes ao dia a cada 8 horas"
android:minWidth="50dp"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fltNewLayout"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/cardView"
android:layout_marginRight="5dp"
android:layout_marginTop="170dp"
android:backgroundTint="#color/colorPrimaryDark"
android:clickable="true"
android:src="#drawable/icone_check"
app:fab_colorPressed="#color/colorPrimary"
app:rippleColor="#android:color/white" />
<LinearLayout
android:id="#+id/linearReceita"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fltNewLayout"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</FrameLayout>
</RelativeLayout>
When I click on the floatbutton, I create a new layout with the data of this layout that is fixed.
But, the fields of the layout created programmatically, overlap, how to fix?
The method below is as follows:
private void createNewLayout(){
linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5,15,5,5);
//linearLayout.setLayoutParams(params);
cardView = new CardView(getContext());
cardView.setLayoutParams(linearLayout.getLayoutParams());
cardView.setPadding(10,10,10,10);
cardView.setMaxCardElevation(6);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cardView.setElevation(6);
}
TextView txtNomeTitle = new TextView(getContext());
txtNomeTitle.setText("Nome");
txtNomeTitle.setLayoutParams(linearLayout.getLayoutParams());
cardView.addView(txtNomeTitle);
TextView txtNomeMedicamento = new TextView(getContext());
txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
edtNomeMed1.setText("");
txtNomeMedicamento.setLayoutParams(params);
cardView.addView(txtNomeMedicamento);
linearLayout.addView(cardView);
}
I solved the issue by adding a new linear layout, this linear adds the textviews vertically, after adding these fields, it is inserted into the cardview.
/**
* New added Linearlayout
*/
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);
Change the code below:
private void createNewLayout() {
linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 5, 5);
cardView = new CardView(getContext());
cardView.setLayoutParams(params);
//cardView.setPadding(10, 10, 10, 10);
cardView.setMaxCardElevation(6);
/**
* New added Linearlayout
*/
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);
cardView.addView(linearCard);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cardView.setElevation(6);
}
TextView txtNomeTitle = new TextView(getContext());
txtNomeTitle.setText("Nome");
txtNomeTitle.setLayoutParams(params);
//cardView.addView(txtNomeTitle);
linearCard.addView(txtNomeTitle);
TextView txtNomeMedicamento = new TextView(getContext());
txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
edtNomeMed1.setText("");
txtNomeMedicamento.setLayoutParams(params);
txtNomeMedicamento.setPadding(10, 10, 10, 10);
// cardView.addView(txtNomeMedicamento);
linearCard.addView(txtNomeMedicamento);
//cardView.addView(linearLayout);
linearLayout.addView(cardView);
}
I was inserting the fields directly into the cardview, without a layout inside it, which made the fields overlap.
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
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>
Create multiple linear layouts and text views programmatically
I have tried to explain please see the code of xml file to understand the question
See image for my desired output
The xml code below is what I want exactly but programmatically
<RelativeLayout
android:id="#+id/fl_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
</LinearLayout>
</RelativeLayout
I want to achieve the same output but programmatically
Try This
add LinearLayout into your xml
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white">
</LinearLayout>
and change in your Java file like this
LinearLayout ll_main = (LinearLayout) findViewById(R.id.ll_main);
for(int i= 0; i <5 ;i++) {
LinearLayout parent = new LinearLayout(Main.this);
LinearLayout.LayoutParams param= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
parent.setLayoutParams(param);
parent.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(Main.this);
tv.setText("T1");
TextView tv2 = new TextView(Main.this);
tv2.setText("T2");
parent.addView(tv);
parent.addView(tv2);
ll_main.addView(parent);
}
i just got my answer and its below
LinearLayout ll_main = (LinearLayout) findViewById(R.id.linear);
ll_main.setVisibility(View.VISIBLE);
int j = 5;
final LinearLayout[] linearlayout = new LinearLayout[j];
for (int i = 0; i < j; i++) {
LinearLayout parent = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
parent.setLayoutParams(param);
parent.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("T1");
TextView tv2 = new TextView(this);
tv2.setText("T2");
parent.addView(tv);
parent.addView(tv2);
linearlayout[i] = parent;
ll_main.addView(parent);
}
}
}
I am tryin to add ten relativeLayouts dynamically by inflating the xml , but the relative layouts getting overlapped. how to change the margins of the inflating xml dynamically via code.
Below is the code reuse.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/aaa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
<ImageView
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ballaya" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="50dp"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:text="Srimannarayana earns Rs 8.65 cr on day 1"
android:textColor="#FFFFFF"
android:textSize="11sp"
android:textStyle="bold" />
<View
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#FFFFFF" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/popover_bg_rghtarw" />
</RelativeLayout>
code of item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#06516E" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#49C7F9">
<TextView
android:id="#+id/HeadLines"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latest News"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/newsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp" >
</RelativeLayout>
</RelativeLayout>
Java Code
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.item);
newsLayout =(RelativeLayout)findViewById(R.id.newsLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout row[] = new RelativeLayout[10];
TextView text[]= new TextView[10];
for(int i=0;i<10;i++){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
row[i] = (RelativeLayout) inflater.inflate(R.layout.reuse,null);
params.setMargins(0, 50, 0, 0);
newsLayout.addView(row[i],params);
text[i]= (TextView) row[i].findViewById(R.id.text);
text[i].setText("AAAAAAA");
}
You can use LinearLayout instead of RelativeLayout for newsLayout and make the orientation as VERTICAL.
And instead of newsLayout =(RelativeLayout)findViewById(R.id.newsLayout);
write: newsLayout =(LinearLayout)findViewById(R.id.newsLayout);
And rest of the code will be as it is.
This will solve your problem.