Android Studio - MainActivity.java can't see id attributes - android

I gave an id attribute:
android: id="#+id/animation_layout"
in the default layout (numbered as 1) in the activity_main.xml
But when I tried to call it in the MainActivity.java by its id:
ConstraintLayout constraintLayout = findViewById(R.id.animation_layout);
It won't find it (numbered as 2)
What am I missing?
also attached the code:
<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"
android:id="#+id/animation_layout"
android:background="#drawable/gradients_list"
tools:context=".MainActivity">
and the Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Gradient animation
ConstraintLayout constraintLayout = findViewById(R.id.animation_layout);
AnimationDrawable animationDrawable = (AnimationDrawable) constraintLayout.getBackground();
animationDrawable.setEnterFadeDuration(2000);
animationDrawable.setEnterFadeDuration(4000);
animationDrawable.start();
Thank you for helping :)

It happens sometime since new android update! that you even provide ids in xml but still the java api of android does not fetch them! so you need to do this step!
1: clean the build removes the old built cache! and this should solve the problem
2: if that does not make your id read bale in java you can use the Rebuild option from the same menu!
3: if that does not work either you can go with the FILE-> invalidate cache and restart

Related

'Resource.Layout' does not contain a definition for 'activity_main' in xamarin

I am getting an error of 'Resource.Layout' does not contain a definition for 'activity_main'. But I am confused, why it is showing this type of error. Because activity_main is available in Resource.Layout, but still showing the same error.
Here is the code,
Resources => Layout => activity_main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:minWidth="25px"
android:minHeight="25px">
<android.webkit.WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView" />
</LinearLayout>
Resources => MainActivity.cs
[Activity(Label = "BSSTMatrimonial", MainLauncher = true)]
public class MainActivity : Activity
{
WebView webView = null;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
// webView = FindViewById<WebView>(Resource.Id.webView);
//webView.SetWebViewClient(new ExtendWebViewClient());
webView.LoadUrl("www.bsstmatrimony.com");
WebSettings webSettings = webView.Settings;
webSettings.JavaScriptEnabled = true;
}
}
Try to Clean & Rebuild your solution. Even you can delete & re-add .axml file.
If still won't work write click on .axml file
Go to properties -->Build action
Make sure Build action set to AndroidResource.
It is a harmless error that can be ignored but you can search your files for Resource.Designer.cs. It is likely that you have two, one in your project's obj/debug folder and one in your project's Resources folder. Check for activity_main in both, it is possible that the file in your Resources folder doesn't have it.
I've been encountering this problem a lot. I think the problem lies in the fact that as xamarin is updated, it changes the variable names and file names in the templates. So if you are using code from a tutorial, if you are seeing this problem, its likely outdated. This means that you have to rename something to fit the current template.
In this case I had to change
SetContentView(Resource.Layout.Main);
to
SetContentView(Resource.Layout.activity_main);

Xamarin:Unable to convert instance of type Android.Widget.LinearLayout to type Android.Support.V7.Widget.Toolbar

The app is running on Android 6 or below. But when running on Android 7 (Nougat) it throws runtime exception.
System.InvalidCastException: Unable to convert instance of type 'Android.Widget.LinearLayout' to type 'Android.Support.V7.Widget.Toolbar'.
The error is thrown in Xamarin (Visual studio 2017) at below code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle); //Runtime error here
.... Other code ...
}
}
I've taken latest updates for All Nuget packages but not able to identify the issue. For testing I'm using Moto G4+ (Android 7.0).
Can anybody help please?
Edit 1:
Layout of toolbar is as : (Under resources > layout > toolbar.axml). This was already there when App version with Android 6 released. But Now visited app back for Android 7 issues.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
I solved this by just cleaning the entire solution and closing the emulator. Then deploy again and it should work.
I tried Señor Hernandez's answer without much success, then realized I had accidentally deleted Resources/Resource.designer.cs from my Android project. I had meant to delete it from disk only, expecting it to get regenerated on rebuild. It did get regenerated, but that did not add it back to the project. Doing so (right-click, include in project) solved the issue on my part.
It got solved by downloading android_m2repository_r29.zip repository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ with instruction given in same.
I also tested the issue by creating a new fresh solution and got some style "Resource not found error". That was also fixed and then the original problem also got fixed.
You get this kind of error when trying to assign a control (a UI element in your xml) on a variable that is not of the same type. I get this error frequently when I make a mess and try to assign an EditText to a TextView variable or vice versa, i. e
EditText myText = FindViewById<TextView>(Resource.Id.myText);
or
TextView myText = FindViewById<EditText>(Resource.Id.myText);
I'm pretty sure that in your case you are trying to assign an Android.Widget.LinearLayout to a variable of type Android.Support.V7.Widget.Toolbar.
I think in your case one of this variables have been defined as LinearLayout in your xml:
Resource.Layout.toolbar
Resource.Layout.tabs
Meanwhile, one of this variables have beeen defined as Toolbar in your C# code, so te cast is not possible:
ToolbarResource
TabLayoutResource
If the problem persist, you can also try to check the type Android.Support.V7.Widget.Toolbar, instead it could be Android.Widget.Toolbar in your xml or code.

