Add & remove custom layout element in Activity Java code - android

I have a main activity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//HOW TO: dynamically add or remove <com.my.custom.MyCustomLayout>
}
}
The content of the above main Activity is:
main.xml
<FrameLayout ...>
<LinearLayout ...>
<com.my.custom.MyCustomLayout
android:id="#+id/custom">
<FrameLayout>
As you see above, I have a custom layout element, which is a Java class extends LinearLayout like following:
public class MyCustomLayout extends LinearLayout{
...
}
In my activity java code, I would like to dynamically add or remove the custom layout element<com.my.custom.MyCustomLayout> in main.xml layout.
How to do it in My activity Java code?

Create a different layout file with your custom view like:
difflayout.xml:
<com.my.custom.MyCustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom">
Take a reference of your container LinearLayout on your code section. Use LayoutInflater and addView() and removeAllViews() of the ViewGroup class(here your LinearLayout)

First consider if you really want to remove the view from the layout or just completely hide it. You need to have a very good reason to go for the latter.
To just hide the view you would do in your activity
findViewById(R.id.custom).setVisibility(View.GONE) // to hide
// or
findViewById(R.id.custom).setVisibility(View.VISIBLE) // to show
If you really want to completely remove the view from the layout, you can
View customView = findViewById(R.id.custom);
ViewGroup parentView = (ViewGroup) customView.getParent();
parentView.removeView(customView)

you must have defined , id for the custom layout. use this id in java code
example : customLayout = (LinearLayout)findViewById(id);
now with customLayout call the layout

Related

Defining from another xml file

I am creating an application that uses a navigation drawer. However, the navigation drawer layout or design that I am using is from a different XML file, not on my MainActivity. I have an image in the layout that I am using that I want to apply as SetOnClickListener on my MainActivity. But I have no idea how I can define the image that is in a separate XML file in my MainActivity.
in your case you have 2 different XML file(layout files)
main Activity
navigation drawer
each layout file must have an own java class to get access to view objects.
but if you don't have another java class for navigation drawer, use an LayoutInflater to inflate the XML layout to an view, then you can access an set the Listener to any of views you want
in main activity add:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View rootView = li.inflate(R.layout.my_navigation_drawer_layout,null,false);
note* replace your layout file name with my_navigation_drawer_layout
now you can declare an image view and use findViewById from rootView we create earlier
final ImageView img = (ImageView) rootView.findViewById(R.id.myImageViewName)
now you can set listener to img :
img.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// write your code here!!!
}
});
What you want to do, is easy using <include> tag over the XML of the MainActivity class
<include
android:layout = "YOUR LAYOUT OF THE NAVIGATION"/>

Convert view into view that extends view

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.

LayoutInflater for using xml and View class

after I asked if I should use XML or a View class for my project you told me, that I should do everything possible in XML and use a class for the rest. You told me, that animating Sprites isn't possible with XML so I wanted to make a View Class. I got the tip to google "LayoutInflater" for this and I did.
There aren't many Informations about inflaters so I visited android's developers database and tried to find out how this works.
As far as I know now, you have to put something into the onCreate method of your main game activity (the setContentView has to be the mainXML).
So now I created a LinearLayout in my mainXML and called it "container" and made this being a ViewGroup called "parent".
Now I have created a global variable "private View view" and wrote this line:
view = LayoutInflater.from(getBaseContext()).inflate(new ViewClass(this),
null);
Thw Problem now is that u can't inflate a class like this and I think I'm doing this whole inflating thing wrong.
Do you have any tips and tricks for me for making it work to have a LinearLayout in my mainXML and being able to make the content from my View Class appear in it?
EDIT:
Got it to work without errors, but nothing happens if I start my game now.
Here is the code pls answer if u have any solutions:
super.onCreate(savedInstanceState);
// inflate mainXML->
View mainView = getLayoutInflater().inflate(R.layout.activity_game, null);
// find container->
LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
// initialize your custom view->
view = new GameLayout(this);
// add your custom view to container->
container.addView(view);
setContentView(R.layout.activity_game);
And my GameLayout:
public GameLayout(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.BLACK);
}
There are two ways of going about this. I'll show you one of them. Do the following in your onCreate(Bundle) before calling setContentView(...):
// inflate mainXML
View mainView = getLayoutInflater().inflate(R.layout.mainXML, null);
// find container
LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
// initialize your custom view
view = new ViewClass(this);
// add your custom view to container
container.addView(view);
Finally:
setContentView(mainView);
Alternatively, you can place your custom view inside mainXML:
<your.package.name.ViewClass
android:id="#+id/myCustomView"
android:layout_width="match_parent"
android:layout_height="match_parent"
.... />

Android: How to get a view given an activity inside a normal 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);

Inflating my own view type from a layout file?

I defined a layout like this:
<LinearLayout>
<ListView />
</LinearLayout>
and I want to use a wrapper class for it:
public class MyView extends LinearLayout {
ListView mListView;
public build() {
mListView = (ListView)findViewById(R.id.mylistview);
}
}
not sure how to inflate this from my layout file:
MyView v = LayoutInflater.from(this).inflate(R.layout.myview, null);
the inflater of course does not know what 'MyView' type is, and returns only View. What's a good way to reconcile this?
Why do you inflate LinearLayout in linear layout ?
What you probably need, define a layout for myView. Inflate it in some sort of initialize procedure ( for that view ).
And than you'll use your new view in xml layout.
More on that here
http://developer.android.com/guide/topics/ui/custom-components.html
You have to define the type of the Layout in xml.
Instead of your xml you need to declare it like that:
<your.package.MyView>
<ListView />
</MyView>

Categories

Resources