How to load an XML inside a View in android? - android

I am having a class that extends View. I have another class that extends activity and I want to add the first class to be loaded inside the activity class.
I tried the following code
package Test2.pack;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
public class Test2 extends Activity {
/** Called when the activity is first created. */
static view v;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
v = (view) View.inflate(Test2.this, R.layout.main2, null);
}catch(Exception e){
System.out.println(" ERR " + e.getMessage()+e.toString());
}
}
}
class view extends View{
public view(Context context) {
super(context);
}
}

Ok tried this, and realized that it doesn't work. The problem is, that the View class does not have methods for adding child views. Child views should only be added to ViewGroups. Layouts, such as LinearLayout, extend ViewGroup. So instead of extending View, you need to extend for example LinearLayout.
Then, in your XML, reference to the layout with:
<my.package.MyView
android:id="#+id/CompId"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Then in your custom class, inflate and add:
public class MyView extends LinearLayout {
public MyView(Context context) {
super(context);
this.initComponent(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
this.initComponent(context);
}
private void initComponent(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.foobar, null, false);
this.addView(v);
}
}

Suggestion: avoid naming your classes names very close to already existing classes (eg. View, Activity);
Since you are extending View (which by default does not draw anything specific) you aren't able to see anything in your activity.
Start by extending TextView object to get a feel.
public class MyTextView extends TextView {
public MyTextView(Context c){
super(c);
}
// ----
public class MyActivity extends Activity {
MyTextView myTextView;
#Override
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(myTextView = new MyTextView(this);
myTextView.setText("It works!");
}
Hope this helps!

Related

Android - Adding separate components to Activity once setContentView is used

I have created an external class, NotesView, which extends View for implementation in my MainActivity.
This View requires information passed from the MainActivity, so its constructor takes an ArrayList of Note objects.
public class NotesView extends View {
private ArrayList<Note> notes = new ArrayList<>();
public NotesView(Context context, ArrayList<Note> notes) {
super(context);
this.notes = notes;
}
In my MainActivity, I used the following code to display this view: (Trying to add a CustomView in the Design tab of the layout does not work as I cannot supply the ArrayList parameter)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notesView = new NotesView(this, noteList);
setContentView(notesView);
}
Unfortunately, I am now not able to add any objects at all through the Design view of the layout, I assume this is because I have used setContentView. I do not wish to add all my components programmatically, is there a way around this?
Calling setContentView replaces the whole view for your layout. That means if you call setContentView twice, whatever was added to the screen from the first call is overridden and no longer accessible.
There are multiple answers to your question, here is a pragmatic one:
What is inside R.layout.activity_main? Let's assume there is a FrameLayout / LinearLayout / RelativeLayout with id root
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup rootView = (ViewGroup) findViewById(R.id.root);
notesView = new NotesView(this, noteList);
rootView.addView(notesView);
}
Another choice, you could also take your custom view to have a setter if you wish:
public class NotesView extends View {
private final List<Note> notes = new ArrayList<>();
public NotesView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void replaceNotes(List<Note> notes) {
this.notes.clear();
this.notes.addAll(notes);
}
Then you can add this view in your XML file (R.layout.activity_main) and call the setter method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotesView notesView = (NotesView) findViewById(R.id.notes);
notesView.replaceNotes(noteList);
}
You can add a setter function to your NotesView class:
public class NotesView extends View {
private ArrayList<Note> notes;
public NotesView(Context context) {
super(context);
}
public void setNotes(ArrayList<Note> notes) {
this.notes = notes;
}
}
And then set it in the main activity:
NotesView notesView = (NotesView) findViewById(R.id.yourNotesView);
notesView.setNotes(noteList);
By the way I recommend Butterknife to cast views in your layout without the verbose findViewByIds, declarations, onXListeners, etc.

Android : setVisibility not work

I want change view linearlayout from class
But not work this code
main.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComNet.readDb();
}
}
And :
ComNet.java
public class ComNet {
public static Context context;
public static void readDb() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewMain = inflater.inflate(R.layout.main, null);
LinearLayout lnrPart = (LinearLayout) viewMain.findViewById(R.id.lnrPart);
lnrPart.setVisibility(View.GONE);
}
}
How change lnrPart from main.xml in class ( ComNet ) ?
View viewMain = inflater.inflate(R.layout.main, null);
with this line you are creating a new View, with the content of main.xml. This object is different from the one you are seeing at screen in your MainActivity. setVisibility is working. You are calling it on the wrong instance
Send the activities rootView to readDB function.
main.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComNet.readDb(findViewById(R.id.main));
}
}
ComNet.java
public static void readDb(View viewMain) {
LinearLayout lnrPart = (LinearLayout) viewMain.findViewById(R.id.lnrPart);
viewMain.setVisibility(View.GONE);
}

