I have used fragments
activity_fragment_demo.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"
android:orientation="vertical"
tools:context=".FragmentDemo" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="#+id/simple_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
FragmentDemo.java
public class FragmentDemo extends FragmentActivity implements OnClickListener {
Button b1, b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_demo);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragment_demo, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
addFragment(new F1(this), false,
FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
break;
case R.id.button2:
addFragment(new F2(this), false,
FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
break;
default:
break;
}
}
void addFragment(Fragment fragment, boolean addToBackStack, int transition) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, fragment);
ft.setTransition(transition);
if (addToBackStack)
ft.addToBackStack(null);
ft.commit();
}
}
F1.java
public class F1 extends Fragment {
Context c;
View v;
public F1(FragmentDemo fragmentDemo) {
// TODO Auto-generated constructor stub
this.c = fragmentDemo;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
v = inflater.inflate(R.layout.f1, container, false);
return v;
}
}
F2.java
public class F2 extends Fragment {
Context c;
View v;
public F2(FragmentDemo fragmentDemo) {
// TODO Auto-generated constructor stub
this.c = fragmentDemo;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
v = inflater.inflate(R.layout.f2, container, false);
return v;
}
}
OUTPUT SCREEN: -- > WHEN I CLICK BUtton on lefthand side
Similarly for next button
Now HOW CAN I Merge the thow buttons like below
Using fragments i have used .....
If i click on button once Activity-A1 must display in button
below .....
similarly If i click on button again Activity-A2 must display
below
Then if i click on third time ...... Activity-A1 must display
again
A Boolean nature of single button that repeats its cycle !
How to achieve this !
There are many ways to do this but here is a simple solution.
First change activity_fragment_demo.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"
android:orientation="vertical"
tools:context=".FragmentDemo" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:visibility="gone"/>
</FrameLayout>
<LinearLayout
android:id="#+id/simple_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
next in FragmentDemo.java just change your onClick method like this:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
addFragment(new F1(this), false,
FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
b1.setVisibility(View.GONE);
b2.setVisibility(View.VISIBLE);
break;
case R.id.button2:
addFragment(new F2(this), false,
FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
b2.setVisibility(View.GONE);
b1.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
Your Activity could keep track of the state with a boolean. For example like this:
private boolean state = false;
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
state = !state;
if (state) {
addFragment(new F2(this), false,
FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
} else {
addFragment(new F1(this), false,
FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
break;
default:
break;
}
}
Related
I am trying to display a pop up window when i click on a menu overflow item. I have written some code also for that. But it outputs nothing.
Here is some code:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.settings_id:
displayPopupWindow();
return true;
case R.id.about_us_id:
return true;
case R.id.logout_id:
startActivity(new Intent(HomePage.this,MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void displayPopupWindow() {
PopupWindow popup = new PopupWindow(this);
View layout = getLayoutInflater().inflate(R.layout.popup, null);
popup.setContentView(layout);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
My Xml file Code is below:
<?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:background="#color/colorPrimary">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="165dp"
android:id="#+id/textView"
tools:text="This is a Pop Up Window!"
android:textSize="36sp"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:textAlignment="center"
android:fontFamily="sans-serif" />
</RelativeLayout>
Where is the error? Please help me.
Here is your solution,
MainActivity.java
public class MainActivity extends Activity {
private PopupWindow mPopupWindow;
private Button btnClosePopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
break;
case R.id.gamepad:
Toast.makeText(getBaseContext(), "You selected Gamepad", Toast.LENGTH_SHORT).show();
break;
case R.id.camera:
Toast.makeText(getBaseContext(), "You selected Camera", Toast.LENGTH_SHORT).show();
initiatePopupWindow();
break;
case R.id.video:
Toast.makeText(getBaseContext(), "You selected Video", Toast.LENGTH_SHORT).show();
break;
case R.id.email:
Toast.makeText(getBaseContext(), "You selected EMail", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private void initiatePopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,
(ViewGroup) findViewById(R.id.popup_element));
mPopupWindow = new PopupWindow(layout, 300, 370, true);
mPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mPopupWindow.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp">
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
I want to add two linear layouts at same position with width same as parent. When I press Button1 linearLayout1 should appear and on Button2 LinearLayout2 should appear. How to do this in android?
Try this:
First create your_xml_file.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:orientation="vertical">
<LinearLayout
android:id="#+id/firstLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<LinearLayout
android:id="#+id/secondLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:focusable="false"
android:layout_centerInParent="true"
android:background="#drawable/ic_launcher"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:focusable="false"
android:background="#drawable/ic_launcher"
android:layout_below="#id/button1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And java clas...
public class YourActivity extends Activity {
private Button button1,button2;
private LinearLayout linearLayout1,linearLayout2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourXml);
button1 = (Button).findViewById(android.R.id.button1);
button2 = (Button).findViewById(android.R.id.button2);
linearLayout1 = (LinearLayout).findViewById(android.R.id.firstLinear);
linearLayout2 = (LinearLayout).findViewById(android.R.id.secondLinear);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayout2.setVisibility(View.VISIBLE);
linearLayout1.setVisibility(View.GONE);
}
});
}
Hope this will help you.Happy coding :)
You can use the method setVisibility() in your code, as:
view.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
If you are not using Fragment for task, you can simply use setVisibility(View.VISIBLE) and setVisibility(View.INVISIBLE) / setVisibility(View.GONE) ( if other UI elements don't depend on view you'll set GONE)
on onClick of buttons. It'll look like this:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.INVISIBLE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.INVISIBLE);
}
});
You can combine multiple fragments in a single activity and reuse a
fragment in multiple activities too. You can think of a fragment as a
modular section of an activity, which has its own lifecycle, receives
its own input events, and which you can add or remove while the
activity is running.
Here's how it can be done using Fragment:
First create a layout file with 2 buttons and a Framelayout, we will write code later to replace the framelayout with the contents in the fragment.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/ll1"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
android:id="#+id/bt1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"
android:id="#+id/bt2"
android:layout_alignBottom="#+id/frame"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ll1"
android:id="#+id/frame"/>
</RelativeLayout>
Next create 2 fragments, for eg FragmentA and fragmentB
public class fragmentA extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.l1, container, false); //create a layout here
// add some code to set some text for eg
return v;
}
}
Create a second fragment B
public class fragmentB extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.l1, container, false); //create a layout here
// add some code to set some text for eg
return v;
}
}
Now add the Activity code with 2 buttons, when the button 1 is clicked invoke fragmentManager.beginTransaction().replace(R.id.frame, new fragmentA()).commit();, this will replace the framelayout with the contents of the fragmentA, similaryly when button 2 is clicked invoke fragmentManager.beginTransaction().replace(R.id.frame, new fragmentB()).commit();
Complete code:
public class MyFragment extends ActionBarActivity implements View.OnClickListener {
Button bt1,bt2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tt);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (v.getId()) {
case R.id.bt1:
fragmentManager.beginTransaction()
.replace(R.id.frame, new BlockCallers()).commit();
break;
case R.id.bt2:
fragmentManager.beginTransaction()
.replace(R.id.frame, new smsSetting()).commit();
}
}
}
For tutorials refer to this link(official link)
I am new at android app development.I have a problem with my app.I am doing a simple 'Todo List App'. When I deploy my app to android phone.First time it works well.When I change orientation of device, onCreateView() and onActivityCreated() in MyAddFragment works multiple.And 'Add Button' doesn't work after changing orientation.****Please help me how to get rid of the problem.
The following my activity and fragment classes and xml files.
public class MainActivity extends Activity implements Communicator{
FragmentManager man;
private ArrayAdapter<String> todoArrayAdapter;
private ArrayList<String> todoItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
man = getFragmentManager();
MyAddFragment f1 = new MyAddFragment();
MyListFragment f2 = new MyListFragment();
FragmentTransaction transaction = man.beginTransaction();
transaction.add(R.id.frameLayout1, f1, "Add");
transaction.add(R.id.frameLayout2, f2,"List");
Log.i("a", "onCreate - ADDED FRAGMENT");
if (savedInstanceState == null) {
todoItems = new ArrayList<String>();
}else {
todoItems = savedInstanceState.getStringArrayList("veri");
}
todoArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,todoItems);
f2.setListAdapter(todoArrayAdapter);
transaction.commit();
}
public void respond(String data) {
MyListFragment fb = (MyListFragment) man.findFragmentById(R.id.frameLayout2);
todoItems.add(data);
todoArrayAdapter.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putStringArrayList("veri", todoItems);
}
}
public class MyAddFragment extends Fragment{
Button btnAdd;
EditText txtEdit;
Communicator comm;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add, container,false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnAdd = (Button) getActivity().findViewById(R.id.btnAdd);
txtEdit = (EditText) getActivity().findViewById(R.id.txtEdit);
comm = (Communicator) getActivity();
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String newRecord = txtEdit.getText().toString();
comm.respond(newRecord);
txtEdit.setText("");
}
});
}
}
public class MyListFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container,false);
return view;
}
}
public interface Communicator {
public void respond(String data);
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
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"
tools:context="com.example.ceng389hw1.MainActivity" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:background="#ab3e0f" >
</FrameLayout>
<FrameLayout
android:id="#+id/frameLayout2"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_below="#+id/frameLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ab3eab"
>
</FrameLayout>
</RelativeLayout>
fragment_add.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ab3e0f">
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/btnStringAdd" />
<EditText
android:id="#+id/txtEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnAdd"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/btnAdd"
android:ems="10"
android:inputType="text" />
</RelativeLayout>
fragment_list.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:orientation="vertical"
android:background="#ab5810" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
You should change two files in your Application.
First step, you should add following code in androidmanifest file.
android:configChanges="orientation|screenSize"
After you add the code to Androidmanifest, Your androidmanifes file must be like below.
<activity
android:name="com.example.ceng389hw1.MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name" >
Secon Step, You must be override onConfigurationChanged function like below.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText( this, "landscape", Toast.LENGTH_SHORT ).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
For more detail -> http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
In your main activity check is fragment already added in activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if(getFragmentManager().getFragment("Add")==null) {
//initialize fragment, create transaction and commit
}
...
}
You can add configChanges parametre inside manifest.xml it will provide you to ignore reinilizating after config changes you can do it simply like that
<activity
android:name="com.youractivity"
android:configChanges="orientation" />
I hope it will help you :)
So I want to have 3 buttons that always appear throughout fragments. I've got an activity with 3 buttons and a fragment layout that contains a fragment with actionbarsherlock inside. The problem is, the buttons in the activity are not clickable and nothing can be executed in the OnClickListener; but the buttons run just fine if I replace the fragment containing actionbarsherlock with a regular fragment without actionbarsherlock inside. I wonder what's wrong.
Here is my code.
activity_main.xml:
<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:paddingBottom="2dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/button1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/fragment_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:text="mission" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:text="feed" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_alignRight="#+id/fragment_content"
android:text="Profile" />
</RelativeLayout>
MyActivity.java:
public class MyActivity extends SherlockFragmentActivity {
/**
* Called when the activity is first created.
*/
Fragment frg;
ActionBarFragment b;
Button btnmission;
Button btnfeed;
Button btnprofile;
boolean small;
static int previousItem;
int tabname;
private Fragment mVisible = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
previousItem=1;
btnmission = (Button) findViewById(R.id.button1);
btnfeed = (Button) findViewById(R.id.button2);
btnprofile = (Button) findViewById(R.id.button3);
btnmission.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("clicked", "button");
if(previousItem!=1){
Intent intent=new Intent(getBaseContext(),MyActivity.class);
startActivity(intent);
}
previousItem=1;
}
});
btnfeed.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(previousItem!=2){
Intent intent=new Intent(getBaseContext(),MissionFragment.class);
intent.putExtra("class", "Feed");
startActivity(intent);
}
previousItem=2;
}
});
btnprofile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
private void setupFragments() {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
frg = (ActionBarFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_content);
if (frg == null) {
frg = new ActionBarFragment();
ft.add(R.id.fragment_content, frg);
}
ft.hide(frg);
ft.commit();
}
private void showFragment(Fragment fragmentIn) {
if (fragmentIn == null) return;
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
if (mVisible != null) ft.hide(mVisible);
ft.show(fragmentIn).commit();
mVisible = fragmentIn;
}
It adds a fragment to the activity and actionbar inside the fragment.
ActionBarFragment.java:
public class ActionBarFragment extends SherlockFragment {
int tabname;
#Override
public void onStart()
{
// TODO Auto-generated method stub
super.onStart();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.empty_view, container, false);
// Getting an instance of action bar
ActionBar actionBar = this.getSherlockActivity().getSupportActionBar();
// Enabling Tab Navigation mode for this action bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Enabling Title
actionBar.setDisplayShowTitleEnabled(true);
// Creating Android Tab
Tab tab1 = actionBar.newTab()
.setText("Nova Missoes")
.setTabListener(new CustomTabListener<ListMission>(this.getSherlockActivity(), "mission list", ListMission.class));
// Adding Android Tab to action bar
actionBar.addTab(tab1);
// Creating Apple Tab
Tab tab2 = actionBar.newTab()
.setText("Missoes Concluidas")
.setTabListener(new CustomTabListener<MissionAccomplished>(this.getSherlockActivity(), "mission accomplished", MissionAccomplished.class));
// Adding Apple Tab to action bar
actionBar.addTab(tab2);
Intent in=getSherlockActivity().getIntent();
tabname=in.getIntExtra("tabname", 0);
Log.i("intent", Integer.toString(tabname));
if(tabname==2){
actionBar.selectTab(tab2);
}
// Orientation Change Occurred
if(savedInstanceState!=null){
int currentTabIndex = savedInstanceState.getInt("tab_index");
actionBar.setSelectedNavigationItem(currentTabIndex);
}
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
int currentTabIndex = getSherlockActivity().getSupportActionBar().getSelectedNavigationIndex();
outState.putInt("tab_index", currentTabIndex);
super.onSaveInstanceState(outState);
}
}
No problem with displaying the buttons and the tabs, it's just that the OnClickListener is not called when the program runs.
I'm trying to do dynamic fragment layouts.
I'm using 2 xml files : one is the main_layout and the other one is the fragment1.
I used the Log.i so I can know where my program is crashing, but it cites that is running the code fine but it doesn't appear the fragment1 on the main_layout.
Here is my code:
main_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/testFragment"
android:visibility="gone" >
</FrameLayout>
fragmenta.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:orientation="vertical"
android:id="#+id/fragmenta"
android:background="#F000F0">
<TextView
android:id="#+id/fragmentatext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragmenta 1" />
</LinearLayout>
here is my Side_Fragment which extends SherlockFragment :
View view;
private final String TAG = "Side_Fragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmenta, container, false);
Log.i(TAG,"fragment installed");
return view;
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
}
and here is the main activity :
private final String TAG = "Main Activity";
FragmentManager fm = getSupportFragmentManager();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Log.i(TAG,"OnCreate");
Button add = (Button) findViewById(R.id.button1);
Log.i(TAG,"called FragmentManager");
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.twitter:
Log.i(TAG,"Twitter is clicked");
// Check to see if the Fragment back stack has been populated // If not, create and populate the layout.
Fragment detailsFragment = (Side_Fragment)fm.findFragmentById(R.id.fragmenta);
Log.i(TAG,"called FragmentManager");
if (detailsFragment == null) {
Log.i(TAG,"pass ifstatment");
FragmentTransaction ft = fm.beginTransaction();
Log.i(TAG,"called FragmentTransaction");
Side_Fragment test = new Side_Fragment();
ft.add(R.id.testFragment,test);
Log.i(TAG,"called add");
ft.addToBackStack(null);
ft.commit();
Log.i(TAG,"called commit");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
return true;
case R.id.facebook:
Log.i(TAG,"Facebook is clicked");
return true;
case R.id.refresh:
Log.i(TAG,"Refresh is clicked");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
final MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i(TAG,"onPause");
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i(TAG,"onResume");
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG,"onStart");
}
}
You've set your FrameLayout width a bit small: android:layout_width="0dp". That will make it impossible to see.