I'm working with AFreeChart, i'm trying to display a chart inside a fragment. I've been running around in circles for a while, i've seen a couple of posts that looks similar to my problem but i still cant get my head around it.
This is how my onCreateView() in the fragment class Looks like.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_market, container, false);
viewGroup = (ViewGroup) view.findViewById(R.id.marketGraphFrame);
chartView = (ChartView) view;
chartView.drawChart(ChartFactory.createBarChart("Headline","Time", "Value", createDataset(),PlotOrientation.VERTICAL, true, true, false));
viewGroup.addView(chartView);
return view;
}
This is the xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NYSE" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LSE" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OMX" />
<LinearLayout
android:id="#+id/marketGraphFrame"
android:layout_width="wrap_content"
android:layout_height="600dp"
android:orientation="vertical" >
</LinearLayout>
<RadioGroup
android:id="#+id/radio_group_market"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/candleStickMarket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonChart"
android:text="#string/candle_stick" />
<RadioButton
android:id="#+id/barChartMarket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonChart"
android:text="#string/bar_chart" />
<RadioButton
android:id="#+id/lineChartMarket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonChart"
android:text="#string/line_chart" />
</RadioGroup>
<RadioGroup
android:id="#+id/radio_group_time_market"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/thirtydays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonTime"
android:text="#string/thirty_days" />
<RadioButton
android:id="#+id/oneweek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonTime"
android:text="#string/one_week" />
<RadioButton
android:id="#+id/oneday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonTime"
android:text="#string/one_day" />
</RadioGroup>
</LinearLayout>
And here is the main part of the error message.
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.example.mercbisandroid.ChartView
please advice.
I got it working with these three lines inside an Activity
viewGroup = (ViewGroup) findViewById(R.id.DiaGroup);
chartView = new ChartView(this);
viewGroup.addView(chartView);
And the ChartView is declared in the class :
ChartView chartView;
I finally managed to get it working. Dont know if its 100% accurate todo it this way but :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_market, container, false);
viewGroup = (ViewGroup) view.findViewById(R.id.marketGraph);
chartView = new ChartView(getActivity());
chartView.drawChart(ChartFactory.createBarChart("Headline","Time", "Value", createDataset(), PlotOrientation.VERTICAL, true, true, false));
viewGroup.addView(chartView);
return view;
}
Related
I am trying to control weightSum for linear layout from fragment. I wondered if it's possible to do that using setWeightSum(float) method.
I tried to control weight sum in Linear layout which id is header_Linear.
<LinearLayout
android:id="#+id/header_Linear"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:orientation="horizontal"
android:weightSum="8">
<TextView
android:id="#+id/table_0.0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:visibility="visible"
android:background="#drawable/header_bg"/>
<TextView
android:id="#+id/table_0.1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:gravity="center"
android:text="M"
android:textColor="#color/colorTableItem"
android:visibility="visible"
android:background="#drawable/header_bg"/>
<TextView
android:id="#+id/table_0.2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="T"
android:textColor="#color/colorTableItem"
android:visibility="visible"
android:background="#drawable/header_bg"/>
<TextView
android:id="#+id/table_0.3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="W"
android:textColor="#color/colorTableItem"
android:visibility="visible"
android:background="#drawable/header_bg"/>
<TextView
android:id="#+id/table_0.4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="T"
android:visibility="visible"
android:textColor="#color/colorTableItem"
android:background="#drawable/header_bg"/>
<TextView
android:id="#+id/table_0.5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="F"
android:visibility="visible"
android:textColor="#color/colorTableItem"
android:background="#drawable/header_bg"/>
<TextView
android:id="#+id/table_0.6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="S"
android:textColor="#color/colorTableItem"
android:visibility="visible"
android:background="#drawable/header_bg"/>
<TextView
android:id="#+id/table_0.7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="S"
android:textColor="#color/sunday_text_color"
android:visibility="visible"
android:background="#drawable/header_bg"/>
</LinearLayout>
This is my fragment that using setWeightSum
public class TimeTableFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_timetable, container, false);
LinearLayout linearLayout = root.findViewById(R.id.header_Linear);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getContext()));
String header_value = sharedPreferences.getString("Header","0");
Toast.makeText(getContext(),""+header_value,Toast.LENGTH_LONG).show();
if (header_value.equals("0")){
linearLayout.setWeightSum(6);
}
return root;
}
When I start the app it crashes. I'd like to know if it's possible to control weightSum without errors.
Thank you very much
Upate
The reason why I got nullPointException even though I declared Linear Layout using findViewById was due to xmlns:android="http://schemas.android.com/apk/res/android"in Layout.
The View that contains xmlns:android="http://schemas.android.com/apk/res/android" would return null after using findViewById in this situation
public class TimeTableFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_timetable, container, false);
return root;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LinearLayout linearLayout = view.findViewById(R.id.header_Linear);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getContext()));
String header_value = sharedPreferences.getString("Header","0");
Toast.makeText(getContext(),""+header_value,Toast.LENGTH_LONG).show();
if (header_value.equals("0")){
linearLayout.setWeightSum(6);
}
}
}
I'm trying to insert a fragment to another and I’ve succeed to do this until I’ve lunch the main fragment for the first time it's working but when I’m trying to reload the fragment the app crash, and i have this error: Caused by: java.lang.IllegalArgumentException: Binary XML file line #17: Duplicate id 0x7f070084, tag null, or parent id 0x7f070082 with another fragment for com.example.user.unchained.FooterHome
i have tried two solutions : the first one is to override the onDestroyView method.
#Override
public void onDestroyView() {
super.onDestroyView();
PlaceholderFragment f = (PlaceholderFragment) getFragmentManager()
.findFragmentById(R.id.f);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
And the second one is to make surf in the that the view is already exist and try to remove it before the new one display
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (v != null) {
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null)
parent.removeView(v);
}
v = inflater.inflate(R.layout.fragment_homes, container, false); }
Even if the two solutions doesn’t work for me and still hove the problem please any other solutions can you propose
The Main fragment XML Layout :
<RelativeLayout 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="#color/dark_grey"
android:id="#+id/fHome"
tools:context="com.example.user.unchained.HomesActivity$PlaceholderFragment">
<ListView
android:id="#+id/homeList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:choiceMode="singleChoice"
android:listSelector="#drawable/list_row_selector" />
<fragment
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.example.user.unchained.FooterHome"
android:layout_alignParentBottom="true"
android:id="#+id/f"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:layout="#layout/footer_home" />
</RelativeLayout>
The fragment XML Layout :
<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="#000"
android:id="#+id/homeFooter"
tools:context="com.example.user.unchained.FooterHome">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="What's New ?"
android:textSize="16dip"
android:layout_marginTop="12dip"
android:layout_marginLeft="10dip"
android:drawableLeft="#drawable/status"
android:id="#+id/watsN"
android:textColor="#color/connexion_button"
android:layout_gravity="left|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" | "
android:textSize="25dip"
android:layout_marginTop="6dip"
android:layout_marginLeft="145dip"
android:textColor="#color/connexion_button"
android:layout_gravity="left|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Check In"
android:textSize="16dip"
android:layout_marginTop="12dip"
android:layout_marginLeft="165dip"
android:drawableLeft="#drawable/checkin"
android:id="#+id/checkIn"
android:textColor="#color/connexion_button"
android:layout_gravity="left|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" | "
android:textSize="25dip"
android:layout_marginTop="6dip"
android:layout_marginLeft="265dip"
android:textColor="#color/connexion_button"
android:layout_gravity="left|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Picture"
android:textSize="16dip"
android:layout_marginTop="12dip"
android:layout_marginLeft="290dip"
android:drawableLeft="#drawable/camera"
android:id="#+id/postPic"
android:textColor="#color/connexion_button"
android:layout_gravity="left|top" />
</FrameLayout>
I think that you should use getChildFragmentManager. You can't use child fragment in XML.
For example how you should do:
if (getChildFragmentManager().findFragmentByTag("settingsFragment") == null) {
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
settingsFragment = new FastSettingsFragment();
ft.add(R.id.container, settingsFragment, "settingsFragment");
ft.commit();
}
I am trying to add a listview to an existing android fragment. I made a layout xml file for items in the listview, modified the previous fragment.xml, wrote a new arrayadapter and added something to the fragment.java. But whenever I call the notifyDataSetChanged() method in the fragment.java, I got an error: android.content.res.Resources$NotFoundException: String resource ID #0x0, and then the app crashes. Below please find my codes.
layout.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"
android:paddingBottom="7dp">
<ImageView
android:layout_width="72dp"
android:layout_height="72dp"
android:id="#+id/iv_checkIcon"
android:layout_margin="7dp"
android:background="#EEEEEE"
android:contentDescription="Provider icon"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#id/iv_checkIcon"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dp"
>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/tv_checkName"
android:text="Provider name"
android:textSize="20dp"
/>
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="#+id/tv_checkInDate"
android:text="Check In Date"
android:textSize="14dp"
android:textColor="#888888"
android:layout_marginTop="4dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_checkPoints"
android:layout_alignParentRight="true"
android:text="100分"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:textSize="18dp"
android:textColor="#CF1A12"
/>
</RelativeLayout>
fragment.xml
<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"
tools:context=".fragment.MeActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/me_bg2"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fake_head"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:id="#+id/iv_fake_head"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/iv_fake_head"
android:text="--"
android:textSize="22dp"
android:textColor="#ffffff"
android:layout_marginTop="15dp"
android:id="#+id/tv_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/tv_name"
android:text="积分"
android:textColor="#ffffff"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:background="#drawable/me_points_bg"
android:id="#+id/tv_points"
/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_histories"
android:layout_gravity="bottom" />
</FrameLayout>
Arrayadapter.java
public class RecordArrayAdapter extends ArrayAdapter<CheckInRecord.CheckInRec> {
private int resourceId;
private Context context;
private List<CheckInRecord.CheckInRec> checkInRecList;
public RecordArrayAdapter(Context context, int resourceId, List<CheckInRecord.CheckInRec> checkInRecList)
{
super(context, resourceId, checkInRecList);
this.resourceId = resourceId;
this.context = context;
this.checkInRecList = checkInRecList;
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(resourceId, parent, false);
}
TextView textViewName = (TextView) convertView.findViewById(R.id.tv_checkName);
TextView textViewCheckInDate = (TextView) convertView.findViewById(R.id.tv_checkInDate);
TextView textViewPoints = (TextView) convertView.findViewById(R.id.tv_checkPoints);
ImageView imageViewIcon = (ImageView) convertView.findViewById(R.id.iv_checkIcon);
CheckInRecord.CheckInRec checkInRec = checkInRecList.get(position);
textViewName.setText(checkInRec.providerName);
textViewCheckInDate.setText(checkInRec.checkInDate);
textViewPoints.setText(checkInRec.providerPoints);
ImageLoader.getInstance().displayImage(checkInRec.providerIcon, imageViewIcon, Utility.displayImageOptions);
return convertView;
}
public int getIsPrize(int position) {return (this.checkInRecList.get(position).isPrize);}
}
fragment.java
public class MeFragment extends Fragment implements ApiRequestDelegate {
private TextView textViewName;
private TextView textViewPoints;
private ProgressDialog progressDialog;
private RecordArrayAdapter recordArrayAdapter;
private List<CheckInRecord.CheckInRec> checkInRecList = new ArrayList<CheckInRecord.CheckInRec>();
public MeFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ApiManager.getInstance().checkInHistories(AppDataManager.getInstance().getUserToken(), AppDataManager.getInstance().getUserPhone(),
Utility.getPictureSize(), this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.fragment_me, container, false);
textViewName = (TextView) fragmentView.findViewById(R.id.tv_name);
textViewPoints = (TextView) fragmentView.findViewById(R.id.tv_points);
ListView listViewRec = (ListView) fragmentView.findViewById(R.id.lv_histories);
recordArrayAdapter = new RecordArrayAdapter(this.getActivity(), R.layout.row_record, checkInRecList);
listViewRec.setAdapter(recordArrayAdapter);
return fragmentView;
}
#Override
public void apiCompleted(ApiResult apiResult, HttpRequest httpRequest) {
if (progressDialog!=null){
progressDialog.dismiss();
}
if (!apiResult.success){
ApiManager.handleMessageForReason(apiResult.failReason, getActivity());
return;
}
CheckInRecord checkInRecord = (CheckInRecord) apiResult.valueObject;
if (checkInRecord != null){
textViewName.setText(checkInRecord.userName);
textViewPoints.setText(String.format("积分%d分", checkInRecord.userPoints));
this.checkInRecList.clear();
this.checkInRecList.addAll(checkInRecord.checkInRecList);
recordArrayAdapter.notifyDataSetChanged();
}
}
}
Make sure checkInRec.providerName, checkInRec.checkInDate and checkInRec.providerPoints values that you set as TextView text have a type of String and cast it to String if it's integer.
// cast to String if checkInRec.checkInDate is integer
textViewPoints.setText(String.valueOf(checkInRec.providerPoints));
UPD:
It seems ListView is located behind header view. Try to change root view of fragment.xml from FrameLayout to LinearLayout with android:orientation="vertical" attribute. Like this:
<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="match_parent"
android:orientation="vertical"
tools:context=".fragment.MeActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/me_bg2"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fake_head"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:id="#+id/iv_fake_head"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/iv_fake_head"
android:text="--"
android:textSize="22dp"
android:textColor="#ffffff"
android:layout_marginTop="15dp"
android:id="#+id/tv_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/tv_name"
android:text="积分"
android:textColor="#ffffff"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:background="#drawable/me_points_bg"
android:id="#+id/tv_points"
/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_histories"
android:layout_gravity="bottom" />
</LinearLayout>
I have SupportMapFragment and I need to add custom controls into it for changing a map type. Calling getView(), I get NoSaveStateFramelayout and I don't think it is a good idea to add it directly into it or its children.
What is the best way how can I can add a button over my map for changing the map type?
I have decided to override onCreateView and encapsulate the map in the code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
View mapView = super.onCreateView(inflater, viewGroup, bundle);
RelativeLayout view = new RelativeLayout(getActivity());
view.addView(mapView, new RelativeLayout.LayoutParams(-1, -1));
// working with view
return view;
}
And it works as I needed.
Try to put your map inside of a Frame Layout with Relative layout together when the map and the relative layout match_parent for the width and the height. In the relative layout place all your desired extended controls. That way the will sit on top on the map.
some thing like that:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/title_gray_background" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:text="#string/tasks"
android:paddingRight="3dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/my_even_darker_gray" />
<Button
android:id="#+id/bLocateMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:background="#drawable/locate_me_button_selector"
android:clickable="true"
android:onClick="locateMeButtonOnClick"
android:text="#string/locate_me"
android:textColor="#color/my_white" />
</RelativeLayout>
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
The view returned from onCreateView of MapFragment is a FrameLayout. So let use it, no need to add another layout (we will reduce view hierarchy).
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
FrameLayout mapView = (FrameLayout) super.onCreateView(inflater, viewGroup, bundle);
mapView.add(someView);
return view;
}
Use this code to add you layout above of the map
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mapView = super.onCreateView(inflater, container, savedInstanceState);
#SuppressLint("InflateParams")
FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_map, null);
view.addView(mapView, 0, new RelativeLayout.LayoutParams(-1, -1));
return view;
}
I did the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--suppress AndroidDomInspection -->
<fragment
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:src="#android:drawable/ic_menu_mylocation"
/>
</RelativeLayout>
Im new to Android and I'm trying to add a TableRow, which I already made with xml, to a TableLayout programmatically. I'm getting Force Closes, most are NullPointerExcpetions.
Here's my java class
public class DayFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.subjects_list, container, false);
TableLayout tl = (TableLayout) view.findViewById(R.id.tl);
TableRow tr = (TableRow) view.findViewById(R.id.tr_row);
tl.addView(tr); //not working, obviously im missing something
//xml parsing stuff
return view;
}
}
This is my layout with the TableLayout:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dp" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<include
android:id="#+id/header"
layout="#layout/table_header" />
<include android:id="#+id/h_divider"
layout="#layout/horizontal_divider"/>
</TableLayout>
</ScrollView>
</HorizontalScrollView>
And the TableRow:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tr_row"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_hour"
android:layout_weight="1"
android:gravity="center"
android:padding="8dp"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:layout_weight="1"
android:background="#drawable/vertical_cell_divider" />
<TextView
android:id="#+id/tv_subject"
android:layout_weight="1"
android:gravity="left"
android:padding="8dp"
android:textSize="18sp" />
<View
android:layout_weight="1"
android:background="#drawable/vertical_cell_divider" />
<TextView
android:id="#+id/tv_start"
android:layout_weight="1"
android:gravity="left"
android:padding="8dp"
android:textSize="18sp" />
<TextView
android:id="#+id/tv_bar"
android:layout_weight="1"
android:gravity="left"
android:textSize="18sp" />
<TextView
android:id="#+id/tv_end"
android:layout_weight="1"
android:gravity="left"
android:padding="8dp"
android:textSize="18sp" />
</TableRow>
I tried lots of different ways to achieve it but still nothing.
Replace code:
TableRow tr = (TableRow) view.findViewById(R.id.tr_row);
tl.addView(tr); //not working, obviously im missing something
with
View tr = inflater.inflate(R.layout.table_row_layout, null,false);
tl.addView(tr); //not working, obviously im missing something