SwitchCompat not showing thumb image on Android 5.0 lollipop emulator - android

I read about the new SwitchCompat that has been introduced to implement the Switch widget in Android 5.0. I tried using the same but I am not able to see the drawable thumb image as seen in below image.
My XML code is as follows,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<android.support.v7.widget.SwitchCompat
android:id="#+id/sampleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showText="false"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/action" />
<TextView
android:id="#+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/sampleSwitch"
android:layout_marginTop="22dp"
android:text="#string/status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
I am able to see the thumb image in preview design (graphical layout tab in eclipse) for the above layout but when I run my code I dont see the image.
Preview design
This is the exception I get
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
Please can someone help solve the problem?

This is a known issue and you should provide the thumb and track:
android:thumb="#drawable/thumb"
android:track="#drawable/bg"
or
SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.sampleSwitch);
switchCompat.setThumbResource(R.drawable.apptheme_switch_thumb_holo_light);
switchCompat.setTrackResource(R.drawable.apptheme_switch_track_holo_light);
you can use this link to customize it.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<android.support.v7.widget.SwitchCompat
android:id="#+id/sampleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textOff="OFF"
android:textOn="ON"
android:text="Toggle Me"
android:clickable="true"
android:checked="true" />
</RelativeLayout>
And the codes
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.sampleSwitch);
switchCompat.setThumbResource(R.drawable.apptheme_switch_thumb_holo_light);
switchCompat.setTrackResource(R.drawable.apptheme_switch_track_holo_light);
}
}

It's a bug with corrupt file in drawable-hdpi on AppCompat
https://code.google.com/p/android/issues/detail?id=78262
To fix it, juste override it with this 2 files
https://github.com/lopespm/quick-fix-switchcompat-resources
Add it on your directory drawable-hdpi
XML
<android.support.v7.widget.SwitchCompat
android:id="#+id/dev_switch_show_dev_only"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
And nothing was necessary on Java

Related

plaese can anyone help me to draw radial seekbar

Radial SeekBar
Please help me to how to draw a Radial SeekBar, Plaese check the image url that is actual design of seek bar. I tried many ways but not worked out properly.
I tried like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
tools:context="com.scanner.com.protracterview.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/number"
android:textSize="18sp"
android:layout_marginBottom="30dp"
android:gravity="center"/>
<com.goodiebag.protractorview.ProtractorView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:arcColor="#91d8f7"
app:arcProgressColor="#91d8f7"
app:tickColor="#4a4a4a"
app:indicatorType="line"
app:tickProgressColor="#abe6"
app:textColor="#000"
app:textProgressColor="#FF0000"
app:angleTextSize="0dp"
app:arcWidth="2dp"
app:progressWidth="2dp"
app:tickOffset="18dp"
app:tickLength="0dp"
app:angle="15"
android:id="#+id/protrat"
app:tickIntervals="15"
app:ticksBetweenLabel="three"
app:touchInside="true"
app:roundEdges="true"
android:layout_below="#+id/number"
android:layout_alignParentEnd="true" />
If you want something like this -
Follow this - (Matt Joseph)
There is a class CircularSeekBar.java paste it in your project
attrs.xml (in res/values/attrs.xml)
and your xml file should be like this,
<mrbrown.arcseekbar.CircularSeekBar
android:id="#+id/circularSeekBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:start_angle="180"
app:end_angle="0"
app:circle_x_radius="500dp"
app:circle_y_radius="500dp"
app:use_custom_radii="true"
app:progress="0"
app:max="100"
app:pointer_alpha_ontouch="100"
app:pointer_color="#0174DF"
app:pointer_halo_color="#880174DF"/>

Why doesn't Eclipse detect new onClick method I've added in my layout file

Here is my full layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="showButtons"
android:text="#string/buttons_label" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:onClick="showSpinners"
android:text="#string/spinners_label" />
</RelativeLayout>
And here is where I call showSpinners:
private void showSpinners(View clickedButton){
goToActivity(SpinnerActivity.class);
}
Eclipse underlines in yellow showSpinners by saying it's never used locally, my guess is it is not detecting my onClick attribute in the layout file.
I'm wondering why it just wont work and what I should do to make it work...
Change the your method to :
public void showSpinners(View clickedButton) {
// do the work
}
The method needs to be visible outside the class (source).

How to use custom iPhone tab in FragmentActivity?

