View is not added when using addView() - android

I am making a window to open an empty dialog and write comments.
The dialog initially has an empty item where i can write one comment.
Comments can be up to one line.
Pressing the Enter key dynamically creates an item that allows you to write the next comment.
I was wondering whether to use RecyclerView or addView() for this function.
Since this dialog was also opened from the recycler view item of the parent, it would be complicated when using RecyclerView again, so I used addView().
I don't know if this is the right choice.
This is because data management seems to be difficult because the created comment must be saved in the parent's recycler view item.
Anyway, using addView() does not add the view.
There is no such thing as an error, but it cannot be added.
What is the cause?
writeing_comment_item
<?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="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/comment_edit" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
app:layout_constraintLeft_toRightOf="#+id/start_point"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_writing_comment_dialog
<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="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22" />
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
WritingCommentDialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment {
LinearLayout mContainer;
EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
editText = view.findViewById(R.id.comment_edit);
mContainer = view.findViewById(R.id.container);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override // IME actionNone (Enter Key)
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
View inflater1 = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, mContainer, false);
mContainer.addView(inflater1);
return true;
}
});
return view;
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
#Override
public void onResume() {
super.onResume();
setDialogSize();
}
private void setDialogSize() {
getDialog().getWindow().setLayout(1000, 1000);
}
}

your problem is that you added a horizontal LinearLayout and after you added an EditText with android: layout_width = "match_parent" that's why your Comment is added on your container view but is not visible because of the EditText.
if I understood correctly you must change
android: orientation = "horizontal" by android: orientation = "vertical" in your fragment_writing_comment_dialog
so that the commentary will be displayed above the EditText
And also you need to change the orientation on your Container LinearLayout
<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="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22" />
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Related

Modal Bottom Sheet isnt transparent althought background set too transparent

All my modal BottomSheets have a white background above them and are not transparent as intended.
I tried opening the view via a separate Fragment like this:
ContextMenuPlaylistFragment contextMenuPlaylistFragment = new ContextMenuPlaylistFragment();
contextMenuPlaylistFragment.show(fragmentManager,"contextmenu playlists");
and like that:
View modelBottomSheet = LayoutInflater.from(mContext).inflate(R.layout.context_menu_playlist, null);
BottomSheetDialog dialog = new BottomSheetDialog(mContext);
dialog.setContentView(modelBottomSheet);
dialog.show();
This is my BottomSheetFragment:
public class ContextMenuPlaylistFragment extends BottomSheetDialogFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.context_menu_playlist, container, false);
}
}
And this is my BottomSheet:
<?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:background="#color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_playlist_search"
android:orientation="horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_search_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/search_playlist"
android:textSize="18sp"
android:layout_marginStart="10dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_playlist_rename"
android:orientation="horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_edit_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rename_playlist"
android:textSize="18sp"
android:layout_marginStart="10dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_playlist_delete"
android:orientation="horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_delete_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/delete_playlist"
android:textSize="18sp"
android:layout_marginStart="10dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I put it in two LinearLayouts to try and make the background transparent.
I also tried it with one LinearLayout that doesn't change the background and has height "wrap_content"
Please check if you have set the android:background attribute in the "AppTheme" style to white. If so, delete it or set it to transparent will solve the problem.

How to change dialog fragment size in xml?

