cannot find symbol variable imageView2 - android-studio

I am just starting with Android Studio. I got some code from the web on how to ass a splash screen to my app, but it has compiling errors. I could use some help.
the errors are:
error. can not find symbol variable imageView2
error. can not find symbol variable s_img
error. can not find symbol variable s_image_black
error. can not find symbol variable s_image_black
I know this is due to my lack of knowledge, but I am just starting and trying to use this example to learn. Any help would be much appreciated.
Cheers
Paul
package org.quaestio.kotlinconvertedwebview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.ImageView;
import org.quaestio.kotlinconvertedwebview.MainActivity;
import java.util.Random;
public class Splashscreen extends Activity {
Thread splashTread;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
imageView = (ImageView)findViewById(R.id.imageView2);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
int[] ids = new int[]{R.drawable.s_img,R.drawable.s_image_black, R.drawable.s_image_black2};
Random randomGenerator = new Random();
int r= randomGenerator.nextInt(ids.length);
this.imageView.setImageDrawable(getResources().getDrawable(ids[r]));
splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
// Splash screen pause time
while (waited < 3500) {
sleep(100);
waited += 100;
}
Intent intent = new Intent(Splashscreen.this,
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
Splashscreen.this.finish();
} catch (InterruptedException e) {
// do nothing
} finally {
Splashscreen.this.finish();
}
}
};
splashTread.start();
}
}
activity_splashscreen below
<?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="#feffc3"
android:layout_gravity="center"
android:id="#+id/lin_lay"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/splash"
android:background="#drawable/splash_img" />
</LinearLayout>

The problem is that you're setting an id wich don't correspond with the xml file. For example imageView = (ImageView)findViewById(R.id.imageView2); you need to change imageView2 with your xml imageview id wich is "splash". For the pthers error you need to check that files in the drawable folder.
Hope it was helpful

The ImageView in your xml has id splash, so in your code, instead of:
imageView = (ImageView)findViewById(R.id.imageView2);
you should do
imageView = (ImageView)findViewById(R.id.splash);
Also, this line:
int[] ids = new int[]{R.drawable.s_img, R.drawable.s_image_black, R.drawable.s_image_black2};
assumes that in your drawable folder there exist all these drawables:
s_img, s_image_black, s_image_black2
but apparently they don't, so you have to copy or create them and put them in the drawable folder.

Related

Create Android splash screen using SVG animation

