Navigate between fragments (ViewFlipper) - android

I have an activity (android.support.v4.app.FragmentActivity) with a ViewFlipper and within this I have several fragments
src/com.package.WelcomeActivity.java
package com.package;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class WelcomeActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}
}
res/layout/activity_welcome.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/flip2"
class="com.package.fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<fragment
android:id="#+id/flip2"
class="com.package.fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- ... -->
<fragment
android:id="#+id/flipN"
class="com.package.fragmentN"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</ViewFlipper>
</LinearLayout>
Now each fragment are composed by two mayor parts: content and actions (back, continue) when the user tab on continue action execute a function inside the fragment but i dont know how to call the ViewFlipper.showNext() and ViewFlipper.showPrevious() inside the fragments

Let your Activity do it.
Make public methods in your WelcomeActivity that call ViewFlipper.showNext() and ViewFlipper.showPrevious, something like this:
public void showNextFragment() {
mViewFlipper.showNext();
}
public void showPreviousFragment() {
mViewFlipper.showPrevious();
}
In your fragments, you can then call the Activity's methods, like this:
WelcomeActivity parent = (WelcomeActivity) getActivity();
parent.showNextFragment();
// or parent.showPreviousFragment();
I just typed the code here and didn't try it, there might be typo, so don't just copy-paste. But I hope it's illustrating my point well.

Related

Design two tabs within activity after putting some views in that activity

I am a newbie in Android, how can I do something like that.
I extend TabActivity when to create Tabs like this but without that header.
If you want to implement the view you post here ,you can code the view by yourself:
the header is in the top ,and below the header ,a TabHost is needed ,below the TabHost,you put a viewpager contains two fragments to display your data,you can control your viewpager with method setCurrentItem when you click tabs.
Try this code
MainActivity
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import com.viewpagerindicator.CirclePageIndicator;
public class LauncherActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
pager.setAdapter(new GuidePagerAdapter(getSupportFragmentManager()));
indicator.setViewPager(pager);
}
}
ativity_main
<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:background="#drawable/background"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
android:orientation="vertical" >
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp" />
</LinearLayout>
</LinearLayout>
Adapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class GuidePagerAdapter extends FragmentStatePagerAdapter {
public GuidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
if (pos == 0)
return new Fragment1();
else
return new Fragment2();
}
#Override
public int getCount() {
return 2;
}
}
You can add two tabs on main_activity and on click of tab change the fragment in adapter.

ActionButton/ActionBar does not showup in TabHost method

I'm trying to have a different action bar for each new tab (activity) in my project
I have created my first Activity and put it as a tab in my Main Activity
my first activity (tab) has the action button ( Start ) in the action bar of its activity
somehow nothing show up in the action bar for this tab
and if so I would to put more activities (tabs) no action bar/buttons will be shown
there is no error in my logcat
this is my code :
AndroidTabLayoutActivity.java
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for activity1
TabSpec activity1 = tabHost.newTabSpec("Photos");
activity1.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, OneActivity.class);
activity1.setContent(photosIntent);
tabHost.addTab(activity1); // Adding photos tab
}
}
OneActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class OneActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photos_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity1_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_start:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="b3du.im.tabLayout.AndroidTabLayoutActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
photos_layout.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">
<!-- Screen Design for Activity1 -->
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="im the first one "
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
activity1_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Exit -->
<item android:id="#+id/action_start"
android:title="Start"
android:showAsAction="withText|always" />
</menu>
you need to add the Action buttons in the main activity (where the tabs are hosted) NOT in the tab activiy.
And later manage them in the tab activity by using
getParent().getActionBar()
and also..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getParent().getMenuInflater().inflate(R.menu.home_tabs, menu);
//getMenuInflater().inflate(R.menu.home_tabs, menu);
return true;
}

Respond to send button

