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);
Related
I have this XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_bar"
style="?android:buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/md_green_400" />
<Button
android:id="#+id/action_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/md_green_400" />
</LinearLayout>
It´s just a buttonBar with two borderless buttons. It works. However, I don't want that, I need to inflate these buttons from a JSONArray. So I did this:
for (int b = 0; b < buttons.length(); b++) {
final JSONObject button = buttons.getJSONObject(b);
LinearLayout buttonBar = (LinearLayout) child.findViewById(R.id.button_bar);
View buttonChild = getLayoutInflater().inflate(R.layout.flat_button, null);
Button action = (Button) buttonChild.findViewById(R.id.action_button);
action.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {}
});
action.setText(button.getString("descricao"));
action.setTextColor(Color.parseColor(button.getString("text_color")));
buttonBar.addView(buttonChild);
}
It works too, but the buttons get a left alignment. I want they justified.
Why it works when I let them fixed but not when I inflate them?
OBS: The "button_bar" is A XML just with a LinearLayout and the "ActionButton" is just a XML with a Button.
This is the root of your problem:
View buttonChild = getLayoutInflater().inflate(R.layout.flat_button, null);
If you don't provide the parent view to the inflate() method, any LayoutParams attributes (e.g. layout_gravity) will be discarded since the parent is the one to interpret those attributes.
You can fix this by changing it to:
View buttonChild = getLayoutInflater().inflate(
R.layout.flat_button, buttonBar, false);
Which will give it the parent you're attaching it to, but not attach it to the hierarchy yet (you do that below with addView()).
I have codes of a RelativeLayout activity.xml
<RelativeLayout
android:layout_height="50dip"
android:layout_width="fill_parent"
android:id="#+id/relativeLayout"
android:background="#686868"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="#FFFFFF"
android:id="#+id/btnBack"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/title_header"
android:text="Header of page"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
/>
</RelativeLayout>
And here is code of Activity:-
public class MainActivity extends Activity {
int valCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
if(valCheck==0)
{
//here set title again for backbutton is "backPreviousPage" and set
//position again of title_header is right of backButton with
//margin=5dip(not set position is centerInParent as code
}
}
}
I don't know the way to get element and set margin in relativeLayout by code..Can you help me?
This may helps you
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.RIGHT_OF, 1001);
params.addRule(RelativeLayout.LEFT_OF, 1000);
Here id 1001 and 1000 is my EditText id which also dynamic added to Relative
To access UI Elements within Android code, you use the findViewById method. For example:
Button btnBack = (Button)findViewById(R.id.btnBack);
Once you have access to all of the elements you need, you can use the setter functions provided in the API to change the layout as need-be. Since most (if not all) UI elements are a child class of the View class, the methods you will need will most likely be defined there. Here is a good reference:
http://developer.android.com/reference/android/view/View.html
All View objects inherit the setLayoutParams method. So you can apply the method suggested by Gunaseelan to your Button and your TextView as well.
Try this code....
<LinearLayout
android:layout_height="50dip"
android:layout_width="fill_parent"
android:id="#+id/linearLayout"
android:background="#686868"
android:orientation="horizontal"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="#FFFFFF"
android:id="#+id/btnBack"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/title_header"
android:text="Header of page"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
/>
</LinearLayout>
and in java code...
public class MainActivity extends Activity {
int valCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button btn = (Button) findViewById(R.id.btnBack);
if(valCheck==0)
{
//here set title again for backbutton is "backPreviousPage" and set
//position again of title_header is right of backButton with
//margin=5dip(not set position is centerInParent as code
btn.setText("backPreviousPage");
}
}
}
Try this way.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
params.setMargins(80, 0, 0, 0); //left, top, right, bottom
params.height = 60; //set height dynamically
params.width = 200; // set width dynamically
relativeLayout.setLayoutParams(params);
I hope this will help you.
I've programmed one of my layouts programatically. And when I tried to implement it in XML I couldn't make it work. It crashes with NullPointerException and I really do not know why.
This is my XML layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DisplayMessageActivity" >
<ImageView
android:id="#+id/canal_1"
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="canal1_Click"
android:src="#drawable/pestanya_seleccionada" />
</RelativeLayout>
And what I'm trying is:
ImageView canal1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* layout prinicpal */
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
canal1 = (ImageView) findViewById(R.id.canal_1);
relativeLayout.addView(canal1);
setContentView(relativeLayout, rlp);
}
It crashes at relativeLayout.addView(canal1);
I don't why this fails. In my head everything should run fine.
Thanks for reading and hope you can help me ;)
Kind regards,
Raúl
You have not set content of the xml layout to the screen and you are finding the id of the ImageView. This is causing NPE.
canal1 = (ImageView) findViewById(R.id.canal_1);
The above statement will cause nullpointerexception since you have not set the layout and your are trying to find the id form the defined in the xml file.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
//add other ui elements to the root layout ie RelativeLayout
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeLayout"// relative layout id
android:orientation="vertical"
tools:context=".DisplayMessageActivity" >
<ImageView
android:id="#+id/canal_1"
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="canal1_Click"
android:src="#drawable/pestanya_seleccionada" />
</RelativeLayout>
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);
}
The problem here I don't get the same output view result in these two cases, I want to fix case 1 to get same output result as case 2:
Inflate textview and linearlayout, and then add textview to linearlayout programmatically.
Add textview to linearlayout in xml.
Code and Output For Case 1:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#000000"
android:text="1"
android:textSize="20sp"
android:background="#AAAAAA"
android:gravity="center"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
/>
onCreate method in LayoutTestActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lt = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);
lt.addView(tv);
setContentView(lt);
}
output view (Not Correct)
Code and Output For Case 2:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="#000000"
android:text="1"
android:textSize="20sp"
android:background="#AAAAAA"
android:gravity="center"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
/>
</LinearLayout>
onCreate method in LayoutTestActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
output view (Correct)
I'm not sure if it makes any difference - but my suggestion is this:
Provide the android:id="#+id/linearLayout"-tag for the LinearLayout in your main.xml.
Then do this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lt = (LinearLayout) findViewById( R.id.linearLayout );
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);
lt.addView(tv);
}
You might also want to test if there is a difference between supplying null or your LinearLayout lt as the second parameter when inflating the TextView e.g.:
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, lt);
You have to set the LayoutParams in the first case. In the second this is done automatically since TextView is in the xml. Hope this helps!
I think in the first place that you have to set the main layout in the setContentView() method directly and afterwards make the inflate.
setContentView(R.layout.main);
LinearLayout mainLayout = (Linearlayout) findViewbyId(R.id.mainLayoutId);
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, mainLayout, false);
mainLayout.addView(tv);
I think this way it should work.
To keep the xml layout params you have to pass the last two parameters to the inflate() method.