I am developing an app using Xamarin.Forms and I am trying to insert the splash screen to my Android project.
I found a few tutorials for creating a splash screen with a background color and a static png image, but I want to use my svg animation as splash screen. I thought I could follow a tutorial for static image and just replace the png image with the svg animation, but it didn't work. Here's what I have so far:
On SplashActivity.cs:
[Activity(Label = "SplashActivity", Theme = "#style/Theme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
}
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
async void SimulateStartup()
{
await Task.Delay(5000);
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
On MainActivity.cs:
// I only changed the MainLauncher property to false
[Activity(Label = "MyApp", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
...
}
On styles.xml (in the Xamarin.Android project):
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">#drawable/desenhando5s</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="colorPrimaryDark">#004632</item>
</style>
When I run the application, it only shows a black screen as splash screen and then shows my login page as always.
Can anybody tell me what I have to do to set my animation as the splash screen?
(FYI: in case anyone wants to know, I created the animation using SVGator)
You could use FFImageLoading to load your svg image in your SplashActivity instead of set it in styles.xml.
Splash screen:
<?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">
<FFImageLoading.Views.ImageViewAsync
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Code:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.layout5);
var filePath = "check";
var imageView = FindViewById<ImageView>(Resource.Id.imageView);
ImageService.Instance.LoadCompiledResource(filePath).WithCustomDataResolver(new SvgDataResolver(64, 0, true)).Into(imageView);
}
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
async void SimulateStartup()
{
await Task.Delay(5000);
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
Updated:
Please check the screenshot. The .svg image is in the drawable folder. The layout5 is the splash screen in the layout folder.

Error inflating class com.inmobi.ads.InMobiBanner

I followed the InMobi guidelines for Android apps step by step but got the following exception from
DataBindingUtil.setContentView(this, R.layout.mainActivity):
Binary XML file line #670: Binary XML file line #670: Error inflating
class com.inmobi.ads.InMobiBanner
<LinearLayout
xmlns:inmobi="http://schemas.android.com/apk/lib/com.inmobi.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView android:text="Ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.inmobi.ads.InMobiBanner
android:id="#+id/bannerAdInMobi"
android:layout_width="320dp"
android:layout_height="50dp"
inmobi:placementId="plid-1526902340491"
inmobi:refreshInterval="60" />
</LinearLayout>
Could anyone offer a tip on how to diagnose it?
Since no one answered after a month, let me post mine.
InMobil's support has kindly helped me find the culprit - layout inflation before InMobiSdk.init(). An app needs to run InMobiSdk.init() before inflating the layout.
My app cannot do this because it uses InMobil ads only under certain conditions (e.g. only the FREE version has ads), so I have removed com.inmobi.ads.InMobiBanner from the layout, and add it in Java code when it is needed.
Ok I Solved this problem . just follow these steps ->
Step 1: Declare the variable in your Activity/fragment
private lateinit var bannerAd: InMobiBanner
Step 2: Initialite InMobiSDK in onCreate()
override fun onCreate(savedInstanceState: Bundle?) {
bannerAd= Helper.InitializInMobiAds(requireContext())
super.onCreate(savedInstanceState)
}
Step 3: here is InitializInMobiAds() method in Helper Class
fun InitializInMobiAds(context: Context): InMobiBanner{
val consentObject = JSONObject()
try {
// Provide correct consent value to sdk which is obtained by User
consentObject.put(InMobiSdk.IM_GDPR_CONSENT_AVAILABLE, false)
// Provide 0 if GDPR is not applicable and 1 if applicable
consentObject.put("gdpr", "0")
// Provide user consent in IAB format
// consentObject.put(InMobiSdk.IM_GDPR_CONSENT_IAB, “ << consent in IAB format >> ”)
} catch (e: JSONException) {
e.printStackTrace()
}
InMobiSdk.init(context, "AcountIdHere" , consentObject, SdkInitializationListener() {
#Override
fun onInitializationComplete(error : Error?) {
if (null != error) {
Log.e("", "InMobi Init failed -" + error.message.toString())
} else {
Log.d("", "InMobi Init Successful")
}
}
})
return InMobiBanner(context, PLID_here)
}
Step 4: now use .load() method in onCreateView() or in whichever block which runs after onCreate()
binding.adView.load()

Facebook like and comment not working on Android webview

I have tried webview with Android studio and FB share and other social media share buttons are working fine. But FB direct like and comment button on my website is not working. When I press like button I can see white screen with left top screen number "1". Please kindly help me.
MainActivity.Java
package com.example.neermaicom;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import static android.content.Intent.*;
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
} catch (Exception e) {
return true;
}
}
});
webView.loadUrl("http://www.neermai.com");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
//This method require to use back button if want to go previous web page
#Override
public void onBackPressed() {
if(webView.canGoBack()){
webView.goBack();
}else {
super.onBackPressed();
}
}
Activity_main.xml
<?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"
tools:context=".MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
This is due to the Facebook user auth, unfortunately android webview is not fully compatible with auth systems, an alternative that I use in my webview applications is to use "Chrome Custom Tabs" to auth access to the system of comments that I use.
In my application when the user clicks on comment, a Google Chrome tab is opened inside my application showing the comments page, when the user is not logged, the chrome auth the user without having to close or minimize my application to open google chrome. Everything is done within my own application.
To start the Chrome Tab when I added an "Intent" to shouldOverrideUrlLoading, below is an example of my code.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("#comments")){
Uri uri = Uri.parse(url);
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.build().launchUrl(PostActivity.this, uri);
return true;
}
else {
view.loadUrl(url);
return true;
}
}
And in Gradle app:
implementation 'androidx.browser:browser:1.3.0-alpha01'
My url for comment pages has "#comments" you change to something standard that you have in your comment page URLs or if the facebook comment system is directly on the post page, put something that is standard in the url of your posts to open in the google chrome tab.