I am new in Android and I tried to create a tab using FragmentActivity from codes that I found online.
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
This is my FragmentActivity http://pastie.org/pastes/5170802/text?key=jhowuevxe2fshlwu5tisg
I would like to use a custom layout that uses an image and text.
//tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:background="#drawable/tab_indicator"
android:padding="5dp">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/icon"/>
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"/>
</RelativeLayout>
I found a similar question here : create custom tab in FragmentActivity but I couldn't figure it out how to apply the solution in my codes. Can someone teach me how? Thank you.
Update:
I managed to inflate the custom layout in my codes. But I faced another error. This is my latest codes http://pastie.org/pastes/5187362/text?key=74r87diquysvruwsam1tq tweaked to FragmentActivity, from AdilSoomro http://adilsoomro.blogspot.com/2011/06/iphone-like-tabs-in-android.html codes (which uses TabActivity) with some references from
http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
My latest codes successfully inflate the layout like this, with the setting http://pastie.org/pastes/5187408/text?key=qxxa5xxrhsburebllyhmw for its layout (tab_indicator.xml)
but I need to align my tab to the bottom. When I align it to the bottom, the code layout_gravity="bottom" worked on Graphical Layout but when I run it, my relative layout's background fill the whole screen, like this with the code http://pastie.org/pastes/5187445/text?key=6dz2tsiggey9se51d2thtq
can someone tell me what I did wrong?
Solved it! Finally! After testing other resources that I referred to, I realized that layout_alignParentBottom in RelativeLayout does not give any outcome. Hence I tried aligning the TabWidget to the bottom instead, and it worked!
This is my main.xml that contain TabHost and TabWidget.
http://pastie.org/pastes/5188297/text?key=lqfp3jnofs5kgsvslm3yg
Keep the tab_indicator.xml / the settings for the text and image like this : http://pastie.org/pastes/5187408/text?key=qxxa5xxrhsburebllyhmw
And here is the a custom iPhone tab that uses FragmentActivity :
P/S : Sorry if my Java file is confusing, I'm new and if there is anything I did wrong, do tell me.
try this Honey >
<?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="55dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="bottom"
android:background="#android:drawable/title_bar"
android:padding="5dp">
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"/>
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_search" />
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="49dp"
android:src="#drawable/ic_action_search" />
</RelativeLayout>

This RelativeLayout layout or its LinearLayout parent is useless

I'm developing an Android 3.1. and above application.
On one activity I generate its layout dynamically. I'm using formdetail.xml as contentview:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.formdetail);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
currentFormId = extras.getString("FormId");
}
}
formdetail.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
downloadFormsButton and formErrorMsg must be always in form.
But using that xml I get this warning:
This RelativeLayout layout or its LinearLayout parent is useless
I need linearLayout to add TableLayouts programmatically.
How can I solve this warning?
Lint is correct in it's warning, your LinearLayout isn't doing anything useful, since its only child is a RelativeLayout. Remove the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" />
</RelativeLayout>
You either ignore it or right click your project. Click Build Path->Configure Build Path.... Under Android Lint Preferences look for UselessParent and set it's severity to ignore or click Ignore All.
EDIT:
I take that last part back. I think Ignore All will wipe all Lint warnings and errors.
I was also having the same issue with my app layout.
I don't know how and it is right way or not, but this get resolved after setting background attribute to both layouts.
Now there is no warning and working perfect. Hope it will also work for you.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#drawable/bg"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg2"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/img_BookImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:contentDescription="#string/India"
android:src="#drawable/india" />
</FrameLayout>
</FrameLayout>
That is just an warning from lint. Lint doesn't know you would later append views to this layout and it will warn you that you added an unnecessary extra view(increasing your layout depth).
You should just ignore the warning(If it really bothers you here is a link on how to remove warnings from lint).

Android application background colour

I have my main.xml up and in the XML code I have:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<ImageView android:id="#+id/imageView1" android:layout_height="wrap_content" android:src="#drawable/logo" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="116dp"></ImageView>
<TextView android:layout_height="wrap_content" android:text="Administration 0.0.1" android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_below="#+id/imageView1" android:layout_centerHorizontal="true"></TextView>
</RelativeLayout>
But when I run the app in an emulator, the default theme still shows with the black background. I can't figure out why it's doing this.
Any ideas?
What background color are you expecting? It shows up correctly for me if you're expecting white. This is what the screen looks like (I have substituted your image with the android icon)
Does your onCreate() look like this -
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

Categories

Resources