I am using a Dialog fragment to show a dialog. However, my dialog does not show in the favorite size and it is very small. I set the size with "match_parent", but it does not show as well as I want.
Dialog xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:textSize="18sp"
android:textColor="#color/color_white"
android:gravity="center"
android:text="لطفا وارد حساب کاربری خود شوید"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/colorPrimary" />
<EditText
android:id="#+id/edt_loginDialog_email"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:drawableRight="#drawable/ic_email_black_24dp"
android:hint="پست الکترونیک"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:drawableRight="#drawable/ic_vpn_key_black_24dp"
android:id="#+id/edt_loginDialog_pass"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:hint="پست الکترونیک"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_gravity="right"
android:text="فراموشی رمز عبور"
android:drawableRight="#drawable/ic_vpn_key_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_loginDialog_signup"
android:layout_marginRight="4dp"
android:textColor="#color/color_white"
android:fontFamily="#font/font"
android:background="#drawable/btn_signup_style"
android:text="ثبت نام"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_loginDialog_login"
android:layout_marginLeft="4dp"
android:textColor="#color/color_white"
android:fontFamily="#font/font"
android:background="#drawable/btn_call_style"
android:text="ورود"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
and it is my dialog fragment code that show how I Implement it
public class LoginDialog extends DialogFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.login_dialog,container,false);
Button btnLogin=(Button)view.findViewById(R.id.btn_loginDialog_login);
Button btnSignup=(Button)view.findViewById(R.id.btn_loginDialog_signup);
EditText edtEmail=(EditText)view.findViewById(R.id.edt_loginDialog_email);
EditText edtPass=(EditText)view.findViewById(R.id.edt_loginDialog_pass);
return view;
}
}
Here is my dialog picture link:
If you want set the dialog size in xml try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="<desire size>"
android:layout_height="<desire size>">
....
</LinearLayout>
</LinearLayout>
but My weapon of choice, set the size programmatically.
Percentage size:
#Override
public void onStart() {
super.onStart();
int dialogWidth = (int) (getResources().getDisplayMetrics().widthPixels * 0.9f);
int dialogHeight = (int) (getResources().getDisplayMetrics().heightPixels * 0.8f);
if (getDialog().getWindow() == null) return;
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
}
Dp size:
#Override
public void onStart() {
super.onStart();
//Reference to dimens.xml
//<dimen name="dialog_with">200dp</dimen>
//<dimen name="dialog_height">200dp</dimen>
int dialogWidth = getResources().getDimensionPixelSize(R.dimen.dialog_width);
int dialogHeight = getResources().getDimensionPixelSize(R.dimen.dialog_height);
if (getDialog().getWindow() == null) return;
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
}
Add this in onStart() of your dialogFragment
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

Dynamically added EditText is not visible when created

I have been reading about how to dynamically add an EditText field to my Linear Layout, every-time a user clicks a TextView (which has an onClick Listener attached).
I have had some mild success - I know that the EditText field is being created because when the button is clicked, all other elements move up as if something is being added to the screen.
My problem is that the EditText aren't visible and I haven't a clue why that is, so any help would be appreciated.
The the app isn't crashing so nothing to add in terms of the StackTrace and Log, as far as the app is concerned, everything is being created.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout 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/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/facebookBlue"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.test.practise.AddTeamMembers">
<android.support.design.widget.TextInputEditText
android:id="#+id/tv_teamNames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/teamName"
android:textColor="#android:color/background_light"
android:textColorLink="#android:color/background_light"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.47"
android:gravity="center"
android:text="Enter Player Names Below!"
android:textColor="#android:color/background_light"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name1"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name2"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name3"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/editTextGroupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_add_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="+ Add Name"
android:textColor="#android:color/background_light"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/facebookBlue"
android:gravity="center"
android:text="Ready to join!"
android:textColor="#android:color/background_light" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Below is the AddTeamMembers Class that calls the above XML
public class AddTeamMembers extends Fragment implements
View.OnClickListener
{
private SharedPreferences pref;
private TextView tv_teamNames, tv_add_name;
private LinearLayout mLayout;
//The below method must be overridden in order to implement a fragment -
this changes the lifecycle method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_team_members,
container, false);
initViews(view);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// 0, Sets Shared pref mode to private
pref = getActivity().getPreferences(0);
tv_teamNames.setText(pref.getString(Constants.Team_Name, ""));
}
private void initViews(View view) {
tv_teamNames = (TextView) view.findViewById(R.id.tv_teamNames);
tv_add_name = (TextView) view.findViewById(R.id.tv_add_name);
mLayout = (LinearLayout) view.findViewById(R.id.editTextGroupLayout);
tv_add_name.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_add_name:
createEditTextView();
break;
}
}
#TargetApi(Build.VERSION_CODES.M)
public void createEditTextView() {
try{
//dynamically create new EditText when user clicks to add another
name
//target user using API 22 and above (lollipop and above)
EditText editTextView = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.M) {
editTextView = new EditText(getContext());
}else{
Toast.makeText(getActivity(), "App is not supported on this
device",
Toast.LENGTH_LONG).show();
}
editTextView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
mLayout.addView(editTextView);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
}
[![Before Button is clicked][1]][1]
[![I have clicked 2 add 3 EditText here to make it obvious whats happening]
[2]][2]
[1]: https://i.stack.imgur.com/DGoPd.png
[2]: https://i.stack.imgur.com/3LWcY.png

