Android Move Image When Tilting Phone - java

I have an image in my view and I want to move it when I tilt my phone. It should look like the IOS wallpaper, when you move your phone. Some Android phones have this too. Is there any library or a good tutorial for this? Or does somebody know how I can do this?
Thanks!

Recently... I found a library called gravity-view
This library moves the image based on the device.
Inside Layout XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<ImageView
android:id="#+id/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
</RelativeLayout>
Inside Activity or Fragment:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gravityView = GravityView.getInstance(this)
.setImage(bg, R.drawable.landingbg)
.center();
}
#Override
protected void onResume() {
super.onResume();
gravityView.registerListener();
}
#Override
protected void onStop() {
super.onStop();
gravityView.unRegisterListener();
}

Related

Activity is opening very slowly

for some reason my activity is opening slowly, do you think that you can help me here?
Now, this is not the only code in Java class, I have 30 buttons that are exactly the same like the one below, only their ID is different since I have 30 card views (widget) in the activity, here is the picture of the design. I also reduced the pictures to the right size for the card, but that did not help. May you please help me?
Java Class:
private Button crkvaspip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_zavrsenilista);
//Dashboard
crkvaspip = (Button) findViewById(R.id.crkvaspip);
crkvaspip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { openCrkvaSvetogPetraIPavla();
}
});
public void openCrkvaSvetogPetraIPavla(){
Intent intent = new Intent(zavrseni_category.this, bioskop_class.class);
startActivity(intent);
}
Android widget card view code:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="210dp"
app:cardCornerRadius="20dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/bioskop1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/thumbnail"
android:maxLines="1"
android:padding="8dp"
android:fontFamily="#font/baloo"
android:text="3D Bioskop"
android:textColor="#222"
android:textSize="20dp" />
<Button
android:id="#+id/crkvaspip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
UPDATE LOGCAT INFO CODE:
2022-08-28 20:21:51.524 4585-4629/com.visit.bp I/OpenGLRenderer: Davey! duration=4111ms; Flags=1, IntendedVsync=38769851610176, Vsync=38773584943360, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=38773585749700, AnimationStart=38773585821500, PerformTraversalsStart=38773586648100, DrawStart=38773756771500, SyncQueued=38773757001800, SyncStart=38773757217800, IssueDrawCommandsStart=38773825367800, SwapBuffers=38773938657200, FrameCompleted=38773963626600, DequeueBufferDuration=332000, QueueBufferDuration=535000,
If the emulator is slow, have you tried disabling your antivirus? Another possibility is to optimize the local images, there are programs such as the free website: https://www.iloveimg.com/ and speed up loading.
Another idea would be to distribute the images in other tabs or activities, because your activity may be overloaded, or load them through other libraries like picasso (I believe it has already been mentioned).

How do I hide or show layout elements when an Android Activity goes out of focus?

I am trying to hide certain elements in a layout when Android goes out of focus, such that when viewed in the "View recent applications" display, the layout elements will not be visible. Upon selecting the application and going into focus, these elements will be visible again.
My implementation below attempts to show the "content" layout when the application is in focus, and show the "overlay" layout when out of focus, via onWindowFocusChanged().
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the content page"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the overlay"/>
</RelativeLayout>
</FrameLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
FrameLayout layoutContainer;
RelativeLayout layoutContent;
RelativeLayout layoutOverlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutContainer = (FrameLayout) findViewById(R.id.container);
layoutContent = (RelativeLayout) findViewById(R.id.content);
layoutOverlay = (RelativeLayout) findViewById(R.id.overlay);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
layoutContent.setVisibility(View.VISIBLE);
layoutOverlay.setVisibility(View.GONE);
} else {
layoutOverlay.setVisibility(View.VISIBLE);
layoutContent.setVisibility(View.GONE);
}
}
}
enter code here
However, my implementation does not work. Anyone with any suggestion as to solve this? Thanks!
If you want to hide views in your layout in recent apps try like this
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);

Loading a webpage into an Android App

I'm working on an android app and I want to be able to load a webpage in the application. I've browsed around the internet for a while and can't seem to get it just right. I know that my internet connection is established, as I can use my browser. I'm new to android development and learning as I go, so any elaboration would be amazing. Here's the code I'm working with:
public class MainActivity extends Activity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Button btnBack;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
/** Opens the URL in a browser */
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
}
and my xml
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="#+id/rlayout1"
android:id="#+id/rlayout2">
<WebView android:id="#+id/webview1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</RelativeLayout>
Here's what happens when I run the application:
Wow guys, sorry if this is spammed but I just found a forum explanation that I didn't see before. Android requires internet permissions in an app for it to access the internet. I thought that it would give me a permissions exception, but instead it just doesn't load it. here's what I added to the xml manifest for it to work correctly
<uses-permission android:name="android.permission.INTERNET" />
Again, sorry I feel really stupid for not knowing about this. You'd think the console would print some sort of security/permission exception

Change background color of SearchView

I am trying to change the background color of my SearchView using both xml and code but none seems to be working.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
</SearchView>
</LinearLayout>
Code:
#InjectView(R.id.searchView) private SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
searchView.setBackgroundColor(Color.WHITE);
}
Why are these not working and how can I solve the problem?
Add -
searchView.setBackgroundColor(Color.WHITE);
Did you connect the XML file to the activity?
For instance -
setContentView(R.layout.**YOURXMLHERE**);

Android flash magazine application

Is it possible to create a magazine based on Android embedding flash in it?
As your question is very wilde and generic, I can only say "yes that's possible"!
Here is a link to embed your swf file into an Android application.
Load an SWF into a WebView
public class Test3Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="file:///android_asset/qualibus.swf";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl(url);
}
}
main.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"
>
<WebView android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
Result:

Categories

Resources