Hey im developing my first proper app for android and have run into 2 small hickups and was hoping for some help basicaly the main problem is that i cant seem to change to a new view from within tabhost. I have 4 tabs 2 just show a simple xml file the 3rd shows a series of pictures which i want to make clickable so that they expand out into player bio's. atm im attempting to do this in the following way but it just causes my application to crash
package com.BPRUFC;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
public class BPRUFCAppActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Fixtures").setContent(R.id.fixtures));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Results").setContent(R.id.results));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Players").setContent(R.id.bio));
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Tour").setContent(R.id.tour));
mTabHost.setCurrentTab(0);
int days = 184;
String tour2 = getString(R.string.tour, days);
}
public void clicked(View view)
{
Intent myIntent = new Intent(BPRUFCAppActivity.this, PlayerBio.class);
BPRUFCAppActivity.this.startActivity(myIntent);
}
}
main Xml stopped after first pic as its quite long
<?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">
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/fixtures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/fixtures" />
</ScrollView>
<TextView
android:id="#+id/results"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Results go here" />
<ScrollView
android:id="#+id/ScrollView02"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/bio"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="90dip"
android:layout_height="90dip"
android:id="#+id/imageView1"
android:src="#drawable/forrest"
android:onClick="clicked"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp">
</ImageView>
Thanks in advance and im sure its a noob mistake
I fixed this myself. I was going about it the wrong way nesting too many layouts. I changed it all so that each tab has 1 frame layout and this is then modified accordingly it now behaves properly.
Related
I know how to do this via a surfaceView and I'm wondering if I should go down that rout.
I'm simply trying to create a splashscreen that has a fullscreen image with an opaque image laid over the top (after a short delay). I can't work out how this is done in XML code.
This is what I have......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/lightDot"
android:src="#drawable/splashlight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#null"
/>
<ImageView
android:id="#+id/bGround"
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
So my 'lightDot' object is semi-transparent and I want to overlay this on top of my bGround resource after a short delay.
This is my code:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SplashAct extends Activity implements OnClickListener{
Button mainButton;
Button lightDot;
ImageView background;
ImageView light;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
background = (ImageView)findViewById(R.id.bGround);
light = (ImageView)findViewById(R.id.lightDot);
background.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
//finish();
Intent toMainGame = new Intent(this, ActOptions.class);
startActivity(toMainGame);
}
}
Thank you.
What you want is a FrameLayout.
Child views are drawn in a stack, with the most recently added child on top.
You can get more fancy with where the overlays go using layout_gravity, but it sounds like this is all you need.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/image" >
</ImageView>
<ImageView
android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/overlay"/>
</FrameLayout>
Use Relative layout not linear layout.
This allows you to place views ontop of one another, as opposed to in a linear list.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/lightDot"
android:src="#drawable/splashlight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#null"
/>
<ImageView
android:id="#+id/bGround"
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
To make the Dot appear after time, use android:visiblity="INVISIBLE" in the xml. (so it starts invisible)
then use light.setVisiblity(View.VISIBLE); in your code.
You could use a LayerDrawable
Define a simple LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splash_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
In your Activity class:
LinearLayout layout = (LinearLayout) findViewById(R.id.splash_layout);
Drawable drawableLayers[] = new Drawable[] {getResources().getDrawable(R.drawable.splash), getResources().getDrawable(R.drawable.splashlight)};
// ^ The order of drawables is important: The above line overlays splashlight on top of splash.
LayerDrawable layerDrawable = new LayerDrawable(drawableLayers);
layout.setBackgroundDrawable(layerDrawable);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
***android:background="#drawable/splash"*** >
// Try this for invisible
iv.getBackground().setAlpha(0);
//Try this for visible
iv.getBackground().setAlpha(255);
//What great about this is that you could create a loop
//to slowly increment the alpha, creating a fade effect instead of
//invisible to visible.
I have got the code:
package com.finalyearproject.cookmefood;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
public class SearchActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView recipes = (ListView)findViewById(R.id.recipes);
}
}
Which is giving me the error "id cannot be resolved".
I have tried cleaning the project but that results in me creating errors in file which currently are fine.
My XML is:
<?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="5dp">
<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"
android:padding="5dp" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<ListView
android:id="#+id/recipes"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:entries="#array.recipes"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Any help would be appreciated, where can I go from here?
#array.recipes in the xml should be #array/recipes.
A simple typo like this will prevent your R class from building meaning you can't find any id's or other resources.
You should also make sure that you get the correct R as you are using both android.R and your R
Try to clean and rebuild your code. I don't know why, but it worked for me.
Use this com.finalyearproject.cookmefood.R.id.recipes.
I have a little problem using Tabs with Views.
First I just copied the sample code where Tabs are used with activitys:
My LayoutFile looks 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"
android:padding="5dp">
<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"
android:padding="5dp" />
</LinearLayout>
</TabHost>
And this is my Java-code:
public class MyActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
setContentView(R.layout.main);
TabHost tH = getTabHost();
Indent intent = new Intent().setClass(this, AnotherActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
//TextView Test = new TextView(this);
//Test.setText("test");
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(intent));
tH.setCurrentTab(0);
}
}
And this works as expected.
But when I uncomment the TextView-lines and call setContent(Test.getId()) instead of setContent(intent), the app crashes.
I also tried to create a textview in the layoutfile, and call setContent(R.id.test),
that also makes it crash.
So this is one problem.
The seccond point is. I do not want to use activitys, because i want to be able to call methods on those classes, which shall represent the Tab-content.
So my original idea is, to derive some classes from view. 1 for each tab, and pass their ids. But therefor the codesample above needs to work first.
greetings Uzaku
I know you said you tried a TextView in the layout file but this should work...
Change the FrameLayout section as follows...
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TEST" />
</FrameLayout>
Then in your code do the following...
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(R.id.test));
Hello Guys and probably some Girls, is it possible to stack Layout Objects. So that one Object is over the other one?
My Problem is this, i've got a tabhost and under the tabhost i've got a tablerow with a textview in it.
I made the tabhost tabs invisible, but it hiddes my tablrow with the textview in it,how can i bring it infront?
Here is the Layout.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"
android:weightSum="1">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom = "#android:id/tabcontent"
android:visibility="invisible"/>
<TableRow
android:id="#+id/ADFilterOverview"
android:layout_height="wrap_content"
android:onClick="onClickADConfig"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#android:id/tabs"
android:layout_width="match_parent">
<TextView
android:text="#string/newadfilter"
style="#style/NormalFont"
android:layout_gravity="bottom|left">
</TextView>
</TableRow>
</RelativeLayout>
</TabHost>
Thx in Advance
Best Regards
safari
Some more information, for better understanding:
Afterwards my code of my Activity looks something like this:
package de.retowaelchli.filterit;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.content.Intent;
public class StatsActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("whatever").setContent(new Intent(this, SFilterStatsActivity.class)));
tabHost.setCurrentTab(1);
}
/** Verweise auf die anderen Seiten **/
public void onClickADConfig(View view){
final Intent i = new Intent(this, ADFilterConfigActivity.class);
startActivity(i); }
}
The linked class from my Tabhost looks for example like this:
package de.retowaelchli.filterit;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ADFilterStatsActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListContent));
}
private static String[] mListContent={"Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3"};
}
So you see, i got a TabHost with one tab in it, which is filled from my ListActivity, the tabhost should be hidden (invisible), over this hidden tab should be the tablerow visible. So i can click it to get to my other class.
I hope this helps you guys...
FOUND A SOLUTION THX Egon
And here is the solution
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom = "#android:id/tabcontent"
android:visibility="invisible"/>
<TableRow android:layout_height="wrap_content" android:id="#+id/ADFilterOverview" android:layout_width="match_parent" android:onClick="onClickADConfig" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true">
<TextView android:layout_gravity="bottom|left" android:text="#string/newadfilter" style="#style/NormalFont"></TextView>
</TableRow>
</RelativeLayout>
</TabHost>
</FrameLayout>
If you want to place one View at the top of the other in Z-axis, you can use FrameLayout. It lets you position a View on the background and other views on top of it. It's really powerful and easy to use, so you must give it a try. Hope this helps.
If you make Parentview invisible then its childview also automatically make invisible. In this scenario you have to define other layout for your new tab.
If this is not correct, please make edit in, or suggest me. Thanx
EDIT: on your xml code I think tabhost is a parentview and tablerow , textview are childview of it.
I believe the problem is that you set the tabwidget to be alignParentBottom and then you set TableRow to have aligntop the tabwidget. Thus it is not visible. Making the tabwidget invisible does hide it, but the place holder is still there.
I am switching activities on tab clicks and successful at this. But, in one of my Activity class I am doing the following:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
main.xml has the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#BDBDBD">
</LinearLayout>
I want to change the background of this layout only and I want tabs to their as it is. But with the current android:layout_height="fill_parent" in main.xml my background is overwriting the tabs which means I am unable to see tabs. and If I make android:layout_height="wrap_content" I cannot see any change taking and tabs are still their.
Please help.
Here is some information..
screen2.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView android:text="AcctId"
android:id="#+id/TextView01"
android:layout_width="50px"
android:layout_height="wrap_content"></TextView>
<EditText
android:id="#+id/acctid"
android:layout_width="200px"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout03"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView android:text="Zip"
android:id="#+id/TextView02"
android:layout_width="50px"
android:layout_height="wrap_content">
</TextView>
<EditText
android:id="#+id/stid"
android:layout_width="200px"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<Button android:text="Login"
android:layout_width="80px"
android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>
<ImageView
android:layout_width="200px"
android:layout_height="200px"
android:src="#drawable/icon"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
</ScrollView>
screen1.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">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#c6c3c6"
android:layout_gravity="bottom"
android:paddingTop="1dip"
android:paddingLeft="1dip"
android:paddingRight="1dip">
<TabWidget
android:layout_gravity="bottom"
android:id="#android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</FrameLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</TabHost>
MainActivity.java
public class onstarAndroidMain extends TabActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
addTabs();
}
private void addTabs(){
Resources resources = getResources();
TabHost tabs = (TabHost) findViewById(android.R.id.tabhost);
tabs.setup();
//tabspec defines the attributes of the tab
TabSpec tab;
tab = tabs.newTabSpec("Screen1");
tab.setContent(new Intent(this, Screen1.class));
tab.setIndicator("Screen1");
tabs.addTab(tab);
tabs.getTabWidget().getChildAt(0).getLayoutParams().height = 30;
tab = tabs.newTabSpec("Screen2");
tab.setContent(new Intent(this, Screen2.class));
tab.setIndicator("Screen2");
tabs.addTab(tab);
tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 30;
tab = tabs.newTabSpec("Screen3");
tab.setContent(new Intent(this, Screen3.class));
tab.setIndicator("Screen3");
tabs.addTab(tab);
tabs.getTabWidget().getChildAt(2).getLayoutParams().height = 30;
//sets the current tab
tabs.setCurrentTab(0);
//add the tabhost to the main content view
}
}
Screen2.java
public class OnstarScreen2 extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.screen2);
}
Now whenever I click the screen2 tab and try to get the screen2.xml, my tabs are going below the screen2 layout. I dont know what to do. Basically what I want to achieve is that the tab should be always their at the bottom irrespective of what view is shown above them. they should not be overlapped with anything else.
screen2.xml has a scrollview and iside that I have a linearlayout which has a login form...but this form whenever is called just comes above the tabs...please help
You are doing something wrong with your tabs. Are you just launching a new activity when the tab is clicked? Then, if that new activitys layout covers the entire screen, ofcourse the tabs will be covered by this.
Follow this guide:
http://developer.android.com/intl/de/resources/tutorials/views/hello-tabwidget.html