I am making tabs in android. Currently this is how my tabs are working. When I click on any tab it will take me to appropriate tab's view which is fine. But lets say when I click on Profile tab then inside this profile view there is a button Add New. When I click on Add New button then it takes me to it's view but it also removes tabs from the view which I don't want. So how can I make tabs always available even when user clicks any link or button inside any view?
Here is my current code
tabs.xml
<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">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</RelativeLayout>
</TabHost>
Tabs.java
public class Tabs extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
Resources resources = getResources();
TabHost tabHost = getTabHost();
Intent startUseSaldo = new Intent().setClass(this, Usesaldo.class);
TabSpec useSaldo = tabHost
.newTabSpec("UseSaldo")
.setIndicator("Use Saldo")
.setContent(startUseSaldo);
Intent startHistory = new Intent().setClass(this, History.class);
TabSpec history = tabHost
.newTabSpec("History")
.setIndicator("History")
.setContent(startHistory);
Intent startProfile = new Intent().setClass(this, Profile.class);
TabSpec profile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile")
.setContent(startProfile);
// add all tabs
tabHost.addTab(useSaldo);
tabHost.addTab(history);
tabHost.addTab(profile);
tabHost.setCurrentTab(1);
}
}
Profile.java
public class Profile extends Activity {
Button bAddNew;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
bAddNew = (Button)findViewById(R.id.bAddNew);
bAddNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent AddFacility = new Intent(getApplicationContext(), AddFacility.class);
startActivity(AddFacility);
}
});
}
}
AddFacility.java
public class AddFacility extends Activity {
#Override
public void setContentView(int layoutResID) {
// TODO Auto-generated method stub
super.setContentView(R.layout.add_facility);
}
}
Try this example - http://gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity
You have to use ActivityGroup with Tabhost
http://richipal.com/post/2624844577
But as ActivityGroups are depericated you should use Fragments with Tabhost
Related
I have my code for a simple activity to return to the main activity once the user clicks on the home button.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greetings);
// Set up onclicklistener for homeIcon imageview to go to back one activity
ImageView homeIcon = (ImageView) findViewById(R.id.home_icon);
homeIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
This is a java file for a single activity, greetings. I want this home button + functionality for many other activities, but I do not want to copy and paste. Should I make another java class, and implement the function inside of it? I tried, but it can not findviewbyid.
Without your code really hard understand why you can not find by id, but I can offer this way:
HomeButtonActivity.java
public class HomeButtonActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Button home_button = (Button) findViewById(R.id.home_button);
home_button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(HomeButtonActivity.this, "Replace with your own action", Toast.LENGTH_SHORT).show();
}
});
}
}
Activity1.java
public class Activity1 extends HomeButtonActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity1);
super.onCreate(savedInstanceState);
}
}
activity1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.HomeButtonActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<include layout="#layout/home_button" />
</android.support.design.widget.CoordinatorLayout>
home_button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="home button"/>
create class e.g ReturnClass and create constructor for it, then pass Context as parameter e.g ReturnClass(Context context) then write
Intent intent = new Intent(context , MainActivity.class);
context.startActivity(intent);
from each activity you can call this class and pass activity to it , and ReturnClass return it to main activity
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm trying to make an app with different intents. I've got two of the ready. I want that a button in the 1st intent makes the 2nd intent to appear.
The issue is that the button is always set to null and throws a NullPointerException when the app is run
My .java classes are:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(vocabulario);
//....
Button buttonVocabulary = (Button) findViewById(R.id.buttonVocabulary);
Button holaPlayerBtn = (Button) findViewById(R.id.holaPlayerBtn);
//...
buttonVocabulary.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view)
{
//Se abre un nuevo Intent con el primer botón
try
{
{
Intent myIntent = new Intent(view.getContext(), Vocabulario.class);
startActivityForResult(myIntent, 0);}
} catch (Exception e) {
e.printStackTrace();
}
}});
My main.xml file is:
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/adViewContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
<Button
android:id="#+id/buttonVocabulary"
android:text="#string/vocabulario"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true" />
</LinearLayout>
</LinearLayout>
In my AndroidManifest.xml there is an element that says:
<activity android:name=".Vocabulario"
android:label="#string/vocabulario"/>
There's something I'm doing wrong. Could you help me with this issue?
Thanks in advance.
change
setContentView(vocabulario);
To
setContentView(R.layout.main);
In your onCreate method you should set the view to the layout:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //set layout id
//...
findViewById(R.id.buttonVocabulary).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
try {
Intent i = new Intent(view.getContext(), Vocabulario.class);
startActivityForResult(i, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
I have looked at posts on Stack Overflow and at tutorials on other websites, and I cannot understand how to use TabHost. Can someone please explain it to me and maybe send me a link to a tutorial?
In ManiActivity extends TabActivity
public class MainActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this ,FirstActivity.class )));
mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this , SecondActivity.class )));
mTabHost.setCurrentTab(0);
}
}
In this activity not use layout "activity_main.xml" .
Tabhost mTabHost = getTabHost(); is create main tab.
mTabHost.newTabSpec("first") is create tabspec id "first".
setIndicator("First") is create text "First" in title tab.
setContent(new Intent(this ,FirstActivity.class )) is use content from FirstActivity.class ( FirstActivity.java )
mTabHost.addTab(....) is add spectab to main tab
mTabHost.setCurrentTab(0) is defult tab when start page.
FirstActivity.java
public class FirstActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.first_layout );
}
}
SecondActivity.java
public class SecondActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.second_layout );
}
}
"R.layout.first_layout" is content from first_layout.xml
"R.layout.second_layout" is content from second_layout.xml
In AndroidManifest.xml add activity name ".FirstActivity" and ".SecondActivity" in example xml.
Finish!!!!!
First of all while TabHost is not deprecated, TabActivity on other hand is deprecated due to Fragment API.
There are two ways to use TabHost; using Fragment via FragmentTabHost and using TabHost.TabContentFactory.
1. Using Fragment via FragmentTabHost
This sample code show you how to use TabHost in Activity.
FragmentTabHostActivity.java
public class FragmentTabHostActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tab_host_activity);
FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
fragmentTabHost.addTab(getTabSpec1(fragmentTabHost), Tab1Fragment.class, null);
fragmentTabHost.addTab(getTabSpec2(fragmentTabHost), Tab2Fragment.class, null);
}
private TabHost.TabSpec getTabSpec1(FragmentTabHost tabHost) {
return tabHost.newTabSpec("First Tab")
.setIndicator("Tab1");
}
private TabHost.TabSpec getTabSpec2(FragmentTabHost tabHost) {
return tabHost.newTabSpec("Second Tab")
.setIndicator("Tab 2");
}
}
fragment_tab_host_activity.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Actually by using Fragment, you can use Tab inside a Fragment (Android docs).
2. Using TabHost.ContentFactory
TabHostActivity.java
public class TabHostActivity extends AppCompatActivity implements TabHost.TabContentFactory {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(getTabSpec1(tabHost));
tabHost.addTab(getTabSpec2(tabHost));
}
private TabHost.TabSpec getTabSpec1(TabHost tabHost) {
return tabHost.newTabSpec("First Tab")
.setIndicator("Tab1")
.setContent(this);
}
private TabHost.TabSpec getTabSpec2(TabHost tabHost) {
return tabHost.newTabSpec("Second Tab")
.setIndicator("Tab 2")
.setContent(this);
}
#Override
public View createTabContent(String tag) {
return LayoutInflater.from(this).inflate(R.layout.activity_tab_1, null);
}
}
activity_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="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</TabHost>
However, I personally recommend using newest Material Design style TabLayout class.
As an example, see the following tutorial on how my tabs are setup initially.
Instead of a normal activity running on one of the tabs, I want another TabActivity. So what I'm trying to do is run a TabActivity within a TabActivity. I believe the issue is that the ID's conflict. I have tried to solve this by changing the ID's on the secondary activity's xml file and calling those manually in the activity, but have had no luck.
I have been searching for hours for a solution for this, but have come up with nothing.
Multiple tab activities with an application is possible.
For instance:
App contains a Launcher TabActivity (HomeTabActivity) with two tabs : Tab 1 and Tab 2
Tab 1 can be a TabActivity with two or more tabs.
Tab 2 can be a TabActivity with two or more tabs.
FirstTab, SecondTab, ThirdTab and FourthTab are simple activities actings as child for child of HomeTabActivity.
xml files containing TabHost as parent element
1. hometab.xml
2. tab1.xml
3. tab2.xml
To differentiate between HomeTabActivity and Its child TabActivities i.e Tab1 and Tab2
I have put TabWidget at top for HomeTabActivity and at bottom for Tab1 and Tab2.
HomeTabActivity (Very First Main Tab Activity):
public class HomeTabActivity extends TabActivity
{
private TabHost mTabHost = null;
private Intent mIntent = null;
private TabHost.TabSpec mTabSpec = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hometab);
initializeTabs();
}
private void initializeTabs() {
mTabHost = getTabHost();
mIntent = new Intent().setClass(this, Tab1.class);
mTabSpec = mTabHost
.newTabSpec("Tab1")
.setIndicator("Tab1",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(mIntent);
mTabHost.addTab(mTabSpec);
mIntent = new Intent().setClass(this, Tab2.class);
mTabSpec = mTabHost
.newTabSpec("Tab2")
.setIndicator("Tab2",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(mIntent);
mTabHost.addTab(mTabSpec);
mTabHost.setCurrentTab(0);
}
}
Tab1 (TabActivity Embedded inside the HomeTabActivity) :
public class Tab1 extends TabActivity
{
private TabHost mTabHost = null;
private Intent mIntent = null;
private TabHost.TabSpec mTabSpec = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
initializeTabs();
}
private void initializeTabs() {
mTabHost = getTabHost();
mIntent = new Intent().setClass(this, FirstTab.class);
mTabSpec = mTabHost
.newTabSpec("Tab1 Child 1")
.setIndicator("Tab1 Child 1",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(mIntent);
mTabHost.addTab(mTabSpec);
mIntent = new Intent().setClass(this, SecondTab.class);
mTabSpec = mTabHost
.newTabSpec("Tab1 Child 2")
.setIndicator("Tab1 Child 2",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(mIntent);
mTabHost.addTab(mTabSpec);
mTabHost.setCurrentTab(0);
}
}
Tab2 (Another TabActivity Embedded inside the HomeTabActivity) :
public class Tab2 extends TabActivity
{
private TabHost mTabHost = null;
private Intent mIntent = null;
private TabHost.TabSpec mTabSpec = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
initializeTabs();
}
private void initializeTabs() {
mTabHost = getTabHost();
mIntent = new Intent().setClass(this, ThirdTab.class);
mTabSpec = mTabHost
.newTabSpec("Tab2 Child 1")
.setIndicator("Tab2 Child 1",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(mIntent);
mTabHost.addTab(mTabSpec);
mIntent = new Intent().setClass(this, FourthTab.class);
mTabSpec = mTabHost
.newTabSpec("Tab2 Child 2")
.setIndicator("Tab2 Child 2",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(mIntent);
mTabHost.addTab(mTabSpec);
mTabHost.setCurrentTab(1);
}
}
hometab.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="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
tab1.xml and tab2.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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/androidblue"
android:layout_weight="1">
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
</TabWidget>
</LinearLayout>
</TabHost>
I have set up four tabs that each hold a listview, I have added code in the listview java file to make the lists transparent, however, I have a semi transparent grey box covering 75% of the screen and I cannot figure out why, I have a background on my other listviews and they are completely transparent, but the lists within the tabhost have the grey box.
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</TabHost>
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class Tabs extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Draw
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, prem.class);
// Initialise a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Prem").setIndicator("Prem",
res.getDrawable(R.drawable.icontabs))
.setContent(intent);
tabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, champ.class);
// Initialise a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Champ").setIndicator("Champ",
res.getDrawable(R.drawable.champ))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, l1.class);
spec = tabHost.newTabSpec("League 1").setIndicator("L",
res.getDrawable(R.drawable.l1))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ll2.class);
spec = tabHost.newTabSpec("l2").setIndicator("Le",
res.getDrawable(R.drawable.l2))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
public class l1 extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] {"Le"};
ListView lv = getListView();
lv.setBackgroundResource(R.drawable.le2);
lv.setCacheColorHint(00000000);
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, names));
}
// Get the item that was clicked
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
if (position == 0) {
In your layout XML file, your tab content (the FrameLayout) should be after the tab widget, not before. It's possible the box you are seeing is the tab widget's background.
Like this:
<?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">
<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>