Custom dialog linear layout weight issue - android

I am using a LinearLayout view with a custom dialog ,when I run the code below, weight is not working correctly as the red view takes the green view's weight and verse vise as image below:
but when I use the same layout in another activity , weight goes correctly as the image below :
ConfirmDialog.java
public class ConfirmDialog extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_confirm, null);
return view;
}
}
dialog_confirm.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"
android:weightSum="7" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="#00ff00" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000" />
</LinearLayout>

Related

setOnClickListener in RelativeLayout not working

I have a structure in my android application
LinearLayout > FrameLayout >ImageView(used as a FrameLayout
Background) & RelativeLayout > TextView & ImageView.
I need that when I click in the FrameLayout or RelativeLayout or FirstImageView ( because it is the Bigger Zone , the textView or 2nd Image view are small ) do an action.
I tried to add setOnClickListener to this item but didn't work.
This is my Code:
public class HomeFragment extends Fragment {
View rootView;
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home_fragment, container, false);
RelativeLayout paleoFoodBackGround = (RelativeLayout) rootView.findViewById(R.id.relativeLytPaleoFoodHome);
paleoFoodBackGround.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("1FOOD_LIST", "onClick: ");
}
});
return rootView;
}
public void clickFood(){
Log.d("2FOOD_LIST", "onClick: ");
}
}
This is my Layout structure:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:orientation="vertical"
android:paddingLeft="2.5dp"
android:paddingRight="5dp"
android:weightSum="100">
<FrameLayout
android:id="#+id/framePaleoFoodHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:paddingBottom="2.5dp">
<ImageView
android:id="#+id/imgViewPaleoFoodBackGround"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center"
app:srcCompat="#drawable/recipes" />
<RelativeLayout
android:id="#+id/relativeLytPaleoFoodHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<ImageView
android:id="#+id/imgViewRecipesIconHome"
style="#style/iconHome"
app:srcCompat="#drawable/cooking"
/>
<TextView
android:id="#+id/txtViewRecipesTxtHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/textViewHomeSquare"
android:text="#string/RecipesHome"
android:layout_below="#id/imgViewRecipesIconHome"/>
</RelativeLayout>
</FrameLayout>
<Framelayout> .... </Framelayout>
</LinearLayout>
I recommend using ImageButton instead of image. Also set your RelativeLayout to have android:clickable="true". Also set all children views that are not clickable with android:clickable="false"

Fragment Width issue in LinearLayout inside HorizontalscrollView

I have a Horizontalscrollview inside which is horizontal LinearLayout.
I tried to add Fragments dynamically into the Linear Layout. Although I set my fragment width as "match_parent", the fragment is not filling in the screen width.
Here's my xml layouts for MainActivity
<LinearLayout 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:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
android:fillViewport="true"
android:id="#+id/baseScrollView">
<LinearLayout
android:id="#+id/scrollViewLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</HorizontalScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnNext"
android:text="Next Page"/>
</LinearLayout>
Here's my code for MainActivity
Button btnNext;
CustomHorizontalScrollView baseScrollView;
LinearLayout scrollViewLayout;
boolean isFragment1Added = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext = (Button) findViewById(R.id.btnNext);
scrollViewLayout = (LinearLayout) findViewById(R.id.scrollViewLayout);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isFragment1Added){
isFragment1Added = false;
addCustomFragment(new Fragment2(), "fragment_2");
}else{
isFragment1Added = true;
addCustomFragment(new Fragment1(), "fragment_1");
}
}
});
}
public void addCustomFragment(Fragment fragmentView, String tag) {
FragmentTransaction viewTransaction = getFragmentManager().beginTransaction();
viewTransaction.add(R.id.scrollViewLayout, fragmentView, tag);
viewTransaction.commit();
}
Here's fragment1.xml. fragment2 is similar to fragment1.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_dark"
tools:context="com.uobtravelplanners.horizontalscrollviewtest.Fragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="FRAGMENT 1"
android:textSize="50sp"
android:gravity="center"/>
</FrameLayout>
here's the screenshot
set the layout_width of scrollViewLayout to wrap_content
set the fragment's width to screen width by programmatically
Edit 1
Get screen dimensions in pixels
first, get the screen width according to this post.
then set the layout_width in onCreateView method like
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// get screen width
int screenWidth = ...
// get the root view of the fragment
View root = inflater.inflate(R.id.your_layout, container, false);
// set layout_width through LayoutParams
root.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, ViewGroup.LayoutParams.MATCH_PARENT));
return root;
}

DialogFragment does not float on the activity window

