My edittext's methods work properly as I have checked it on debbug, but in the graphical layout there are no changes at all.
When changing the text programatically, in the graphical layout doesn't appear, but if I try myedittext.getText() on the debbug expressions/watches it returns the correct string, so the settext() method is working OK but the changes are not shown.
Here is the code:
public class easmpartne extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easmpartne);
FragmentTabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this,
getSupportFragmentManager(), android.R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("General"),
Tab1.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Categ"),
Tab2.class, null);
LayoutInflater inflater = this.getLayoutInflater();
viewGeneral= inflater.inflate(R.layout.tab1, null);
viewCateg= inflater.inflate(R.layout.tab2, null);
txtRef=(EditText)viewGeneral.findViewById(R.id.txtRef);
txtRef.setText("example");
txtRef.setEnabled(false);
}
}
Neither settext nor setenabled seems no have any effect on the graphical layout when executing.
easmpartne.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" >
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</android.support.v4.app.FragmentTabHost>
tab1.xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:layout_marginLeft="5dp"
android:fillViewport="true"
android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linlayouteasmpartne"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tablelayout1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/ref"
android:id="#+id/textView" />
<EditText
android:layout_width="170dp"
android:layout_height="wrap_content"
android:id="#+id/txtRef" />
</TableRow>
....
(I don't know if this could be a problem with de tabhost as it is the first time I use it)
Is that the correct way to get the edittext in this case?
What are the possible problems/solutions?
Thanks.
the point is, you see, that the layout is not kind of a shared variable so when you or the runtime reads it, it instances all views referenced inside and you have to stick to it. Then you follow the lifecycle of the components so to set the values you want you should do it in onViewStateRestored(Bundle) when the view is actually done or something like that, or onStart or so
Related
I am trying to convert an existing project with findViewByIds over to ViewBinding. All of the view IDs outside of the ScrollView are available in Android Studio for use with "binding.viewId..." The problem is none of the views with IDs that are within the ScrollView are available for use with "binding.viewID..." So I am not able to access ImageViews that are within two <include layouts that are within a ViewFlipper in the layout. The simple ViewFlipper toggles between an up arrow ImageView and a down arrow ImageView. The ViewFlipper has an <include layout for the up ImageView and a second <include layout for the down ImageView. The ViewFlipper is inside a LinearLayout and the LinearLayout is inside the ScrollView.
//Activity
private ActivityEditBinding binding;
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityEditBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
// activity_edit.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".AddorUpdateCardActivity">
...
<RelativeLayout
android:id="#+id/secondLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar" >
<ScrollView
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/secondLinearLayout"
... >
<LinearLayout
android:id="#+id/lowerVFRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:orientation="horizontal" >
<ViewFlipper
android:id="#+id/expandVF"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginBottom="5dp" >
<include layout="#layout/expand_more_act_edit"
android:id="#+id/expandMore" />
<include layout="#layout/expand_less_act_edit"
android:id="#+id/expandLess" />
</ViewFlipper>
</LinearLayout>
</ScrollView>
</RelativeLayout>
// expand_more_act_edit.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/downArrow"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="sd"
android:clickable="true"
android:focusable="true"
android:src="#drawable/ic_expand_more" />
<!-- the down arrow -->
I confirmed that the autogenerated ViewBinding classes have been created. Below is where I am trying to attach a ClickListener to the downArrow but I can't access the downArrow reference. What am I missing here?
***Android Studio says "Cannot resolve symbol 'expandMore'***
binding.expandMore.downArrow.setOnClickListener(v -> {
// change the down arrow to the up arrow and show the 1-20 EditText lines.
expandVF.setDisplayedChild(expandVF.indexOfChild(findViewById(R.id.expandLess)));
moreViewStub.setVisibility(View.VISIBLE);
});
Does ViewBinding not work within a ScrollView? What am I missing here?
View binding will generate camelCase id from id that you have provided in your layout.If your view id is already in camelCase(expandVF) in your layout,sometimes android studio won't recognize that because layout view id and binding class view id are same.that may produce 'Cannot resolve symbol error'.
so instead of expandVF,expandMore,expandLess use expand_vf,expand_more,expand_less.Rebuild and run,may work.
You need to use binding.expandVF instead of directly using expandVF
and to use id of layout which have been included, you need to use binding.expandLess.ID_YOU_WANT_TO_USE
In your case, I don't see the problem in the code so I'm guessing maybe the Android build tool version is quite old - ViewBinding doesn't work very well
Try to update the build tools to the latest version 4.2.0 in build.gradle of project
dependencies {
// At least it should be from 4.0.0
classpath 'com.android.tools.build:gradle:4.2.0'
}
And distributionUrl in gradle-wrapper.properties
# tools.build 4.2.0 requruired gradle distribution at least 6.7.1+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
Change the viewBinding declaration if it is different from the code below:
buildFeatures{
viewBinding = true
}
Then Clean-Rebuild your project.
Check out Compatibility Android Gradle plugin - Release Notes
convert your activity_edit.xml, expand_less_act_edit.xml, and expand_more_act_edit.xml layout into data binding by wrapping into layout tag.
your activity_edit.xml will look like
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".AddorUpdateCardActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/lowerVFRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:orientation="horizontal" >
<ViewFlipper
android:id="#+id/expandVF"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginBottom="5dp" >
<include layout="#layout/expand_more_act_edit"
android:id="#+id/expandMore" />
<include layout="#layout/expand_less_act_edit"
android:id="#+id/expandLess" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</layout>
your expand_more_act_edit.xml will look like
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/downArrow"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="sd"
android:clickable="true"
android:focusable="true"
android:src="#drawable/ic_expand_more" />
</layout>
your main class to access the ImageView will be.
private ActivityEditBinding binding;
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding= DataBindingUtil.setContentView(this, R.layout.activity_edit);
//binding.expandMore.downArrow this is how you will gonna access the view. now do what ever you want with this.
}
It looks like you have missed ViewFlipper closing tag and there are other layout issues in xml. Check if that is the issue.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".AddorUpdateCardActivity">
<RelativeLayout
android:id="#+id/secondLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar" >
<ScrollView
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/secondLinearLayout">
<LinearLayout
android:id="#+id/lowerVFRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:orientation="horizontal" >
<ViewFlipper
android:id="#+id/expandVF"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginBottom="5dp" >
<include layout="#layout/expand_more_act_edit"
android:id="#+id/expandMore" />
<include layout="#layout/expand_less_act_edit"
android:id="#+id/expandLess" />
</ViewFlipper>
</LinearLayout>
</ScrollView>
I've corrected some but not sure since I don't know the whole layout. Since there are some ellipses in the layout you have provided. Other than that ViewBinding has no issues with ScrollView AFAIK.
for databinding
in activity.java
private ActivityEditBinding binding;
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding= DataBindingUtil.setContentView(this, R.layout.activity_edit);
}
in activity_edit.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".AddorUpdateCardActivity">//...
<ScrollView
<LinearLayout
android:id="#+id/lowerVFRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:orientation="horizontal" >
<ViewFlipper
android:id="#+id/expandVF"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginBottom="5dp" >
<include layout="#layout/expand_more_act_edit"
android:id="#+id/expandMore" />
<include layout="#layout/expand_less_act_edit"
android:id="#+id/expandLess" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</layout>
now
binding.expandMore.downArrow.setOnClickListener(v -> {
});
for ViewBinding here
I'm getting a resource not found error when I try to do this:
TabWidget tabz = FindViewById<TabWidget>(Resource.Id.tabs);
compiler doesn't see the TabWidget even when it's clearly labeled by id in my Main.axml file
Error CS0117 'Resource.Id' does not contain a definition for 'tabs'
Here's my full code:
<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"
android:visibility="visible" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
EDIT, sorry, this is the full code... I don't know why I didn't copy this last time. The above example is missing TabHost
<?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:minWidth="25px"
android:minHeight="25px"
android:id="#+id/tabHost1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linearLayout1">
<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="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</TabHost>
</LinearLayout>
How can I force the compiler to recognize my tabs id? I've tried rebuilding the resource.designer.cs .. putting the declaration of TabWidget inside all of the activities... nothing works. My C# can't see the TabWidget
full project can be found here:
https://github.com/hexag0d/BitChute_Mobile_Android_a2
I also tried this and it didn't work
Xamarin Android Resource file not found
thanks, in advance
The issue is not with the visual studio compiler but the way that you have declared the id for your View,
If you check this blog by James White you will see how Id and the Layout Attributes actually work in Android,
The Right way of declaring the Id would be something like this android:id="#+id/tabs" whereas what you are doing is this android:id="#android:id/tabs"
Hence, When you do add them and try to find it using Resource.Id.tabs, There is nothing available in Id by the name tabs as it was never added by the Xamarin ResourceDesginer.cs file.
So the end result should actually be something like this :
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<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/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc916"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Also, note that I have replaced all the fill_parent's with match_parent's the reason behind it being that fill parent is now obsolete or you can say replaced (After API-8) and hence must not be used, which can be found here.
Update:
I have updated the way tab host works, Also updated the XML above and I am adding a reference link where you can find the right way of using both the TabHost and TabWidget, Also a good SO example for the same.
About the questions in your comment:
1,
that is very good advice, but one note is that when
SetContentView(localLayout); is used inside an activity that already
has a content view, it crashes the app.
The app crashed because localLayout in SetContentView(localLayout) is a LinearLayout but a TabWidget needs a TabHost. So you can directly use:
SetContentView(Resource.Layout.Main);
where Main.xaml is like:
<?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:theme="#style/MyTheme">
<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"
android:layout_weight="0.0"
android:visibility="visible" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
2,
LinearLayout localLayout =
FindViewById(Resource.Layout.Main); View
whichYouWantToUse_findViewById =
LayoutInflater.Inflate(Resource.Layout.Main, null); TextView tv =
FindViewById(Resource.Id.textView169); TabHost _tabHost =
(TabHost)whichYouWantToUse_findViewById.FindViewById(Resource.Id.tabhost);
' still can't see the tabhost ... I'm losing my mind here
The reason why you cannot see the tabhost is, firt, TabHost is FrameLayout, not LinearLayout, second, Resource.Id.tabhost should be Android.Resource.Id.TabHost. So I edited your code to :
FrameLayout localLayout = FindViewById<FrameLayout>(Resource.Layout.Main);
whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.Main, null);
//TextView tv = FindViewById<TextView>(Resource.Id.textView1);
TabHost _tabHost = (TabHost)whichYouWantToUse_findViewById.FindViewById(Android.Resource.Id.TabHost);
3,
So the interesting thing is if I change tabhost to tahost ala
android:id="#+id/tahost" the resource appears in the designer but when
I change to android:id="#+id/tabhost" the resource disappears.. very
strange..
The id of TabHost need always be #android:id/tabhost", it cannot be modified to tahost or any others.
So the code in the onCreat Method is like this:
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
View whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.layout1, null);
TextView textInAnotherLayout = (TextView)whichYouWantToUse_findViewById.FindViewById(Resource.Id.textView1);
textInAnotherLayout.Text = "Yeah";
TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
TabWidget tabWidget = tabHost.TabWidget;
var tabContent = tabHost.TabContentView;
FrameLayout localLayout = FindViewById<FrameLayout>(Resource.Layout.Main);
whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.Main, null);
TabHost _tabHost = (TabHost)whichYouWantToUse_findViewById.FindViewById(Android.Resource.Id.TabHost);
The following is the old answer.
Answer modified: The way to get TabHost and TabWidget in Activity:
TabHost tabHost = this.TabHost;
TabWidget tabWidget = tabHost.TabWidget;
The way to use one resource from one XAML file in the activity for
another:
LinearLayout localLayout = FindViewById<LinearLayout>(Resource.Layout.Main);
SetContentView(localLayout);
View whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.layout1, null);
Button button1 = (Button)whichYouWantToUse_findViewById.FindViewById(Resource.Id.button1);
I download your project and fixed this problem by modifying the property of >android:id
I have been trying to change the theme for TabHost. So far I have got till here:
I have achieved this by using the following xml:
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/signupLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0"
android:gravity="center"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0" >
<ScrollView
android:id="#+id/scrollView02"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
<ScrollView
android:id="#+id/scrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</FrameLayout>
</LinearLayout>
My MainActivity.java:
ContextThemeWrapper wrapper = new ContextThemeWrapper(
ActivityMain.this,
android.R.style.Theme_Holo_Light);
final LayoutInflater inflater = (LayoutInflater) wrapper
.getSystemService(LAYOUT_INFLATER_SERVICE);
dialog = new Dialog(wrapper);
dialog
.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog
.setContentView(R.layout.dialog_layout);
TabHost tabs = (TabHost) dialog
.findViewById(android.R.id.tabhost);
tabs.setup();
tabs.setCurrentTab(0);
TabSpec tspec1 = tabs.newTabSpec("Tab1");
tspec1.setIndicator("SIGN UP");
tspec1.setContent(R.id.scrollView02);
tabs.addTab(tspec1);
TabSpec tspec2 = tabs.newTabSpec("Tab2");
tspec2.setIndicator("LOG IN");
tspec2.setContent(R.id.scrollView01);
tabs.addTab(tspec2);
As I'm using Dialog class for the view and integrating TabHost inside the dialog, that's why I'm using ContextThemeWrapper for this to have some theme on the Dialog.
Now, my question is that how can I change the Holo.Light theme to Dark theme. Here is the picture what I want:
I know that android does not have Holo.Dark theme as of now. That is only available for ActionBars. So how can I achieve this solution.
Any kind of help will be appreciated.
This will work:
//Changing the tabs background color and text color on the tabs
for(int i=0;i<tabs.getTabWidget().getChildCount();i++)
{
tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.BLACK);
TextView tv = (TextView) tabs.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#ffffff"));
}
And for the indicator, have a layout like this beneath tabwidget
<LinearLayout
android:id="#+id/tab_indicator"
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#bdbdbd" >
<LinearLayout
android:id="#+id/tab_indicator_left"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_weight="1"
android:background="#f44b3b" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab_indicator_right"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_weight="1"
android:background="#bdbdbd" >
</LinearLayout>
</LinearLayout>
And change the background color of indicator like this based on the tab selection.
tabindicator1.setBackgroundColor(Color
.parseColor("#f44b3b"));
See the link it will helpful
How to change default color to Tab Host
and also refer this it will helpful
http://joshclemm.com/blog/?p=136
I would suggest using as much of android's source as possible. It really makes things cleaner in my opinion. I added a basic example of what I used below. Not perfect but closer than anything else I was able to fine and cleaner than most examples.
https://github.com/android/platform_frameworks_base/tree/master/core/res/res
For instance, for the holo theme, use this.
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/tab_indicator_holo.xml
and get all the resources and put them into your project. After that, use the link
http://joshclemm.com/blog/?p=136
and modify it to work as you want.
Your Layout file
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabHost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip"
android:background="#000">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</FrameLayout>
Code - same as josh clemm
mTabHost=(TabHost)getActivity().findViewById(R.id.tabHost);
mTabHost.setup();
//mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
setupTab(new TextView(getActivity()), "Tab 1");
setupTab(new TextView(getActivity()), "Tab 2");
setupTab(new TextView(getActivity()), "Tab 3");
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabHost.TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
And then tab_bg file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/tab_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<TextView android:id="#+id/tabsText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Title"
android:textSize="15dip" android:textColor="#android:color/white" />
</LinearLayout>
In res/values/styles.xml, change the theme parent to "android:Theme.Holo" instead of "android:Theme.Holo.Light"
This will change the entire app's theme obviously, but you can also use different styles for different activities.
I am using two ScrollViews and want to set visibility of one as GONE or INVISIBLE but when I do it by following java code the application terminates. If I do the same thing using Properties pan, in the eclipse, it just works. What is wrong?
Java code is:
public class Test extends Activity {
List<ScrollView> sv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
setContentView(R.layout.main);
}
}
main.xml
<?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"
android:gravity="fill_vertical">
<ScrollView android:layout_height="wrap_content" android:id="#+id/scrollView1" android:layout_width="match_parent">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/linearLayout1" android:orientation="vertical">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</ScrollView>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scrollView2">
<TextView android:text="TextView" android:layout_width="match_parent" android:id="#+id/textView2" android:layout_height="match_parent"></TextView>
</ScrollView>
</LinearLayout>
you haven't setContentview(yourLayout.xml)
it wont be able to findViewById.
do the findViewById AFTER the setContentView
First of you have to set layout file.
setContentView(R.layout.new_view);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
try this if you are getting any error then please post logcat.
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