I've got an app that uses tabs for navigation, and on one of those tabs there is a spinner. However, when the spinner is selected and the actual select window comes up, all the text is white on a white background.
I've tried styling the layout but nothing I do changes the color of the font.
the main class
public class RealmsOfWickedry extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
firstTabSpec.setIndicator("Home").setContent(new Intent(this,FirstTab.class));
secondTabSpec.setIndicator("Catalog").setContent(new Intent(this,SecondTab.class));
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
}
public static View makeSpinner(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.spinner, null);
Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
adapter.add("Item 1");
adapter.add("Item 2");
adapter.add("Item 3");
adapter.add("Item 4");
spinner.setAdapter(adapter);
return v;
}
}
the class with the spinner
public class SecondTab extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Second Tab Content */
TextView textView = new TextView(this);
textView.setText("Choose a Category");
setContentView(textView);
setContentView(RealmsOfWickedry.makeSpinner(getParent()));
}
}
tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost">
<LinearLayout android:id="#+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent"></FrameLayout>
</LinearLayout>
</TabHost>
spinner.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">
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/cat_prompt"
android:theme="#style/DropdownStyle"
/>
</LinearLayout>
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="OverallStyle" parent="#android:Theme.Light">
<item name="android:windowBackground">#drawable/bg</item>
<item name="android:textColor">#color/white</item>
</style>
<style name="WelcomeStyle" parent="#android:Theme.Light">
<item name="android:typeface">monospace</item>
<item name="android:gravity">center</item>
</style>
<style name="CustomStyle" parent="#android:Theme.Light">
<item name="android:typeface">monospace</item>
<item name="android:gravity">top</item>
</style>
<style name="DropdownStyle" parent="#android:Theme.Light">
<item name="android:textColor">#color/red</item>
</style>
</resources>
Can anyone help me out with this?
It looks like you're trying to override the system theme to show a different color which is the right path to be on. Your spinner xml contains a reference to android:theme I haven't see that one before and it doesn't appear to be part of the API for this widget. To get your DropdownStyle to work, first, add it as part of your OverallStyle style with an item name of #android:attr/spinnerDropDownItemStyle. Second, change DropdownStyle's parent to #android:Widget.DropDownItem.Spinner. I'm assuming that OverallStyle is applied to the Activity or Application in the Manifest already. This will change the style for all Spinner drop down Items.
To apply only to this view's drop down items do only step two above then add style="#style/OverallStyle" to the spinner in its layout.
Additional Information:
<style name="DropdownStyle" parent="#android:Widget.DropDownItem.Spinner">
<item name="android:textColor">#color/red</item>
</style>
<style name="OverallStyle" parent="#android:Theme.Light">
<item name="android:windowBackground">#drawable/bg</item>
<item name="#android:attr/spinnerDropDownItemStyle">#style/DropdownStyle</item>
<item name="android:textColor">#color/white</item>
</style>
-OR-
themes.xml
<style name="DropdownStyle" parent="#android:Widget.DropDownItem.Spinner">
<item name="android:textColor">#color/red</item>
</style>
spinner.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/cat_prompt"
style="#style/DropdownStyle"
/>
</LinearLayout>
Related
I'm trying to make a clickable star button which will have a fill in color of red when clicked on. Before I click the build button, Android Studio does not highlight and error in my program. However, it is after I click the build button that Android Studio shows that everything is wrong with my styles.xml file but I am not sure how I can fix it.
MainActivity.java
package com.pace.importantbutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onDefaultToggleClick(View view){
Toast.makeText(this,"DefaultToggleClick", Toast.LENGTH_SHORT).show();
}
public void onCustomToggleClick(View view){
Toast.makeText(this,"CustomToggle", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="onDefaultToggleClick"
/>
<ToggleButton
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:background="#drawable/toggle_selector"
android:onClick="onCustomToggleClick"
/>
</LinearLayout>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<Style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Theme will be customized here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</Style>
</resources>
toggle_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_baseline_star_outline_24" android:state_checked="false"></item>
<item android:drawable="#drawable/ic_baseline_star_24" android:state_checked="true"></item>
</selector>
ic_baseline_star_outline_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#android:color/holo_red_light"
android:pathData= "..."
/>
</vector>
ic_baseline_star_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#android:color/holo_red_light"
android:pathData= "..."
/>
</vector>
I do it in my project by using an ImageView.
ImageView favorite = findViewById(R.id.favoriteButton);
//Here you put the logic to start with the outline or the filled star
if (user.isFavorite(productKey)) favorite.setImageDrawable(getDrawable(R.drawable.ic_filled_star));
else favorite.setImageDrawable(getDrawable(R.drawable.ic_outline_star));
//Finally you set your listener to change the image
favProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (favorite.getDrawable().getID()==R.drwable.ic_filled_star){
favorite.setImageDrawable(getDrawable(R.drawable.ic_outline_star));
//The other things you want to do
}
else{
favProduct.setImageDrawable(getDrawable(R.drawable.ic_filled_star));
//The other things you want to do
}
}
});
I have a SearchableSpinner and I want to display the text in the center of it
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="#+id/spinner_client"
style="#style/spinner_style_v2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:drawSelectorOnTop="true"/>
</LinearLayout>
and here the custom style. I tried adding <item name="android:textAlignment">center</item> but it's not working also I've tried <item name="android:paddingTop">10dp</item>
to see if the text will move to the bottom a little bit but it's not moving.
<style name="spinner_style_v2">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/gradient_spinner</item>
<item name="android:popupBackground">#DFFFFFFF</item>
</style>
Spinner adapter
try {
if (clientList != null && clientList.size() > 0) {
ArrayAdapter<Client> clientArrayAdapter = new ArrayAdapter<Client>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, clientList);
clientArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
clientSpinner.setAdapter(clientArrayAdapter);
clientSpinner.setTitle("Choose Client");
}
} catch (Exception ex) {
Log.d(TAG, ex.toString());
}
To center item in your Spinner create a Custom layout and center TextView with android:gravity="center" like the XML below:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</TextView>
and in your java code use this layout as #LayoutRes in your Adapter :
ArrayAdapter<Client> clientArrayAdapter = new ArrayAdapter<Client>(getApplicationContext(),R.layout.simple_text, clientList);
I cannot get the bottom navigation to show up in androidx at all. I have even copied examples from other answers that are supposed to work, and they show up in the preview. activity.xlm:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/frameLayout"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
navigation.xml
<?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/homeButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_agenda"
android:title="a funny item"/>
<item
android:id="#+id/favoritesButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_info_details"
android:title="favorites"/>
<item
android:id="#+id/ideaButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_agenda"
android:title="ideas"/>
</menu>
Styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
activity code:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
What am I missing? Also why would it show up in the preview in android studio and not show when the app runs? Anyhelp is greatly appreciated
Yor forgot add setContentView(R.layout.activity_main); in your MainActivity
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I tried to create my own custom loading dialog on android. That was no problem, but the dialog creates a really ugly shadow when it appears
I tried to use a default color theming my android and not my own. And i tried to set a transparent windows background and set the elevation already to 0dp
Screenshot
enter link description here
styles.xml
<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!--<item name="android:windowBackground">#color/colorPrimary</item>-->
<item name="android:windowBackground">#color/colorAccent</item>
</style>
<style name="AppTheme.Dark.Loading.Dialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#color/colorPrimary</item>
</style>
dialog-layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:theme="#style/AppTheme.Dark.Loading.Dialog"
android:padding="16dp"
>
<TextView
android:id="#+id/text_view_loading_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authenticating..."
android:textSize="16sp"
android:textStyle="bold"
android:gravity="center"
/>
<ProgressBar
android:id="#+id/progress_bar_loading_dailog"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#id/text_view_loading_dialog"
android:indeterminate="true" />
</LinearLayout>
Dialogclass
public class LoadingDialog extends AppCompatDialogFragment {
private TextView messageTextView;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_loading_dialog, null);
builder.setView(view);
messageTextView = view.findViewById(R.id.text_view_loading_dialog);
return builder.create();
}
Activity
LoadingDialog loadingDialog = new LoadingDialog();
loadingDialog.show(getSupportFragmentManager(), "loading dialog");
I expected a normal black or light grey shadow, like the depracted ProgressDialog by Google
In your activity, add the clearFlags() method, like this:
LoadingDialog loadingDialog = new LoadingDialog();
loadingDialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
loadingDialog.show(getSupportFragmentManager(), "loading dialog");
Yes that helps! Thank you very much. But removing this flag, remoes the whole dimming background. I build a workaround with a invisible dimmed View on my activity, which i make visible everytime my dialog pops up.
I added that to my activity layout
<View
android:id="#+id/dimBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorDimBackground"
android:visibility="invisible">
</View>
My color is this:
<color name="colorDimBackground">#B3000000</color>
The #B3 is the hexcode for 70% opacity. You can find the a table with all hex values here:
Hex values for opacity
And before a I show the dialog, i make the view visible
findViewById(R.id.dimBackground).setVisibility(View.VISIBLE);
LoadingDialog loadingDialog = new LoadingDialog();
loadingDialog.show(getSupportFragmentManager(), "loading dialog");
Maybe there is a more practical solution for this problem, but i have no idea!?
This is the result:
Strangely, this was working fine, this is how it looks on my app released on Google Play:
And this is how it looks now:
All I did was migrate to AndroidX.
Here is my dialog layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialogLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#drawable/rounded_corners">
<Button
android:id="#+id/btnAdd2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:text="#string/import_video"
android:textColor="#color/dark_text" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corners">
<Button
android:id="#+id/btnAdd1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:text="#string/add_student"
android:textColor="#color/dark_text" />
</FrameLayout>
</LinearLayout>
and here is how I inflate it:
View dialogView = View.inflate(getApplicationContext(), R.layout.dialog_main, null);
LinearLayout dialogLinear = dialogView.findViewById(R.id.dialogLinear);
//Here is where is set the background to transparent
dialogLinear.setBackgroundColor(0x00000000);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
Button btnAdd1 = dialogView.findViewById(R.id.btnAdd1);
Button btnAdd2 = dialogView.findViewById(R.id.btnAdd2);
btnAdd1.setTypeface(mCustom_font_Bold);
btnAdd2.setTypeface(mCustom_font_Bold);
btnAdd1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do stuff
}
});
btnAdd2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do stuff
}
});
alertD.setView(dialogView);
if (alertD.getWindow() != null) {
alertD.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
alertD.show();
I searched and couldn't find why this would be happening. Can someone please give me some advise?
I have tried setting it in in the layout itself.
Edit 1:
After trying the answer below, it now looks like this, instead of above:
I resolved it by adding the following styles:
<style name="NewDialog">
<item name="android:windowIsFloating">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">#android:color/transparent</item>
</style>
Setting the style like this:
final AlertDialog alertD = new AlertDialog.Builder(this, R.style.NewDialog).create();
And to fix what happened in Edit 1, i changed my LinearLayout to a RelativeLayout
You Can Create a theme and assign that theme to your AlertDialog
Define theme in your styles.xml
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
And Assign that theme to your AlertDialog
final AlertDialog alertD = new AlertDialog.Builder(this,R.style.CustomDialog).create();
Or
2.You should try and use Dialog instead of AlertDialog
As Explained in this Answer.