Programmatically Adding views in inflated layout - android

I am trying to add views in linear layout programmatically which is a part of inflated view but after adding views in linear layout it is not displaying
MainActivity code:-
final LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.suscription_alert,null);
LinearLayout ll = view.findViewById(R.id.LinearLayout);
for(int i =0;i<4;i++) {
TextView t = new TextView(MainActivity.this);
t.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
t.setText("Hello");
ll.addView(t);
}
view.invalidate();
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.setView(view)
.create();
Objects.requireNonNull(alert.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
alert.show();
Subscription Alert layout:-
<?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"
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:layout_gravity="center"
android:text="SUBSCRIPTIONS"
android:textColor="#F5D90A"
android:textSize="23sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="#+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingTop="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
output:
programmatically added views are not showing here,
How to solve this problem and why it is happening anybody have any idea?

Your LinearLayout needs to have height greater than 0 to add views at run-time otherwise it won't be able to calculate the height.
For example make it wrap_content
<?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"
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:layout_gravity="center"
android:text="SUBSCRIPTIONS"
android:textColor="#F5D90A"
android:textSize="23sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="#+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>

Related

Android add view blank

I add a view to my project like so:
View inflater = getLayoutInflater().inflate(R.layout.fragment_miles, null, true);
//Add views
LinearLayout contentDraw = (LinearLayout) findViewById(R.id.linear_main);
contentDraw.addView(inflater);
However when I display the view I get a blank page.
Here is my view layout file:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linear_miles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<FrameLayout
android:id="#+id/rootMilesFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:visibility="gone"
android:layout_weight="9"
app:layout_constraintTop_toTopOf="#+id/nav_miles">
<TextView
android:id="#+id/text_miles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="#000000"
android:text="Hello Miles!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_miles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:layout_weight="1"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
</FrameLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I cannot seem to figure out what is happening.
Here is my code for loading the view:
FrameLayout mLayoutMiles = (FrameLayout) findViewById(R.id.rootMilesFrag);
for (int i = 0; i < mLayoutMiles.getChildCount(); i++) {
View v = mLayoutMiles.getChildAt(i);
v.setVisibility(View.VISIBLE);
v.postInvalidate();
}
mLayoutMiles.setVisibility(View.VISIBLE);
mLayoutMiles.postInvalidate();
The page does show the bottom navigation panel at the top of the screen, however it does not show the edit text or the green background.

Android ConstraintLayout: set one element in the center of another one

I'm using ConstraintLayout in order to create activity view
There is a code snippet:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:gravity="bottom"
android:orientation="vertical"
tools:context=".presentation.screens.registration.RegistrationActivity">
<LinearLayout>...</LinearLayout>
<Button
android:id="#+id/login_next_btn"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#drawable/button_green_disabled"
android:layout_width="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="24dp"
android:text="#string/next"
android:textColor="#color/font_white"
android:textSize="16sp"
android:layout_height="48dp"
android:enabled="false"
android:onClick="onNextClick"/>
<ImageView
android:id="#+id/loader_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/login_next_btn"
android:src="#drawable/ic_loader"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The question is: How can I set ImageView (android:id="#+id/loader_image") in the center of Button (#+id/login_next_btn)?
Try this:
<ImageView
android:id="#+id/loader_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="#id/login_next_btn"
app:layout_constraintRight_toRightOf="#id/login_next_btn"
app:layout_constraintBottom_toBottomOf="#id/login_next_btn"
app:layout_constraintTop_toTopOf="#id/login_next_btn"
android:src="#drawable/ic_loader"/>
If you have to set it in the center just change the constraints of the image with :
app:layout_constraintBottom_toBottomOf="#id/login_next_btn"
app:layout_constraintTop_toTopOf="#id/login_next_btn"
app:layout_constraintStart_toStartOf="#id/login_next_btn"
app:layout_constraintEnd_toEndOf="#id/login_next_btn"

How to center align elements of a layout that is added by using <include/>

I have created a simple layout that contains 2 imageViews.
This is my code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/root_team1"
android:layout_gravity="center"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/team_one_IV"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:background="#004187"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/team_one_IV2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginTop="2dp"
android:layout_centerVertical="true"
android:background="#004187"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/team_one_IV" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is what its showing me when i view the layout.
Now i want to include this layout in another layout. This is what i did.
<include
android:id="#+id/firstTeam"
layout="#layout/team_1"
android:layout_width="0dp"
android:layout_height="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
and this is what i get after including this layout in another layout.
As you can see the layout is attached to the top, but i want it to be in the center of the container, i have defined container height to 300dp. Do i need to use some other type of layout to achieve this?
EDIT:
Complete layout code:
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<include
android:id="#+id/team_1"
layout="#layout/team_1"
android:layout_width="0dp"
android:layout_height="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
So if the layout you are trying to include is the first layout in this post you have to options:
make those imageViews' width wrap_content
or add
app:layout_constraintWidth_default="wrap"

The space between rows inside Recyclerview with gridlayoutmanager is messed up

I have a recyclerview with a gridlayoutmanager attached to it. The columns are spaced correctly but the rows are messed up. The space between each row seems to change each time I scroll up and down.
This is where I setup the recycler view and the grid layout manager:
var inflater = view.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var rootLayout = view.findViewById<LinearLayout>(R.id.ll_linear)
var heroListView = inflater.inflate(R.layout.hero_list_tab_card, rootLayout, true)
//get the recycler view
var heroListRv = heroListView.findViewById<RecyclerView>(R.id.rv_heroes)
heroListRv.layoutManager = GridLayoutManager(view.context, 4)
Here is hero_list_tab_card:
<?xml version="2.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/tv_primary_attribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAllCaps="true"
android:textColor="#color/colorTextWhite"
android:text="#string/test"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:layout_width="60dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#id/tv_primary_attribute"
app:layout_constraintEnd_toEndOf="parent"
android:src="#drawable/dotabuddy_icon"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_heroes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
app:layout_constraintTop_toBottomOf="#id/tv_primary_attribute"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the actual hero elements that are populated inside the grid:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/iv_heroimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/dotabuddy_icon"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="#+id/tv_heroname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorTextWhite"
android:text="#string/test"
app:layout_constraintTop_toBottomOf="#id/iv_heroimage"
app:layout_constraintStart_toStartOf="#id/iv_heroimage"
app:layout_constraintEnd_toEndOf="#id/iv_heroimage"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Please let me know if you want me to post any other information. Any help will be greatly appreciated.
Edit: for clarification Im talking about the huge gap between row 4 and 5
You should add a constraint from the ImageView to your TextView as well, also if you want to add space between your item I recommend using ItemDecoration.
As you're in a ConstraintLayout you can specify the ratio of your image using app:layout_constraintDimensionRatio="H,16:9" for example or 4:3 if you want a square.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/iv_heroimage"
android:layout_width="0dp"
android:layout_height="60dp"
android:src="#drawable/dotabuddy_icon"
android:scaleType="fitXY"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#id/tv_heroname"/>
<TextView
android:id="#+id/tv_heroname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorTextWhite"
android:text="#string/test"
android:maxLines="1"
app:layout_constraintTop_toBottomOf="#id/iv_heroimage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I recommend you should fix maxLine for this TextView and be sure the image you load into ImageView same height or you should fix height of ImageView.

Why is my Dialog Fragment showing so thin?

I have a simple DialogFragment which contains 3 EditTexts and a Button. I have set the layout_width of its main layout 350dp but when I run the project it will be shown so slim.
This My DialogFragment XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:animateLayoutChanges="true"
android:background="#AFE6FF"
android:orientation="vertical"
android:padding="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:id="#+id/layout_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/et_mobile"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/layout_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/et_title"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/layout_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_title">
<EditText
android:id="#+id/et_body"
android:layout_width="match_parent"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<Button
android:id="#+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_body" />
</LinearLayout>
</ScrollView>
and it will be shown by this code:
MyDialogFragment.newInstance().show(getSupportFragmentManager(), "my_dialog_fragment");
I need it to be shown Wider.
You need to change the LayoutParams of your dialog like the below code. put it in onResume():
Window window = yourDialog.getWindow();
if (window != null) {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(lp);
}
I think that the default dialog's LayoutParams ( wrap_content,wrap_content ) overriding your definition from XML.
It's better to use it this way and not use a fixed size values (350dp) to keep your layout responsive to all screen sizes.
You want to override onCreateDialog and add your custom layout there instead. Like below. I use ViewBinding to create my views.
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(requireContext())
.setView(binding.root)
.setCancelable(false)
.create().apply {
setCanceledOnTouchOutside(false)
}
}

Categories

Resources