Constraint layout with child constraint layouts not expanding horizontally

I'm trying to build a rectangular view (spanning the width of the screen) which is split horizontally into three blocks, and I am doing so using a constraint layout. The left and right blocks have a specified width, but the centre block should expand to fit the remaining space. Because I require that the left-most block have a different background colour to the parent, I have grouped its children into another constraint layout. I have given the layout XML below:
<?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"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:background="#10000000"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.ConstraintLayout
android:id="#+id/abc"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#50000000"
android:layout_marginEnd="8dp"
android:padding="2dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/jkl"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/def"
android:gravity="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ghi"
android:gravity="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/def" />
</android.support.constraint.ConstraintLayout>
<!-- This should expand to fit the remainder of the screen width -->
<android.support.constraint.ConstraintLayout
android:id="#+id/jkl"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintEnd_toStartOf="#+id/stu"
app:layout_constraintStart_toEndOf="#+id/abc">
<TextView
android:id="#+id/mno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|start"
android:textColor="#a0000000"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pqr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|start"
android:textColor="#a0000000"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mno" />
</android.support.constraint.ConstraintLayout>
<ImageButton
android:id="#+id/stu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="#drawable/x"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/jkl"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Unfortunately, the middle block does not expand to fit the remaining width of the screen. Does anyone know how to achieve this? Am I doing anything fundamentally wrong here?
Edit:
Based on Ben P.'s response it may be that the XML above is fine, but the layouts in which this layout is contained are at fault. I have therefore provided them below.
This layout XML is the parent of the XML above:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.Parent">
</android.support.v7.widget.RecyclerView>
This layout XML is the parent of the RecyclerView XML above:
<?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.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include layout="#layout/bottom_navigation" />
</android.support.constraint.ConstraintLayout>
Edit 2:
As requested I've provided the complete code base:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private FragmentManager mFragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Foo());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
Foo.java:
public class Foo extends Fragment {
private String[] mDataset;
private OnFragmentInteractionListener mListener;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public Foo() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment Foo.
*/
// TODO: Rename and change types and number of parameters
public static Foo newInstance(String param1, String param2) {
Foo fragment = new Foo();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
initDataset();
}
private void initDataset() {
mDataset = new String[1];
mDataset[0] = "Hello World";
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_foo, container, false);
mRecyclerView = view.findViewById(R.id.foo_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new FooAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
FooAdapter.java:
public class FooAdapter extends RecyclerView.Adapter<FooAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView mFoofoo;
private TextView mBarbar;
public ViewHolder(View itemView) {
super(itemView);
mFoofoo = itemView.findViewById(R.id.foofoo);
mBarbar = itemView.findViewById(R.id.barbar);
}
public TextView getFoofoo() {
return mFoo;
}
public TextView getBarbar() {
return mBar;
}
}
public FooAdapter(String[] mDataset) {
this.mDataset = mDataset;
}
#Override
public FooAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bar, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(FooAdapter.ViewHolder holder, int position) {
String text = mDataset[position];
holder.getBarbar().setText(text);
holder.getFoofoo().setText(text);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
activity_main.xml:
<?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.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eaff73"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
fragment.xml:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/foo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8df78b"
tools:context="com.example.Foo">
</android.support.v7.widget.RecyclerView>
bar.xml:
<?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"
android:layout_width="match_parent"
android:layout_height="104dp"
android:layout_marginTop="4dp"
android:background="#10000000"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/backg"
android:background="#50000000"
android:layout_width="104dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/foofoo"
android:gravity="start"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/action"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_width="wrap_content"
android:src="#drawable/pic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/barbar"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/barbar"
android:autoSizeMaxTextSize="24sp"
android:autoSizeMinTextSize="14dp"
android:autoSizeStepGranularity="2dp"
android:autoSizeTextType="uniform"
android:background="#ef0505"
android:gravity="top|start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:textColor="#a0000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#id/action"
app:layout_constraintStart_toEndOf="#id/backg"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Any help is greatly appreciated!
I do not know why this works, but it does.
In bar.xml, replace your outer ConstraintLayout's height with wrap_content (instead of 104dp).
Also in bar.xml, replace your ImageView's height with 104dp (instead of 0dp).
This has the net effect of keeping your layout's design the same... it just has the ConstraintLayout's height defined by the ImageView's height, instead of the other way around.
If I had to guess at why this makes a difference, I'd assume that there's some quirk in the way LinearLayoutManager measures its children, and that when it sees a fixed height it ignores the match_parent width. But that's wild speculation.
Update
Here is a flat ConstraintLayout that does what I believe you want:
<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="100dp">
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#33000000"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="#+id/image"
app:layout_constraintRight_toRightOf="#+id/image"
tools:text="line 1"/>
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toBottomOf="#+id/text1"
app:layout_constraintLeft_toLeftOf="#+id/image"
app:layout_constraintRight_toRightOf="#+id/image"
tools:text="line 2"/>
<TextView
android:id="#+id/text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#+id/image"
app:layout_constraintRight_toLeftOf="#+id/button"
tools:text="line 3"/>
<TextView
android:id="#+id/text4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toBottomOf="#+id/text3"
app:layout_constraintLeft_toRightOf="#+id/image"
app:layout_constraintRight_toLeftOf="#+id/button"
tools:text="line 4"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="button"/>
</android.support.constraint.ConstraintLayout>
And what it looks like:
Original
This does not strictly answer the question (since as far as I can tell, there is no problem with the posted layout), but...
I see no reason to implement this UI using ConstraintLayout. The benefit of ConstraintLayout is that it allows you to avoid nesting ViewGroups inside your top-level View. But as your ConstraintLayout is written, you're still grouping your TextViews inside a mid-level View.
If you are not taking advantage of ConstraintLayout's ability to do organization without nesting, I think it is a better idea to use a simpler top-level view. Here is your UI reimplemented with LinearLayouts:
<?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:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:background="#10000000"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/abc"
android:layout_width="100dp"
android:layout_height="match_parent"
android:padding="2dp"
android:background="#50000000"
android:orientation="vertical">
<TextView
android:id="#+id/def"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
tools:text="line 1"/>
<TextView
android:id="#+id/ghi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="14sp"
tools:text="line 2"/>
</LinearLayout>
<LinearLayout
android:id="#+id/jkl"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/mno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#a0000000"
android:textSize="14sp"
android:textStyle="bold"
tools:text="line 1"/>
<TextView
android:id="#+id/pqr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#a0000000"
android:textSize="14sp"
tools:text="line 2"/>
</LinearLayout>
<ImageButton
android:id="#+id/stu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:src="#drawable/plus"/>
</LinearLayout>

Dialog using DialogFragment not showing up properly

I am using DialogFragment to display a dialog.But the dialog was displayed with the title.When i used no title my dialog was not being displayed properly.
Dialog with title
Dialog without title
Xml
<?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="match_parent"
android:background="#drawable/share_background"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:textSize="25sp"
android:textColor="#fff"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3" />
</LinearLayout>
</LinearLayout>
DialogFragment Code
public class ShareDialogFragment extends DialogFragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.share_dialog, null, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
}
Why this is happening????
Try this:
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Share"
android:textSize="25sp"
android:textColor="#fff"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
Hope this helps.
Fix layout height and width of your parent linear layout to a fixed value.
Example:
android:layout_width="200dp"
android:layout_height="wrap_content"
Try this:
inflater.inflate(R.layout.share_dialog, container, false);

Categories

Resources