I am having the next problem:
i have an activity with its own layout called camera_layout, when i
had all my layout in that file all went ok,
now to display some of
the views over my extended surfaceview i have added another layout
with the code for those views
in order to add this linearlayout to
the frame layout that contains my extended surfaceview after i add
the the surfaceview, on my oncreate method i set some views using
findviewbyid,
at this moment all is ok
after this on my onpreview
method i get a nullpointer exception at that views i set.
Here is my code:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("Function", "onCreate iniciado");
setContentView(R.layout.camera_layout);
//tFrame=new Target_Frame(this);
cViewActual=(CView)findViewById(R.id.cViewActual);
bestMatchCView=(CView)findViewById(R.id.cViewBestMatch);
actualCLabelTextView=(TextView)findViewById(R.id.textViewActualC);
bestMatchLabelTextView=(TextView)findViewById(R.id.BestMatchtextViewLabel);
coNametextView=(TextView)findViewById(R.id.textViewCName);
mCamera=getCameraInstance();
mPreview=new CameraPreview(this,mCamera,cViewActual,bestMatchCView,colorNametextView);
FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
hasFlash=checkFlash(this);
flashActivated=false;
flashButton=new ImageButton(this);
flashButton.setImageResource(R.drawable.flashofficon);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
params.gravity=Gravity.CENTER;
flashButton.setLayoutParams(params);
addListenerToFlashButton();
preview.addView(mPreview);
preview.addView(flashButton);
View itemLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_bar_layout,null,false);
preview.addView(itemLayout);
}
My camera layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="#+id/button1"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_gravity="top|left"
android:text="Button" />
</FrameLayout>
</LinearLayout>
And my layout for the views I want to put over the surfaceview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="113dp"
android:layout_height="fill_parent"
android:gravity="right"
android:id="#+id/itemToolLayout"
android:orientation="vertical">
<TextView
android:id="#+id/textViewActualC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Actual Color"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.example.prueba.CView
android:id="#+id/cViewActual"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/BestMatchtextViewLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Best Match"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.example.prueba.CView
android:id="#+id/cViewBestMatch"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/textViewCName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture" />
</LinearLayout>
use
actualCLabelTextView=(TextView)findViewById(R.id.textViewActualC);
instead of
actualCLabelTextView=(TextView)findViewById(R.id.textViewActual);
because TextView with textViewActual id not exist in current Activity layout it is textViewActualC
and always attach relevant logcat results with question if application is crashing
I have changed some things that were wrong and i post here the new code, the problem was that my cViewActual and all the views after this are null, because they aren´t on my principal layout file, i thought that after inflate a view->itemlayout with the layout of my secondary layout file i could access to the views of that layout using findViewById, but it seems that this doesn´t find the views, now i change some thing to use findViewById on itemlayout view and now i can find the views of the secondary layout
Here is my modified code:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("Function", "onCreate iniciado");
setContentView(R.layout.camera_layout);
FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
hasFlash=checkFlash(this);
flashActivated=false;
flashButton=new ImageButton(this);
flashButton.setImageResource(R.drawable.flashofficon);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
params.gravity=Gravity.CENTER;
flashButton.setLayoutParams(params);
addListenerToFlashButton();
View itemLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_bar_layout,null,false);
tFrame=new Target_Frame(this);
cViewActual=(ColorView)itemLayout.findViewById(R.id.colorViewActual);
bestMatchCView=(ColorView)itemLayout.findViewById(R.id.colorViewBestMatch);
actualColorLabelTextView=(TextView)itemLayout.findViewById(R.id.textViewActualColor);
bestMatchLabelTextView=(TextView)itemLayout.findViewById(R.id.BestMatchtextViewLabel);
colorNametextView=(TextView)itemLayout.findViewById(R.id.textViewColorName);
mCamera=getCameraInstance();
mPreview=new CameraPreview(this, mCamera,cViewActual,bestMatchCView,colorNametextView);
preview.addView(mPreview);
preview.addView(flashButton);
preview.addView(itemLayout);
}
Related
This XML is inflated from my main activity, I would like to know if how do I set the inflated view as full screen or fill_parent as shown in the XML, because when I test my app it will give me a half size screen shown on the right side.
The first pic is from my pc, and the second is from my tab.
Here's my XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal|center_vertical"
android:background="#drawable/splash"
android:paddingBottom="#dimen/_16sdp"
android:paddingLeft="#dimen/_16sdp"
android:paddingRight="#dimen/_16sdp"
android:paddingTop="#dimen/_16sdp" >
<Button
android:id="#+id/g1btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/g1tv2"
android:layout_centerHorizontal="true"
android:text="BUTTON"
android:textSize="#dimen/_70sdp" />
<TextView
android:id="#+id/g1tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""THIS""
android:textSize="#dimen/_70sdp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/g1tv2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="TAP"
android:textSize="#dimen/_70sdp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="GAME 1"
android:textSize="#dimen/_50sdp" />
</RelativeLayout>
Here's my MainActivity.Java
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
View game1 = getLayoutInflater().inflate(R.layout.game1, null);
RelativeLayout item = (RelativeLayout)findViewById(R.id.Main);
item.addView(game1);
}
The problem is not your Wrapping layout, you are adding a view to another View with no layout params with respect to the parent.
Tell your inflated view, this is your parents layout params, for example:
View view = getLayoutInflater().inflate(R.layout.game1, null);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id. Main);
view.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
relativeLayout.addView(view);
this should solve your problem without just putting a LinearLayout wrapping it.
Good Luck and Happy coding!
Add this code before setContentView();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Use Linear layout as the parent View and give android:orientation = "vertical". Relative Layout is a a Layout where the positions of the children are described in relation to each other or to the parent.If you want to use relative layout then you have to use the addrule property or else all the child will be drawn over the previous child.
View game1 = getLayoutInflater().inflate(R.layout.temp, null);
LinearLayout item = (LinearLayout)findViewById(R.id.main);
item.addView(game1);
The following works, but I want to eliminate the xml so that I can change the image programmatically:
Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p6_touch);
ImageView floatImage = (ImageView) findViewById(R.id.p6_imageView);
floatImage.setOnTouchListener(this);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_black" >
<ImageView
android:id="#+id/p6_imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:src="#drawable/p6_trail_map" >
</ImageView>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:onClick="showFlashlight"
android:src="#drawable/legend_block24" />
</FrameLayout>
In the documentation there is a table that maps XML to Java.
For instance: android:src is equivalent to setImageResource().
You will need to check the inherited table for attributes from any super class.
For instance: android:id is equivalent to setId().
width, height, and gravity are all set in a LayoutParams object and passed to setLayoutParams().
Understand that not every XML attribute has a matching Java method (and vica versa), but all of the attributes you are using do.
An Example, let's call this file activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_black" >
<!-- We'll add this missing ImageView back in with Java -->
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:onClick="showFlashlight"
android:src="#drawable/legend_block24" />
</FrameLayout>
Now in our Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Let's create the missing ImageView
ImageView image = new ImageView(this);
// Now the layout parameters, these are a little tricky at first
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
image.setScaleType(ImageView.ScaleType.MATRIX);
image.setImageResource(R.drawable.p6_trail_map);
image.setOnTouchListener(this);
// Let's get the root layout and add our ImageView
FrameLayout layout = (FrameLayout) findViewById(R.id.root);
layout.addView(image, 0, params);
}
I want to put a layout into another one via layoutInflater.
Here's the main_layout.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/ads_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:id="#+id/host_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/navigation_buttons_layout"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ads_image_view" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/navigation_buttons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
and here's the vitrin_layout.xml file:
<?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:background="#ff0000"
android:text="smallred" />
and finally the onCreate method for my main activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout);
View view = LayoutInflater.from(getBaseContext()).inflate(
R.layout.vitrin_layout, null);
parent.addView(view, LayoutParams.FILL_PARENT);
setTitle(getString(R.string.title_activity_vitrin));
}
The problem is that the vitrin_layout, does not fit into its parent(host_layout); although I've set fill_parent where ever I could!
I can't figure out why.
Edited: I'm really sorry, but i'd pasted wrong vitrin_layout file, I fixed it.
Use this addView method. By the way don't use getBaseContext() unless you know why you are using it, instead use context of an activity (this).
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout);
View view = LayoutInflater.from(this).inflate(R.layout.vitrin_layout, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
parent.addView(view, params);
setTitle(getString(R.string.title_activity_vitrin));
}
parent.addView(view, LayoutParams.FILL_PARENT);
this method was ViewGroup.addView(View child, int index) invoked actually.
you need to invoke ViewGroup.addView(View child, ViewGroup.LayoutParams params) method to set a LayoutParams.
i got a problem with setContentView which throws me a null pointer.
In my mainFrame i got a small SurfaceView which takes about 50% of the screen.
I wanted to implement a method which makes that SurfaceView go Full Screen if you long clicked it. I did it like this:
final FrameLayout frame = (FrameLayout) findViewById(R.id.frame_layout);
frame.setOnLongClickListener(new OnLongClickListener() {
boolean clicked = false;
#Override
public boolean onLongClick(View v) {
if (clicked){
clicked = false;
(MyActivity.this).setContentView(R.layout.main);
} else {
clicked = true;
(MyActivity.this).setContentView(R.layout.fullScreen);
}
return true;
}
});
Can someone help me how to fix this problem?
Greetings
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical"
android:weightSum="1"
android:background="#drawable/background_new" >
<TextView
android:text="#string/measures"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/text_measures"
android:textColor="#ff444444"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
/>
<ImageButton
android:contentDescription="#string/desc_edge"
android:layout_toRightOf="#id/text_measures"
android:id="#+id/edges_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edges_black"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="30dp"
android:onClick="edge_handler"
android:background="#null"
/>
<FrameLayout
android:background="#xml/border"
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="35dp"
android:layout_marginRight="200dp"
android:layout_marginTop="80dp"
android:layout_below="#id/text_measures"
>
</RelativeLayout>
I know it looks like a FrameLayout but dont get fooled it was done by someone else and it is actually a SurfaceView.
Just change the layout parameters of the SurfaceView onLong click of frame. No need to setContentView again.
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
surfaceView.setLayoutParams(lp);
Got my TextView in the xml:
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:text="TextView"/>
I want to create multiple TextViews but i want to look them the same as this.
So I tried:
TextView newTextView = new TextView(this);
newTextView.setLayoutParams(myTextView.getLayoutParams());
I think this should get all the layout parameters from myTextView straigthlghly(?) from the xml, and pass them to newTextView to set them.
My problem is: Nothing happens. It does not take effect, why?
here's a sample project which shows that it works.
you can see that it works by looking at the preview of the visual editor , which looks different than what is shown at runtime.
i think that your mistake was that you didn't set 0px (or 0dp, zero is still zero) for the weighted views.
main.xml (layout) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/container">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="0px" android:text="TextView1"
android:layout_weight="1" android:background="#ffff0000" />
<TextView android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="TextView2"
android:background="#ff00ff00" />
</LinearLayout>
TestActivity.java :
public class TestActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv1=(TextView)findViewById(R.id.textView1);
final TextView tv2=(TextView)findViewById(R.id.textView2);
final LayoutParams layoutParams=tv1.getLayoutParams();
tv2.setLayoutParams(layoutParams);
// adding textView programatically:
final TextView tv3=new TextView(this);
tv3.setText("textView3");
tv3.setBackgroundColor(0xff0000ff);
tv3.setLayoutParams(layoutParams);
final ViewGroup root=(ViewGroup)findViewById(R.id.container);
root.addView(tv3);
}
}