ViewStub not getting hidden/deflated android - android

I have following class and it's obeject which is inflated viewstub.
Statusbar-- class extends RelativeLayout
Statusbar b= (Statusbar)((ViewStub)findViewById(R.id.stub_one)).inflate();
Now I want to make this Statusbar visible/invisible or inflate/deflate depending on my need.
I tried b.setVisibility(View.INVISIBLE). But it didn't work.
How this can be done?

findViewById(R.id.stub_one).setVisibility(View.GONE);

Related

What is tag <view> in layout xml used for?

I notice that there is a tag <view>(not <View>) which can be used in Android layout xml. When I add such a tag in a layout xml, the app can be successfully compiled but crashes when running. So what is this <view> used for?
view is an alternative way for specifying a view to inflate. You need to combine it with class attribute to specify the class name.
This is "useful" if your view class is an inner class and you cannot use something with a $ in the tag name in XML. (Having inner classes as Views you inflate is not a very good idea though.)
For example:
<view class="your.app.full.package.Something$InnerClass" ... />
Reference: http://androidxref.com/5.0.0_r2/xref/frameworks/base/core/java/android/view/LayoutInflater.java#696
View is widget of android that is use to add any kind of view.Like in problems when we use scrollView in a activity and at bottom of activity there is buttons ,That time we add view to looks better.We can set any kind of color to View .
for exp
This will make a View of 20dp height.

making a class without layout

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);

Adding Buttons to FingerPaint Android API Sample1

Im kind of new to Android.
I am playing around with the Android FingerPaint API Sample. I have figured out how to add buttons and functions to the Menu, but how do I get buttons on the actual screen?
Is it possible to place buttons over the drawing surface? Or would I need a linear (Vertical) layout on the left, and place buttons in there. Either would be fine.
Help? Thanks.
The Android FingerPaint sample code does not use a layout; it instead just has a subclass of View called MyView, and then the Activity sets its content view to be an instance of MyView.
If you want to have more than one View on the screen, you'll need to use some sort of layout. You could use a LinearLayout if you want the MyView for painting to be above, below, or to the side of the buttons; if you want the buttons to be on top of the MyView, take a look at using a FrameLayout or RelativeLayout.
You can either then define the layout in XML or create it manually in code. The former is more flexible and maintainable, but there will be a few hiccups.
First, create a layout XML showing how you want your components to be laid out. For this example, we'll call it finger_paint.xml. Make sure you have a MyView in there somewhere, something like:
<view class="com.example.android.apis.graphics.FingerPaint$MyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Then, replace the line that looks like
setContentView(new MyView(this));
with
setContentView(R.layout.finger_paint);
Note that because MyView does not (yet) have the proper constructor for being instantiated by the LayoutInflater, this will not work yet, so let's fix that. Add an additional import near the top of the file:
import android.util.AttributeSet;
and then add an AttributeSet parameter to the constructor of MyView:
public MyView(Context c, AttributeSet as) {
super(c, as);
// rest of constructor is same as in the sample
}
You will also have to change MyView to be a static inner class of FingerPaint.
You may find the Building Custom Components document and the NotePad sample useful as you figure this out.
Good luck!
Are you trying to dynamically position your buttons?
If so, you can use setLayoutParams to set the layout properties of the button.

Adding edittext to View layout

Regarding this example -
http://android-coding.blogspot.com/2011/05/detect-multi-touch-event-test-on-custom.html
How can I add EditText to the view ?
I tried to add it to the layout but when I'm trying to update it I'm getting error message.
Thanks
I found a nice example here - http://www.kellbot.com/2009/06/android-hello-circle.
I added EditText to the layout and its working fine.
In order to use this example the code should be written as explained in Sebastien's comment, in which he uses parent as described in this line:
final Activity parent = this;
The MultiTouchView from the link extends a View. You cannot add other Views to a View. You will need the MultiTouchView to extend a ViewGroup. See the android doc on building custom components.
To be able to add views, I think replacing the following line in the sample code
public class MultiTouchView extends View {
with
public class MultiTouchView extends FrameLayout {
should work because FrameLayout is a subclass of ViewGroup. You may want to chose a different layout depending on your needs.

Android GraphicsView enbbeded in layout

How to enbbed a GraphicsView into a layout? Or how do I add buttons and textViews to a GraphicsView? Could some one post a simple working example?
-Henry
I don't think you can add other Views inside a View component.
GraphicsView is not a default Android SDK class, so we can't know what you have there, sorry.
Make a Layout component to be able to add View components inside it.
To embed your GraphicsView in a layout in xml use something like:
<com.package.GraphicsView
id="#+id/graphics"
...
/>

Categories

Resources