AppCompat v7 not being referenced properly

I'm new to Xamarin Android. I've installed the AppCompatv7 Support Library from the GetComponents option.
But whenever I try and do anything with it, I get no intellisense, like it isn't actually added to the project. Like below:
When I look more into the assembly it comes up with an option like below, saying it might not be installed. But as you can see from the picture, it is installed under my references.
If I click Add Package in the below picture, nothing happens.
When I compile the code, it can't find functions in the ActionBarActivity base class, so I'm guessing it's not adding it properly into my project.
Anyone know why this is happening? Cheers
When I compile the code, it can't find functions in the ActionBarActivity base class, so I'm guessing it's not adding it properly into my project.
ActionBarActivity is obsolete.
To use Android.Support.V7.Widget.Toolbar, after installing the Xamarin.Android.Support.v7.AppCompat package, you can simply inherit your MainActivity from AppCompatActivity instead of ActionBarActivity.
Then for example my toolbar is like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toolbartitile"
android:text="Kodej" />
</android.support.v7.widget.Toolbar>
Include this toolbar in Main layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/toolbar"
layout="#layout/mytoolbar" />
</LinearLayout>
And finally in your MainActivity:
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
}
}
You just need to reference Android.Support.V7.App in your code:
using Android.Support.V7.App;
All references in my demo:
If you've installed the libs correctly and there is no no intellisense, you can try to rebuild your app, close and reopen your VS.
Install it using the NuGet Command Console :
Install-Package Xamarin.Android.Support.v7.AppCompat -Pre

Using Crosswalk in an Android Cordova Project with Embedded WebView

I have an existing Android Cordova project which uses an embedded WebView. What this means is that the Activity does not extend CordovaActivity, but instead embeds the SystemWebView and initializes within the onCreate.
The following is currently how this is being done:
Within the layout XML file
<?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="fill_parent" >
.... other layout elements not related to Cordova....
<org.apache.cordova.engine.SystemWebView
android:id="#+id/cdvWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Within the Activity's onCreate:
SystemWebView systemWebView = (SystemWebView) findViewById(R.id.cdvWebView);
CordovaWebView cdvWebView = new CordovaWebViewImpl(new SystemWebViewEngine(systemWebView));
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(this);
cdvWebView.init(this, parser.getPluginEntries(), parser.getPreferences());
Due to the bug in Lollipop versions 5.0.+ missing the "set" button, I want to implement the Crosswalk plugin into the project.
Unfortunately, all the documentation I'm finding assumes that a typical Cordova install is being used. I haven't been able to get the embedding and initialization of the XWalkWebView working correctly and keep getting a blank white screen.
Has anybody has success with a similar scenario?
I'm not sure, but this might answer your question. It seems to show implementing an XWalkWebView outside of a typical cordova project:
https://github.com/kurli/crosswalk-website/wiki/How-to-use-Crosswalk-Embedded-API-on-Android

how to use android process button lib

I've download android process button lib and import it into my eclipse. :
android process button lib :
I created an android project then I added this lib into my project :
now, I want to use this library but I get this error :
ProgressGenerator cannot be resolved to a type
I am using eclipse.
#NIPHIN answer is correct. As you can notice library is using gradle folder structure.
Here are 2 options:
Move com.dd... folders to src folder.
Create new project library, and simply copy all res and classes to your new created folder.
CHeck the project structure, reorganize the folder "java" to reflect folder structure as same folder "src" in eclipse. Eclipse and Studio IDE have different folder structures.
Am not sure of how you integrated it, but the example clearly state to import
import com.dd.processbutton.iml.ActionProcessButton;
import com.dd.processbutton.iml.GenerateProcessButton;
import com.dd.processbutton.iml.SubmitProcessButton;
Even though the project has been added as a dependency library, you would still need to have these import statements. May be eclipse is having trouble automatically adding these imports? Jut add them i manually. If your folder structure and import are correct, it should work.
Thread is a bit old, but perhaps I can help you out.
I´ll show you an Example with the ActionProcessButton
first of all in your layout, u need the specific Button. For example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.dd.processbutton.iml.ActionProcessButton
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:textColor="#android:color/white"
android:textSize="18sp"
android:text="#string/login"
android:id="#+id/loginButton"
android:textAllCaps="true"
custom:pb_colorComplete="#color/green_complete"
custom:pb_colorNormal="#color/blue_normal"
custom:pb_colorPressed="#color/blue_pressed"
custom:pb_colorProgress="#color/purple_progress"
custom:pb_textComplete="#string/login_successfull"
custom:pb_textProgress="#string/login_auth" />
</RelativeLayout>
inside your activiy / Fragment / whatever:
public class LoginFragment extends Fragment {
private ActionProcessButton loginButton;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_fragment, container, false);
loginButton = (ActionProcessButton) view.findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loginButton.setProgress(50); // starts the Animation and sets the text defined in your .xml file for Progress
loginDone();
}
});
}
private void loginDone(){
//...
// do something time-consuming
loginButton.setProgress(100); // tolds the button, that your operation is done.
}
// ...
}
Of course, you can update the Progress state dynamically, but as I know, the critical values for setProgress are -1(for login failed), 0, 50 and 100.

Categories

Resources