Fragment is not appearing on the hosting activity - android

I am adding a fragment to an activity as shown below in the code. but at run time i found that the fragment is never shown.
i examined the fragment layout and the its viewgroup in the main layout.
please let me know why it is not showing.
MainAct:
I am adding a fragment to an activity as shown below in the code. but at run time i found that the fragment is never shown.
i examined the fragment layout and the its viewgroup in the main layout.
please let me know why it is not showing.
MainAct:
public class ActMain extends AppCompatActivity {
public static String TAG = ActMain.class.getSimpleName();
private FragmentButtons mFragButtons = null;
//private Fragment mFragmentButtons = null;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "#onCreate");
this.initViews();
}
}
frag
public class FragmentButtons extends Fragment {
private final static String TAG = FragmentButtons.class.getSimpleName();
public interface iActionHandler {
public void onButton00Clicked();
public void onButton01Clicked();
public void onButton02Clicked();
public void onButton03Clicked();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.d(TAG, "#onCreateView");
View view = inflater.inflate(R.layout.frag_buttons, container, true);
return view;
}
}
mainLayout:
<?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:weightSum="2">
<fragment
android:id="#+id/actMain_FragButtons"
android:name="com.example.pc_amr.servicewithid_01.fragments.FragmentButtons"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<!--<FrameLayout
android:id="#+id/actMain_FragContents00"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"/>-->
</LinearLayout>
fragLayout:
<?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:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:id="#+id/btn00"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn01"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn02"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn03"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:id="#+id/btn10"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn11"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn12"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn13"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:id="#+id/btn20"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn21"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn22"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/btn23"
android:layout_width="0dp"
android:onClick="onButtonClicked"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

The reason that it is not showing it is because in your onCreate method, you have not done setContentView and that is why it is empty and not showing.
Add the following line:
setContentView(R.layout.mainLayout);
This should allow the fragment to be shown.

I used this way and it worked with me
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_1_layout, container, false);
return v;}

Related

Layout Params not working in Custom Dialog Fragment

I am trying to set my custom dialog fragment to use the full width of the screen. The one solution is to set LayoutParams on view. But they are not working.
Things I Tried
Changing parent Layout
using getLayoutParams().getClass() which returns null
Layout Validator shows correct layout so no prob here, here is my Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
style="#style/MaterialAlertDialog.MaterialComponents.Title.Text.CenterStacked"
android:id="#+id/ask_hint_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/puppy_vw"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<pl.droidsonroids.gif.GifImageView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:src="#drawable/solving"
android:adjustViewBounds="true">
</pl.droidsonroids.gif.GifImageView>
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/MaterialAlertDialog.MaterialComponents.Title.Text.CenterStacked"
android:text="#string/ad_confirm_text"/>
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd="true"
android:layout_below="#id/puppy_vw"
android:id="#+id/ad_confm_btns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_centerInParent="true">
<com.google.android.material.button.MaterialButton
android:id="#+id/yes_hint"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#color/white"
android:backgroundTint="#color/cardview_dark_background"
style="#style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:text="Yes">
</com.google.android.material.button.MaterialButton>
<!--
<com.google.android.material.button.MaterialButton
android:id="#+id/no_hint"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:backgroundTint="#color/cardview_dark_background"
style="#style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:text="No">
</com.google.android.material.button.MaterialButton>
-->
<com.google.android.material.button.MaterialButton
android:id="#+id/why_hint"
android:layout_margin="5dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/cardview_dark_background"
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
android:text="Why Ads?">
</com.google.android.material.button.MaterialButton>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
Here is my DialogFragment class
public class AdLoadFragment extends DialogFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
AskHintDialogBinding binding=AskHintDialogBinding.inflate(inflater,container,false);
View view=binding.getRoot();
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MyUtilsApp.showLog(String.valueOf(view.getClass()));
view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
}
}

Add things to backdrop of Android DialogFragment