use getApplicationContext to get another activity context

I would like to get the Context of another activity where I created an object (an adapter that contains a list of objects I need to display in the second activity).
public class Activity1 extends Activity {
private Context context = this;
private GridView gridView;
private MyAdapter myAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
myAdapter = new MyAdapter(context);
gridView = (GridView) findViewById(R.id.mylayout2);
gridView.setAdapter(myAdapter);
}
public class Activity2 extends Activity {
Context context = this;
private MyAdapter myAdapter;
private GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout3);
//what I want to do
cardAdapter = new CardAdapter(manageCardContext); //I want this adapter to be the one in Activity1
gridView.setAdapter(myAdapter);
}
How can I do that ?
I created an adapter in Activity1 that contains a list of objects.
Only create an Adapter in the activity that will use the Adapter, and only use that Adapter in that activity.
With the adapter I can display the list of objects, but I need to display them in Activity2
Then move the adapter-creation code from Activity1 to Activity2. Or, if you need the same sort of adapter in both places, copy the code, or have both activities inherit from some base class that has the adapter-creation code.
In similar vein to CommonsWare suggestion, I defined a public adapter class outside the Activity, just in a class file (sorry, this is C# code, but you should get the point if you already written yer adapter)
CommonAdapter.java
public class CommonAdapter : ArrayAdapter<AttachmentDetails>
{
private readonly List<AttachmentDetails> _attachments;
public CommonAdapter( Context context, List<AttachmentDetails> attachments )
: base( context, 0, attachments )
{
_attachments = attachments;
}
public override View GetView( int position, View convertView, ViewGroup parent )
{
...
}
}
and the instantiate wherever i need it
_recordAttachmentsGrid.Adapter = new CommonAdapter(this.ApplicationContext, Model.Attachments);

Adding View to a Custom ViewGroup

I am trying to add Views to a custom ViewGroup. The ViewGroup gets drawn, but no Views that were added to it are visible. LineView's (which extends a View) onDraw() method does not get called. What am I doing wrong?
public class TestActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ShapeView shapeView = new ShapeView(this);
shapeView.setBackgroundColor(Color.RED);
drawContainer = (RelativeLayout)findViewById(R.id.draw_container);
drawContainer.addView(shapeView);
}
}
public class ShapeView extends ViewGroup {
private LineView mLineView;
public ShapeView (Context context) {
super(context);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(200, 200);
this.setLayoutParams(p);
mLineView = new LineView(context);
this.addView(mLineView);
}
}
I maybe wrong but should you not be adding the shapeView to your layout "main"? What is drawContainer? I can't see it being instantiated and not in the docs. I assume main contains a relative/linear layout.
Access it by:
RelativeLayout rel = (RelativeLayout) findViewById(R.id.container);
(where the id is changed to match your id)
then:
rel.addView(shapeView);

I am not getting addView() method for object of view i.e v in my code below?

I want to add button dynamically in this view i.e. v
public class MyviewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater=this.getLayoutInflater();
View v=inflater.inflate(R.layout.main, null, true);//view
}
}
The View class doesn't have an addView method. You need to use one of the base classes that extends ViewGroup such as LinearLayout.

Categories

Resources