I use this example for creating custom tab. I don't know how to add image in tab view.
I try adding ImageView element in tabs_bg.xml but image is not displayed, only text
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgTab"
android:background="#drawable/img"/>
Can someone help me with this?
Thanks
You must specify the android:src attribute for the ImageView:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgTab"
android:src="#drawable/img"/>
With no content (src), its size will be 0x0 (due to wrap_content).
Specifying the indicator (header) of a tab view is made from java code, usually chained:
myTabHost.newTabSpec(tag).setIndicator(myTab).setContent(intent);
where myTabHost is your TabHost instance, and myTab is a View instance that will be used as the header of this tab.
You can create your own tab: define its layout in xml and add all the views (image, text...) you need on it.
For reference (complete sample) see the update part of this answer.
The layout/tab.xml file contains the layout of the tab headers (including an icon too).
Related
The TabLayout documentation gives an example of nesting TabItem directly inside TabLayout like so:
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.design.widget.TabItem
android:text="#string/tab_text"/>
<android.support.design.widget.TabItem
android:icon="#drawable/ic_android"/>
</android.support.design.widget.TabLayout>
But it gives no example of how this could be used in practice, and the documentation for TabItem says:
This view is not actually added to TabLayout, it is just a dummy which allows setting of a tab items's text, icon and custom layout.
So what is TabItem for? After extensive Googling, I cannot find a single example of anyone defining TabItems in XML. Is there any way to set up a tabbed activity using TabItem in the resource file as shown above?
This appears to be a relatively recent addition to the design library, apparently added in version 23.2.0, though it's not mentioned in the revision history. It's functionality is pretty basic, and the only attributes it seems to use are the three given in its docs: text, icon, and layout.
From testing, it seems it's basically an XML shortcut for creating a new Tab, and setting its text, icon, and custom View, as one would usually do in code. When it says "This view is not actually added to TabLayout", I believe it's meant to suggest that it's not a View in the regular sense, in that you can't set any kind of standard layout attribute on it, like layout_width or background. It simply serves to cause the TabLayout to create a new Tab for each TabItem, and call setText(), setIcon(), and setCustomView() accordingly.
For example, to add a Tab in code, we would usually do something like this:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
// Add Tab
TabLayout.Tab tab = tabLayout.newTab();
tab.setCustomView(R.layout.tab);
tab.setText("Tab 1");
tab.setIcon(R.drawable.ic_launcher);
tabLayout.addTab(tab);
Whereas now we can replace everything after the comment above by adding a TabItem in the layout.
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:layout="#layout/tab"
android:text="Tab 1"
android:icon="#drawable/ic_launcher" />
</android.support.design.widget.TabLayout>
Do note that the same requirements for the custom View layout still apply. That is, the TextView for the text must have the system Resource ID #android:id/text1, and the ImageView for the icon must have the ID #android:id/icon. As an example, the R.layout.tab from above:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Quick addition to #Mikes very helpful answer:
Android Studio now has a Template on how to use a TabLayout with TabItem setup in an XML layout. Create all needed files with "New > Activity > Tabbed Activity" and choose "Action Bar Tabs(with ViewPager)" as shown in the screenshot:
If you want to adjust the look of the TabItem without a custom view: use white vector assets as tab android:icon and tint them with a selector (providing different colors based on android:state_selected)
The color of the line under the currently selected tab is set as app:tabIndicatorColor on tag TabLayout.
It took me a while to get it to work, so the complete steps turned into such a long answer that I don't want to copy them here. You can find my more detailed answer with full code at:
https://stackoverflow.com/a/49603559/414581
please see com.google.android.material.tabs.TabItem class it accepts icon text from attributes, but seems like you will need to add Tags on runtime.
I have an app with multiple fragments and I would like to know how to add a background that is different for each fragment. The layout I am using has scrollable tabs which all use the same xml file. I also have a MainActivity that sets the view and an adapter for each fragment. I know you can add a background using the xml file with android:background or something of the sort as well as setting it to the view in the main activity but I can't figure out how to do it to each tab. Thank you for any help!
To add background to fragment, you have to wrap it in some container
<LinearLayout
android:id="#+id/linearlayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ccc"
android:layout_weight="1"
android:orientation="vertical">
<fragment android:name="com.example.simplefragmentexample.LayOutOne"
android:id="#+id/frag_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
If you wish to use same xml file, you should set images programmatically
LinearLayout l = (LinearLayout) findViewById(R.id.linearlayout01);
l.setBackground(Image);
or use several xmls with android:background.
You can get the root View of the fragment with the Fragment getView() method. Then you can set the background of the View using one of the setBackground() methods of the View. For example to set a random background color to each fragment:
for ( Fragment f : fragments ) {
f.getView().setBackgroundColor ( (new Random()).nextInt() );
}
PS: I never used fragments, therefore my answer could be wrong.
Any body please tell me how we can decrease or increase size of text in Tab button . Is it possible to do so?? I want use some large text in App.
Please help me.....
Thanks in advance
When we add tabs to our TabHost, we use TabHost.newTabSpec() to create a TabSpec object. Using the default tab look, we would call setIndicator(String, Drawable) on the TabSpec (passing the text and image we want displayed on the tab), and then call setContent(intent) on the TabSpec, passing an intent for an Activity we want to be used as that tab’s content. However, in this case, we want to use a custom layout for the tabs, and we want to simply use the Views defined in our tab_activity_layout.xml for the content, instead of individual Activities.
For custom tabs, first, we must define the custom layout of our tabs as a layout resource file. Then we will programatically inflate a View object using that layout, set whatever attributes we want, and pass it into TabSpec.setIndicator(View). Here is a very simple example layout, and some of the code to use it: Please check the link
Custom View for Tab
If you use the code yourEditText.setTextSize(16);
or if you use the .xml android:textSize="16sp"
You just could make your custom tab buttons and it is quite easy and straightforward:
Create a normal like xml layout for the tab. For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/rlayout"
android:gravity="center_vertical|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon4"
android:src="#drawable/somedrawable"
android:layout_height="42dp"
android:layout_width="42dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="News"
android:textSize="12dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:paddingBottom="1dp"
android:layout_below ="#+id/icon4"/>
</LinearLayout>
Then in your tab activity class infalte this layout, like this:
LayoutInflater inflater0 = LayoutInflater.from(this);
View view0 = inflater0.inflate(R.layout.tabbuttonlayout, null);
Afterwards just use it normally in the tab host:
th = getTabHost();
th.addTab(th.newTabSpec("tag0").setIndicator(view0).setContent(intent0));
I hope this helps to better customize your tab.
I have a fragment and i create Layouts and buttons in it (programmatically) to have that kind of view :
<LayoutMaster>
<Layout1>
<Button 1>
<Button 2>
...
</Layout1>
<Layout2>
<Button 1>
<Button 2>
...
</Layout2>
...
</LayoutMaster>
I need that my buttons fill the screen and be all the same size. So when i used an XML file i add those properties to layouts and buttons and it worked :
android:layout_width="fill_parent"
android:layout_height="fill_parent"
But when i'm doing it in my java file (so i don't use a xml layout), when the fragment render, i have only one big button that is taking all the screen. So i guessed i have to refresh the view and add a LayoutMaster.invalidate() but didn't worked.
I'm doing this in the onCreateView of the fragment.
As usual, thanks again for your help !
You are setting wrong attributes. You are using a linear layout which stacks one view over the other and then you are setting layout_height of the button to be fill_parent that's why the first button will occupy the entire space.
Instead you should use layout_wight attribute which helps determine how much percentage of space a view will occupy. If you want all your view to be of same size then use the layout_weight = 1 for all the views.
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
You should use these attributes all the views which needs to be of same size.
I'm trying to work out how to create the layout for a gridview for my game. I have a gridview which will be the level selector. At the moment I have each gridview item as a TextView, just showing the Level Number (1, 2, 3, etc).
I would like to add 3 imageviews over the top of the textview and be able to manipulate the image shown in the views. The 3 image views are stars, showing what difficulty the level was completed on.
I figure I need to write my own Adapter and inflate the XML layout when creating the views for the grid items but I'm stuck with how to create the actual layout for this, the overlapping part has me scratching my head?
Here's a mockup of what I've tried to describe, and what I want to create:
PS. I know I could use 4 images and set the textview background to one of those images but I wondered if there was a more technical way of creating the layout.
I would use a relative view!
The basics would be, define your button background Bitmap/Drawable with the #1 on it, and then, for each button, have a layout with the copper, silver and gold stars. You can use android:layout_alignParentLeft="true" and android:layout_alignParentBottom="true" on the Copper start and base the other alignments off of that one. Then you just set the stars drawable based on if the star is toggled on or of.
I'm going to use rough psuedo code here:
<RelativeLayout android:id="button1">
<ImageView android:id="copper_star" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" />
<ImageView android:id="silver_star" android:layout_alignParentBottom="true" android:layout_rightOf="#id/copper_star"/>
...etc...
</RelativeLayout>
Once you've got it looking the way you want, you can see this discussion by Mark Murphy. He explains how to set the properties of a button to do what you'd like.
I went with the un-technical 4 images in the end, it was simple and easy and there aren't any issues scrolling through the grid so it's all good.