kotlin tcp socket client not working python server

I want to make an app that will connect to my python server using sockets.
When I press the connect button it does not even print the got connection on my PS, please help. Thank you
I have this basic code in kotlin:
//Kotlin Code
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import java.io.DataOutputStream
import java.net.Socket
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.start_page)
fun connect(v : View) {
try{
val soc = Socket("192.168.1.5", 1419)
val dout = DataOutputStream(soc.getOutputStream())
dout.writeUTF("1")
dout.flush()
dout.close()
soc.close()
}
catch (e:Exception){
e.printStackTrace()
}
}
}
The connect function is activated when clicked on a button, this is the xml code for my start screen
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<Button
android:id="#+id/connect"
android:layout_width="108dp"
android:layout_height="50dp"
android:layout_marginBottom="127dp"
android:layout_marginEnd="228dp"
android:layout_marginStart="256dp"
android:onClick="connect"
android:text="#string/connect"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
and this is my code in python server:
#Python code
import socket
s = socket.socket()
s.bind(('0.0.0.0', 1419))
s.listen(5)
c, addr = s.accept()
print ('Got connection from', addr)
code_encoded = c.recv(1024)
code_decoded = code_encoded.decode('utf-8')
print(code_decoded)
c.close()
s.close()
I fixed it by implementing asynctask in my function and used java instead of kotlin but it should work similarly in kotlin as well.
The function is now, like this,
class ServerConnection extends AsyncTask<MainActivity.ConnParams, Void, Void> {
#Override
protected Void doInBackground(MainActivity.ConnParams... params)
{
String ip = params[0].ip;
int port = params[0].port;
String message = params[0].message;
try
{
Socket socket = new Socket(ip, port);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.write(message);
printWriter.flush();
printWriter.close();
socket.close();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
}
Although asynctask is not good for tasks where you need it to be in background for longer periods of time, at which time I would recommend using android services.

Camera SurfaceView flicker in view pager while scrolling

In my App I am using ViewPager with FragmentStatePagerAdapter to display 4 different layout.
Layout 1 , 3 , 4 consists of ListView and 2nd layout contains SurfaceView Camera. Now when I am scrolling horizontally camera at the both edges get flicker.
I search on google, and find different solutions. like
1) giving minus margin with android:layout_gravity = "center".
2) viewPager.requestTransparentRegion(viewPager);
Here in first case, it works good at first time but when comming back from resume it cuts from right showing black rectangle.
also I have also tried with different parent layout but the same scenario
happened and also tried to give margin programmatically, but could not find any solution.
Here is my Camera xml.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="#+id/surface_view"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_marginRight="-100dp"
android:layout_marginLeft="-100dp"
android:layout_height="match_parent"
android:visibility="visible" />
</LinearLayout>
and here is my fragment adapter.
public class DashboardAdapter extends FragmentStatePagerAdapter {
public DashboardAdapter(FragmentManager fm) {
super(fm);
fragmentList = new ArrayList<>();
fragmentList.add(new InboxFragment());
fragmentList.add(new CameraFragment());
fragmentList.add(new ContactFragment());
fragmentList.add(new AddFriendsFragmnet());
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return InboxFragment.newInstance(position);
case 1:
return CameraFragment.newInstance(position);
case 2:
return ContactFragment.newInstance(position);
case 3:
return AddFriendsFragmnet.newInstance(position);
default:
return CameraFragment.newInstance(position);
}
}
#Override
public int getCount() {
return 4;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
}
Any help would be too much thankful.
I have replaced SurfaceView with TextureView to open a camera.
Link
Here is a link for an example of TextureView Camera. It's working now.
Thanks

Resources