id reference is showing error and not giving import option - android-studio

my image View id is showing an error, probably not importing... this is my xml for Image View...
<ImageView
android:id="#+id/abc"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:srcCompat="#tools:sample/avatars"
android:contentDescription="TODO" />
and this is MainActivity where in glide, image view id is not accepting...
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
{response ->
Glide.with(this).load(url).into(abc);
},
{ })
these are my imports...
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide
please assist me to resolve that...

Can do this using the traditional way, assuming you are not using Kotlin Synthetics or View Binding:
val imageView = view.findViewById<ImageView>(R.id.abc)
Then do this at where you want to use the abc image view like this:
Glide.with(this).load(url).into(imageView);
If you want to use the Kotlin Synthesis method, can follow this link here to add in the imports but it is not recommended anymore as it is deprecated to give way for View Binding.

Related

Unresolved reference by Android Studio 2020.3.1

I've used the Fullscreen Activity to create a project in Android Studio 2020.3.1. On buld of the app, I get "Unresolved reference: LastChange". The 3 lines pointed to (in FullscreenActivity.kt) are:
override fun onConfigurationChanged(newConfig: Configuration) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
LastChange.setText("Landscape")}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
LastChange.setText("Portrait")}
else {
LastChange.setText("Unrecognized")}
super.onConfigurationChanged(newConfig)
}
The TextView (in activity_xml) is coded as:
<TextView
android:id="#+id/LastChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/LastChangeText"
android:textSize="24sp" />
LastChangeText (in strings.xml) is defined as:
<string name="LastChangeText">N/A</string>
Although your textView has "LastChange" id, that's not the way you call it from activity.
At "onCreate" method, after super.onCreate(), you'll need:
setContentView(R.layout.<YOUR_LAYOUT_NAME>);
val lastChangeTextView = (TextView) findViewById(R.id.LastChange);
And, after that, you can call methods on "lastChangeTextView".
lastChangeTextView.setText("Landscape")
Strongly recommend taking a look at Documentation:
https://developer.android.com/guide/topics/ui/declaring-layout

How to display listItem on emulator on recyclerview?

what I am trying to do is show a custom card in a recyclerview.
I can see it on the design view on android studio, but no when the emulator runs.
I just need to display de listItem.
Is is posible? if so, any help or suggestion would be great, thanks.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcvPoolVariables"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="22dp"
android:layout_marginStart="17dp"
android:layout_marginEnd="17dp"
tools:listitem="#layout/row_pool_objectives"
app:layout_constraintTop_toBottomOf="#+id/containerVariableAndWeeks"
app:layout_constraintBottom_toBottomOf="parent"/>
1.xml
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/categoryview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
tools:listitem="#layout/innercategorylist">
</androidx.recyclerview.widget.RecyclerView>
2.java file
CategoryModel[] myListData1 = new CategoryModel[]{
new CategoryModel( R.drawable.kiwi,"kiwi"),
new CategoryModel(R.drawable.watermelon,"watermelon" ),
new CategoryModel(R.drawable.strawberry,"strawberry")
};
innerCategoryAdapter = new InnerCategoryAdapter(myListData1, new InnerCategoryAdapter.HomeAdapterInterface() {
#Override
public void onRowClick(int searchText) {
}
});
3.Adapter class id define and assign and model class to variable getter setter set
If the "preliminary version" is shown in the design tab, then there are no errors on the part of xml documents. It's in "regular" code part. I think the error lies in the output of elements on the device - either you do not pass data objects for output in recycler view, or there is an error in the processing of this data. Check the recycler view classes architecture and code of an adapter class. Also read this guide, I hope it'll help you.

How to address "Cannot access class" error when binding to a class in a subpackage?

Starting with Android 3.2.1, binding to classes defined in sub-packages (e.g. Sub.Thing in com.example) results in an error:
Cannot access class 'Sub.Thing'. Check your module classpath for missing or conflicting dependencies
What's the cause of this errror? How can it be fixed?
Sample Code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout
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"
tools:context=".MainActivity"
>
<data>
<import type="com.example.Sub.Thing"/>
<variable name="data" type="Thing"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:text="#{data.name}"/>
<TextView
android:id="#+id/value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:text="#{data.value}"/>
</LinearLayout>
</layout>
Thing.kt:
package com.example.Sub
data class Thing(
var name:String,
var value:String
)
MainActivity.kt:
package com.example
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.Sub.Thing
import com.example.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
val activityMain:ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
activityMain.data = Thing("bam", "bug-AWWK!")
}
}
By convention (and for good reason), only class names are supposed to start uppercase. It appears newer versions of the compiler assume (enforce?) this convention (uppercase letters later in the name are accepted, though this may cause other issues).
The solution is simple: rename the subpackage to use lowercase, which can be done by right-clicking it in the Android project view and clicking Refactor → Rename, or by clicking it and opening the Refactor → Rename menu item.

Android EditText get nothing

it's my EditText.
I run the app don't type anthing
when I sout gettext().toString
I got really nothing
not null or "" why?
<EditText
android:id="#+id/ed_account"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg_edittext"
android:hint="#string/employeeHint1"
android:inputType="number"
android:text=""
android:paddingLeft="10dp" android:paddingStart="10dp"
/>
this is for someone to type some info for me to do something
I use hint, I don't want type anything to over the hint
and I have to make sure user to type something.
Try to modify this line
android:text="some text...."
your question is not clear, am sorry but please improve your english. And without the java code, am not sure i got your problem right, but try using this to check if the EditText is null:
EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return;
}

Is it possible to have two alignments to the application name of an android app?

I am tyring to achieve two alignments in the title of an android app. The title according to my requirement needs to have a 'name' which is left aligned and a 'version number' which is right aligned.
So far what I have tried doing is to test whether the label attribute of the application tag of the AndroidManifest.xml files accepts html, but it does not seem to :(. Also from some of the other similar questions I came to know that the style of the title could be changed using a custom theme. But this does not answer my question:
"Can we have two different alignments (say left and right), or for that case two different themes, to the parts of the application name?"
P.S: A screenshot of the desired application title is attached below.
You can create custom title bar ..
here is sample code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.i_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
final TextView titleLeft = (TextView) findViewById(R.id.title_left);
final TextView titleRight = (TextView) findViewById(R.id.title_right);
titleLeft.setText("Checker");
titleRight.setText("Version 5.44");
}
i_title.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/title_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="app name" />
<TextView
android:id="#+id/title_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="app version" />
</RelativeLayout>

Categories

Resources