Can't make the Window of my Activity translucent - android

I'm trying to make my Activity translucent, i.e. see-through, but it's not working. It's always opaque. That's my code so far:
public class SvetlinTranslucentActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window w = getWindow();
w.setFormat(PixelFormat.RGBA_8888);
w.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
w.setBackgroundDrawable(new ColorDrawable(0x00FF0000));
}
}
I've been looking at the code from the official API demos but still no success.

I use android:background in my layout
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#D0000000">
and its certainly see through, I can see the other activity below it.

I would go for the AndroidManifest.xml, look for the target activity, and set: android:theme="#android:style/Theme.Translucent"

Related

Android WebView re-rendering UI after changing orientation.

In My application i am setting orientation of the application on button click, using setRequestedOrientation(), My application has one webview which displays local web page.
I am using solution as mentioned in below link, to stop reloading of page when orientation changes.
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
Only difference is that i am not having onSaveInstanceState and onRestoreInstanceState in my activity.
What happens in my case is that, my web view does not reload when i change the orientation but it just re-renders the UI upon changing the orientation.
It happens like it first displays the UI and then it displays the white blank screen for few seconds and then again it displays UI.
How can i workaround it?
Once upon a time I used their implementation however I after reading up on the Android documentation I realized that much of their code was pointless since registering for config changes (in the manifest) means that your activity won't be destroyed as it normally is.
Long story short, you don't need to use a FrameLayout as a container for your webView, you may not need onConfigurationChanged, etc. If I get some time I will post some code as an example.
For now, make sure you have the following in your manifest file
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
instead of
android:configChanges="keyboard|keyboardHidden|orientation"
since on orientation change devices may get get a screenSize change
EDIT This is what I have done (skimmed from one of my projects so I may have missed minor details)
in AndroidManifest.xml
<activity android:name=".Activities.WebWrapperActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"/>
web_wrapper_activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView android:id="#+id/web_wrapper_activity_web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="outsideOverlay"/>
</LinearLayout>
The Activity (or a portion of it)
public class WebWrapperActivity extends Activity {
private WebView _webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_wrapper_activity_layout);
init();
}
private void init() {
_webView = (WebView) findViewById(R.id.web_wrapper_activity_web_view);
_webView.setScrollbarFadingEnabled(true);
_webView.getSettings().setLoadsImagesAutomatically(true);
_webView.getSettings().setJavaScriptEnabled(true);
_webView.getSettings().setAllowFileAccess(true);
_webView.getSettings().setSavePassword(false);
_webView.getSettings().setPluginState(WebSettings.PluginState.ON);
_webView.loadUrl(“http://www.google.com”);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
_webView.saveState(outState);
}
}
As i mentioned previously, you may not need onConfigurationChanged if your webView is the only view in the Activity.
If you have any issues let me know

The LinearLayout returned is null in the subclass of PreferenceActivity

I have a SettingsActivity which is a subclass of PreferenceActivity in my library project
The oncreate method looks like this
addPreferencesFromResource(R.xml.preferences);
setContentView(R.layout.main);
The preference.xml file has structure like element PreferenceScreen -> PreferenceCategory -> Preference
My main main.xml Looks like this
<?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:orientation="vertical">
<!--Listview will be replaced by preferences -->
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The above class SettingsActivity is subclassed in another project and Looks like this
public class SettingsActivityFree extends SettingsActivity
{
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//...
// layout returned will be null ..
LinearLayout layout = (LinearLayout)findViewById(R.layout.main);
// Add the adView to it
layout.addView(adView);
}
The problem is I am trying to get LinearLayout from the parent class so that I can add my stuff in it, but for some reason it returns null , the reason I have LinearLyout in the SettingsActivity class is because I want to put some ads etc at the top of preferences and without putting ad specific code in the Library project
Please advise If I am missing something here,
Thanks
First you need to give your LinearLayout an id using android:id="#+id/whatever". Then you can search for it using findViewById(R.id.whatever).
You should really check the developer site. You are not doing it correctly. Look up the documentation on ListView and ListActivity. Learn how it works before using it.
Also as a hint:
You don't do this (LinearLayout)findViewById(R.layout.main);
When retrieving a view, you will find it in R.id.(whatever) not in R.layout.(whatever)

Using GlSurfaceview in activity