I installed the Android SDK bundle today and I am following the "My First App" tutorial and I am stuck, it states:
Open the MainActivity class (located in the project's src/ directory) and add the corresponding method:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
Where do I put this in the file? and is this the "MainActivity.java" file?
I have tried and I keep getting errors so I am obviously going wrong somewhere.
activity_main.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="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
}
Hope I have made my problem clear, I looked on the forum for an answer but I couldn't find anything.
If you have a button in your(say activity_main.xml) xml layout and you have the below attribute for button
android:onClick="sendMessage"
and you have the below in MainActiivty.java
setContentView(R.layout.activity_main);
You should have the below in MainActivity.java
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
Example:
MainActivity.java
// Your imports
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //setting the layout to activity
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
// other widgets
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:onClick="sendMessage"
android:text="Button" />
</RelativeLayout>
If you are a new android developer and doing your first so start from basic like launch new activity it contain hello world or any text view, button then you will clear idea about application.
create your android application
in XML layout drag the button and text view
run your first app.
you will get your output.
Put it in MainActivity.java at the top right after
public class MainActivity extends ActionBarActivity {
After you do this, you may have to import. Do this by pressing control / shift / O (not zero)

I cannot figure out whats wrong with the onClickListener function of button here in fragments

**I want to click on a button in my fragment 2 , and replace fragment 1 with fragment 3 as shown in the code.
But the findViewById(r.id.mybutton) is returing 'null' .
I tried debugging the code but its not able to use the created 'buttontoggle'
Hope you can help me out with the above problem
Thanks in advance :)
package com.vivekmishra1991.testfrag;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.fragment_container)!=null){
if(savedInstanceState!=null){
return;
}
//fragment 1
f1 F1= new f1();
F1.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, F1).commit();
//fragment2(the fragment with the button)
f2 F2=new f2();
F2.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container1, F2).addToBackStack(null).commit();
}
//code for buttton onclick function
Button toggleButton = (Button)findViewById(R.id.mybutton);
toggleButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ // fragment 3 that has to be replaced with fragment 1
f3 F3=new f3();
F3.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,F3).addToBackStack(null).commit();
}
});
}
#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;
}
}
EDIT
Here is my activityMain.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/fragment_container"
android:layout_height="0dp"
android:layout_weight="6"
android:layout_width="match_parent" />
<FrameLayout
android:id="#+id/fragment_container1"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"/>
</LinearLayout>
f2.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">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Next!"
android:id="#+id/mybutton"
android:layout_gravity="left|center_vertical"/>
</LinearLayout>
You can check your .xml file to make sure that "mybutton" does exist.
If so, try Project > Clean. Occasionally, eclipse doesn't add the button to the R.java file, and cleaning the project tends to fix that. If its not that either, you can always try to recreate the button in your xml file.

findFragmentByTag returning null

I am trying to allow my app to call functions from my fragment classes in the fragmentactivity. However I am having a lot of issues just finding the fragment. Right now I can find one fragment by id which I think is the fragment hosting the tabs. But when I get the child fragment manager I get returned a null. Can anyone help?
This is the fragment activity
package com.example.profileactivity;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
public class ProfileActivity extends FragmentActivity {
// Fragment TabHost as mTabHost
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this,getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("user").setIndicator("User",
getResources().getDrawable(R.drawable.ic_user_tab)),
UserProfileTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("payment").setIndicator("Payment",
getResources().getDrawable(R.drawable.ic_payment_tab)),
PaymentInfoTab.class,null);
}
public void buttonClick(View view){
FragmentManager fm = getSupportFragmentManager();
if(fm.findFragmentById(R.id.realtabcontent) == null){
Log.d("TABHOST", "didnt find fragment");
}
else{
Log.d("TABHOST", "found fragment");
//PaymentInfoTab paymentTab = (PaymentInfoTab)fm.findFragmentByTag("payment");
//paymentTab.boom();
if(fm.findFragmentById(R.id.realtabcontent).getChildFragmentManager().findFragmentByTag("payment") != null){
PaymentInfoTab paymentTab = (PaymentInfoTab)fm
.findFragmentById(R.id.realtabcontent)
.getChildFragmentManager()
.findFragmentByTag("payment");
paymentTab.boom();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.profile, menu);
return true;
}
}
This is the xml for the fragmentactivity.
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Try using the tag on the activity's FragmentManager directly.
The complete reference of your example seems to be this. Follow this example with more details, you will see that the writer keeps track of tabs and their tag.
I had the similar issue with fetching fragments after tab change which always returned null. Posting Runnable at the end of TabHost message queue did the trick:
tabHost.post(new Runnable() {
#Override
public void run() {
Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(tag);
fragment.updateList(newList);
}
});

Categories

Resources