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.
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);
I'm new to Android, I have a fragment which have this XML code
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:id="#+id/linearRoot"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_name"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:text="bonjour"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_age"
android:layout_below="#+id/person_name"
android:text="ce test a réussi"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
With this one, I want to add several CardView Dynamically.So, as a test, I'm trying to add a second CardView in the LinearLayout this way :
public class TopRatedFragment extends Fragment {
LinearLayout ll;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
ll = (LinearLayout)rootView.findViewById(R.id.linearRoot);
CardView cv = new CardView(getActivity().getApplicationContext());
ll.addView(cv);
return rootView;
}
}
but it does nothing at all. I'm wondering what is the right way to do this.
you have to add LayoutParams (width, height) in order to actually show your CardView
cv.setLayoutParams(new ViewGroup.LayoutParams(20,20));
for example with 20dp width and height
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);
}
i am having a dynamic text view based on the xml file.....Please help me for my code......
my codes
MessageViewPage.java
package com.dpn.pack;
public class MessageViewPage extends Activity {
TextView tv1=new TextView(this);
ScrollView sv;
String nickname,body;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_view_page);
Bundle b = getIntent().getExtras();
nickname= b.getString("nick");
body=b.getString("body");
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_view_page, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.textViewSender);
textView.setText("Sender : "+nickname);
// insert into main view
View insertPoint = findViewById(R.id.textViewSender);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
}
my xml file as follows
message_view_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#drawable/view">
<TextView
android:id="#+id/textViewSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="35dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:id="#+id/textViewBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewSender"
android:layout_below="#+id/textViewSender"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:textSize="15dp"
android:textColor="#F69DD1" />
<Button
android:id="#+id/buttonReply"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginBottom="50dp"
android:text="Reply" />
<Button
android:id="#+id/buttonListen"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="45dp"
android:layout_marginBottom="50dp"
android:text="LIsten" />
</RelativeLayout>
i am having an error in
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Please any one tell me the solution.....
Your insertPoint is null. Because you don't set the layout before calling findViewById. So it will not find anything.
You have to call
setContentView(R.layout.message_view_page);
before.
you use a line RelativeLayout item = (RelativeLayout) findViewById(R.id.textViewSender);
in your code but hoe can you get textViewSenderid with out adding your xml file message_view_page.xml
you should add
setContentView(R.layout.message_view_page);
after
super.onCreate(savedInstanceState); this line.
I just used created by first Dialog using DialogFragment.
Everything works great except I can't get the Dialog to wrap it's layout.
My layout has the height of all elements to wrap_content.
In MyFragmentDialog I can't even find a method that would imply that it can be used to set the height of the FragmentDialog. What am I missing? How do I make a DialogFragment fit it's content?
The DialogFrament's onCreateView method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set title for this dialog
getDialog().setTitle("Backup & Restore");
getDialog().setCancelable(true);
View v = inflater.inflate(R.layout.backup_restore, container, false);
TextView msg = (TextView) v.findViewById(R.id.br_label_message);
msg.setText("Backups are placed in the Downloads Directory:\n" + BACKUP_PATH.getAbsolutePath());
// TextView files_label = (TextView) v.findViewById(R.id.label_restore);
Spinner files = (Spinner) v.findViewById(R.id.br_restore_file);
if (BACKUP_PATH.exists()) {
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String filename) {
File sel = new File(dir, filename);
return filename.contains(FTYPE) || sel.isDirectory();
}
};
mFileList = BACKUP_PATH.list(filter);
} else {
mFileList = new String[0];
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item, mFileList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
files.setAdapter(adapter);
files.setOnItemSelectedListener(this);
Button backup = (Button) v.findViewById(R.id.br_backup_btn);
Button restore = (Button) v.findViewById(R.id.br_restore_btn);
Button cancel = (Button) v.findViewById(R.id.br_cancel_btn);
backup.setOnClickListener(this);
restore.setOnClickListener(this);
cancel.setOnClickListener(this);
return v;
}
This is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="#+id/br_label_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:text=""
android:textSize="14sp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/br_tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/br_label_restore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="8dp"
android:text="Restore file"
android:textSize="14sp" />
<Spinner
android:id="#+id/br_restore_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow>
<Button
android:id="#+id/br_restore_btn"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Restore"
android:textSize="14sp" />
<Button
android:id="#+id/br_backup_btn"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Backup"
android:textSize="14sp" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/br_cancel_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="14sp" />
</LinearLayout>
Thanks,
It turns out to be an issue with LinearLayout despite setting a height on it, in DialogFragment it seems to be taking more space than I want it to. I switched layout to be a RelativeLayout the content Dialog seems to resize to fit the content.
You can make it work with all kind of layout in adding the size of the layout in the onResume of your dialogFragment :
#Override
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(300, 300);
window.setGravity(Gravity.CENTER);
}
I took a leap into the Dialog api so I am certainly not sure but you could try to call getWindow on the dialog and then call setLayout(width, height)
getDialog().getWindow().setLayout(300,300);
I don't see the top portion of your LinearLayout tag, but I've seen cases where all that was needed was the
android:orientation="vertical"
XML attribute. It can cause a headache but is fortunately easy to fix.