I have an Activity and i had set activity's content view as "R.layout.main.xml".And i have an another class which contains animation created using openGL. Now i need to use this animation in the background of the Activity.
The code is like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pixie);
mGLView = new ClearGLSurfaceView(this);
setContentView(mGLView);
}
But my app is Crashing ..How can i solve this.
When you call the setContentView() a second time, you replace what had been set the first time, leaving you with only the background. The crash is most likely because you depend on the elements in the main layout, which is removed.
Rather than calling setContentView() twice, you should include the GLSurfaceView in the main layout. Below is an example of how this can be done:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent>
<your.application.package.ClearGLSurfaceView
android:layout_width="match_parent"
android:layout_width="match_parent"/>
<!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie-->
</FrameLayout>
Then you can load this layout in your onCreate() as usual (main_pixie_new refers to the above xml, I just gave it that name to keep things as clear as possible):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pixie_new);
}

How can I access views in activities within a TabActivity?

I've got a TabActivity containing other activities intended to split up a form. The TabActivity has in its layout a button intended to collect the data from all the form-related views across all the activities contained within the TabActivity and store it. The problem I'm running into is that the TabActivity doesn't appear to have access to these views; when I call findViewById() with one of them, I get a NullPointerException.
The documentation seems sparse about exactly how TabActivity works with respect to controlling the activities it contains. If it destroys an activity when switching from it to a different one, the situation I'm in would make sense. I'd like to know the best approach for accomplishing the goal described above.
src/com/vendor/MyTabActivity.java:
public class MyTabActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_tab_activity);
final Button saveButton = (Button) findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// NullPointerException happens here
String fieldValue = ((TextView) findViewById(R.id.text_field)).getText().toString();
}
});
}
}
res/layout/my_tab_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost">
<LinearLayout>
<TabWidget android:id="#android:id/tabs"/>
<FrameLayout android:id="#android:id/tabcontent" />
<Button android:id="#+id/save_button"/>
</LinearLayout>
</TabHost>
src/com/vendor/NestedActivity.java:
public class NestedActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nested_activity);
}
}
res/layout/nested_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout>
<EditText android:id="#+id/text_field"/>
</RelativeLayout>
</ScrollView>
Your problem comes from these two lines in MyTabActivity...
setContentView(R.layout.my_tab_activity);
...and...
String fieldValue = ((TextView) findViewById(R.id.text_field)).getText().toString();
...although you obviously know the findViewById(R.id.text_field) is what's causing it.
Using findViewById(...) only works when trying to access UI elements which have been inflated as part of your current Activity. As there isn't a TextView with the resource id of R.id.text_field in the my_tab_activity.xml, it's never going to work.
Accessing activities which are tab content from the TabHost / TabActivity is tricky. My suggestion would be to use SharedPreferences which can be accessed from everywhere in your app. Once a TextView (or any other user-input item) is changed, save it to a SharedPreferences using a 'key' which identifies which activity/tab it came from. From then on, the TabActivity can collate the data easily.
You can get a reference to activities running inside of the tab activity using getLocalActivityManager() or getCurrentActivity(). For the activity object you get back you can do activity.findViewById() to get a reference to a view inside of the specific activity. But to point out TabActivity has been deprecated and you should be using Fragments to do what you are looking for. If you are targeting a version of Android earlier than 3.0 you can use the compatibility library to access fragments.

Simple activity switch not working. No Errors

I have a basic calculator app I'm making. Two activities, the main one and ResultView.
I've made it where I click a button on activity A to go to activity B. The log says activity B is started and "displayed" successfully, the title for the new activity loads, but the body does NOT show. I added a simple Text view with static text.. see the result.xml at the bottom. I also tried inserting information programmatically, but that didn't do.
When I debug the program, I tried putting breakpoints as the activity is called with startActivity() as well as on the first line of the onCreate method within the ResultView class (my activity "B") but the program never hits the second breakpoint. In fact, it looks as if Looper.class is called in the end.
This bit of code is placed in the button handler on acitivity A:
i.putExtra("test1",34);
i.putExtra("test2",35);
this.startActivity(i);
This in the onCreate function in activity B:
public void OnCreate(Bundle
savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
The activity is in the manifest, within the "application" tag:
<activity
android:name="ResultView"></activity>
If I didn't supply enough info, let me know.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llParent"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="HELLO WORLD"
/> </LinearLayout>
If more info is needed, let me know...in short, "HELLO WORLD" does not display at all.
It's not OnCreate, it's onCreate (lowercase o). Otherwise the method won't be overriden. The #override annotation has no effect if it's omitted, it's just for readability for the programmer.
Are you sure that the public void line or the line before that contains #Override? If not, you're not overriding the OnCreate method. The code should read
#Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
EDIT
Of course the "O" must not be a capital "O"...

Categories

Resources