Padding is not working on child views of popup window android - java

LayoutInflater layoutinflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.example,null);
final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Here's the child views that I'm trying to set the padding in.
Imageview img = popupView.findViewById(R.id.image);
I tried to set the padding in XML by 10dp in all corners but the image is still the same, I event put it in the java file. img.setPadding(10, 10,10,10); yet still the same imageview.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="#+id/rotaset"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/customborder">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layoutagain"
android:layout_width="145dp"
android:layout_height="163dp"
android:layout_margin="1dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="14dp">
<ImageView
android:id="#+id/example"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:background="#drawable/zonesstyle"
android:scaleType="fitXY"
android:padding="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Simulate" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Related

How to correctly resize a View so its bottom is aligned with other View's top in ConstraintLayout programatically?

I have a ScrollView and a FrameLayout inside a ConstraintLayout. When I click a button, I want the ScrollView layout to resize so the bottom of the ScrollView is aligned with the top of the FrameLayout. However, the constraints won't change when the button is clicked.
I've tried to put the ScrollView inside a LinearLayout instead, but that didn't work either.
When button is clicked:
private void fixTableConstraint() {
ConstraintLayout constraintLayout = findViewById(R.id.home_constraint_layout);
View scrollView = findViewById(R.id.homeScreenScroll);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.homeScreenScroll, ConstraintSet.BOTTOM, R.id.timeMenuOverlay, ConstraintSet.TOP, 0);
constraintSet.applyTo(constraintLayout);
ConstraintLayout.LayoutParams cl = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(cl);
}
XML layout:
<?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:id="#+id/home_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/homeScreenScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
tools:layout_editor_absoluteX="0dp">
<TableLayout
android:id="#+id/homescreenTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ScrollView>
<FrameLayout
android:id="#+id/timeMenuOverlay"
android:layout_width="match_parent"
android:layout_height="152dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<fragment
android:id="#+id/timeMenuFragment"
android:name="multicus.com.scheduleplannerv2.TimeMenuFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Normal Layout:
https://imgur.com/VsfSgVa
What happens in my case when button is clicked:
https://imgur.com/pXT8v6W
What I actually want:
https://imgur.com/6OML007
Your constraints changes will not work because you have
ScrollView layout_height = "match_parent"
You can do it like this
<ScrollView
android:id="#+id/homeScreenScroll"
android:background="#color/green_dark"
android:layout_width="match_parent"
android:layout_height="0dp" // this is important
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/timeMenuOverlay" // and this
tools:layout_editor_absoluteX="0dp">
<TableLayout
...
/>
</ScrollView>
<FrameLayout
android:id="#+id/timeMenuOverlay"
android:visibility="gone" // !!
android:layout_width="match_parent"
android:layout_height="152dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
Instead of changing your layout constraints, change only the visibility.
timeMenuOverlay visibility => GONE, expand your ScrollView to the bottom
timeMenuOverlay visibility => VISIBLE => expected result

How to fix my LinearLayout to scroll down when content exceeds view

I am adding views to my adapter dynamically and when the views exceeds the screen boundaries I am unable to see the full views.
I have tried android:isScrollContainer="true" and android:scrollbars="vertical" without any luck.
public void onAddField(View v) {
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
spin = rowView.findViewById(R.id.type_spinner2);
weightLayout = rowView.findViewById(R.id.myLayout);
getdata();
spin.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arrayList2));
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#E5E5E5"
android:orientation="vertical"
android:scrollbars="vertical"
android:isScrollContainer="true">
Try to use this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:text="TextView" />
</LinearLayout>
</ScrollView>
</LinearLayout>
This way your ScrollView will expand with your LinerLayout.

Unable to See ImageView with RecyclerView in a RelativeLayout

I'm attempting to create a simple image gallery with the thumbnails shown at the bottom using a RecyclerView and the large/detailed photo shown above it in an ImageView.
The problem is - the RecyclerView with the thumbnails is shown and created - however the large/detailed image is never shown, even when hardcoding it's background. I believe this to be an XML related issue - but I cannot seem to spot the problem.
Any suggestions are appreciated.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/black_color"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MMSGallery">
<ImageView
android:id="#+id/detail_photo"
android:background="#drawable/mms_img1"
android:adjustViewBounds="true"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:layout_margin="2dp"
android:layout_width="match_parent"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/detail_photo"
android:background="#color/black_color" />
</RelativeLayout>
Java:
public class SpaceGalleryActivity extends AppCompatActivity {
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_space_gallery);
mImageView = (ImageView) findViewById(R.id.detail_photo);
Glide.with(this)
.load("http://i.imgur.com/zuG2bGQ.jpg")
.placeholder(R.drawable.ic_launcher)
.into(mImageView);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_images);
recyclerView.setHasFixedSize(true);
// recyclerView.setLayoutManager(layoutManager);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
SpaceGalleryActivity.ImageGalleryAdapter adapter = new SpaceGalleryActivity.ImageGalleryAdapter(this, SpacePhoto.getSpacePhotos());
recyclerView.setAdapter(adapter);
}
....
the problem is that you are setting RecyclerView to match_parent.which overlaps your ImageView
so change your layout to
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/detail_photo"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:background="#drawable/mms_img1"
android:scaleType="centerCrop" />
<android.support.v7.widget.RecyclerView
android:layout_marginTop="28dp"
android:id="#+id/rv_images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/detail_photo"
android:layout_below="#id/detail_photo"
android:background="#color/black_color"
/>
</RelativeLayout>
It would work for you.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<ImageView
android:id="#+id/detail_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:background="#drawable/placeholder"
android:scaleType="centerCrop" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_images"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_below="#+id/detail_photo"
android:background="#color/red_500" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".activities.MMSGallery">
<ImageView
android:id="#+id/detail_photo"
android:background="#mipmap/ic_launcher"
android:adjustViewBounds="true"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:layout_margin="2dp"
android:layout_width="match_parent"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_images"
android:layout_below="#+id/detail_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black_color" />
</RelativeLayout>
Changing from a RelativeLayout to a LinearLayout resolved the issue.

