White rectangle on top of DialogFragment - android

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

Related

Nested viewgroup is null after binding with butterknife

Here's my fragment_photo_gallery.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/disconnected_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone">
<ImageView
android:id="#+id/disconnected_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_offline" />
<TextView
android:id="#+id/disconnected_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/disconnected_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:text="#string/disconnected_title_text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="#+id/disconnected_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/disconnected_title_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:gravity="center"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="#string/disconnected_subtitle_text"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#A2AAB0" />
<Button
android:id="#+id/disconnected_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/disconnected_subtitle_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="#string/disconnected_button_text" />
</RelativeLayout>
</RelativeLayout>
I bind all the stuff in the class which extends fragment:
public class PhotoGalleryFragment extends Fragment {
....
#BindView(R.id.disconnected_view)
RelativeLayout disconnectedView;
....
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
unbinder = ButterKnife.bind(this, v);
return v;
}
#Override public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
....
}
And get NPE on this method:
private void showDisconnectedView() {
disconnectedView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
My build gradle looks ok so I don't know why I get such an error
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
The guy who got similar problem figured out by changing annotationProcessor to apt but in my case, it just won't ever sync.
QUICK UPDATE
The problem still happened even if I replace
#BindView(R.id.disconnected_view)RelativeLayout disconnectedView;
with
disconnectedView = v.findViewById(R.id.disconnected_view); in my onCreateView method. So it must be not butterknife fault.
Use Butterknife without unbinder, You need to return already inflated view rather than creating new view & inflating it.
Also apply view specific code in onViewCreated() method.
public class PhotoGalleryFragment extends Fragment {
#BindView(R.id.disconnected_view)
RelativeLayout disconnectedView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
ButterKnife.bind(this, v);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
disconnectedView.setVisibility(View.GONE);
}

use fragment_container in replace transition

I'm trying to transit between 2 fragment from games.xml to levels.xml
its like angry bird game menu:
games: http://i.stack.imgur.com/9S7Ar.png
levels:http://i.stack.imgur.com/uB8rP.jpg
games.java:
public class Games extends Fragment implements OnClickListener{
public Games(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.games, container, false);
return rootView;
}
#Override
public void onClick(View v) {
Fragment f = null;
switch (v.getId()) {
case R.id.s4:{
f = new Levels();
break;
default:
break;
}
if (f != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.lvl_container, f);
transaction.addToBackStack(null);
transaction.commit();
}
}
level.java:
public class Levels extends Fragment {
public Levels(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.levels, container, false);
return rootView;
}
}
levels.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lvl_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="#string/s4"/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtLabel"
android:src="#drawable/lvlgray_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
games.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/lastgame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="#string/games"/>
<ImageButton
android:id="#+id/s4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lastgame"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="#drawable/s4" />
the problem is when I click on imagebotton nothing happens (should go to levels), I think there is problem with these line
transaction.replace(R.id.lvl_container, f);
but I don't know what is it. Any ideas?
I've already seen this (developer.android.com/guide/components/fragments) but no help! What did I miss?
Is there any other way to transition between 2 fragments?
Thank you
Add this line in onCreateView at Games.java:
ImageButton im = (ImageButton) rootView.findViewById(R.id.s4);
im.setOnClickListener(this);
This make refrence to your imagebutton and set onClickListener on it to call your onClick method.

Dynamically include another layout in Fragment Activity

This is my Home.xml layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rellay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/placesgradient">
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="click"
/>
**<I want to include another layout here on a click of a button dynamically on click of a button >**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="#id/btn2"
android:text="hello this is text..see above" />
</RelativeLayout>
This is my Home.java file
public class Home extends Fragment implements OnClickListener {
Button b1;
RelativeLayout r1;
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
b1 = (Button) rootView.findViewById(R.id.btn2);
b1.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View arg0) {
ViewGroup con = null;
r1 = (RelativeLayout) rootView.findViewById(R.id.rellay);
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
r1.addView(layoutInflater.inflate(R.layout.test1, con , false));
}
}
When I click on the button it places the test1.xml layout file at the top of the home.xml layout. I need it to be between the button and the text. I checked this code on stack before but it doesn't seem to work with the FragmentActivity. Here 1 indicates the index or the second child in this case.
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );
How can I achieve it? Help!!
I would put some container view at the position you want your layout to be like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rellay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/placesgradient">
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="click"
/>
<FrameLayout
android:id="#+id/flContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="#id/btn2"
android:text="hello this is text..see above" />
</RelativeLayout>
Then you can retrieve the container in your Fragment like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
// Retrieve your container
flContainer = rootView.findViewById(R.id.flContainer);
b1 = (Button) rootView.findViewById(R.id.btn2);
b1.setOnClickListener(this);
return rootView;
}
And later when you want to add your layout you can add a child to the FrameLayout like this:
flContainer.addView(subLayout);
If you later want to change the layout in your container you can do it like this:
flContainer.removeAllViews();
flContainer.addView(otherLayout);
Use LayoutParams.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.btn2);
r1.addView(layoutInflater.inflate(R.layout.test1, con , false),params);
Or put the view in your layout and toggle visibility with View.setVisibility(View.GONE/View.VISIBLE)

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.

Custom dialog linear layout weight issue

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>

Categories

Resources