In fragment A, it has listView. When the list is clicked, it should pass the data to fragment B.
A.java
B fragment2 = new B();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
B.java
public class B extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.b, container, false);
Log.d("TAG","fragment2");
return view;
}
}
a.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/some_text"
android:textSize="20sp" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</FrameLayout>
b.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:orientation="vertical"
android:fillViewport="false">
<AbsoluteLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="218dp"
android:layout_height="47dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Update Page"
android:id="#+id/textView114"
android:layout_x="12dp"
android:layout_y="20dp" />
</AbsoluteLayout>
</ScrollView>
This is how my Fragment A looked like.
When the list is clicked.
What's wrong here :?
in your a.xml use the following :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/app_name"
android:textSize="20sp" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</FrameLayout>
I think may be your problem in instantiating the EditText fields, you'd better declare your items after the view is created, so you have to override the method onViewCreated and make your declarations in it, then put your database work in a try-catch block to make sure it isn't the reason of any problems:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
name3 = (EditText) view.findViewById(R.id.editText9);
date3 = (EditText) view.findViewById(R.id.editText12);
Bundle bundle = this.getArguments();
date = bundle.getString("date1");
ID = bundle.getString("ID");
RetrievePage(date, ID);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.b, container, false);
return view;
}
public void RetrievePage(String date, String id) {
final String date2 = date;
final String id2 = id;
Log.d("m", id2);
name3.setText("Lim ");
date3.setText(date2);
try {
database = dbHelper.getWritableDatabase();
c = database
.rawQuery(
"SELECT i.Weather FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i._id WHERE i.Name = ? AND i._id= ? ",
new String[] { String.valueOf("Lim"),
String.valueOf(id2) }, null);
if (c != null) {
while (c.moveToNext()) {
Info I = new Info();
Force WF = new Force();
String Weather = c.getString(c
.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
}
}
c.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
About fragments overlapping, you must declare each fragment as a separated fragment, and the main FragmentActivity is the activity that holds the fragments which in your case must be an empty activity with only the ViewPager or FrameLayout that contain fragments:
<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="#drawable/bg" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ac_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9" />
</RelativeLayout>
So, you must have MainActivity that contains two fragments FragmentA and FragmentB
for reference look at this answer.
Related
I am trying to replace the content of frame_layout with a view of fragment.
I used to do that by just using transaction.replace but it just put the view on the top of the main content.
Here is the code:
That's where I replace the frame_layout whose ID is contain:
VideosFragment videosFragment = new VideosFragment();
Bundle bundle = new Bundle();
bundle.putCharSequence("Character", view.getContentDescription());
videosFragment.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.contain , videosFragment).addToBackStack(null).commit();
here is the xml of main_Activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.shobakyDC.muchannel.MainActivity">
<FrameLayout
android:id ="#+id/contain"
android:layout_width="match_parent"
android:background="#drawable/islamic_green"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:paddingStart="50dp"
android:paddingTop="10dp"
android:verticalSpacing="10dp"
android:paddingRight="0dp"
android:paddingLeft="50dp"
android:paddingEnd="0dp">
</GridView>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/Navigator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/header"
android:layout_gravity="start"
android:background="#android:color/white"
app:itemTextColor="#color/navcolor"
app:itemIconTint="#color/navcolor"
app:menu="#menu/navigationmenu">
</android.support.design.widget.NavigationView>
the video_fragment:
public class VideosFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.videofragment_layout , container , false);
Bundle args = getArguments();
TextView textView = view.findViewById(R.id.textView);
textView.setText(args.getString("Character"));
return view;
}
}
In xml of Main Activity, you have to make an empty FrameLayout with an ID like this:
<FrameLayout
android:id ="#+id/contain"
android:layout_width="match_parent"
android:background="#drawable/islamic_green"
android:layout_height="match_parent">
</FrameLayout>
and then make a separate xml layout for this fragment. Now you have to replace this fragment layout with frame layout in Main Activity.
I've been trying to replace the current fragment with another one when an item on the list view has been pressed. However when the button is pressed, the onClick method is fired but it does not replace the fragment.
JournalFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_journal_view, container, false);
context = getContext();
ListView myView = (ListView) rootView.findViewById(R.id.listView);
TableControllerUser TCU = new TableControllerUser(context);
final TableControllerJournal TCJ = new TableControllerJournal(context);
int accID = TCU.getLoggedInId();
Cursor cursor = TCJ.getAllJournals(accID);
Cursor allFood = TCJ.getAllFoodJournals(accID);
Cursor allActivity = TCJ.getAllActivityJournals(accID);
Cursor[] cursors = {cursor, allFood, allActivity};
MergeCursor m = new MergeCursor(cursors);
final JournalAdapter adapter = new JournalAdapter(context, m, 0);
myView.setAdapter(adapter);
myView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor) adapter.getItem(position);
Toast.makeText(getActivity(), "onClick Pressed!", Toast.LENGTH_SHORT).show();
String title = c.getString(c.getColumnIndexOrThrow("title"));
if (title.contains("Glucose")) {
String glucose = c.getString(c.getColumnIndexOrThrow("glucose"));
String dateTime = c.getString(c.getColumnIndexOrThrow("glucose_time"));
String journalId = c.getString(c.getColumnIndexOrThrow("_id"));
String split[] = dateTime.split(" ");
String date = split[0];
String time = split[1];
Fragment gluFrag = new GlucoseFragment();
Bundle bundle = new Bundle();
bundle.putString("time", time);
bundle.putString("date", date);
bundle.putString("glucose", glucose);
bundle.putString("journalId", journalId);
gluFrag.setArguments(bundle);
((GraphActivity) getActivity()).replaceFragments(gluFrag, bundle);
}
}
});
return rootView;
// Inflate the layout for this fragment
}
}
JournalFragment.XML
<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"
tools:context="edu.tp.sghealthapp.JournalViewFragment"
android:id="#+id/jv">
<ListView
android:id="#+id/listView"
android:layout_below="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#color/iron"
android:dividerHeight="1dp" />
</LinearLayout>
JournalListViewLayout.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="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/txt_listTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/arrowIcon"
android:layout_toStartOf="#+id/arrowIcon"
android:textColor="#000000"
android:focusable="false"
android:textSize="16sp"
android:typeface="sans" />
<ImageView
android:id="#+id/arrowIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:focusable="false"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_arrow" />
</RelativeLayout>
GraphActivity.java
public class GraphActivity extends AppCompatActivity {
Context context;
TabLayout mTabLayout;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_graph);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
ViewPagerAdapter pgAdapter = new ViewPagerAdapter(getSupportFragmentManager());
pgAdapter.addFragments(new GraphFragment(), "Graph");
pgAdapter.addFragments(new JournalViewFragment(), "Journal");
mViewPager.setAdapter(pgAdapter);
mTabLayout.setupWithViewPager(mViewPager);
}
public void replaceFragments(Fragment newFragment, Bundle bundle) {
Fragment fragment = newFragment;
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
fragment.setArguments(bundle);
ft.replace(R.id.jv, fragment);
ft.commit();
}
}
EDIT:
ActivityGraph.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
tools:context=".GraphActivity"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/graph">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primary"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#color/black">
<!--To be fixed in future to occupy whole bar when mode = fixed-->
</android.support.design.widget.TabLayout>
<edu.tp.sghealthapp.library.graphViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tabLayout"></edu.tp.sghealthapp.library.graphViewPager>
</RelativeLayout>
EDIT 2
Tried Harron's and Vivek_Neel's methods, both of them work but cause this problem with the formatting
Putting android:layout_below="#+id/viewPager" in the linearLayout causes the fragment to no longer display
EDIT 3
Solved the formatting problem with this https://stackoverflow.com/a/34573034/5509513
activity_graph.xml :
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
tools:context=".GraphActivity"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/graph">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primary"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#color/black">
<!--To be fixed in future to occupy whole bar when mode = fixed-->
</android.support.design.widget.TabLayout>
<edu.tp.sghealthapp.library.graphViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tabLayout"></edu.tp.sghealthapp.library.graphViewPager>
<LinearLayout
android:id="#+id/jv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<FrameLayout
android:id="#+id/jv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Put this code snip and into activity and check
Try wrap_content the height of View Pager and add Relative Layout with #+id/jd and give it layout below view pager.
Refer this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/graph"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabSelectedTextColor="#FFF"
app:tabTextColor="#000">
<!--To be fixed in future to occupy whole bar when mode = fixed-->
</android.support.design.widget.TabLayout>
<edu.tp.sghealthapp.library.graphViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tabLayout" />
<RelativeLayout
android:id="#+id/jv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/viewPager" />
</RelativeLayout>
EDIT 1
Second way to do it create new layout only with Frame Layout. And use it's Place Holder while replacing your Fragment.
<?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:orientation="vertical" >
<FrameLayout
android:id="#+id/jd"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
i am a beginer android-developer and i need some help:
There is a Fragment in my Project - TasksFragment, there is a ListView element on it. On ListView, using ArrayAdapter Items are formed by a custom layout for Item and then added to ListView. The problem is, that ListView don't react on click processing events.
Here are the code fragments:
TaskAdapter class...
public class TaskAdapter extends ArrayAdapter<Task> {
public TaskAdapter(ArrayList<Task> tasks) {
super(getActivity(), 0, tasks);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_task, null);
}
Task task = getItem(position);
TextView titleTextView = (TextView) convertView.findViewById(R.id.task_list_item_titleTextView);
titleTextView.setText(task.getTitle());
CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.task_list_item_solvedCheckBox);
solvedCheckBox.setChecked(task.isSolved());
return convertView;
}
}
ItemClick fragment.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tasks_fragment, null);
mTasks = TaskLab.get(getActivity()).getTasks();
final ListView lvMain = (ListView) view.findViewById(R.id.listViewTasks);
lvMain.setClickable(true);
TaskAdapter adapter = new TaskAdapter(mTasks);
setHasOptionsMenu(true);
lvMain.setAdapter(adapter);
lvMain.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Task task = (Task)lvMain.getItemAtPosition(position);
Toast.makeText(getActivity().getApplicationContext(), task.getTitle() + " was clicked", Toast.LENGTH_LONG).show();
Intent i = new Intent(getActivity(), AddTaskActivity.class);
startActivity(i);
}
});
return view;
}
XML Layout for List_Item_Task
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/task_list_item_solvedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentRight="true"
android:enabled="false"
android:padding="4dp"/>
<TextView
android:id="#+id/task_list_item_titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/task_list_item_solvedCheckBox"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="22dp"/>
</RelativeLayout>
Xml layout for tasks_fragment.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="#3f638b">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewTasksFragmentHeader"
android:text="Задачи"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listViewTasks" />
</LinearLayout>
Yes your listview is not clickable.
Just add this in your checkbox layout
android:focusable="false"
Your items xml should look like this
<?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:orientation="vertical" >
<CheckBox
android:id="#+id/task_list_item_solvedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" //Add this
android:layout_alignParentRight="true"
android:gravity="center"
android:padding="4dp" />
<TextView
android:id="#+id/task_list_item_titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/task_list_item_solvedCheckBox"
android:paddingBottom="5dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="5dp"
android:textSize="22dp"
android:textStyle="bold" />
Now it should work. I have tested it :)
I'll go straight to the problem. What am I doing wrong when replacing fragments in Android? The expected result when I clicked on Sponsors:
The reality(what I actually got: basically, it just the Sponsors fragment, which just lays on Other fragment):
I'm calling a fragment by
fragment = new Other(this);
Sponsors.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Sponsors" />
</RelativeLayout>
Sponsors.java
...
public class Sponsors extends android.support.v4.app.Fragment {
private Context context;
public Sponsors(Context context) {
this.context = context;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.sponsors, null);
return rootView;
}
}
other.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/other_frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COMMON"
android:textStyle="bold"
android:textColor="#color/header_red"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/header_red"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/sponsors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sponsors"
android:textColor="#color/subtext"
android:textSize="16sp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp" />
</LinearLayout>
</FrameLayout>
Other.java
public class Other extends android.support.v4.app.Fragment {
public Other(Context context) {
this.context = context;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.other, container, false);
sponsors = (TextView) rootView.findViewById(R.id.sponsors);
sponsors.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (savedInstanceState == null) {
Sponsors duedateFrag = new Sponsors();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.other_frame_container, duedateFrag);
ft.addToBackStack(null);
ft.commit();
}
}
});
}
Change add to replace in FragmentTransaction
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.other_frame_container, duedateFrag);
I want to programmatically add a view to ScrollView that is apart of a Fragment layout.
I have attempeted the following code:
public class MessageDetail extends Fragment {
View layout;
TextView messageText;
Conversation conversation;
List<Message> messages;
LayoutInflater inflater;
ViewGroup container;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.inflater = inflater;
this.container = container;
layout = inflater.inflate(R.layout.messenger_detail, container,
false);
messageText = (TextView) layout.findViewById(R.id.tvMessage);
return layout;
}
public void loadConversation(Conversation conversation) {
messageText.setText(conversation.getSubject());
LinearLayout messagesView = (LinearLayout)layout.findViewById(R.id.llMessageDetail);
LinearLayout messageBody = new LinearLayout(getActivity());
View body = inflater.inflate(R.layout.messagebody, container);
messagesView.addView(body);
}
}
messagebody.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMessageBody"
android:layout_width="fill_parent"
android:layout_margin="5dp"
android:layout_height="200dp"
android:background="#353535"
android:orientation="vertical" >
<TextView
android:id="#+id/tvMSBodyFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Research Station:"
android:textColor="#FFFFAA"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tomatohigh"
/>
</LinearLayout>
message_detail.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="match_parent"
android:background="#d3d6da"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#FFAA00" >
<TextView
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="Select a Message:"
android:textColor="#353535"
android:textSize="30dp" />
</LinearLayout>
<ScrollView
android:id="#+id/svMessageDetail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#9900AA"
android:minHeight="300dp" >
<LinearLayout
android:id="#+id/llMessageDetail"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
</LinearLayout>
The final implementaion would see me loop through a list of messages and add the a view with the message_body layout and change each part of the layout to the correct values of each message
Any ideas how to achieve this?
You can do like this:
YourView varView = new YourView(this);
myButton.setLayoutParams(new YourLayout.LayoutParams());
yourParentLayout.addView(varView);
Example: Add a button in parent linear layout
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
myLayout.addView(myButton);