Passing data from an Activity to a Layout in Android - android

I have an Android Activity as below.
public class DummyActivity extends android.support.v7.app.ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy_layout);
Intent intent = getIntent();
int dots = intent.getExtras().getInt("dots");
}
}
This activity gets the value of dots from another activity without any problem.
Now I want to pass dots to the layout dummy_layout which is given below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.test.DummyView
android:id="#+id/dummyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
I tried creating a hidden field in 'dummy_layout' but could not get it working. Is there any straight forward way I can do this ?. I need to access the value of 'dots' in the init method of 'dummyView' which is the class handling 'dummy_layout' as seen in the layout xml.

Why don't you just do
((DummyView) findViewById(R.id.dummyView)).setDots(dots)
in your onCreate?

Related

Common Header with click events of Header from all Activities in Xamarin android

I am developing a xamarin android application, where I used to call a Header activity in all Activities. My code is as Fallows
My Main.axml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Header aligned to top -->
<include layout="#layout/Header"
android:id="#+id/includeheader"
android:layout_alignParentTop="true" />
<!-- Content below header and above footer -->
<include layout="#layout/Content"
android:id="#+id/includecontent" />
<!-- Footer aligned to bottom -->
<include layout="#layout/Footer"
android:id="#+id/includefooter"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
My Header.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="50dip">
<TableRow
android:background="#2c2c2c"
android:padding="10dip">
<TextView
android:id="#+id/scanHome"
android:layout_width="0dip"
android:layout_weight="2.5"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"
android:text="" />
<Button
android:id="#+id/Settings"
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_marginLeft="10dip"
android:layout_weight="0.17"
android:gravity="center"
android:width="35dip"
android:clickable="true"
android:onClick="SettingsClick"/>
<Button
android:id="#+id/logout"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="0.27"
android:gravity="center"
android:width="40dip" />
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.class
namespace LayoutApp
{
[Activity(Label = "LayoutApp", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Header
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
setHeading("Scan Home");
}
}
}
Header.class
[Activity(Label = "LayoutApp", MainLauncher = false)]
public abstract class Header : Activity , View.IOnClickListener
{
private TextView HeaderText;
private Button Settings;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Header);
Settings = FindViewById<Button>(Resource.Id.Settings);
Settings.Click += delegate
{
};
}
protected void setHeading(string text)
{
if (HeaderText == null)
HeaderText = FindViewById<TextView>(Resource.Id.scanHome);
if (HeaderText != null)
HeaderText.Text = text;
}
public void SettingsClick()
{
}
}
Hence I am using Header Activity in MainActivity like in native android using include Property. When I load Main Launcher, Header is also displaying but click events are not working from the MainActivity where as text is applying from setHeading method.
When debugging , error is populating as 'java.lang.illegalistateexception: could not find a method SettingsClick(View) in the activityclass for onclick handler on view class'.
So, my issue here is I would like to get click events of Header.
The layout files (AXML) are not linked to Activities with the same names as theirs.
In your Main.axml code, you are including (adding) the layout code from Header.axml; however, your MainActivity.cs code has no relations with the Main.axml, just like the HeaderActivity.cs code has no relations with the Header.axml code.
In the MainActivity.cs code, the SetContentView(Resource.Layout.Main) applies the Main.axml layout (which contains the Header.axml code) to your MainActivity, but does not apply the methods from HeaderActivity.cs.
If you want an app with Toolbar and Bottom Bar in your app, there is a good tutorial here: http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/
You should also take a look on Android Fragments.
#kumar Sudheer, I don't know the xamarin development, but I can understand what you want to do by looking at the code and exception you get.
Just pass the View object as parameter in the SettingClick method of your header activity
public void SettingsClick(View v)
{
}
In android when you define the click handler in layout file then the signature of that method would be public void <clickHandler>(View v) {}.
You've not passed the View parameter in the method that's why system is unable to find your method in Activity and that's why you are getting the java.lang.IllegaliStateException

oncreate function doesnt start