I create a class extends DialogFragment class, my code is as below, my problem is the dialog hides under status bar(in the system) and the toolbar(in the activity), I refer the question here DialogFragment not floating, acts embeded or as another fragment add the onCreate function and set the style, but the dialog still only hides under toolbar, not as the tutorial said it will float on the activity window.
public class PasswordDialog extends DialogFragment {
......
public static PasswordDialog newInstance(PdfFragment pdfFragment) {
Log.i(sClassTag,"newInstance in PdfFragmentPasswordDialog");
PdfFragmentPasswordDialog passwordDialog = new PdfFragmentPasswordDialog();
passwordDialog.mPdfFragment = pdfFragment;
setInteractionListener(pdfFragment);
return passwordDialog;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Pick a style based on the num.
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(sClassTag,"onCreateView in PdfFragmentPasswordDialog");
// Inflate the layout to use as dialog or embedded fragment
mView = inflater.inflate(R.layout.layout_password, container, false);
addButtonListener();
addEdittextListener();
return mView;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.i(sClassTag,"onCreateView in onCreateDialog");
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
void showPasswordDialog(boolean isFirstTime) {
....
FragmentManager fragmentManager = getActivity().getFragmentManager();
show(fragmentManager, "dialog");
...
}
The layout file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/ic_img_passwordkey_48"
android:id="#+id/key_icon"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:contentDescription="#string/password_input_hint_message"/>
<Space
android:layout_width="match_parent"
android:layout_height="16dp" />
<EditText
android:imeOptions="flagNoExtractUi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textColorHint="#color/password_dialogUI_hint_text_color"
android:textColor="#color/password_dialogUI_text_color"
android:textSize="12sp"
android:hint="#string/password_input_hint_message"
android:ems="10"
android:id="#+id/dialogUI_edit_text"
android:textCursorDrawable="#null"
android:fontFamily="sans-serif"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textDirection="locale"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/password_error_warning_message"
android:textColor="#color/password_dialogUI_warning_color"
android:id="#+id/dialogUI_warning_text"
android:textSize="12sp"
android:visibility="invisible"
android:fontFamily="sans-serif"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_marginStart="8dp"/>
<Space
android:layout_width="match_parent"
android:layout_height="8dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp">
<Button
android:text="#string/password_ok_button"
android:textColor="#drawable/layout_disable_text_color"
android:minWidth="64dp"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:id="#+id/password_dialogUI_ok_button"
android:layout_alignParentEnd="true"
style="?android:attr/borderlessButtonStyle"
android:background="#drawable/layout_password_button_background"
android:layout_marginEnd="8dp"/>
<Button
android:text="#string/password_cancel_button"
android:textColor="#drawable/layout_disable_text_color"
android:minWidth="64dp"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:id="#+id/password_dialogUI_cancel_button"
style="?android:attr/borderlessButtonStyle"
android:layout_toStartOf="#+id/password_dialogUI_ok_button"
android:background="#drawable/layout_password_button_background"
android:layout_marginEnd="8dp"/>
In Activity, try to use you use fragment.show(), instead of call your own show().
PasswordDialog fragment = PasswordDialog();
fragment.show(getFragmentManager(), "TAG_HERE");
2. Add android:minWidth and android:minHeight to your xml.
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:minWidth="1000dp"
android:minHeight="1000dp">
See below:
Full Screen DialogFragment in Android
Added:
To resize your DialogFragment, you can set values in onResume.
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
// You can define popup_width and popup_height
int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
window.setLayout(width, height);
window.setGravity(Gravity.CENTER); // Optional
}
See below:
How to set DialogFragment's width and height?
https://stackoverflow.com/a/17916441/850347
here is steps to custom DialogFragment:
1.inflate custom view from xml on method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(true);
View view = inflater.inflate(R.layout.XXX,
container, false);
//TODO:findViewById, etc
return view;
}
2.set your dialog's width an height in onResume(),remrember in onResume()/onStart(),seems didn't work in other method
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(width, height);
window.setGravity(Gravity.CENTER);
//TODO:
}

White rectangle on top of DialogFragment

When using a dialogFragment I get a white space rectangle at the top of the fragment without knowing why.
What I should get
What I actually get
My DialogFragment:
public class SelectDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate layout with recycler view
View v = inflater.inflate(R.layout.select_frag, container, false);
return v;
}
}
select_frag.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:background="#000"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/textView" />
</RelativeLayout>
You should set style without title:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
Also you can found more useful example here

android include change textView value

I include a layout in my fragment1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="#+id/family_header"
layout="#layout/header" />
header xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#33cc66"
android:orientation="horizontal" >
<ImageView
android:id="#+id/nav_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/nav_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical" />
<ImageView
android:id="#+id/nav_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I want to change TextView "nav_text" value dynamically in my Fragment1.class . how to do it
you can create class that extends fragement....
public class DetailFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rssitem_detail,
container, false);
return view;
}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(item);
}
}
in this class you can change value of textview dynemically.....please put your code here.so we can understand what is your actual problem.
Use this code in your fragment oncreateview method like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container,false);
final LinearLayout headerLayout= (LinearLayout ) v.findViewById(R.id.family_header);
TextView txtHeaderText=(TextView)headerLayout.findViewById(R.id.nav_text);
txtHeaderText.setText("My New Text");
return v;
}
I hope it's work for you.

Categories

Resources