I followed the procedure explained on the link bellow but for some reason the switch is never displayed. I changed the visibility to match my app namespace as suggested in other threads to no avail. The procedure works fine if I try adding Icon's or regular buttons.
Thanks a lot.
I'm running it on a Nexus 4 with Android 4.4.4
How to add a switch to android action bar?
Here is my current xml contents.
/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.testapp.MainActivity" >
<item
android:id="#+id/myswitch"
android:title=""
app:showAsAction="always"
android:actionLayout="#layout/switch_layout"
/>
</menu>
/layout/switch_layout.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="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Ok, So I finally found something that works for me.
I created a layout file :/layout/text_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
> <!-- android:layout_height="50dp" -->
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Then on my MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar mActionBar = getActionBar();
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.test_layout, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
BOOOMMM, there you go.
It's impressive how simple things can be so complicated on Android sometimes.
Thanks for the suggestions.
Related
I am not able to use ripple effect on pressing on a tab please help I have tried a lot and struggling with it whole day.How can I use ripple effect on both pre-lollipop and lollipop devices any suggestion?
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager=(ViewPager)findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(),getApplicationContext()));
mTabs=(SlidingTabLayout)findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
mTabs.setCustomTabView(R.layout.customtablayout,R.id.textTab);
mTabs.setBackgroundColor(getResources().getColor(R.color.colorAccent));
mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorPrimary));
mTabs.setViewPager(mPager);
}
activitymain.xml
<sarveshchavan777.inrerface2.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
customtablayout.xml
<ImageView
android:id="#+id/imageTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="#+id/textTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center" />
use this code for any item you want to give it ripple effect.
copy this code into a new XML file in the drawable folder. and set it as the background of any item you want ripple effect for.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/icons"
tools:targetApi="lollipop">
<item android:drawable="?attr/colorPrimaryDark" />
</ripple
This should work
mTabs.setTabRippleColor(getResources().getColorStateList(R.color.colorPrimaryDark));
Simple question that I don't find an answer for.
Is it possible to break line after the icon in a menu item making them not inline anymore? (The menu slides in from left by using NavigationView in Android support library)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/cards"
android:checked="false"
android:icon="#drawable/card_icon"
android:title="Cards" />
...
...
...
</group>
This is how I want it to look like.
But instead they are aligned inline.
Update after answer:
Using a modified android.R.layout.activity_list_item (changed to vertical aligned left):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="1dip"
android:paddingBottom="1dip"
android:paddingStart="8dip"
android:paddingEnd="8dip"
android:orientation="vertical">
<TextView android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal"
android:paddingStart="?android:attr/listPreferredItemPaddingStart" />
<ImageView android:id="#+id/icon"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_gravity="left" />
</LinearLayout>
And I added an actionLayout to the menu items:
<item
android:id="#+id/cards"
android:checked="false"
android:icon="#drawable/cards_icon"
android:actionLayout="#layout/vertical_menu_item"
android:title="Cards" />
But nothing happens. They are still inline. I also tried using app:showAsAction="always" and app:actionLayout="#layout/vertical_menu_item".
I suggest you to create a custom view (eg: vertical layout with ImageView and TextView), and in onCreateOptionsMenu() you can use setActionView() method of the MenuItem to use this custom view as Item view.
Or from menu xml:
android:actionLayout="#[package:]layout/layout_resource_name"
I'm using a menu item with actionLayout because I need to add a textView along with my icon for the item on the ActionBar. everything shows up correctly but I don't know (and couldn't find anywhere) how to add the animation that shows up when user taps on a menu item on Lollipop devices.
I would like to show number of comments on the side of comment icon in my toolbar.
Here is what I have:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_like"
android:icon="#drawable/list_discuss_like"
android:title="Like"
app:showAsAction="always"/>
<item
android:id="#+id/menu_comment"
app:actionLayout="#layout/menu_item_comment"
android:title="Comment"
android:icon="#drawable/ic_comment_white_24dp"
app:showAsAction="always"/>
</menu>
and for my menu_item_comment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:hapticFeedbackEnabled="true"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="#drawable/ic_comment_white_24dp"/>
<TextView
android:id="#+id/ab_comment_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="(12)"
android:textColor="#color/grayTextHint"/>
</LinearLayout>
The animation and feedback works on the like item but not on the comment one.
Any help is appreciated.
Set the clickable element's background to the theme's action bar item background.
<LinearLayout
...
android:clickable="true"
android:background="?android:attr/actionBarItemBackground">
...
</LinearLayout>
currently I'm trying to implement the FragmentTabHost for my project. I'm still new on this fragments but I found it very great in terms of reusing the layouts, etc which is why I wanted to push myself further on to it. Now I read the tutorials on how to create a Tabs with fragment and I came on this tutorial:
http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/
Now this works just fine, except that the tabWidget is on top of my layout where I wanted it to be on bottom. I find that I need to setup the tabWidget after all the tabs has been initialized so I tried to add these codes:
mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
mTabWidget.setBackgroundColor(Color.WHITE);
mTabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
mTabWidget.setGravity(Gravity.BOTTOM);
Now this one already eliminated the divider and changes the color but obviously won't put my Widget on the bottom part of my layout. Now how would I do that?
I also tried to edit the Tabhost xml and just put the TabWidget after the FrameLayout but nothing happens. here's the 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" >
<FrameLayout
android:id="#+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
I have refer this link github example
This will be your layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
For your custom Tabs :
mTabHost.addTab(setIndicator(mTabHost.newTabSpec("Tab1"),
R.drawable.image1),
public TabSpec setIndicator(Context ctx,TabSpec spec, int resid) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(ctx).inflate(R.layout.tabs_text, null);
v.setBackgroundResource(resid);
TextView text = (TextView) v.findViewById(R.id.tab_title);
text.setText(spec.getTag());
return spec.setIndicator(v);
}
Edit
//To set drawable to your perticular TAB
mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_login);
end Edit
If you want to add drawable (selector):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tab_compose_h" android:state_selected="true"/>
<item android:drawable="#drawable/tab_compose_h" android:state_pressed="true"/>
<item android:drawable="#drawable/tab_compose"/>
</selector>
I have an activity in android which I set up a button under a SurfaceView. And the result looks like this:
And I'm satisfied with it except one thing, the writing on the button is astray and must be setup correctly.
For this I used animations like this:
takePhoto = (Button) findViewById(R.id.btnPhoto);
takePhoto.setText(t);
RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.myanim);
ranim.setFillAfter(true);
takePhoto.setAnimation(ranim);
WHERE the res/anim/myanim looks like:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="0" />
But the result is a little bit disappointing cause the whole looks like:
The text is ok, but the dimensions are reduced.
What I want is to keep the text correctly and the initial size.How could I do that???
It has nothing to do with the button's properties in the xml file cause I haven't changed them when loading the animation....Ideas?Thanks...
EDIT: xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>
<SurfaceView android:layout_width="450dip"
android:layout_height="fill_parent" android:id="#+id/surface_camera" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_toRightOf="#id/surface_camera"
android:layout_height="fill_parent"
>
<Button android:layout_width="680dip"
android:layout_height="680dip"
android:id="#+id/btnPhoto"
android:layout_alignParentBottom="true"
android:text="Take Photo"
android:layout_gravity="bottom|right" />
</RelativeLayout>
</RelativeLayout>
IMPORTANT:
In the cases described below...my activity is in `LANDSCAPE` mode and the position of the phone is portrait.Another issue is that when I turn my phone in landscape mode(and here I mean the position of the phone, cause the activity is still landscape) the button doesn't go at the bottom of the phone is stays there.No matter how I turn the phone the button is in the same position!!!!
You should read this
Make two layout folders like follows ...
In both of folders your layout file name should be same.
In layout-port folder your layout file should look like this ...
android:layout_toRightOf="#id/surface_camera"
replaced by ...
android:layout_Below="#id/surface_camera"
as follows ...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>
<SurfaceView android:layout_width="450dip"
android:layout_height="fill_parent" android:id="#+id/surface_camera" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_toBelow="#id/surface_camera"
android:layout_height="fill_parent"
>
<Button android:layout_width="680dip"
android:layout_height="680dip"
android:id="#+id/btnPhoto"
android:layout_alignParentBottom="true"
android:text="Take Photo"
android:layout_gravity="bottom|right" />
</RelativeLayout>
</RelativeLayout>
Even this will considered as bad practice but you can also handle this manually, try this ..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
int orientation = getResources().getConfiguration().orientation;
if(orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.main1);
}
else {
setContentView(R.layout.main2);
}
}
Also If you are doing this way No need to make two layout folders, but needs two layout files main1 and main2 in same folder
Yet another option ...
In manifest ...
<activity android:name=".ActivityName" android:configChanges="keyboardHidden|orientation">
+
In your Activity ...
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if(orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.main2);
}
else {
setContentView(R.layout.main);
}
}
Try this XML instead:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="fill_parent">
<SurfaceView android:id="#+id/surface_camera"
android:layout_width="450dip"
android:layout_height="fill_parent" />
<FrameLayout android:layout_width="fill_parent"
android:layout_toRightOf="#id/surface_camera"
android:layout_height="fill_parent">
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView android:id="#+id/btnPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Take Photo" />
</FrameLayout>
</RelativeLayout>
You will rotate the Text not the button.