ListView in TabHost [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to put a listView (existing list named sitesList) in a TabHost but when I run my app, it closes (without the code for tabhost the app works), here is my code:
public class MainActivity extends TabActivity {
private SitesAdapter mAdapter;
private ListView sitesList;
private static final String Noutati = "Noutati";
private static final String Favorite = "Favorite";
private static final String PROFILE_SPEC = "Profile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("StackSites", "OnCreate()");
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
TabSpec inboxSpec = tabHost.newTabSpec(Noutati);
Intent inboxIntent = new Intent(this, Noutati.class);
inboxSpec.setContent(inboxIntent);
TabSpec outboxSpec = tabHost.newTabSpec(Favorite);
//Intent outboxIntent = new Intent(this, OutboxActivity.class);
//outboxSpec.setContent(outboxIntent);
tabHost.addTab(inboxSpec);
tabHost.addTab(outboxSpec);
sitesList = (ListView)findViewById(R.id.sitesList);
...
public class Noutati extends ListActivity {
private ListView sitesList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noutati);
sitesList = (ListView)findViewById(R.id.sitesList);
this.setListAdapter((android.widget.ListAdapter) sitesList);
}
}
Activity_main
<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>
noutati.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/sitesList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Thank you very much

EDIT:
I see that you have named your variable Noutati and your class Noutati. Thy changing the variable to something like noutatiName.
As I have never encountered this before, I'm not sure this will work, but the error most likely appears because you have to set the indicator for each tab spec.
So after TabSpec inboxSpec = tabHost.newTabSpec(Noutati); you should also type inboxSpec .setIndicator("Notuati");
Hope it helps!
Also you should do that after TabSpec outboxSpec = tabHost.newTabSpec(Favorite);
Link

Related

Problems with setting up tabs using Tabhost in Android?

So, i was following the NewBoston series on Android tutorials. So, there is this tutorial on setting up tabs using Tabhost and Tabspecs. Now, i tried exactly what the tutorial does but crashes and it points to a particular line in my code which is setContentView() method in the OnCreate(). I tried even removing it and ran the code but the program just crashes. What could be my mistake? I also tried the code in the following article to see if that works but it still crashes.
http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html
This is my code-
Splash.java is the class that starts up the Launcher activity
public class Splash extends TabActivity {
TabHost th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
th = (TabHost)findViewById(R.id.tabHost2);
TabHost.TabSpec tab1 = th.newTabSpec("First Tab");
TabHost.TabSpec tab2 = th.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = th.newTabSpec("Third tab");
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,TabActivity1.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this, TabActivity2.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this, TabActivity3.class));
th.addTab(tab1);
th.addTab(tab2);
th.addTab(tab3);
}
}
TabActivity1.java-
public class TabActivity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab1 Activity");
setContentView(tv);
}
}
TabActivity2.java:-
public class TabActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab2 Activity");
setContentView(tv);
}
}
TabActivity3.java
public class TabActivity3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab3 Activity");
setContentView(tv);
}
}
splash.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">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost2"
android:layout_gravity="center_horizontal">
<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">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Change this line
th = (TabHost)findViewById(R.id.tabHost2);
to
th = (TabHost)findViewById(android.R.id.tabHost2);
This will do.
Because, in the layout file, you might have set Tabhost id like this android:id="#android:id/tabhost2">

Creating two tabhosts in one app

I am making an android application in which I use two different tabhosts in two different activities, However I am having trouble differentiating between then and my app stops when I enter the activity with the second tabhost.
The code in my first activity is as follows:
private TabHost tHost_;
private TabSpec tSpec_;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mypls_shared);
setTitle("Workout Scheduler");
tHost_ = (TabHost) findViewById(R.id.tabhost);
tHost_.setup();
tSpec_ = tHost_.newTabSpec("tag1");
tSpec_.setContent(R.id.myTab);
tSpec_.setIndicator("My Workouts");
tHost_.addTab(tSpec_);
tSpec_ = tHost_.newTabSpec("tag2");
tSpec_.setContent(R.id.shareTab);
tSpec_.setIndicator("Shared Workouts");
tHost_.addTab(tSpec_);
}
in my xml file the android id of the tabhost is specified as follows:
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
the code in my second file is as follows:
private TabHost tHost_;
private TabSpec tSpec_;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three_by3);
setTitle("Workout Scheduler");
tHost_ = (TabHost) findViewById(R.id.tabhost);
tHost_.setup();
}
the xml file looks like this:
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1" >
However when I enter the second activity, the app stops. I know that a tabhost must be named as such:
android:id="#+id/tabhost"
but what do i do if I have 2 tabhosts, which is referred to by R.id.tabhost?

How do I use tabHost for Android

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.

How to create an ListView without ListActivity

Someone could tell me how can I create a ListView without using a ListActivity?
I need to put in my layout several other views, and I wish the layout was not fully occupied by the ListView. It must be part of the layout, not the entire layout. I want to create a normal Activity.
Code snippet:
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.savings);
//...
ListView lvPoupanca = (ListView)this.findViewById(R.id.lvPoupanca);
// the adapter
TestRepository repo = new TestRepository(this);
Cursor c = repo.getCursor();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[]{"Nome", "ValorAtual"}, new int[] {R.id.tvNome, R.id.tvValorTotal});
lvPoupancas.setAdapter(adapter);
// ...
}
Edit:
The saving.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvPoupancas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
Hope somebody can help me.
Thanks and sorry about my bad english.
Like with any other View in any Activity...
ListView l = new ListView(this);
// OR
ListView l = (ListView) findViewById(R.id...);
...
l.setAdapter(...);
// If using first way: setContentView(l);
For example
I have replied to a similar question before. But that was in a different context. So, I'm putting some reference code here. It will help you to fix the issue.
public class ListandtextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String [] str = {"ONE","TWO","THREE"};
final ListView lv = (ListView) findViewById(R.id.listView1);
final TextView tv = (TextView)findViewById(R.id.tv1);
ArrayAdapter<Object> adapt = new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1, str);
lv.setAdapter(adapt);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tv.setText("You Clicked Something");
}
});
}
}
And the XML is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" android:textSize="20dip"/>
</LinearLayout>
By the way, before asking questions, collect as much information as possible for others to help you. If it does not work, it will be better to put the error log in the question rather than just saying it does not work.
Don't Extend ListActivity then. Extend an Activity and in onCreate() put the following code as per your requirement.
setContentView(R.layout.ur_xml);
Listview list = (ListView)findViewById(R.id.lvPoupancas);
list.setAdapter(your Adapter);

ListView transparency in Android

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>

Categories

Resources