making a class without layout - android

I am wondering if I can create a class that has no layout (xml) that you don't have to set it on a setcontentview. For clarification, I would like to to have a background picture for my class without creating a layout or xml on it. I just want to have a class. I want to have a background named triviabackground.png (I want this PNG file to be my background picture).
Can you show me how to code it, or provide me with a reference to a tutorial?
public class Trivia extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}
i mean like canvas?

You can have an activity without any view but you can't see anything that is not exist ;) background is a view itself and every View needs layout

Yes, you can create Activity without a Layout. Layouts can be used by your objects, but are absolutely NOT mandatory. But if you want any backgrounds then you cannot have them alone as background is part of layout. You do not need XML layout file - you can create it directly from code if you need.
EDIT*
FrameLayout layout = new FrameLayout();
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
setBackgroundResource( R.drawable.background );
setContentView(layout);

Related

Is it possible to open a new layout(.xml) file with button click?

I have a MainActivity.java file with a single layout. I created a new layout that is simply called layout2, and want to call it somehow in the MainActivity.java file, to switch between the 2 layouts with a btn click, but without any .java class file (like in Activities: .java file goes with his own single layout).
For short:
An Activity open (just) a new layout file.
Thanks for any answers.
ImageButton ibtt;
ImageButton ib = (ImageButton)findViewById(R.id.ibtt);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.setContentView(R.layout.secondlayout);
}
});
Not entirely sure what you are trying to do but a Viewflipper sounds something you might like. Define a viewflipper with as many children as you want in the xml. The children can be separate layouts each. Use the<include> tag. Plenty of examples with viewflipper on StackOverflow. I prefer using a statekeeper along with one always.
EDIT: Here's an example How to use view flipper with three layouts?

Android: How to use 2 layout

I'm trying to do an application where there is a background, and with canvas i draw some lines. But there is a problem. Because to make background and other graphicals things i use a .xml file named "activity_main.xml" and after i do
setContentView(R.layout.activity_main); to activate it. But when i draw the line i must use this:
drawView = new DrawView(this)
setContentView(drawView);
Where drawView is the class that allow me to draw the line.
So the first setContentView result useless and i don't know how to do some change (for examples the background) if i had to work with drawView!
"You have no way to add a DrawView in your activity_main layout ?"
Sorry, i suppose yes, but i don't know how to do.
You can add your view to your layout like this:
ViewGroup layout = (ViewGroup) findViewById(R.id.your_layout_id);//your container's id in your activity_main.xml
drawView = new DrawView(this);
layout.addView(drawView);
Why not just add the DrawView to your existing layout with addView()?
You'll have to position it correctly of course, probably within a RelativeLayout.

Android - adding different views(widgets) to the screen without using a layout?

I am preparing to do an android demonstration of sorts and one of the first apps that i would like to write would be a screen filled with different widgets(which of course are views) but would like to put them on the screen without any layout built to hold them. is this possible or do you have to use a layout to put more than one view(widget) on the screen at once?
So right now i can do something like:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView view1 = new TextView(this);
view1.setText("I am view one");
setContentView(view1);
}
}
In this case i really havent specified a layout but there doesnt seem to be a way to position multiple widgets on the screen without setting a layout. The purpose of this would be to show why you would want to use layouts. perhaps there is a way to display widgets on the screen without having to call the setContentView method.
You can only add multiple widgets/views to something called a ViewGroup. If you take a look at the documentation you'll see - not surprisingly - that basically all layouts extend this class. Similarly, if you look up the documentation on e.g. a TextView, you'll find that it doesn't extend ViewGroup (it does inherit from View, just like ViewGroup, which means it's on a different branch in the hierarchy tree).
In other words: you will need some sort of a layout in order to display more than a single widget/view at a time. You will also always need an explicit call to setContentView(), unless you use something like a ListActivity or ListFragment that by default creates a layout with a ListView as root.
That being said, your example is actually just a programmatical way of setting the following layout on the activity:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="I am view one" />
You can do it like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout = new FrameLayout(this);
TextView view1 = new TextView(this);
view1.setText("I am view one");
frameLayout.addView(view1);
// add more widgets into ViewGroup as you want
// then set the viewgroup as content view
setContentView(frameLayout);
}

Change from one layout view to another and going back?

