Textview layout center vertical and horizontal in ScrollView by programmatically - android

Application has scrollview in XML layout.
<ScrollView
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
>
</ScrollView>
I want to one textView into this scrollView center.
I tried This code, set horizontal center, but vertical top.. I want both centered.
LinearLayout l1 = new LinearLayout(getActivity());
l1.setOrientation(LinearLayout.VERTICAL);
l1.setGravity(Gravity.CENTER);
l1.setBackgroundColor(Color.WHITE);
TextView errorView = new TextView(getActivity());
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
errorView.setText("TextView");
errorView.setTextColor(Color.BLACK);
errorView.setLayoutParams(params);
errorView.setGravity(Gravity.CENTER);
l1.addView(errorView);
scrollViewCon.addView(l1, lparams);
Why is this happening?

Change your xml as shown below -
Add fillViewport as true.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:fillViewport="true"
>
Removed the relative layout addition part as it is not required.

Related

How I can use java code for creating design on android

I am trying to put TextView into FrameLayout into Linear layout.
When I do it using XML it's OK, but using Java code there are problems.
So I can't move text view using layout_gravity in Java code. Also, I don't understand why text matching parent when I use a parameter wrap content. must be
XML code
<LinearLayout
android:id="#+id/chatBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_gravity="right"/>
</FrameLayout>
Java code
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(this);
tv.setText(message);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
p.gravity = Gravity.LEFT; tv.setLayoutParams(p);
fl.addView(tv);
LinearLayout chatbox = (LinearLayout) findViewById(R.id.chatBox);
chatbox.addView(fl, 0);
you are adding TextView tv to FrameLayout, so you should use FrameLayout.LayoutParams for this TextView
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
p.gravity = Gravity.LEFT;
tv.setLayoutParams(p);
fl.addView(tv);
You need to LayoutParams for your Textview not Linearlayout Params. That is causing the problem. You can create it like;
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))

Why layout_gravity not work for TextView?

As the title description, when set layout_gravity for TextView in LinearLayout, this property does not work well, and when I set it through LinearLayout.LayoutParams.gravity, it does not work as well. But for ImageView with same way, it works.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.openssl.LayoutTestActivity">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
android:background="#color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="xml"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/ic_launcher_round"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Java code
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
TextView textView = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.setMargins(10, 0, 10, 15);
textView.setLayoutParams(layoutParams);
textView.setText("Code");
linearLayout.addView(textView);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher_round);
LinearLayout.LayoutParams layoutParams1 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 10, 15);
layoutParams1.gravity = Gravity.BOTTOM;
imageView.setLayoutParams(layoutParams1);
linearLayout.addView(imageView);
in the TextView you are creating programmatically, you are giving the TextView a 15dp bottom margin, If you want to make the TextView itself at the bottom, set the gravity of the layoutParams without giving the view unnecessary margins:
layoutParams.setMargins(10, 0, 10, 0);
And talking about the textView in xml, if you want to align the text itself of the textView to be at the bottom, set the height to match_parent and set view gravity to bottom:
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="xml"/>
for the TextViewin the code, try to set the height to match_parent and give the view a gravity of BOTTOM inside the layoutParams :
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.MATCH_PARENT, Gravity.BOTTOM);

View going out of screen in dynamic image views inside horizontal linear layout

I am programmatically inflating combination of ImageView and TextView inside my LinearLayout.My linear layout is under a horizontal scroll view. But when I set layout_gravity="center_horizontal" to the parent of my linear layout, the content inside linear layout going out of the screen.
See what is happening:
I am using layout_gravity to achieve this
Without layout_gravity it appears like this
But if I use layout_gravity and there are many items inside that HorizontalScrollView then 1 or 2 items don't show. You can see the first uploaded image for understanding the scenario. In the first image I can't scroll more towards right and I can only see see of the ImageView and TextView combination and moreover, one more such combination is out of the screen completely.
Here's the xml structure
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none"
android:id="#+id/hotel_detail_services">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/imageViewLayout"
>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
Java manipulation
#Override
protected void onPostExecute(List<hotel_detail_model> hotel_detail_models) {
super.onPostExecute(hotel_detail_models);
LinearLayout layout = findViewById(R.id.imageViewLayout);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
if (hotel_detail_models.get(0).getTv()==1)
addHotelSpeciality(layout,"TV",R.drawable.tv);
if (hotel_detail_models.get(0).getAc()==1)
addHotelSpeciality(layout,"AC",R.drawable.ac);
if (hotel_detail_models.get(0).getGeyser()==1)
addHotelSpeciality(layout,"Geyser",R.drawable.geyser);
if (hotel_detail_models.get(0).getBus()==1)
addHotelSpeciality(layout,"BUS",R.drawable.bus);
if (hotel_detail_models.get(0).getCab()==1)
addHotelSpeciality(layout,"CAB",R.drawable.cab);
if (hotel_detail_models.get(0).getFood()==1)
addHotelSpeciality(layout,"FOOD",R.drawable.food);
if (hotel_detail_models.get(0).getRailway()==1)
addHotelSpeciality(layout,"Train",R.drawable.train);
if (hotel_detail_models.get(0).getAirport()==1)
addHotelSpeciality(layout,"Airport",R.drawable.airport);
if (hotel_detail_models.get(0).getMedical()==1)
addHotelSpeciality(layout,"Medical",R.drawable.medical);
progressDialog.dismiss();
}
//Add Image Dynamically
private void addHotelSpeciality(LinearLayout layout, String image_name, int image_id) {
LinearLayout linearLayout = new LinearLayout(hotel_details.this);
ImageView image = new ImageView(hotel_details.this,null,R.style.dynamicImageHotel);
TextView textView = new TextView(hotel_details.this,null,R.style.dynamicTextHotel);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen._25sdp),
getResources().getDimensionPixelSize(R.dimen._25sdp));
params.weight = 1.0f;
params.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params3.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
textView.setLayoutParams(params2);
image.setImageDrawable( getResources().getDrawable(image_id));
textView.setText(image_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextAppearance(R.style.dynamicTextHotel);
}else {
textView.setTextAppearance(hotel_details.this,R.style.dynamicTextHotel);
}
// Adds the view to the layout
linearLayout.addView(image);
linearLayout.addView(textView);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params3); linearLayout.setPadding(getResources().getDimensionPixelSize(R.dimen._15sdp),0,0,0);
layout.addView(linearLayout);
}
Isn't that what you are trying to achieve by using HorizontalScrollView? ScrollViews allow content to be larger than the physical display so when you add enough "hotelSpecialities" it goes out of the screen and you can scroll through it. If you are trying to fit all the content on the screen then you would not use a HorizontalScrollView.
Okay now that I know what you are trying to achieve, putting your ScrollView inside a RelativeLayout will give you that.
This is what worked out for me
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<HorizontalScrollView
android:id="#+id/hotel_detail_services"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/imageViewLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
You can remove the parent LinearLayout for imageViewLayout too if you don't need it. Let me know if you need anymore help!