How to include a Layout Fragment in Android

I'm trying to add a layout fragment, but I keep getting the error:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
This is how the fragment is contained in rev_lay_drawer_nav.xml:
<?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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/helpAboutLL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/about_bags_bttn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About BAGS" />
<Button
android:id="#+id/help_bttn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/rev_dr_thinner_help_top"
android:text="Help" />
</LinearLayout>
</LinearLayout>
This is how I'm calling it:
mContext = context;
revLayDrawerLayoutInflater = LayoutInflater.from( mContext );
revLayDrawerView = revLayDrawerLayoutInflater.inflate( R.layout.rev_lay_drawer_nav, null, false );
LinearLayout helpAboutLL = (LinearLayout) revLayDrawerView.findViewById(R.id.helpAboutLL);
LinearLayout revDrawerNavViewContainer = new LinearLayout(mContext);
revDrawerNavViewContainer.setOrientation(LinearLayout.VERTICAL);
revDrawerNavViewContainer.addView( helpAboutLL );
What is the right way to go about it?
The fact that you used findViewById means that you already have a view with a parent layout.
And you can't add a view to another layout while it has that other parent view.
It's not clear why you need nested LinearLayouts so change your XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/helpAboutLL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/about_bags_bttn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About BAGS" />
<Button
android:id="#+id/help_bttn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/rev_dr_thinner_help_top"
android:text="Help" />
</LinearLayout>
And inflate and add just that
LinearLayout revDrawerNavViewContainer = new LinearLayout(mContext);
revDrawerNavViewContainer.setOrientation(LinearLayout.VERTICAL);
View helpAboutLL = revLayDrawerLayoutInflater.inflate( R.layout.rev_lay_drawer_nav, null, false );
revDrawerNavViewContainer.addView( helpAboutLL );

Why does dynamically creating a Layout be different from creating it in xml?

This is my xml created file for a layout and it displays as I want it to.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#android:drawable/dialog_holo_light_frame">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/imageSectionCustomRowL"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:id="#+id/titleCustomRowL"
android:paddingRight="10dp"
android:textSize="30sp"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#959595"
android:paddingBottom="10dp"
android:paddingTop="5dp"
android:id="#+id/subTitleCustomRowL"
android:paddingLeft="20dp"
android:paddingRight="10dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And this is more or less the same thing but created dynamically via code
LinearLayout parentLL = (LinearLayout)findViewById(R.id.newsMainPopulationLL);
LinearLayout parentRowLL = new LinearLayout(news.this);
parentRowLL.setOrientation(LinearLayout.VERTICAL);
Drawable bg = news.this.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame);
//Drawable bbg = getDrawable(android.R.drawable.dialog_holo_light_frame);
parentRowLL.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
parentRowLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertDpToPx(120)));
LinearLayout parentRowChildHLL = new LinearLayout(news.this);
parentRowChildHLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
parentRowChildHLL.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout containerForPic = new LinearLayout(news.this);
containerForPic.setLayoutParams(new LinearLayout.LayoutParams(convertDpToPx(75), convertDpToPx(75)));
containerForPic.setOrientation(LinearLayout.VERTICAL);
ImageView picInList = new ImageView(news.this);
picInList.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout contentVerticalLL = new LinearLayout(news.this);
contentVerticalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
contentVerticalLL.setOrientation(LinearLayout.VERTICAL);
TextView headingTV = new TextView(news.this);
headingTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
headingTV.setPadding(convertDpToPx(10), convertDpToPx(10), convertDpToPx(10), 0);
headingTV.setTextColor(Color.parseColor("#000000"));
headingTV.setTextSize(30);
TextView contentTV = new TextView(news.this);
contentTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contentTV.setPadding(convertDpToPx(10), convertDpToPx(5), convertDpToPx(10), convertDpToPx(10));
contentTV.setTextColor(Color.parseColor("#959595"));
contentVerticalLL.addView(headingTV);
contentVerticalLL.addView(contentTV);
parentRowChildHLL.addView(contentVerticalLL);
containerForPic.addView(picInList);
parentRowChildHLL.addView(containerForPic);
parentRowLL.addView(parentRowChildHLL);
parentLL.addView(parentRowLL);
Here convertDptoPx is a int() that works properly. However for some reason the Linear layout parentRowChildLL even though set to LinearLayout.HORIZONTAL for orientation for some reason displays vertically (According to the layout boundaries I see using the developer options setting)
Is there something basic I'm missing out yet? When I see the boundaries I have 3 rows. And also I tried encasing my image view in the layout because otherwise it is not even visible.
Thank You.

Categories