Android application background colour - android

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

Related

SwitchCompat not showing thumb image on Android 5.0 lollipop emulator

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

Textview on(above) Button, doesn't work in Android 5(API 21)

It looks like a bug in 5 Android(API 21). I need a textview on button, textview should placed above the button. It works correct on Android 4.1(API 16) and incorrect on 5 Android(API 21). There are Screenshots and code:
Android 4.1 - It is correct, red textview above the button
Android 5 - it is incorrect, red textview under the button!
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlBottom"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#00FF00">
<Button
android:id="#+id/bVio"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="VIO"></Button>
<TextView
android:id="#+id/bVio_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textSize="12dip"
android:textColor="#FFFFFF"
android:background="#drawable/rounded_textbox"/>
</FrameLayout>
</RelativeLayout>
rounded_textbox - it is just shape... if remove background, all looks same, textview under button in 5 android.
Please, advice!
Yes. It is big chnage in Android L(API 21). There is new thing - Elevation, it is something like z-index in HTML. So to fix this bug you need use android:elevation="100dp" OR android:translationZ="100dip" for view that should be on top. So correct code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlBottom"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#00FF00">
<Button
android:id="#+id/bVio"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="VIO"></Button>
<TextView
android:id="#+id/bVio_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textSize="12dip"
android:textColor="#FFFFFF"
android:background="#drawable/rounded_textbox"
android:elevation="100dp"/>
</FrameLayout>
</RelativeLayout>
You need to wrap your button in a layout separately from the TextView, I used FrameLayout to wrap it and make sure your TextView is under the button's layout and it should work.

FrameLayout content order

I'm learning Android and I was trying out FrameLayout containing ImageViews, I tried to do a little app that switchs between two images when you click on them the code is the following:
My xml looks like this:
>
<FrameLayout 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"
tools:context=".Hola" >
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1" />
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2" />
</FrameLayout>
And my main program:
public class Hola extends Activity {
ImageView primera;
ImageView segunda;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teta_layout);
primera = (ImageView) findViewById(R.id.primera);
segunda = (ImageView) findViewById(R.id.segunda);
}
public void metodoprimera (View view){
primera.setVisibility(View.GONE);
segunda.setVisibility(View.VISIBLE);
}
public void metodosegunda (View view){
segunda.setVisibility(View.GONE);
primera.setVisibility(View.VISIBLE);
}
}
This program should show an image and as soon as you click on it it should hide that image and show the other and so on.
The thing is that this won't work , but as soon as I switch the imageview order in the xml, it works, and i don't really understand why it should not work this way.
Thank you guys in advance
Try adding:
android:visibility="gone"
to the XML for one of the ImageView's.
I'll try to go more explicit with the explanation, here is the thing:
If I have the XML like this it will show the images (depending on visibility) but the oClick thing will not work:
<FrameLayout 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"
tools:context=".Hola" >
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1"
android:visibility="gone" />
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2"
android:visibility="visible" />
</FrameLayout>
If I swap the ImageView's order in the XML It will work perfectly:
<FrameLayout 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"
tools:context=".Hola" >
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2"
android:visibility="visible" />
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1"
android:visibility="gone" />
</FrameLayout>
It's not about the visibility of each Imageview since it does not matter what value I put on them It won't work the first way but i will work the second way.
Seems like a weird issue since the oder of the imageviews should not matter if you set them visible or gone ...

How to set alpha value for my Relative layout? [duplicate]

This question already has answers here:
Set Alpha/Opacity of Layout
(5 answers)
Closed 3 months ago.
Hi I'm trying to set alpha value for my relative layout but, i am getting error how to solve this help me.....
I have three layout in my xml layout 1st layout using for background 2nd layout using for header 3rd layout using for footer. I wish to set alpha value 2 & 3rd layout so i am trying many ways still i have no idea please tell how to set alpha value
xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:background="#drawable/blue">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60px"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:background="#drawable/gradient_black"
android:id="#+id/ttest">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="6dp"
android:layout_toRightOf="#+id/imageView1"
android:text="settings"
android:textColor="#color/white"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55px"
android:background="#drawable/gradient_black"
android:gravity="bottom"
android:orientation="horizontal"
android:layout_alignParentBottom="true" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ssss"
/>
</RelativeLayout>
</RelativeLayout>
code:
public class DesignActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
ImageView imgHead = (ImageView)findViewById(R.id.imageView1);
ImageView imgbottom = (ImageView)findViewById(R.id.imageView2);
imgbottom.setImageResource(R.drawable.back);
imgbottom.setLayoutParams(new RelativeLayout.LayoutParams(width/8, height/8));
imgHead.setImageResource(R.drawable.b);
imgHead.setLayoutParams(new RelativeLayout.LayoutParams(width/8, height/8));
// RelativeLayout relative = (RelativeLayout)findViewById(R.id.ttest);
}
}
RelativeLayout rl; ...
rl.setAlpha(0.5F);
<RelativeLayout
android:id="#+id/rl"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/bg"
android:alpha="0.5">
In these cases I generally tend to want to set the colour and alpha at the same time, so I just use rl.setBackgroundColor(0xAACCCCCC); where A is alpha value and C is colour, in hex format.
e.g: rl.setBackgroundColor(0x88000000); for 0.5 transparent black background.
or in XML: android:background="#88000000"

Make custom title view from defaults on Android

How do I use androids default window title style to make my own similar TextView?
I have done a lot of guessing and made a TextView that has everything that
the default title bar has, except for the text shadow (and some padding/margins etc. I think).
Here's essentially what I've tried:
MainUI.xml
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
}
title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:textAppearance="#android:style/TextAppearance.WindowTitle"
android:background="#android:drawable/title_bar"
android:text="This is my new title" />
Edit:
I found some interesting related articles at makemachine and anddev.
Although I don't like it, I copied some attributes from the implementation style.xml.
Is there any way to avoid copying the attributes in this static way?
The following rendered almost perfect, the difference is in fact that the original did "cut" the first 2-3 pixels of the title shadow, while my TextView doesn't.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center_vertical"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
android:singleLine="true"
android:textAppearance="#android:style/TextAppearance.WindowTitle"
android:background="#android:drawable/title_bar"
android:text="This is my new title" />
It's also important to override the default android:windowTitleBackgroundStyle with a transparent color, because the default includes some padding etc. that you don't want to be wrapping your custom title bar.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#android:color/transparent</item>
</style>
</resources>
Remember to enable the theme in your AndroidManifest.xml
try this it worked for me:
put the rest of the code in place..
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.main_header);
TextView home = (TextView) findViewById(R.id.home);
if ( home != null ) {
/* your code here */
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// the action that u want to perform
Menu.this.finish();
Intent i= new Intent(Main.this,Destination.class);
startActivity(i);
}
});
}
create a main_header layout file
<?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" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Main Menu"
android:textStyle="bold"
android:textSize="10pt" />
<TextView
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:textStyle="bold"
android:text="Home"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
</LinearLayout>
here when we execute the code it will show a custom title bar where there are two text's in it and when we click on that it will go the next class ( to your destination class). we can also change the color , size font etc.. and also add buttons etc to the custom title bar.

Categories

Resources