I have an activity with a background:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#drawable/background_fingerboard" />
How can I change background image from code? I actually use mono, but Java samples will also be helpful.
first add LinearLayout id as in your layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayoutid"
android:minWidth="25px"
android:minHeight="25px"
android:background="#drawable/background_fingerboard"
and in code part set background as::
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayoutid);
linearLayout.setBackgroundResource(R.drawable.background_fingerboard);
In java we do like this
Give id to LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id=#+id/parentLayout
Then in code
LinearLayout layout=(LinearLayout)findViewById(R.id.parentLayout);
layout.setBackgroundColor(Color.BLUE); Or
layout.setBackgroundDrawable(d); Or
layout.setBackgroundResource(resid);
LinearLayout vi = (LinearLayout ) findViewById(<id>)
vi.setBackgroundResource(R.drawable.<id2>);
you need to provide the id to your LinearLayout in XML as well....
Related
I want to have 3 layouts in my ScrollView but when I add them it actually doesn't scroll. I tried to put all layouts in different files, then include them, tried to put them in ListView and it doesn't work too. In this option, when I put in ScrollView the LinearLayout and there include the rest of the layouts the application show nothing. That's probably becouse I don't know how to refer to that nested layouts... This is my XML:
activity_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="match_parent"
android:layout_height="wrap_content"
>
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include layout="#layout/first"/>
<include layout="#layout/second"/>
<include layout="#layout/third"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
#EDIT views included (all are the same so I put just one):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
And in the main activity I put the objects into layouts like this:
setContentView(R.layout.activity_main);
RelativeLayout first = (RelativeLayout) findViewById(R.id.first);
RelativeLayout second = (RelativeLayout) findViewById(R.id.second);
RelativeLayout third = (RelativeLayout) findViewById(R.id.third);
...some code creating views...
first.addView(myView1);
second.addView(myView2);
third.addView(myView3);
Need to put fix size in included layout, for example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
I don`t understand why do you need to add view manually when you already added it to the layout.
You should also eliminate top Linear Layout since there is no use of it.
I have a custom layout called "debug.xml" in folder "layout/" which is desired to add programmatically into a predefined Layout in activity_main.xml called centerLayout. The "debug.xml" is something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/debug_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/commandEditText"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:inputType="text" />
</LinearLayout>
</LinearLayout>
I want to add this "debug_layout" into a centerLayout:
LinearLayout centerLayout = (LinearLayout) findViewById (R.id.center_layout);
LinearLayout debugLayout = (LinearLayout) findViewById (R.id.debug_layout);
centerLayout.addView(debugLayout);
But it encounters NULL pointer excetion at "centerLayout.addView(debugLayout);". So it seems the debug_layout is not initialized or something. Does anyone can help me?
Are you sure that your centerLayout is not null? If not, did you tried?:
LinearLayout centerLayout = (LinearLayout) findViewById (R.id.center_layout);
LinearLayout debugLayout = getLayoutInflater().inflate(R.layout.debug, centerLayout, false);
centerLayout.addView(debugLayout);
you need to inflate the debugLayout with the LayoutInflater
I am trying to make the java file set the view for the activity dinamically by using a specific number of linearlayouts and setting the source images
so how can I use java code to create a specific number of Image Views
<?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="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/menuline"
android:layout_width="match_parent"
android:layout_height="85dp" >
</ImageView>
</LinearLayout>
</ScrollView>
To create an ImageView (assuming this in an Activity):
ImageView imageView = new ImageView(this);
imageView.set(...) //configure your imageView according your needs
...
imageView.setLayoutParam(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,85); //set width and height
To add your imageView to the linearLayout :
linearLayout.addView(imageView);
Okay, so I have my LinearLayout inside of a ScrollView which I have defined in my xml layout as shown below:
<?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">
<ScrollView
android:minWidth="25px"
android:minHeight="25px"
android:paddingBottom="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container" />
</ScrollView>
</LinearLayout>
I then access this LinearLayout with this code which is contained in an Activity and attempt to add a button to it:
var container = FindViewById<LinearLayout>(Resource.Id.container);
var myButton = new Button(this) {
Text = "Button Text"
};
myButton .Click += delegate { /*Do stuff*/ };
container.AddView(myButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent));
My issue is that the button does not show up when I debug my application. What is it that I am missing?
I guess there was a problem with visual studio. I closed and reopened it and the button seemed to show up. Weird...
My main xml layout file....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayoutPlayerActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundimage"
android:orientation="vertical" >
</LinearLayout>
I created the viewpager.....
that layout file......
<?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="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/carousel_layout_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:overScrollMode="never" />
</LinearLayout>
I load the images into this view pager by creating three classes & another xml file......
now I want to add this viewpager into linearlayout of main xml file....
It means I want dynamically view the viewpager in mainxml file. how to that? pls give me answer
thankyou
If you have two Layouts such as LinearLayout you can add one onto the other with addView(). In your Activity it would look something along the lines of,
LinearLayout layout = (LinearLayout) findViewById(R.id.relLayoutPlayerActivity);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout.addView(inflater.inflate(R.layout.idOfLayoutHoldingYourViewPager, null));