I Have an android activity with a custom view set to it. The custom view is a java class. I need to infer some methods of view from it's activity. How can I do this?
1)Created CustomView class with extends of view(whatever you want)
2)Create a method within CustomView class like,
public void check(){
}
3)Use custom view in you xml or activity
4)Find the view from activity,
private CustomView obj;
obj = (CustomView)findViewById(R.id.view1);
5)Now if i want to access the check method
obj.check();
Related
I have implemented swipe in tablayout of my activity call Performance_Medicine
public class Performance_Medicine extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
return inflater.inflate(R.layout.performance_medicine, container, false);
}
}
Now, I am trying to implement cardview in same activity. But getting error
like below image
You are getting the error because you are trying to pass an instance of Performance_Medicine which extends Fragment. You need to pass in a context which you can do via this.getActivity() or this.getContext(). If you can pass the application context via a singleton or this.getActivity().getApplicationContext() then you may be better off memory wise.
Remember that a Fragment has its own lifecycle, but it runs in the context of its host Activity, that means you can not use 'this' for getting the Context, instead you need to use getActivity(). Also, as sam_c says, in your onCreate() method, the last line of code must be the 'return...' since this method has the return type 'View', and if you call the return statement, the method won't execute anything after this. Hope this helps to clarify.
I have two View holders . On clicking a button in one view I want a text to get updated in another view of the Recycler.
It works fine with getRootview().
But on scrolling when the view gets hidden, recycler crashes (as getRootView no longer returns anything).
How can I implement this ?
ViewHolder1:
public static class CartHeader extends RecyclerView.ViewHolder {
public TextView list_cart_header_textView_total;
private TextView list_cart_header_textView_title;
}
ViewHolder2:
public class CartDBItem extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView list_cart_product_imageView_add;
}
onClick of list_cart_product_imageView_add:
CartHeader ch=new CartHeader(view.getRootView());
storeHeader=Double.parseDouble(ch.list_cart_header_textView_total.getText().toString());
Lists in Android use the Model-View-Controller paradigm. The RecyclerView is the View, of course, and the adapter is the Model.
Within the Controller (onClick handler), when you want to change something in the View you change the Model then update the View. So you change the text at its source in the adapter, then call notifyDataSetChanged() to let the RecyclerView know to refresh its views from the adapter.
You should only access view holders when creating their layouts or binding their data.
Is there a possibility that in the xml resource layout to have a base view and when inflate it to convert it to a specific view ?
For example having a custom view called MyCustomView that extends EditText, and some views that extends MyCustomView like MyCustomViewNumber or MyCustomViewPassword and a layout like this :
<com.example.MyCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.....>
</com.example.MyCustomView>
Is it possible that after I inflate this xml, MyCustomView to became one of MyCustomViewNumber or MyCustomViewPassword (inherit all attributes from those two). MyCustomViewNumber will be an EditText(better said a MyCustomView) that in the constructor method has setInputType to number.
View baseView = LayoutInflater.from(getContext()).inflate(R.id.my_layout, container, false);
baseView = new MyCustomViewNumber(getContext()). //with this line I want that my view from the layout to take all attributes from MyCustomViewNumber.
Recapitulating:
public class MyCustomView extends EditText
public class MyCustomViewNumber extends MyCustomView {
ctors > this.setInputType("number");
}
public class MyCustomViewPassword extends MyCustomView{ ctors > same as above }
Inflate MyCustomView. Set the inflated view to MyCustomViewNumber or MyCustomViewPassword. Is it possible ?
Basically I do this because I need the "layoutParams". I know that I could get the layout params from the inflated view, remove it and then add the new one with that parameters.
No, the class whose fully qualified name you use in your XML is instantiated by the system, and thus must be a concrete class.
I have a normal class (not an activity). Inside that class, I have a reference to an activity.
Now I want to access a view (to add a child) contained in the layout xml of that activity.
I don't know the name of the layout file of that activity. I only know the ID of the view, which I want to access (for example: R.id.my_view).
How can I do that?
Regarding the NullPointerException (which you should add to the question), always make sure you've called setContentView() in your Activity before trying to access a View defined in XML. Example usage:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
}
...
}
Then, somewhere,
ViewGroup group = (ViewGroup) context.findViewById(R.id.group); // In your example, R.id.my_view
The reason you need to have called setContentView() is that before it's called, your View(Group) doesn't exist. Because findViewById() is unable to find something that doesn't exist, it returns null.
As simple as that!
View view = activity.findViewById(R.id.my_view);
In case of the Layout:
LinearLayout layout = (LinearLayout) activity.findViewById(R.id.my_layoutId);
And to add the Views:
layout.addView(view);
You could make your method accept an Activity parameter and then use it to find the view by id.
Ex:
public class MyClass{
public void doSomething(Activity context){
TextView text=(TextView)context.findViewById(R.id.my_textview);
}
}
Then in your activity:
obj.doSomething(YourActivity.this);
I want to replace a view with other other by code
For example i need to change a imageview to progressBar by code.
public void changeToProgressBar(ImageView imageview,Context context){
ProgressBar one = new ProgressBar(context);
one.setLayoutParams(imageview.getLayoutParams());
imageview.setVisibility(View.GONE);
one.setVisibility(View.VISIBLE);
//NOW I NEED TO PLACE 'one' EXACTLY THE PLACE WHERE 'imageview' WAS THERE
}
I need to use this many place. I know to set by sending the parent viewgroup.
Is there anyway to get the parent viewgroup from imageview
Thanks
This is trivial to do and other posters have linked to or hinted at the solution. You can get the parent of a View by View.getParent(). You need to cast the returned value to a ViewGroup (q: can a View's parent be anything else than a ViewGroup?). Once you have the parent as a ViewGroup you can do ViewGroup.removeChild(viewToRemove) to remove the old view and add the new one using ViewGroup.addChild(viewToAdd).
You might also want to add the new view at the same index as the remove view to make sure that you don't put the new view on top of or below other views. Here's a complete utility class for you to use:
import android.view.View;
import android.view.ViewGroup;
public class ViewGroupUtils {
public static ViewGroup getParent(View view) {
return (ViewGroup)view.getParent();
}
public static void removeView(View view) {
ViewGroup parent = getParent(view);
if(parent != null) {
parent.removeView(view);
}
}
public static void replaceView(View currentView, View newView) {
ViewGroup parent = getParent(currentView);
if(parent == null) {
return;
}
final int index = parent.indexOfChild(currentView);
removeView(currentView);
removeView(newView);
parent.addView(newView, index);
}
}
Something to consider is that you'll lose any positioning in a relative layout when you replace one view with another. One solution to this would be to make sure that the view you want to replace is wrapped in a another view and that wrapped container view is the one that is positioned in a relative layout.
Retrieve the view you would like to change by calling findViewById() from the activity level http://developer.android.com/reference/android/app/Activity.html#findViewById(int) Then find the sub view you would like to change http://developer.android.com/reference/android/view/View.html#findViewById(int)
Then use the functions from http://developer.android.com/reference/android/view/ViewGroup.html to manipulate the view.
Just as User333 described that could be one solution..
It's also possible to delete the imageview by calling yourview.removeView(imageview) and then create your progress bar and put that inside the view instead by yourview.addView(progressbar)
You can change Android Activities view from any other simple java class or in other activity.
you only need to pass current view and get your element by this view you want to change As :
public class MainActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting UI
View currentView = getWindow().getDecorView().getRootView();
//showHeaderLayout is simple java class or it can be any activity
changeLayout.setView(currentView);
}
Class : changeLayout
public class changeLayout{
public static View setView(final Activity activity, final View myView)
{
// myView helps to get Activity view , which we want to change.
TextView tv = (TextView) myView.findViewById(R.id.tv);
tv.setText("changs Text Via other java class !!");
}
}
Note : Passing view makes you able to change any activity view outside an Activity.
I think following ould be the best approach as in this we don't need to set layout params.
setvisibility(Visibility.GONE)
setvisibility(Visibility.VISIBLE)
I am sure that following link not only help you, but even shows you the direction for your requirement. https://stackoverflow.com/a/3760027/3133932
For this, take both imageview and progressBar and set the visibility according to your requirements.
For example
If you want progressBar to be visible, put setvisibility(Visibility.GONE) for imageview and put setvisibility(Visibility.VISIBLE) for progressBar