I have tab layout.That has only icons not text.I tried with clickOnImageButton and ClickOnButton clickOnImage and also pressOnMenuItem(R.drwable.icon)but not worked.How can i do this with solo?
Note: Image View present at the top of the tab.(tab is at bottom)
Tabhosts are evil. Luckily i also have had to automate them and so know the answer.
what you do is you have to get the tab bar view (android.R.id.tabs) and then cast it to a Tabhost or a ViewGroup then you can get each of the tabs via .getChildAt(x) where x is the index of the tabs.
ViewGroup tabs = (ViewGroup) solo.getView(android.R.id.tabs);
View viewYouWantToDoStuffWith = tabs.getChildAt(x); //change x to the index you want.
In your case you would then want something like:
solo.clickOnView(viewYouWantToDoStuffWith);
you can use method solo.clickOnView(solo.getView(resourceId));
where resourceId could be something like R.id.id_Of_Button.
see this link in that they are using image + text. so remove the text made as image that you have to do it.
http://www.androidhive.info/2011/08/android-tab-layout-tutorial/
this is only for text you can set that with image and made as custom tab.
http://www.androidpeople.com/android-tabhost-tutorial-part-1
Hope it will work for you .
Related
I'm using the new support TabLayout from Android. The thing is that I wanted to use selectors to change the icon when a tab is selected.
I've been looking into the source code and it seems to me that the it never changes the state of the view (and for that reason I can't use the selector).
Does anyone knows some workaround?
Thank you!
Assume your my_selector.xml is,
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_on" android:state_selected="true"/>
<item android:drawable="#drawable/icon_off"/> <!-- default -->
</selector>
then you can call setIcon directly,
tab.setIcon(R.drawable.my_selector);
Verified with 'com.android.support:design:22.2.0'.
I found that when I first set the custom view for each tab in the TabLayout I need to set the first one (index 0) as selected.
TabLayout toolbarTabLayout = (TabLayout) findViewById(R.id.tabs);
toolbarTabLayout.setupWithViewPager(mViewPager);
toolbarTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
toolbarTabLayout.setTabMode(TabLayout.MODE_FIXED);
toolbarTabLayout.setTabTextColors(R.color.colorPrimary, R.color.white);
// Iterate over all tabs and set the custom view
for (int i = 0; i < toolbarTabLayout.getTabCount(); i++) {
TabLayout.Tab tab = toolbarTabLayout.getTabAt(i);
View v=mSectionsPagerAdapter.getTabView(i);
// no tabs are actually selected at start, this will make sure the
// selector for the colors comes in right when initialized
if (i==0)
v.setSelected(true);
tab.setCustomView(v);
}
This seems to force the first tab as selected when the custom view is applied. It really feels like a hack, hopefully someone else will figure out the real issue and propose a better fix.
There is way to set customView as a tab with setCustomView(View view) method. So you can create a textview and set a selector to it and set this view to tab.
Hope it helps you!
If you did everything right (and i believe this) so you arrived at same point than me. Maybe it is a little bug in new android appcompat library.
i found an workaround (it's called Gambiarra in a good Portugues) to solve this problem. you need to call the method select() from Tab class like this:
mTabLayout.getTabAt(x).select();
BUT it's very important: x variable must be different than current selected tab index.
This is what worked for me:
Assuming you have your selectors set in the drawable res folder (like Xingang Huang showed above).
In your MainActivity (where you setup your TabLayout) you include your array of icon selectors and then you loop through it like this:
for (int i = 0; i < yourTabLayout.getTabCount(); i++) {
ImageView imageView = new ImageView(this); //your context, in this case MainActivity.class
imageView.setImageResource(arr_tabIcons[i]); //tabIcons is the array of icons
if (i==0) {
imageView.setSelected(true);
}
yourTabLayout.getTabAt(i).setCustomView(imageView);
}
tab.setIcon(R.drawable.icon)
works as well but in my case the icons looked really small, so I had to use the solution with the ImageView to fill the tab view.
Happy coding ;)
I've seen many of questions for how to add TextView in a RelativeLayout programatically, but everybody adding it in LinearLayout with orientation vertical. Can any one suggest me or give me a link that how to add multiple TextView in RelativeLayout at right until it has space and then change the line.
I want a layout like this..
May be this works for you !
You can customize chips-edittext-library for your need. Customizing by setting background to transparent, and editable to false.
-> Or you can use any other library which is used for displaying emoticon in EditText and customize it according to your need. Like android-emoticon-edittext-spike
You can use the Flow Layout library that manages the view arrangement itself.
When there is no space left the added View is moved to the next line
I just started playing with the new support library with ActionBar support. I'm trying to implement a bar that looks basically identical to the Ice Cream Sandwich layout on the edit contact screen. I understand I probably need to implement a custom view something similar to this - How to display custom view in ActionBar?. What I don't understand is exactly what that view is, and the best way to implement it.
Here's the screenshot of what I want in my actionbar:
Is that just a view with an image and some text, or a styled button, or something totally different? It has some state pressed properties.
Thanks for the help.
You can try using a custom view, by adding the following code when you want the button to appear :
// Inflate the view from XML file
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);
// Tell the view to occupy the whole actionBar view (MATCH_PARENT)
LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
getActionBar().setCustomView(v, layout);
I'm developing an android app with fragments. While most of my layouts are pre-determined in the XML, I would like to programmatically insert a new view between views that were already loaded in a LinearLayout at startup.
How do I go about with this?
Thanks
Its possible to specify index while u dynamically add a view to a LinearLayout.
Set height of the first view as
android:layout_height="0dp"
android:layout_weight="1"
Set height = wrap_content for the second view in XML
Then while u are adding new View dynamically, set its height = wrap_content and add it to the parent LinearLayout like this
parentLinearLayout.addView(childView, index);
//index = position where you want to insert the new view.
It might help you. :)
the red View should have the default setting View.setVisibility(View.GONE) right at the beginning. When its time to show up you can switch over to View.setVisibility(View.VISIBLE). I cant verify the solution right now, but it should do the trick. So in this case you are not inserting a new View but make an existing one visible.
I have an android app which asks a question followed by x number of options.
Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
At the moment I have written the code in the layout's xml to display a maximum of 4 options. If number of options is 2 I hide the options 3 and 4 using something like
tvoption1.setVisibility(View.GONE);
tvoption2.setVisibility(View.GONE);
However this is not very scalable. Can anyone tell me how to add options for java dynamically. Or is there a better approach?
A View can be added at runtime by using the inflater like this:
LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.news_categories_item, null);
TextView categoryValueTextView = (TextView)linearLayout.findViewById(R.id.news_category_item_value);
mMainLinearLayout.addView(categoryValueTextView);
In this example, a LinearLayout containing a TextView is inflated. A reference to the constituent TextView is then obtained, and the TextView is dynamically added (at runtime) to the main linear layout (mMainLinearLayout).
The inflater object may be obtained in an Activity by using getLayoutInflater().
create your row layout separately, from the main xml
Get LayoutInflater service from context:
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATE_SERVICE);
use following method to addview to main xml, I assume you have parent layout llParent in xml and you want to add items in this llPaent, from list list.
for(int i=0;i<list.size();i++)
{
LinearLayout llView=(LinearLayout)inflater.inflate(R.layout.row);
//get view id and set values
TextView txt=(TextView)llView.findViewById(R.id.text);
}
A ListView is a good view for displaying several similar items. Here is a tutorial (Other views with adapters are good too, such as GridView or Gallery).
You will probably want to create your own adapter for the list, so you can display all three views (checkbox, image and text) as one item, but there are lots of examples on that available on the net as well as here on SO.