My question is about Android/Java.
How can I call my custom view with a name such as
<input>
instead of
<org.javaforum.input>
without adding anything in the MainActivity.java?
I already read this: https://stackoverflow.com/a/17478278/16627950
But there I must add something in MainActivity.java.
I also read this: https://stackoverflow.com/a/30066376/16627950
But I don't know where do the variables mInflator and mPrefix come from.
I also read this: https://stackoverflow.com/a/13316335/16627950
But I don't know what are the programmatically steps to do so.
So how can I hook into the layout inflation in code, and handle instantiating my class for that tag myself?
mInflator and mPrefix, are custom properties declared at the top of the activity class, nothing special, in order to implement the custom layout you have to call:
val myInflater = LayoutInflater.from(this)
.cloneInContext(this)
myInflater.factory2 = MyLayoutInflater()
This from your onCreate method of the activity, I think that is not possible to add a factory without tounching the activity code
Related
I am trying to access the activity on which my Imageview is, so I can use the URL of an Image of type SVG and display it to the user using the GlideToVectorYou library.
GlideToVectorYou.justLoadImage(activity, IMAGE_URI, targetImageView)
But when I try to get access to the activity using R.layout.activityname, a syntax error appears.
this is the code that I'm using
Uri myurl = Uri.parse(match.getFlag());
GlideToVectorYou.justLoadImage(R.layout.item_basketball, myurl, iv_location);
Thank you!
R.layout.item_basketball is just an integer ID for your activity layout - not the activity instance itself. If you want the activity in your adapter you would need to pass it in when you construct the adapter and save it as a class member (example below), or check if your adapter base class already can provide it via getActivity() or getContext() or a similar method.
class MyAdapter(private val activity: Activity) : BaseAdapter() {
fun someMethod() {
// then you can access "activity" in your adapter methods
GlideToVectorYou.justLoadImage(activity, IMAGE_URI, targetImageView)
}
}
and when you create it in your Activity, you would just do something like this
val adapter = MyAdapter(this)
You need a activity reference. R.layout.somethinghere is the layout reference.
On your adapter constructor add a activity parameter and use it inside the adapter.
If you call adapter constructor from an activity, just pass "this" as parameter. If call from a fragment, use "requireActivity" (if using kotlin) or analogous method (getActivity, for example) if using Java
I'm trying to implement a chat app using stfalcon's ChatKit library. I've followed the docs in their repo, but there are things I'm not sure I'm getting right.
First, I created a new activity called DialogsListActivity, and copied the xml in the activity's xml file.
From here I first copied the xml part to the activity's xml file.
Next comes the adapter setup. I copied the given code after the OnCreate method, including the last line (dialogsListView.setAdapter(dialogsListAdapter);) as the last line in OnCreate. The whole activity now looks like this:
ListView dialogsListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogs_list);
dialogsListView.setAdapter(dialogsListAdapter);
}
DialogsListAdapter dialogsListAdapter = new DialogsListAdapter<>(dialogs, new ImageLoader() {
#Override
public void loadImage(ImageView imageView, String url) {
//If you using another library - write here your way to load image
Picasso.with(DialogsListActivity.this).load(url).into(imageView);
}
});
}
Questions:
is this the right place to put the adapter in?
is it ok to put set the dialogsListView as an attribute and defining it inside OnCreate()?
the dialogs from new DialogsListAdapter gets a Cannot resolve symbol 'dialogs' message.
the new ImageLoader() from same place gets a Class 'Anonymous class derived from ImageLoader' must either be declared abstract or implement abstract method 'loadImage(ImageView, String, Object)' in 'ImageLoader'
What am I missing there?
For the IDialog and IUser implementation I created the classes DefaultDialog and Author, and copied the given code. As I expected, the 'symbols' returned by the methods 'cannot be defined'. Where should they be defined and how?
Next in the tutorial is the Data management section which I think would set those values.
I already downloaded the sample project and tried to look inside, but I cannot find the public class DefaultDialog that implements IDialog or anything similar. Plus, I got pretty lost trying to understand the library from that sample project.
Any help would be much appreciated.
Thanks in advance.
Sorry rather a newbie learning android studio where I downloaded a project here to get my hands dirty with coding a camera app uploaded to a server. But somehow I couldn't get it to work after I tried compiling it, with a error for this statement in the MainActivity.java file.
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.color.action_bar))));
Error stated: Expected resource of type string less... (Ctrl+F1)
This inspection looks at Android API calls that have been annotated with various support annotations (such as RequiresPermission or UiThread) and flags any calls that are not using the API correctly as specified by the annotations. Examples of errors flagged by this inspection:
Passing the wrong type of resource integer (such as R.string) to an API that expects a different type (such as R.dimen).
Forgetting to invoke the overridden method (via super) in methods that require it
Calling a method that requires a permission without having declared that permission in the manifest
Passing a resource color reference to a method which expects an RGB integer value.
Thanks in advance.
try this
actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
don't forget to set color names in your project's res/values/colors.xml file
xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="action_bar">#fff</color> //write the color you want here
</resources>
ColorDrawable takes integer as parameter. You are trying to pass string.
getString(id) method returns string. getString this method is used read string from strings.xml as getString(R.string.yourid). You should define the colors.xml under /res/values/ folder. and you can read as follows: getColor(R.color.action_bar) . This method is deprecated. You should use
ContextCompat.getColor(yourcontext,R.color.action_bar)
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(ContextCompat.getColor(this,R.color.action_bar))));
You used this
getResources().getString(R.color.action_bar)
But R.color.action_bar is not a string value. Therefore the error Expected resource of type string
You have to use the correct method to get the color resource.
getResources().getColor(R.color.action_bar)
But that's deprecated, so use this
ContextCompat.getColor(MainActivity.this,R.color.action_bar)
Use getSupportActionBar() if you Activity is extends AppCompatActivity
and dont use Color.parseColor()
AppCompatActivity
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
Activity
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
Hi i am trying to understand the use of context though i couldn't. Following is a program using context. My question is what is the significance of " context = class.this " ?
class public VcardActivity extends Activity
{
String Vcard = "vcard";
Context context;
}
public void onCreate ( Bundle bn )
{
super.onCreate(bn);
setContentView(R.layout.main);
context = VcardActivity.this;
}
Your current code doesn't show the use of context. It shows that the Activity is a context.
TextView someText=new TextView(context);
This code of mine shows, I am passing a context into the constructor of a TextView in order to make this object. The reason is, this object needs to know the information, state of the current context, and this is the reason why many views, classes, helpers needs a context.
context = VcardActivity.this;
in your code you are having your activity object to assign to the Context context. This works because Activity class inherits from Context and many classes needs a Context to create it.
In your case, the field context is not necessary at all. It rather is used as a shortcut to VcardActivity.this here. You could remove it without any problems and use VcardActivity.this or even only this where you used to use context.
You don't need to create a separate Context variable inside of an Activity. You use Context for certain objects/methods that need to know what is starting them. Activity already has a Context so you don't need to create it. If you need to use Context within an Activity, say when creating an Intent you can just use ActivityName.this or here VcardActivity
See this SO answer for a good explanation of using which kind of Context when.
Context Docs
I'm currently writing a plugin that is attempting to add a view to the current LinearLayout of my application.
Though I am struggling to get access to the linear layout from within a plugin, I can add the view fine if I do so within my main activity as so:
MyView view = new MyView(this);
root.addView(myView);
But to get the root LinearLayout in my plugin I have assumed that:
this.cordova.getActivity();
is my main activity and have been trying to cast it to the type of my main activity and call a function I added that will return the root LinearLayout object as so:
MyActivity myAct = (MyActivity)this.cordova.getActivity();
MyView view = new MyView(myAct);
myAct .GetLinearLayout().addView(view);
Though this doesn't seem to work and I receive no errors or such to help figure out why?
Anyone know how I can get access to the layout to add my view?
Okay so I solved this slightly differently to what I was attempting to do above.
Firstly casting the activity returned by cordova:
MyActivity myAct = (MyActivity)this.cordova.getActivity();
Does actually work and returns the instance of your main activity, so that wasn't the problem.
In the end I couldn't figure out why adding another view to the root cordova layout from the main activity worked but not when I did so in a plugin so what I did was to create my view from within the activities onCreate() then I provided an accessor to the view class back to my plugin and worked on from there.
The layout no longer exists after init() completes. The last thing that happens in init() is setContentView() is called with the layout. When setContentView() is called with a layout, the layout is inflated and the individual views in the layout are added to the activity.