Want to add something to Dialog's backdrop like
I tried to read all the documents but can't find a way to add check in text to the backdrop.
I have done it like this, hope it will help
Layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:calendar="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/mainLayout"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/fifty_five_dp"
android:background="#android:color/transparent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/twenty_dp"
android:layout_marginTop="#dimen/one_twenty_dp"
android:layout_marginRight="#dimen/twenty_dp"
calendar:cardCornerRadius="#dimen/three_dp"
calendar:cardElevation="#dimen/two_dp"
calendar:cardPreventCornerOverlap="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/fifty_five_dp"
android:background="#color/dull_white"
android:padding="#dimen/thirteen_dp"
android:textSize="#dimen/sixteen_sp">
<TextView
android:id="#+id/leaveFromTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:fontFamily="#font/sf_pro_display_bold"
android:text="#string/select_leaves_placeholder"
android:textColor="#color/darkGreyDeepDeep"
android:textSize="#dimen/sixteen_sp" />
<TextView
android:id="#+id/leaveCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:fontFamily="#font/sf_pro_display_bold"
android:text="#string/days_placeholder"
android:textColor="#color/darkGrey"
android:textSize="#dimen/twelve_sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:calendar="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/seven_dp"
android:layout_marginTop="#dimen/fifteen_dp"
android:background="#drawable/box"
android:padding="#dimen/ten_dp">
<RelativeLayout
android:id="#+id/okayLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/thirteen_dp">
<ImageView
android:id="#+id/calenderDrawable"
android:layout_width="#dimen/seventeen_dp"
android:layout_height="#dimen/seventeen_dp"
android:layout_centerVertical="true"
android:src="#mipmap/calener" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="#dimen/ten_dp"
android:layout_toRightOf="#+id/calenderDrawable"
android:text="Calender"
android:textColor="#color/blue_light"
android:textSize="#dimen/fourteen_sp" />
<Button
android:id="#+id/btnDone"
android:layout_width="#dimen/seventy_dp"
android:layout_height="#dimen/twenty_six_dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="#dimen/three_dp"
android:background="#drawable/calender_button_background"
android:text="DONE"
android:textColor="#color/white"
android:textSize="#dimen/thirteen_sp" />
</RelativeLayout>
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/okayLayout"
android:layout_marginTop="#dimen/ten_dp"
android:background="#color/viewColor" />
<com.neomeric.nock.com.andexert.calendarlistview.library.DayPickerView
android:id="#+id/pickerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/view"
android:layout_marginTop="#dimen/ten_dp"
android:background="#color/dull_white"
calendar:enablePreviousDay="false" />
</RelativeLayout>
</LinearLayout>
DatePickerPopup.java
public class DatePickerPopup extends DialogFragment implements DatePickerController {
private DayPickerView dayPickerView;
public DatePickerPopup() {
// Empty constructor is required for DialogFragment
// Make sure not to add arguments to the constructor
// Use `newInstance` instead as shown below
}
public static DatePickerPopup newInstance(OnDatesSelected datesSelected, Date first, Date last) {
DatePickerPopup frag = new DatePickerPopup();
Bundle args = new Bundle();
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.date_selection_layout, container);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return root;
}
public void setDateAndDaysCount(SimpleMonthAdapter.SelectedDays<SimpleMonthAdapter.CalendarDay> selectedDays) {
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public int getMaxYear() {
return 2021;
}
#Override
public void onDayOfMonthSelected(int year, int month, int day) {
}
#Override
public void onStart() {
super.onStart();
// To make it match parent
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
#Override
public void onDateRangeSelected(SimpleMonthAdapter.SelectedDays<SimpleMonthAdapter.CalendarDay> selectedDays) {
}
}
How it looks
Make a linear layout with 2 internal layouts, where the upper one will have a transparent background and the other one will have the content.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#android:color/transparent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
app:srcCompat="#drawable/ic_clock" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_margin="10dp"
android:background="#000000"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>

How to initialize OnClickListener for included layout inside fragment?

This is representation of my app layout:
Where should I create onClickListeners for included layouts? I tried inside the fragment but I could not get through with findViewById. So I tried from Main Activity but I'm not sure how to get to included layouts from there.
I also tried this inside the fragment:
public class MainMenu extends Fragment implements View.OnClickListener{
View button_call;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View myView = inflater.inflate(R.layout.fragment_main_menu, container, false);
button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
// implements your things
}
public MainMenu() {
}
}
But then Fragment seems to be empty
Fragment XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="60dp">
<include android:id="#+id/btn_call"
layout="#layout/call_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"/>
<include android:id="#+id/button2"
layout="#layout/message_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<include android:id="#+id/button3"
layout="#layout/navigate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<include android:id="#+id/button4"
layout="#layout/remind_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</ScrollView>
Steps
Create a xml file you want to inclue (eg. web.xml)
use include tag in fragment's xml and connect your layout you need to include using layout="#layout/
Initialize include tag inside fragment (when you do this make sure about the root tag of your web.xml)
Once initializing include tag is done access views inside the layout that included (web.xml) using include tag
Example
Here in my fragment I have an include tag in it's XML. It connects to my web.xml and ..web.xml's parent tag/ root tag is a FrameLayout ..
Now see how I initialize my include tag..(If you do not use the right root tag to initialize include,it will crash with a null pointer)
I have an id called g_betta in my web.xml( XML layout that I included in my fragment)
see how I access that view..
public class FragmentAbout extends Fragment {
private RelativeLayout relativeLayoutConnectedInsideIncludeTag;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_fragment,container,false);
FrameLayout view =(FrameLayout) rootView.findViewById(R.id.include_tag);
relativeLayoutConnectedInsideIncludeTag = (RelativeLayout) view.findViewById(R.id.g_betta);
relativeLayoutConnectedInsideIncludeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Yes I Found you", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
my fragment xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<include
android:id="#+id/include_tag"
layout="#layout/web"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
my include--> web.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/maroon"
android:gravity="center_horizontal">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/g_betta"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HELLO"
android:textColor="#FFF"
android:textSize="20dp" />
<RelativeLayout
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"
android:background="#color/colorPrimaryDark"
android:gravity="bottom"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
</RelativeLayout>
<ImageView
android:id="#+id/imageone"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/lin"
android:layout_margin="16dp"
android:background="#drawable/girl"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/imagetwo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/imageone"
android:layout_margin="16dp"
android:background="#drawable/amanda"
android:scaleType="fitXY" />
</RelativeLayout>
</ScrollView>
</FrameLayout>
Do like this:
button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// write your code here...
});

