I have this XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fondoverde3"
android:gravity="top"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="adasdasdasdas"
ads:loadAdOnCreate="true" />
</LinearLayout>
<ScrollView
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
......
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout4"
android:layout_marginTop="10dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
So what I would like is to refer in my java code to the LinearLayout2 an add some things. I prove with an easy code to know that my code works (and it works!! I show the two textviews), exactly with this:
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tituloIngr2 = new TextView(this);
tituloIngr2.setText("AAAAA");
ll.addView(tituloIngr2);
TextView tituloIngr1 = new TextView(this);
tituloIngr1.setText("BBBBB");
ll.addView(tituloIngr1);
But what I would like to do is to show in this linearLayout2 many Textviews that I take from a StringArray, so the code is the next:
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
ll.setOrientation(LinearLayout.VERTICAL);
for (int m= 0; m<few.length; m++) {
TextView ingr= new TextView(this);
ingr.setText(few[m]);
ingr.setPadding(5, 5, 5, 5);
ingr.setGravity(0x11);//sacados todos los values de developers
ingr.setTextColor(0x96610098);
ingr.setBackgroundResource(android.R.color.white);
ingr.setTextSize(22);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 25, 0, 0);
ll.addView(ingr, layoutParams);
}
this.setContentView(ll);
The error is a force close, and the log cat:
11-27 17:09:58.213: E/AndroidRuntime(1440): java.lang.RuntimeException: Unable to start activity ComponentInfo{back.now/back.now.Tree}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Remove this call:
this.setContentView(ll);
ll is already part of the content view. Trying to add it again as the content view is likely what's triggering the exception. You also don't need this call:
ll.setOrientation(LinearLayout.VERTICAL);
The orientation is specified as vertical in the xml.
Related
I want to display a picture advertisement at the bottom of the display area. I've tried using bottom but it didn't work. Can you help me?
MainActivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
<LinearLayout
android:id="#+id/adLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"></LinearLayout>
<ListView
android:id="#+id/ilcelerListview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
Picture link : For Example
I can give your two tip to do that.
You can use RelativeLayout instead of LinearLayout at MainActivity.
For that, you can easy to add a view which you want to add to bottom.
Or
ViewGroup rootView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
//you can use params to set in the bottom or other positions
//view is yours
rootView.addView(view, params);
i'm very new to Android and AChartengine and i'm working in my project.
Assume i have a Layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/displayECG"
android:layout_width="fill_parent"
android:layout_height="100px"
android:orientation="horizontal" >
</LinearLayout>
And in MainActivity.java i code like:
view = ChartFactory.getLineChartView(this, dataSet, renderer);
LinearLayout layout = (LinearLayout) findViewById(R.id.displayECG);
layout.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
But when i run my App and i got a graph, it very smaller than layout space. I want a graph, it fits with my layout, so how can i do that?
Thanks,
This question already has answers here:
android add checkbox dynamically
(3 answers)
Closed 9 years ago.
I have to add CheckBox dynamically to my Activity layout, the XML of the layout is as follows
`
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentSV"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/parentLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<RelativeLayout
android:id="#+id/feedbackRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/feedbackCustomerNameLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Name : "
android:textColor="#android:color/black" />
<TextView
android:id="#+id/feedbackCustomerName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textColor="#android:color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/feedbackPlansLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/feedbackCustomerNameLL"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Plans Explained"
android:textColor="#android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/feedbackPlansCheckBoxLL"
android:orientation="horizontal" >
<!--
Required to Add CheckBoxes Here Dynamically
-->
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
`
CheckBoxes are to be added in the commented area. How to add them dynamically, because I have to add them in runtime based on data sent from server. I cannot remove the ScrollView or any other view from the hierarchy.
Give that layout an id such as...
<LinearLayout
android:id="#+id/check_add_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/feedbackPlansCheckBoxLL"
android:orientation="horizontal" >
<!--
Required to Add CheckBoxes Here Dynamically
-->
</LinearLayout>
Initialize a parent layout using the given id as...
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.check_add_layout);
Then create your CheckBox...
CheckBox checkBox = new CheckBox(this);
checkBox.setId(id);
checkBox.setText("text");
Create parameters about its size, padding, alignment...
LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
checkParams.setMargins(10, 10, 10, 10);
checkParams.gravity = Gravity.CENTER;
now add this newly created CheckBox to the that parent layout...
parentLayout.addView(checkBox, checkParams);
Follow following steps to add CheckBox dynamically :
STEP 1: Assign id to LinearLayout in xml to for accessing in code :
<LinearLayout
android:id="#+id/yourlayout"
....
/>
STEP 2: access layout in code :
LinearLayout linear=(LinearLayout)findViewById(R.id.yourlayout);
STEP 3: Add CheckBox to layout :
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<your_count;i++){
CheckBox checkBox = new CheckBox(context);
checkBox.setLayoutParams(lparams);
linear.addView(checkBox);
}
Try This:
LinearLayout layout = (LinearLayout) findViewById(R.id.yourlayout);
CheckBox chkTeamName = new CheckBox(this);
chkTeamName.setText(teamName.get(i));
chkTeamName.setTextColor(Color.BLACK);
layout.addView(chkTeamName);
CheckBox checkBox = new CheckBox(context);
// set the properties including layoutparams
// Then add it to parent
Parent.addView(checkBox);
Give an ID to your LinearLayout and do
LinearLayout linear = (LinearLayout) findViewById('your id');
Checkbox cb = new CheckBox(this);
//Give customization to it
linear.addView(cb);
Simple. :) Isn't it. Hope this helps :)
I manage to add adMob to the title screen, which layout is written in a *.xml-file.
setContentView(R.layout.main);
AdView adView = (AdView)findViewById(R.id.ad);
adView.requestFreshAd();
main.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/de.xazen.tictactoe"
android:background="#drawable/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
/>
</RelativeLayout>
The game itself is written in Tttview.
public class TttView extends View
The Activity Game.java create a instance of TttView and uses setcontent(tttView) to use it as layout.
public class Game extends Activity{
private TttView tttView;
.
.
.
setContentView(tttView);
}
How can I add adMob to the game itself?
Well it's not obvious from your question what tttView is or how it is setup, but if it creates a layout type to display you can add a AdView programatically into a LinearLayout or similar like this:
// Setup layout parameters
LinearLayout.LayoutParams params;
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Create a linear layout
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6,6,6,6);
AdView mAdView = new AdView(this);
layout.addView(mAdView,params);
Or if you are loading the layout from an XML file you can do similar what you have above
setContentView(R.layout.game);
Where game.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/de.xazen.tictactoe"
android:background="#drawable/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<tttView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
...
/>
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
/>
</RelativeLayout>
I can't seem to control the dialog width. I have a simple layout like so`
<?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:theme="#android:style/Theme.Dialog">
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/name_prompt_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/name_prompt"
android:padding="10dip"/>
<EditText
android:id="#+id/name_inp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:maxLength="48"
android:inputType="text" />
<TextView
android:id="#+id/t1_prompt_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/t1_prompt"
android:padding="10dip"/>
<Spinner
android:id="#+id/t1_inp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:maxLength="48"
android:inputType="text"
android:singleLine="true"
android:layout_weight="1"
android:entries= "#array/t1_allowed_values" />
</LinearLayout>
</ScrollView>
</LinearLayout>
for some reason the dialog is only wide enough for the text input field about 11 chars wide. How do I make the dialog width fill the screen?
I had the same problem.
I used following code to make dialog fill_parent and it worked fine.
public class SharePost extends Dialog
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.adaptor_contentsharepost);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/dialogWidth"
android:layout_width="match_parent"
android:layout_height="match_parent">
contents here
</LinearLayout>
I'm using:
getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Set a minimum width at the top most layout.
android:minWidth="300dp"
For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="300dp">
<!-- Put remaining contents here -->
</LinearLayout>
As Matthias points at out How can I get a Dialog style activity window to fill the screen? UMAR's solution works, but only if the window attributes are set AFTER setContentView() is called.
Try this. It works for me.
dialog = new Dialog(Activity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.feedback_popup);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
Since API 8 FILL_PARENT is deprecated. Use MATCH_PARENT instead.
params.height = LayoutParams.MATCH_PARENT;