Android: How to use 2 layout - android

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.

Related

Image-view not showing in xml defined Frame-Layout on Android application

I have a frame-layout defined in xml a follows:
//pseudo code
<FrameLayout
android:height = "match_parent" // same for width
android:background="#android:color/white"
padding = "20dp"
id = "polygraph"
layout_gravity="center"/>
I access this xml view via code as such:
LayoutInflater inflater = this.getLayoutInflater();
frameInflate = (FrameLayout) inflater.inflate(R.layout.mframe,null);
frar = (FrameLayout)frameInflate.findViewById(R.id.polygraph);
I create the following image-view dynamically but somehow it doesn't show inside this frame-layout which is one of many views inside another frame-layout that holds all the views of my User-Interface
view1 = new ImageView(this);
view1.setAdjustViewBounds(false);
matrix = new Matrix();
view1.setScaleType(ImageView.ScaleType.Matrix);//I have tried: ScaleType.Center it did not work at all
view1.setImageMatrix(matrix);
view1.setBackgroundColor(Color.GRAY);
params11= new FrameLayout.LayoutParams(200,300,Gravity.CENTER);
view1.setLayoutParams(params11);
DummyDraw drawD = new DummyDraw(this);
view1.setImageDrawable(drawD);
frar.addView(view1);
//somewhere down on onWindowsFocusChange()
frar.invalidate(); //try to invalidate to see if it will show the imageview
No image-view shows at all. I have tried different ways, such as:
frar.addView(view1,params11); //did not work
//instead of view1.setImageDrawable
view1.setBackground(drawD); //nothing
view1.setImageResource(R.drawable.somerandomdrawable);//nothing
I have no idea why this view isn't showing. Could it be the drawable "drawD"? If it is this drawable, then why isn't showing at least the background color "gray"
which I set in the code? I have tried to get rid of the ScaleType.Matrix to something else, such as ScaleType.Center, but nothing seems to work.
Any advice or suggestions will be appreciated
thanks
I had ran into the same problem a few months ago. But I did forget how to handle it. LayoutInflater wasn't needed to solve the problem. Seems like I can't do this call inside onCreate() "(FrameLayout)findViewById(R.id.polygraph)" and then try to add my programmatically created image-view to my xml layout element. It gave me a null pointer exception all the time.
Moving both calls way after the layout pass, such as on onStart() or onWindowsFocusChanged() works well. The image-view is added to the xml frame-layout, which in the end it works well for my app, since I had the intentions of adding the image-view well beyond the layout pass.

How to make a zig-zag layout?

I want to create zig-zag layout same as following attached image:
I tried a lot by creating diagonal lines and arranging them with icon but couldn't make it same.
I implemented diagonal lines with the help of accepted answer from following questions:
Diagonal line across view
How rotate line in Android XML?
However I'm stuck to arrange lines with icons exactly same as in image.
I created this custom ZigZagLayout.java file to cater your requirement. You just have to update the package name in the 1st line.
It basically extends RelativeLayout, so you can use it in your layout-xmls just like any other ViewGroup class. Once you have instantiated this layout, just add child-views to it like it is done for RelativeLayout via addView(View child).
Example code snippet with dynamically created view:
ZigZagLayout zigZagLayout = (ZigZagLayout) findViewById(R.id.layout_zigzag);
Button btn = new Button(this);
btn.setText("Test Button");
btn.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
zigZagLayout.addView(btn);
I've also added few interfaces to this ZigZagLayout for your easy interaction like ability to set the connector-line stroke width, visibility, color, margins, etc.
Try it out and let me know if it suffices your requirement. Cheers.
If you have layout for each circular item , you may use relative layout to align them, using align_below, align_left with margin, align_right with margin tags.
Please provide further detail, what are the lines connecting them and exactly what all are requirements for UI and functionality.

Android : How can I have XML layout and the views that I added to my layout?

I have a relative layout and some textviews in it...
I defined this relative layout in the java, and add a view to it.
but in the result I just have the view that I added to layout .. and it doesn't show any text boxes..
SampleView sample= new SampleView(this);
rl=(RelativeLayout) findViewById(R.id.rl);
rl.addView(sample);
how can I fix it? (in this code, it just shows the sample view's object ... not textviews that I have in the layout)
Debugging is your solution.
Make sure that the view that you are adding does not overlap your textView.
Make sure your textview has text. Try checking its size in run time.
And finally use Android Studio layouting tool, to have a preview of your layout. You can use tools to generate something suitable for preview.
I solved it. my mistake was:
canvas.drawColor(Color.WHITE);
in the onDraw ...

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

Drawing into a LinearLayout

I'm trying to draw an image into a LinearLayout, or TableRow, but I'm having already a XML FILE (I want to draw inside some LinearLayout). I don't know exactly how to do it. I was trying this:
TableRow mytable = (TableRow) findViewById(R.id.tablatexto);
ImageView myImage = (ImageView) findViewById(R.drawable.personaje1);
mytable.addView(myImage);
Thanks
Editing: I want to explaint it better. What I want to is creating a animation drawing images depending of a thread. So, I want to draw to some in LinearLayout from the Activity.
If you wan't to add dynamic layout in the container layout then you can use containerLayout.addView(childView);
childView is the dynamic layout which you can create using code or you can get a view of the layout using LayoutInflater class.

Categories

Resources