layout_anchor and app:layout_anchorGravity not working in fragments

I'm using CoordinateLayout in fragment's layout file. There I need to use ImageButton between two layouts. I got sample code from [this][1] answer.
but when i put my fragment in viewpager it dose not work correctly here is my code.
<img src="https://i.stack.imgur.com/wZasU.png"/>
public class Profile extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View Rootview =inflater.inflate(R.layout.tab_profile_fragment, container, false);
CircleImageView fab = (CircleImageView) Rootview.findViewById(R.id.fab);
return Rootview;
}
}
Xml code :
<android.support.design.widget.CoordinatorLayout
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/aasda"
android:weightSum="1"
android:orientation="vertical">
<LinearLayout
android:id="#+id/viewA"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".3"
android:orientation="horizontal">
</LinearLayout>
<RelativeLayout
android:id="#+id/viewB"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".7"
android:background="#color/white"
android:gravity="top"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/tab_profile_cardviewname"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</android.support.v7.widget.CardView>
</RelativeLayout>
</LinearLayout>
<com.citizenassistant.View.Componnent.CircleImageView
android:id="#+id/fab"
android:layout_width="90dp"
android:layout_height="90dp"
android:clickable="true"
android:src="#color/blue"
android:backgroundTint="#color/white"
app:layout_anchor="#id/tab_profile_cardviewname"
app:layout_anchorGravity="top|center_horizontal"/>

Create a layout in XML and use it several times

I´d like to design a layout in XML and want to duplicate it dynamicly, depence on the size of a list. It has to be done in a fragment. Unfortunally I cannot find how to do it in fragments.
This ist the idea of the XML:
<?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"
>
<FrameLayout
android:id="#+id/complete_map_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ICH bin die MAP!! Ich möchte ein Bild werden"
android:id="#+id/textView7"
android:layout_gravity="center_horizontal" />
<!-- Erstellung einzelner Räume -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="20dp"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:id="#+id/roomRow">
<!-- Bezeichnung Raum -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:orientation="vertical"
android:id="#+id/roomNameOne"
android:background="#ff0000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/roomName"
android:layout_gravity="center_vertical"
android:rotation="-90"
android:text="PLATZHALTER"/>
</LinearLayout>
<!-- Inhalt Raum -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:orientation="horizontal"
android:id="#+id/WeaponNameOne">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/testWeapon"
android:layout_gravity="center_vertical"
android:text="WAFFE"
android:background="#00ff00"/>
</LinearLayout>
<!-- Inhalt Raum2 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:orientation="horizontal"
android:id="#+id/weaponNameTwo"
android:background="#0000ff">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/testWeapon2"
android:layout_gravity="center_vertical"
android:text="WAFFE"/>
</LinearLayout>
<!-- Bezeichnung Raum2 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.10"
android:rotation="90"
android:id="#+id/roomNameTwo"
android:background="#ff0000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/roomName2"
android:layout_gravity="center_vertical"
android:text="PLATZHALTER"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView8"
android:layout_gravity="right|bottom" />
</FrameLayout>
</ScrollView>
This is the java-code:
public class MapOverview extends Fragment {
Bundle extras;
View fragLayoutV;
Game game;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fragLayoutV = inflater.inflate(R.layout.fragment_map, null);
extras = getActivity().getIntent().getExtras();
game = (Game) extras.get("GAME");
return fragLayoutV;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FrameLayout frame = (FrameLayout)fragLayoutV.findViewById(R.id.complete_map_layout);
//LinearLayout roomInflator = (LinearLayout)frame.findViewById(R.id.roomRow);
Log.e("RAUM", game.getRooms().get(0).getName());
for(Room r:game.getRooms()){
frame.addView((LinearLayout)frame.findViewById(R.id.roomRow));
}
}
}
Of course I get an error by doing this. Can anybody help?
The aim is to have a picture like this:
http://i.stack.imgur.com/3DS73.png
Don´t matter about the colors ^^
You need to re-inflate your layout every time you want to add it. You can not add the same View multiple times.
Try something like this:
LayoutInflater inflater = LayoutInflater.from(this.getContext());
for (Room r : game.getRooms()) {
View view = inflater.inflate(R.layout.your_duplicate_layout, frame, false);
// Do whatever you want with view
frame.addView(view);
}
Where R.layout.your_duplicate_layout correspond to your current layout with id=roomRow.

Categories

Resources