Trying to launch another activity from my main activity using a button i placed on my main activity, now when the button is clicked it prints "call activity" and so it works fine, but when the second activity is launched it seems like it doesn't execute oncreate method, as it doesn't print "start" and doesn't call GetCar(); method. The only thing it does is to show the xml.
button that launches second activity (print "call activity" is working fine)
public void allCars(View v){
setContentView(R.layout.activity_cars_menu);
System.out.println("call activity");
}
second activity code (doesn't print "start" or call GetCar().)
public class CarsMenu extends Activity {
Button btn;
public static String[] carId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cars_menu);
System.out.println("start");
GetCar();
}
}
second activity xml (can't see viewlist probably because i can't set text (doesn't call GetCar()) .
<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"
all of my activity is on the manifest and i'm not getting any errors on logcat.
<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"
tools:context="${packageName}.${activityClass}" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="btnCarClick"
android:text="Refresh" />
<ListView
android:id="#+id/MlistView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">
</ListView>
</RelativeLayout>
You might want to start your Activity:
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(myIntent);
Just setting the content view is not enough
Your starting your second Activity in the wrong way, and you only change your view in your first Activity.
In order to change activity you need to call
startActivity (Intent intent)
for more info you can read on
http://developer.android.com/training/basics/firstapp/starting-activity.html
Actually you are just changing the content UI of current activity. As
setContentView(R.layout.activity_cars_menu);
does only that. In real your 2nd activity is never started even. For starting second activity you should do
Intent i = new Intent (Activity1.this, CarsMenu.class);
startActivity (i);
Make sure you have added CarsMenu activity to your Android Manifest.

Not able to set source of an imageview in Included layout in an activity

I have created two XML layout and Activity respecively.
A XML file A contains a layout with Image view Pointed to Activity A.
A XML file B, in that i included XML A and made Activity B extends Acitivity A
In Acivity A oncreate i set the image source for the XML file A. but its not setting the image source and not getting any error also. Plz help me
Here is my code
Xml A
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MasterBaseLayOut"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
XML B
<RelativeLayout 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" >
<include
android:id="#+id/Master"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/xmlA" />
<Button
android:id="#+id/btn1"
android:onClick="btn1_onclick"
android:text="#string/title_1"/>
</RelativeLayout>
Activity A
public class ActivityA extends Activity{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.XMLA);
ImageButton img1 =(ImageButton)findViewById(R.id.imagebutton);
img1.setBackgroundResource(R.drawable.imgtest);
}
}
Activity B
public class ActivityB extends ActivityA {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.XmlB);
}
}
Tough you have no code, but i assume you are not getting the imageView in Activity B.So in Activity
B's onCreate() method call the Activity A's onCreate using super keyword,most probably you will get the imageview with source image.
public void onCreate(Bundle b) {
super.onCreate(b);
}
This is what you have:
ActivityA extends ActivityB
This is what it should be:
ActivityB extends ActivityA
Also, you should consider pressing "Ctrl + Shift + F" to format your code, just a little tip.

Show an advertisement on Android

I have to show an advertisment on the bottom of the screen using leadboltcontroller jar..I have implemented the code and I have given all the sets of permissions as well. But I am not getting any advertisment. Here is my code:
public class Leadbolt_DemoActivity extends Activity {
/** Called when the activity is first created. */
private AdController mucontroller;
private String MY_LB_SECTION_ID="328886602";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout rlbottom=(RelativeLayout)findViewById(R.id.relbottom);
final Activity act = this;
rlbottom.post(new Runnable() {
public void run() {
mucontroller = new AdController(act, MY_LB_SECTION_ID);
mucontroller.setAsynchTask(true);
mucontroller.loadNotification();
mucontroller.setAdditionalDockingMargin(50);
mucontroller.loadAd();
}
//});
});
I have given one addid of another app in another machine but still i am not getting any addid..Is the addid unique for each app & for each machine???Please help me.
use Relativelayout for footer positioning of ads
<?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">
<bla.bla.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF0000"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/ad"/>
here is the post which you are looking for..

Dynamic Strings and Xml

I'm new to Android (and surely I'm not an expert Java developer), so I'm sorry for the dumb question
I would like to "capture" the id of a textView and to set its text value dynamically,
here is how I'm trying to do this:
public class AndStringTestActivity extends Activity {
/** Called when the activity is first created. */
String abc = "abcabc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(abc);
setContentView(R.layout.main);
}
}
and here is the Xml from which i'm reading the id of the textview i would like to write inside
// 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="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
the error i receive is the usual Application stopped unexpectedly
Thanks.
Call setContentView before trying to find the TextView. The TextView does not exist before you have called setContentView (which will inflate your XML layout)
Call setContentView(R.layout.main) before you call findViewById().

Categories

Resources