How to Left Align TableRow objects?

They currently appear on the center.
Android Java Code
public void populateTable() {
for(DoctorBean post: beanPostArrayList){
String result = "";
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ImageView docImageView = new ImageView(getActivity());
docImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
docImageView.setPadding(5, 5, 5, 5);
docImageView.setImageResource(R.drawable.ic_doctor);
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(5, 5, 5, 5);
result += "\n" + post.getHeroName();
textView.setGravity(Gravity.LEFT);
docImageView.setForegroundGravity(Gravity.LEFT);
row.setGravity(Gravity.LEFT);
textView.setText(result);
row.addView(docImageView);
row.addView(textView);
tableLayout.addView(row);
}
}
Layout Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*">
</TableLayout>
</ScrollView></LinearLayout>
So there I want to align the ImageView and the TextView to the left.
An alternative approach should solve your issue
You don't need to add explicit scroll for a TableView
TableView can be directly added to your main LinearLayout. As you have already specified the height of MapView
Suggested Modified xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLL"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
Corresponding code change
LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.mainLL);
//Table Layout parameters
TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
TableLayout tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);
TableRow trHead = new TableRow(context);
LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
trHead.setLayoutParams(tableRowParams);
TextView nameHead = new TextView(context);
nameHead.setText("Content left");
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.LEFT);//or Gravity.CENTER according to your requirement
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText("Content right");
detailHead.setGravity(Gravity.RIGHT);//or Gravity.CENTER according to your requirement
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
//add table layout to linear layout
linearLayout.add(tableLayout);
NOTE:
Many values have been referred from resource files, You can either
neglect/replicate those. I have added two textviews. You may need to
change according to your requirement

Android - Dynamically added linearlayout not wrapping to height of children added later, dynamically

I am adding 7 linear layouts to a vertical linear layout, and in each one, I am looping through a cursor and adding a view that has two textviews vertically, for each row of the cursor.
Everything is working fine, except the linear layout is not wrapping to the content of the child view.
I had a feeling it's because I was setting wrap_content in the layoutparams before adding the child views, but I updated the linearlayouts layoutparams just before adding it to the main view and it still doesn't wrap.
Dynamically adding code
while(c.moveToNext()) {
if(!(c.getString(c.getColumnIndex("date")).equals(startDate))) {
if(thisLin != null) {
LayoutParams thisLinParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
thisLin.setLayoutParams(thisLinParams);
seven_wrapper.addView(thisLin);
}
thisLin = new LinearLayout(getActivity());
// originally I was setting layoutparams here but I removed this
// when I thought it wasn't wrapping properly as there were no child views
// at this stage
//LayoutParams thisLinParams = new LayoutParams(
// LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//thisLin.setLayoutParams(thisLinParams);
TextView t = new TextView(getActivity());
t.setText(c.getString(c.getColumnIndex("date")));
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
t.setLayoutParams(lparams);
thisLin.addView(t);
}
sevenItem = inflater.inflate(R.layout.seven_item, container, false);
TextView time = (TextView) sevenItem.findViewById(R.id.seven_item_time);
TextView name = (TextView) sevenItem.findViewById(R.id.seven_item_name);
time.setText(c.getString(c.getColumnIndex("time")));
name.setText(c.getString(c.getColumnIndex("name")));
thisLin.addView(sevenItem);
}
seven_wrapper.addView(thisLin);
c.close();
Fragment View
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/seven_day_rows_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</HorizontalScrollView>
Child view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/seven_item_time"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<TextView android:id="#+id/seven_item_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/seven_item_time"/>
</RelativeLayout>
I was being an idiot.
My child view wrapped to parent, and the parent wrapped to the child, so it still remained the original size.
Changed the child to wrap_content on the height and it all worked.

Categories

Resources