want to make an Android app that starts with a main layout and when you push a button (called stateButton) that is in this layout the layout changes to a main2 layout containing another button (called boton2), and when you push this one you get back to the first main.
I want to do this in the same activity without creating or starting another one.
Here I show you part of the code:
public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
private Button stateButton;
private Button boton2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.stateButton = (Button) this.findViewById(R.id.boton);
this.boton2 = (Button) this.findViewById(R.id.boton2);
stateButton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==stateButton) {
setContentView(R.layout.main2);
}
else if(v==boton2) {
setContentView(R.layout.main);
}
}
}
The mains only have some images, text views and the buttons.
But I've some troubles. Can't it just be as simple as that or what am I missing or what is wrong?
When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.
Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.
The FrameLayout handles this wonderfully... Use this with the <include... contstruct to load multiple other layouts, then you can switch back and forth between them by using setvisibility(View.VISIBLE); and setVisibility(View.INVISIBLE); on the individual layouts.
For example:
Main XML including two other layouts:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="#+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<include android:id="#+id/buildinvoice_step1_layout" layout="#layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
<include android:id="#+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="#layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>
Code to switch between layouts:
findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);
You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.
Your boton2 button will be NULL because the definition of the button is in main2.xml.
The only views you will be able to find are the views which are defined in main.xml.
Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.

How can I reuse a ViewGroup defined in an XML file multiple times in Android?

I am trying to create a simple game. I would like to populate a LinearLayout which I have defined in an XML with several RelativeLayouts. Each time the user presses a button I would like a child View to be added to the LinearLayout. Each RelativeLayout would change slightly based on the users input (which buttons were pressed, etc).
I essentially want to create a new RelativeLayout based on an XML layout file. I then want to manipulate some attributes of the RelativeLayout's child Views (specifically the src of some ImageViews) and add it to a LinearLayout. This in itself is not particularly difficult. I can get at each of the RelativeLayout's children using findViewById but I start having problems when I want to create n number of RelativeLayouts based on the same XML. I'm pretty sure duplicate IDs are causing crashes. Can a RelativeLayout work without using IDs? Should I try and find a different way of constructing the interface using different ViewGroups?
I'm not sure if what I am asking is even possible but I know that creating these items on-the-fly using the code alternative to the XML layouts is a horrible thought.
Thank you in advance.
EDIT: I lost my train of thought halfway through, the previous example had some bugginess. I've updated.
pedr0 has the right idea, but to clarify, you could try something to this effect (assuming you have a RelativeLayout defined in relative.xml). This isn't tested, but the general idea should be valid. You don't even have to do a separate method, you can do it inline in a click handler, or whatever, but I just did the addChildView method for the sake of example:
LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the LinearLayout that you plan to add rows to
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear_layout);
//get the LayoutInflater from the system
inflater = getSystemService(LAYOUT_INFLATER_SERVICE);
//call getNewChildView with whatever drawable id you want to place
//as the source. If you want to pass in other types, just change
//the parameters (e.g. Drawable, Bitmap)
linearLayout.addView(getNewChildView(R.drawable.my_image));
}
public RelativeLayout getNewChildView(int resId) {
//inflates a copy of your RelativeLayout template
RelativeLayout rl = (RelativeLayout)inflater.inflate(R.layout.relative, null);
//this assumes an ImageView in your RelativeLayout with an id of image
ImageView img = (ImageView)rl.findViewById(R.id.image);
img.setBackgroundResource(resId);
return rl;
}
EDIT: I couldn't even work out how to use the code tags properly sigh
Thank you both for your quick responses. I had something very similar in mind and in fact was on the same track and using the LayoutInflator. I simplified your example a little as I do not need to pass the id of a drawable around.
LayoutInflater inflater;
private void drawGuess() {
// The top level LinearLayout to add to
LinearLayout topLevel = (LinearLayout) findViewById(R.id.guessList);
// get the inflater`
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
// add the view (based on stored data elsewhere)
topLevel.addView(getLatestGuess());
}
private RelativeLayout getLatestGuess() {
//inflates a copy of your RelativeLayout template
RelativeLayout rl = (RelativeLayout)inflater.inflate(R.layout.guess_layout, null);
//this assumes an ImageView in your RelativeLayout with an id of image
ImageView guessOne = (ImageView)rl.findViewById(R.id.guess1);
guessOne.setImageResource(R.drawable.red);
}
Running this code works, the catch is it only works the first time you call it. The second time you call getLatestGuess() it crashes. Removing the IDs in the XML (e.g. guess1) from all the child views of the RelativeLayout results in no more crashing. Unfortunately now I have a fairly useless RelativeLayout that I can no longer get at the child views of as they do not have IDs. Presumably, IDs are required to be unique.
I think you have to create an root element, and then add the new child.
Like this:
RelatoveLayout root = ....;
RelativeLayout toadd = inflate(your XML);
apply by code change depending of users input, for example:
toadd.setBackgroundResource(....);
and finally add the result to the root :
root.addView(toadd).
I really hope to help you!
Bye!

Categories

Resources