How to make a clickable star-button in android studio? - java

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
}
}
});

Related

androidx BottomNavigationView not showing up

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);
}

Customizing autocompletetextview

I need to implement a custom search functionality with autocomplete feature with custom adapter. I wrote my own custom adapter, Code is given below:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/icon_bg"
android:orientation="vertical"
tools:context="com.emc.kulkaa.myfinancials.MainActivity">
<AutoCompleteTextView
android:id="#+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView"
android:layout_alignStart="#+id/imageView"
android:layout_below="#+id/imageView"
android:layout_margin="16dp"
android:layout_marginTop="20dp"
android:background="#drawable/round_border_edittext"
android:drawableEnd="#drawable/clear"
android:drawableLeft="#drawable/white"
android:drawableStart="#drawable/white"
android:ems="10"
android:hint="#string/search_hint"
android:singleLine="true"
android:textColor="#color/colorWhite"
android:textColorHint="#color/colorWhite" />
</LinearLayout>
custom_item.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="fill_parent"
android:background="#color/cardview_color">
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15sp"
style="#style/simple"/>
</LinearLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name = "android:autoCompleteTextViewStyle">#style/simple</item>
</style>
<style name="simple" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:popupBackground">#drawable/simpleautocustom </item>
</style>
</resources>
simpleautocustom.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorWhite" />
<stroke
android:width="0dip"
android:color="#color/colorBlue" />
<corners android:radius="20dip" />
<padding
android:bottom="10dip"
android:left="25dip"
android:right="25dip"
android:top="10dip" />
</shape>
java code
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView mEdtSearch;
#Override
protected void onCreate(Bundle savedInstanceState) {
String arr[] = {"DSA LSA", "CHILD", "AICHILES", "CHILDREN", "FRANCE", "FRENCH"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, arr);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdtSearch = (AutoCompleteTextView) findViewById(R.id.edt_search);
mEdtSearch.setFocusable(true);
mEdtSearch.setFocusableInTouchMode(true);
mEdtSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mEdtSearch.setAdapter(adapter);
}
});
}
}
Above code works fine, here is the screenshot of output:
However it doesn't still match expected output UI which is given below:
How can I change styles and custom shape so that I can achieve the output?
I have tried something for you, hope it will solve your problem and you can customize xmls i.e background of custom_item.xml TextView according to your need.
activity_main.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:background="#9A000000"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView"
android:layout_alignStart="#+id/imageView"
android:layout_below="#+id/imageView"
android:layout_margin="16dp"
android:layout_marginTop="20dp"
android:background="#drawable/round_border_edittext"
android:drawableEnd="#drawable/clear"
android:drawableLeft="#drawable/white"
android:drawableStart="#drawable/white"
android:ems="10"
android:hint="#string/search_hint"
android:popupBackground="#android:color/transparent"
android:singleLine="true"
android:textColor="#color/colorWhite"
android:textColorHint="#color/colorWhite"/>
</LinearLayout>
custom_item.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="wrap_content"
android:background="#android:color/transparent">
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/bg_rounded_border"
android:maxLines="1"
android:padding="15dp"
android:singleLine="true"
android:textColor="#color/white"
android:textSize="15sp"/>
</RelativeLayout>
bg_rounded_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/hint_color"/>
<corners android:radius="#dimen/padding_less"/>
</shape>
rest your MainActivity (java) code is same, there is no need to set a style to your list item text view.
Here is the screen shot of the output.
You can remove styles.
Only you should add android:padding="7dp" to parent LinearLayout and android:background="#drawable/simple_auto" to TextView in custom_item. It is working.
custom_item:
<?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="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
android:padding="7dp"
android:background="#color/cardview_color">
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/simple_auto"
android:padding="10dp"
android:textSize="15sp"/>
</LinearLayout>

Android Buttons don't animate the way they're supposed to

