I'm new in android studio and I'm having problem with drawing. I understood that "onDraw()" function can only be implemented in class that extendes "View". But I don't under how do I connect my activity to the view class, so we will see on the screen the view class, not the activity one.
Any help? Thanks in advance!
Its a pretty much basic thing android. You can find more info here.
I'll just show you an example.
First the View class,
public MyView extends View{
public MyView(Context context,AttributeSet set){
super(context,set);
}
public void onDraw(Canvas canvas){
//draw the item
}
}
In XML
<LinearLayout xmlns:android="http://www....."
android:orientation="veretical"
android:width="match_parent"
android:height="match_parent">
<!--you custom view here-->
<com.your.package.name.MyView
android:width="match_parent"
android:height="match_parent"/>
</LinearLayout>
Use this XML as the content view of your activity.
Just add your's custom view object to realtive layout.Hear Relative is where you want to see canvas it may complete layout or portion of the layout
RelativeLayout viewlayout=(RelativeLayout)findViewById(R.id.viewlayoutid);
viewlayout.addView(new yours_customviewclass(this,null));//yours_customviewclass is your custom view means which extends view class,overide ondraw methods in this class
Related
I am building a video playing app in Java where I use three Fragments. In one of them I have a list of videos to be played (using a simple ListView widget) where I use a Custom Adapter to pass the titles and the bitmaps thumbnails/uri paths of those videos. When I use Glide to generate and show the bitmaps in the ImageView section of the Custom Adapter, it works (passing only uri paths from Fragment), because the context in the getView() function works as well.
But I want to generate the bitmap thumbnails using Glide in the ListView Fragment, not in the Custom Adapter class, because I need those thumbnails in other Fragments of the app. Unfortunately I didn't managed to create the thumbnails in the ListView Fragment, because the context I am writing at Glide.with() in the onCreateView() function doesn't work!
What is the proper context to be used for Glide in the ListView Fragment to see it and generate the bitmap thumbnails?
I used this code in the ListView Fragment:
Glide.with(this)
.asBitmap().load(uri)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
bitmapThumbnail = resource;
}
});
but it doesn't generate any thumbnails.. :((
In your Fragment you can get the parent Activity Context by calling getContext()
I just found out that the problem wasn't the context I added to Glide.with() section, but the fact that the bitmap generated inside the "onResourceReady(...)" method stays in there and it cannot be used outside the method, only inside and as an argument for an "imageView.setImageBitmap()" method...
As I understood by now, Glide only intermediates between the url/uri/path and the imageView widget, it's not generating any bitmaps outside itself! This is a valuable thing I learned about Glide!
Many thanks for all the help I received!
I created an Android app with list view. Whenever the an element is clicked, it passes intent to cardview.I have a single cardview in my layout file, and use it for every element in the listView.
For each list item, I define different implementation for the CardView in Java passing intents from one java file to the other. .
Please is this a good practice using a single card view and Recycler View for multiple elements changing their text programmatically. I haven't seen anyone do something like that.
Edit
#Ankit something like ...
Something like
>
<android.support.v7.widget.RecyclerView/>
</LinearLayout>```
**Tutorial page 1**
```public class Lesson_Galaxy_One extends AppCompatActivity
{ super.onCreate(savedInstanceState);
setContentView(R.layout.lesson_galaxy);
}```
**Tutorial 2**
```public class Lesson_Galaxy_Two extends AppCompatActivity
{ super.onCreate(savedInstanceState);
setContentView(R.layout.lesson_galaxy);
}```
**Tutorial 3**
```public class Lesson_Galaxy_Three extends AppCompatActivity
{ super.onCreate(savedInstanceState);
setContentView(R.layout.lesson_galaxy);
}```
Each with different elements for the RecyclerView's CardView with intents being passed from one to the other.
There won't be any issues of using a single card view for multiple list items.It's totally fine .
I'm working on a "home feed"-like feature where there's a main Fragment with several other fragments added to its layout, making up the content page. I'd like the main fragment class to be able to instanstiate all the fragment classes that inherit from a certain parent fragment class. This way the code would be more dynamic instead of adding a bunch of <fragment> tags to my xml files.
I'm kinda stuck on making up a decent architecture. How would you go on about doing this?
UPDATE:
Here's what I'm basically trying to do, but don't know how:
public class FeedFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View parentView = inflater.inflate(R.layout.fragment_home, container, false);
// Get fragments and dynamically add them to
// the FeedFragment's layout
getEntryFragmentsList();
// ...
return parentView;
}
}
public abstract class FeedEntryFragment extends Fragment {
// Somehow add fragment to list of entry fragments
}
public class TestFragment extends FeedEntryFragment {
// Already added to list of entry fragments
}
I don't think that possible to decrease the count of xmls by inheritance. I think you should try to split your xml configurations and use some <merge> or <include> to build the full one from the parts.
May be I can provide more help if you will describe your problem in more details.
You should use FrameLayout to add fragment(s) dynamically by using the FragmentTransaction.
You can also use a ViewPager with tabs or bottom tabs to show multiple fragments. Please check sample from my Dynamic Support library for the complete code.
Abstract fragments
DynamicFragment - Abstract base fragment from the Dynamic support library.
DynamicViewPagerFragment - Abstract fragment which extends the DynamicFragment to implement the ViewPager functionality.
Implementation
HomeFragment - Sample fragment extends the DynamicFragment to implement the home screen.
SettingsFragment - Sample fragment extends the DynamicViewPagerFragment to implement the settings functionality using multiple fragments inside a view pager.
Tutorial Implementation
This better suits your need. TutorialActivity returns a list of fragments to be displayed inside a ViewPager.
DynamicSimpleTutorial generates a DynamicTutorialFragment according to the supplied parameters.
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.
My code is working and so, but I don't understand one thing.
I've started game programming and now works my whole SurfaceView and so on. I have draw a background so it all works.
When I created this I followed a tutorial, so know I'm commenting on my own language. :)
When I now started to comment some things; I can't get rid of this, can someone tell me why my constructor for my view has the Context parameter and AttributeSet paramater? What makes it necessary to have these and why do I need to set the localContext value to my GameView context?
Game
public class Game extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(new GameView(this, null));
}
}
GameView
public GameView(Context localContext, AttributeSet attrSet){
super(localContext, attrSet);
context=localContext;
InitView();
}
The InitView(); just initialize every objects and variables I created; if this stuff is needed in order to explain the constructor-thing to me; I will post it.
Thanks!
The Context carries a bunch of state with it that Views use to operate. This includes things like configuration state to help the resource system determine which resources to use among a number of others.
The AttributeSet is used when your view is inflated from an XML layout. This is how XML attributes get bound to view properties during layout inflation.