I am implementing a popup menu for my android application. I want to be able to customize the items of the menu and change the default values (for the item padding, margin, padding between item icons etch.). Currently i am able to change only the text color and size of each item with the properties:
<item name="android:textColor">#color/white</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
which are set within :
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
How is this possible, which attributes i need to set in the styling of the menu?
Hello, You can create customized layout specifically for the popup and you can give each items different margin padding..
Check out below code.
private fun createPopUp() {
val popUpView: View = layoutInflater.inflate(R.layout.popup, null)
val mpopup = PopupWindow(
popUpView, ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT, true
)
popUpView.close.setOnClickListener{
mpopup.dismiss()
}
}
Here is the code for the popup layout you can create a separate XML for that and add layout and items into that with each attributes available within.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="#dimen/_16sdp">
<Button
android:id="#+id/close"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="#string/date"
android:textColor="#color/white" />
</RelativeLayout>
Related
I'm trying to add a style to a button in Xamarin Android from code, because the buttons will be generated dynamically and need to change colors based on other system events.
Using the code shown here, I expect that the two rows, one rendered from XML and the other from code, will look exactly alike. Instead, I see that the style ButtonDefaultTheme is only applied to the first row.
I know setting the style from code is possible based on this answer and this one. I think I could accomplish what I need using a layout inflater as in this answer, but I'm very curious why the code I have won't work.
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
LinearLayout roomButtonLayout = FindViewById<LinearLayout>(Resource.Id.roomButtonLayout);
LinearLayout roomInfo = new LinearLayout(this)
{
Orientation = Orientation.Horizontal,
LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent, 1f)
};
roomInfo.SetGravity(GravityFlags.CenterVertical);
roomInfo.SetPadding(20, 10, 0, 10);
TextView roomNameDisplay = new TextView(this)
{
Gravity = GravityFlags.Left,
TextSize = 20, // defaults to scaled pixels
Text = "Room 100",
LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent, 1f)
};
roomInfo.AddView(roomNameDisplay);
Button button2 = new Button(new ContextThemeWrapper(this, Resource.Style.ButtonDefaultTheme), null, 0)
{
Text = "View2"
};
roomInfo.AddView(button2);
roomButtonLayout.AddView(roomInfo);
}
}
In Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_height="wrap_content"
android:id="#+id/roomButtonLayout">
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="0dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:gravity="left"
android:textSize="20sp"
android:text="Room 100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:text="View1"
style="#style/ButtonDefaultTheme" />
</LinearLayout>
</LinearLayout>
In styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="ButtonDefaultTheme" parent="android:style/Widget.Holo.Button">
<item name="android:paddingBottom">0dp</item>
<item name="android:paddingTop">0dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:minWidth">100dp</item>
</style>
</resources>
Result:
Minimum API level needed: 19
Compiling using: 21 in actual app, 23 in test app. Same visual results in both.
I made a DialogFragment with a RadioButton ontop and a ListView below containing more RadioButtons. And now I'm just wondering what style the ListView uses that the RadioButtons don't look the same as the "stand-alone" one.
Here is a snippet of my dialog.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/buttonGroup"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RadioButton
android:id="#+id/none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/none" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/white" />
<ListView
android:id="#+id/conferences"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And my list_item.xml that I inflate as the rows in getView of my ArrayAdapter:
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/conference"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
My AppTheme is based on "Theme.AppCompat". I didn't change any style of a specific item in my View.
The "stand-alone" RadioButton has a white circle with a blue dot when selected and even has a different style while you press it down. The RadioButtons in the ListView are all black with a black dot and don't have a "press-down" style. It would be cool if I wouldn't need to programatically set styles for not checked/"press-down"/checked. I already tried to set 2 base Android themes that had "RadioButton" in their names on the ListView but nothing changed. Maybe there is a way to get the style of the "stand-alone" RadioButton and set it in the xml files for the other RadioButtons?
I would post an image but I'm new here so not already allowed to do this.
Update
I changed my AppTheme to this:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:textViewStyle">#style/TextAppearance.AppCompat.Large</item>
<item name="android:radioButtonStyle">#style/RadioButtonStyle</item>
</style>
<style name="RadioButtonStyle">
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Large</item>
</style>
And the RadioButtons in the ListView changed but not the "stand-alone" one. When I add the style to the RadioButton:
<RadioButton
android:id="#+id/none"
style="#style/RadioButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/none" />
the text changes but the RadioButton-circle is still there. For the RadioButtons in the ListView the circles are gone...
Make sure that LayoutInflater in your Adapter uses Activity context and not the application context.
Update:
Please remove android prefix from your style:
<item name="radioButtonStyle">#style/RadioButtonStyle</item>
And update RadioButtonStyle as follows:
<style name="RadioButtonStyle" parent="#style/Widget.AppCompat.CompoundButton.RadioButton" />
I have a listview set to use singleChoice. All I want to do is change the default background color to white and the text color to black. I cannot figure out how to do this. Here is my xml layout:
<ListView
android:id="#+id/lvSpeeds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/llToolbar"
android:layout_below="#id/rgSpeedUnits"
android:textColor="#android:color/black"
android:choiceMode="singleChoice"
android:background="#ffffff"
android:cacheColorHint="#00ffffff"
android:clickable="true"
android:divider="#ff000000"
android:dividerHeight="1dp"
android:focusable="true"
android:scrollingCache="true" />
EDIT: I should have pointed out that I want to change this only using xml layout files and NOT in code. I already know how to do this in code. Using a custom layout other than android.R.layout.simple_list_item_single_choice forces you to implement an adapter, bind, write more code, and so on. From viewing a lot more posts, it does not appear possible to change the text color using only xml. In fact, it doesn't seem possible to change anything on a row as the underlying layout android.R.layout.simple_list_item_single_choice is not accessible.
for a list view Use android selector
like this
and save it with anyname.xml and giveits reference to background of your list
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/about_btn_hover"></item>
<item android:drawable="#drawable/about_btn"></item>
</selector>
and for changing text color add color folder in your res directory
create an xml save it textchangecolor.xml
and add the following lines to it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/whiteColor"></item>
<item android:color="#color/bluetxt"></item>
</selector>
and give its refernce to the textcolor
try to this:
insert to this code in adapter in getview mathod:
LinearLayout mRowLayout = (LinearLayout) vi
.findViewById(R.id.list_item_layout);
final TextView title = (TextView) vi
.findViewById(R.id.list_item);
title.setText(Massage[position]);
mRowLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
title.setTextColor(Color.RED);
});
here list_item code:
<?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:id="#+id/list_item_layout"
android:orientation="vertical" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/row_single"
android:layout_margin="5dp">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/black"
android:padding="10dp"
android:textSize="16sp"
android:layout_toLeftOf="#+id/arrow"
android:id="#+id/list_item"/>
</RelativeLayout>
</LinearLayout>
The best solution I could find was to use styles.xml and set a theme for my dialogs.
styles.xml
<style name="DialogTheme" parent="AppTheme">
<item name="android:textColor">#color/text</item>
<item name="android:textColorAlertDialogListItem">#color/text</item>
+ other styles
</style>
In Java Built Dialogs:
ContextThemeWrapper theme;
theme = ContextThemeWrapper(view.getContext(), R.style.DialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(theme);
In XML built Dialogs:
<myLayout
....
android:theme="#style/DialogTheme" />
Im trying to customize the actionbar sherlock tabs so they display the tab text below the tab icon. After searching and reading about the subject, I'm still not having much luck.
Here are the results I'm getting:
Custom tab1 selected
Custom tab1 not selected, standard tab2 selected
This is what I have so far:
Created my own styles.xml where I change some of the actionbar settings:
<style name="SkoletubeTheme" parent="Theme.Sherlock">
<item name="actionBarSize">#dimen/st__action_bar_default_height</item>
<item name="actionBarTabStyle">#style/Skoletube.TabView</item>
<item name="actionBarTabTextStyle">#style/Skoletube.Tab.Text</item>
</style>
<style name="Skoletube.TabView" parent="Widget.Sherlock.ActionBar.TabView">
<item name="android:background">#drawable/abs__tab_indicator_holo</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item>
</style>
<style name="Skoletube.Tab.Text" parent="Widget.Sherlock.ActionBar.TabText">
<item name="android:textAppearance">#android:style/TextAppearance.Medium</item>
<item name="android:textColor">#android:color/primary_text_dark</item>
<item name="android:textSize">10sp</item>
</style>
Created an xml ressource where I change and owerwrite some textsettings for the actionbar. Here I have increased the height of the actionbar, which in turn increases the height of the tabs. I also decreased the size of the text in order to make room for fitting icon and text in the tab
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Default height of an action bar. -->
<dimen name="st__action_bar_default_height">58dip</dimen>
<!-- Text size for action bar titles -->
<dimen name="ab__action_bar_title_text_size">10dp</dimen>
<!-- Text size for action bar subtitles -->
<dimen name="ab__action_bar_subtitle_text_size">6dp</dimen>
<!-- Top margin for action bar subtitles -->
<dimen name="ab__action_bar_subtitle_top_margin">-3dp</dimen>
<!-- Bottom margin for action bar subtitles -->
<dimen name="ab__action_bar_subtitle_bottom_margin">5dip</dimen>
</resources>
Then I've defined a custom layout for the tabs to use, where I place the text below the icon:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customTabLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:focusable="true"
android:gravity="center"
style="?attr/actionBarTabStyle"
>
<ImageView
android:id="#+id/sk_abs__tab_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
/>
<com.actionbarsherlock.internal.widget.ScrollingTextView
android:id="#+id/sk_abs__tab_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/sk_abs__tab_icon"
android:layout_alignWithParentIfMissing="true"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
style="#style/Skoletube.Tab.Text"
/>
<FrameLayout
android:id="#+id/abs__tab_custom"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="5dip"
android:layout_weight="1"
android:visibility="gone"
/>
Just for the record the attribute passed as the style for the relative layout points to this:
<style name="Widget.Sherlock.ActionBar.TabBar" parent="android:Widget"></style>
Then I've applied these changes to my app as follows, the first tab has my customized implementation, the second tab has the standard implementation:
final ActionBar actionBar;
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(true);
// Set up tabs
myMediaTab = actionBar.newTab();
myMediaTab.setCustomView(R.layout.skoletube_tab_layout);
ImageView myMediaImg = (ImageView) myMediaTab.getCustomView().findViewById(R.id.sk_abs__tab_icon);
myMediaImg.setImageResource(R.drawable.tab_mymedia);
ScrollingTextView myMediaText = (ScrollingTextView) myMediaTab.getCustomView().findViewById(R.id.sk_abs__tab_txt);
myMediaText.setText("Tab1");
myMediaTab.setTabListener(this);
myMediaTab.setTag("MyMediaTab");
actionBar.addTab(myMediaTab);
myChannelsTab = actionBar.newTab();
myChannelsTab.setIcon(drawable.tab_mychannels);
myChannelsTab.setText("Tab2");
myChannelsTab.setTabListener(this);
myChannelsTab.setTag("myChannelsTab");
actionBar.addTab(myChannelsTab);
While I think I'm close to the solution, I think I've missed a step somewhere.
Obviously the blue bar behind the text/below the image needs to not be there, but I also haven't seemed to find out where I can set the focused image(I want the img to change color when a tab is selected) andtext color. Do I need to make the imageview focusable and handle it through that or?
I'm still fairly new at this, so the approach I have taken here might be wrong if there is a better/smarter way to go about doing this please do say.
Appreciate any suggestions and ideas.
I have solved that by using a compound drawable:
tv = new TextView(this);
tv.setText(R.string.tab_movies);
tv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.nav_movies, 0, 0);
mBar.addTab(mBar
.newTab()
.setCustomView(tv)
.setTabListener(
new TabListenerImpl<TheatersTabActivity>(this, "theaters",
TheatersTabActivity.class)));
In order to deal with the selected or not selected image, I am using a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_selected="true"
android:drawable="#drawable/nav_movies_on"/>
<item
android:state_selected="false"
android:drawable="#drawable/nav_movies_off"/>
</selector>
I don't know if you have found an answer for this or not yet but one solution could be to modify the tab image to include the text, with GIMP or photoshop something, and then just set those images in the tabs, instead of images and text. It is a bit of an awkward way of doing it but it would work.
Hope this helps!
I am creating a custom dialog and I want to know how to change the background of the title bar.
I've tried two approaches:
1 - I've tried the AlertDialog.Builder method 'setCustomTitle'. I created a simple layout view comprising of a textview with layout width and height 'match_parent' and background color. When I run the app, only the top half of the title bar is showing the background color. The bottom half is still showing the default theme background color. Does anyone know why?
2 - I've created my own dialog theme. Ive created a style with parent inheritance to '#android:style/Theme.Holo.Light.Dialog'. I've then passed that in the AlertDialog.Builder constructor - new AlertDialog.Builder(this, R.style.test_dialog). It seems good but somehow the dialog is wrapped within a dialog. A square box is surrounding the dialog.
Does anyone know why?
You can create a style like,
<style name="cust_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
</style>
<style name="dialog_title_style" parent="android:Widget.TextView">
<item name="android:background">#android:color/black</item>
<item name="android:padding">10dp</item>
</style>
And you can instantiate dialog:
Dialog dialog=new Dialog(this,R.style.cust_dialog);
dialog.setContentView(R.layout.fragment_features_dialog);
dialog.setTitle(R.string.features);
Now the dialog shows up with black title background color.
The dialog-wrapped-within-a-dialog appearance is caused by the dialog's window background. Every dialog has this, but the default Android dialogs have the window background set to transparent. To do this, add this item in your custom dialog theme:
<style name="CustomDialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I am using Mono Android(Xamarin); am showing you another alternative that am using in my app to create a dialog in a fragment:
Dialog itemDialog = new Dialog(this.Activity);
TextView alertTitle=(TextView)itemDialog.Window.DecorView.FindViewById(Android.Resource.Id.Title);
alertTitle.SetTextColor(Android.Graphics.Color.Blue);
alertTitle.SetBackgroundColor(Android.Graphics.Color.Orange);
itemDialog.SetContentView(Resource.Layout.listview_custom_dialog);
string[] options = new string[] { "Open", "Mark as Unread","Mute","View
Profile","Block Connection","Delete Conversation" };
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity,
Resource.Layout.listitem_custom_dialog,Resource.Id.textViewDialogDescription,
options);
Resource.Layout.listitem_custom_dialog: this is custom listview layout, here is the xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/textViewDialogDescription"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#ffffff"
android:textColor="#386B96"
android:paddingLeft="4dp"
android:textSize="14dp" />
</RelativeLayout>
ListView lv = itemDialog.FindViewById<ListView>
(Resource.Id.listViewDialogItems);
lv.Adapter = adapter;
adapter.NotifyDataSetChanged();
itemDialog.SetCancelable(true);
itemDialog.SetTitle("Conversation");
itemDialog.Show();
Android.Resource.Id.Title: this is the id of the textview containing the dialog title. and it is predefined by android.
this way you will get a dialog that you can style in way you want.
Create an xml layout file for your title and inflate it and set to to the AlertDialog as
View view = getLayoutInflater().inflate(R.layout.cust_dialog_title, null);
alertDialog.setCustomTitle(view);
You can just set custom title like this
LayoutInflater inflater = this.getLayoutInflater();
View titleView = inflater.inflate(R.layout.custom_title, null);
new AlertDialog.Builder(SubCategoryActivity.this)
.setCustomTitle(titleView);
and in custom_title layout you can create custom title like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="#+id/llsubhead"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/exemptionSubHeading4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:text="Exemption Sub Head"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>