I am trying to achieve a screen like this in Android. I have taken the same code from the site & tried it, but it's not animating the buttons. I'm getting by running the following code
My content_main.xml contains the following code: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="meetutu.juspay.com.meetutu.MainActivity"
tools:showIn="#layout/activity_main"
android:background="#2980B9">
<ImageView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/meetutuImageView"
android:src="#drawable/meetutuimage"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/meetutuImageView"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="The joy of meeting an expert teacher"
android:textAlignment="center"
android:textColor="#A3E4D7"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="for a craving learner"
android:textAlignment="center"
android:textColor="#A3E4D7"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"/>
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:id="#+id/loginButton"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="LOGIN"
android:layout_marginTop="44dp"
android:layout_below="#+id/textView2"
android:layout_alignLeft="#+id/textView1"
android:layout_alignStart="#+id/textView1" />
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:id="#+id/signupButton"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="SIGN UP"
android:layout_alignTop="#+id/loginButton"
android:layout_alignRight="#+id/textView1"
android:layout_alignEnd="#+id/textView1" />
</RelativeLayout>
</RelativeLayout>
And my MainActivity.java contains following code:
package meetutu.juspay.com.meetutu;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.*;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView logoImage;
private TextView textView1, textView2;
private ViewGroup container;
public static final int STARTUP_DELAY = 300;
public static final int ANIM_ITEM_DURATION = 1000;
public static final int ITEM_DELAY = 300;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus)
animation();
super.onWindowFocusChanged(hasFocus);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void animation()
{
logoImage = (ImageView) findViewById(R.id.meetutuImageView);
container = (ViewGroup) findViewById(R.id.container);
ViewCompat.animate(logoImage)
.translationY(-200)
.setStartDelay(STARTUP_DELAY)
.setDuration(ANIM_ITEM_DURATION)
.setInterpolator(
new DecelerateInterpolator(1.2f)).start();
for(int i = 0; i < container.getChildCount(); i++)
{
View v = container.getChildAt(i);
ViewPropertyAnimatorCompat viewAnimator;
if(!(v instanceof Button))
{
viewAnimator = ViewCompat.animate(v)
.translationY(50).alpha(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(1000);
}
else
{
viewAnimator = ViewCompat.animate(v)
.scaleY(1).scaleX(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(500);
}
viewAnimator.setInterpolator(new DecelerateInterpolator()).start();
}
}
}
style.xml contains the following code:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.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>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
I'm new to Android, so I don't know much about types of animation and how they can be implemented. I want to achieve the animation and smoothness given in the link. Please let me know what else I should add to my code to make it work like that.
Thank you for your time!!
Here is example :-
Use these xml and put these 4 into drawable/anim folder
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1"
android:startOffset="500" >
</scale>
move_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="10%p"
android:toYDelta="0%p"
android:duration="800"
android:startOffset="0" />
</set>
move_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="-15%p"
android:toYDelta="0%p"
android:duration="500"
android:startOffset="300"/>
</set>
move_down_slow.xml
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="-10%p"
android:toYDelta="0%p"
android:duration="300"
android:startOffset="500" />
</set>
Use this code for the animation in Oncreate() method..
Declare it as globally ......
private Animation animation,animation1,animation2,animation3,animation4;
and this is code ......
animation3 = new AlphaAnimation(0.0f, 1.0f);
animation3.setFillAfter(true);
animation3.setDuration(1200);
mLinear.startAnimation(animation3);
mText.setText("Meetutu");
mText1.setText("The joy of meeting an expert teacher");
mText2.setText("for a craving learner");
animation = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_up);
mText.startAnimation(animation);
animation1 = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_down);
mText1.startAnimation(animation1);
animation2 = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_down_slow);
mText2.startAnimation(animation2);
animation4 = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
mButton.startAnimation(animation4);
NOTE:- mText,mText1,mText2 is as TextView , mButton is Button and mLinear is Parent root in the layout.xml file.....
enjoy coding......

PostExcute custom dialogue image not changing?

For some reason this isn't working with a dialog. I've used the showImage method loads of time before in this app via postExecute and its worked fine. However its not showing the image it should when doing it this way. Am I missing something? At the moment I've just got the image URL hardcoded to test, but later it will be passed to this method from doInBackground.
protected void onPostExecute() {
Dialog liveView = new Dialog(myView.this, R.style.Dialog);
liveView.setContentView(R.layout.liveview_dialogue);
TextView title = (TextView)liveView.findViewById(R.id.liveViewTitle);
Button button = (Button) liveView.findViewById(R.id.liveViewButton);
ImageView imgDisplay= (ImageView)liveView.findViewById(R.id.liveViewImage);
showImage("http://mytest.com/test.jpg", imgDisplay);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
liveView.show();
dialog.cancel(); //used to cancel the previous progress dialog.
}
My xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/liveViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip"
android:text="Tracking"
/>
<ImageView android:id="#+id/liveViewImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<Button
android:id="#+id/liveViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dip"
android:text="OK" />
</RelativeLayout>
My theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
Show image method:
public void showImage(String url, ImageView passed){
int loader = R.drawable.ic_launcher;
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(url, loader, passed);
}
ImageLoader.class: http://pastebin.com/Z2BvvzXk
MemoryCache.class: http://pastebin.com/bJUwPs45
FileCache.class: http://pastebin.com/0XHBawd0
Utils.class: http://pastebin.com/KXbXRVC2

Android - Styling Spinner within TabView

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>

Categories

Resources