How to connect a layout view with an activity - android

In my main view, I have:
public class PlayersActivity extends Activity {
ViewFlipper flipper;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playercontainer);
flipper = (ViewFlipper) findViewById(R.id.flipper);
}
}
with this view:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="#+id/first" layout="#layout/first" />
<include android:id="#+id/second" layout="#layout/playerdetailsview" />
</ViewFlipper>
It displays the first view correctly but I want it to be connected to a java class so I created an FirstActivity class where I can control all my components in the first view
but how do I attach the first.xml layout with the FirstActivity java class ?

Say your new xml file is foo.xml:
Put foo.xml file in your res/layout directory.
In your new class use setContentView(R.layout.foo);
Specify your new class in your manifest file.
See also the topic on declaring layout.

1) Create an xml file (say foo.xml).
2) Put foo.xml in res/layout directory.
3) Edit foo.xml and put some android layout code and save it. e.g.,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ViewFlipper>
</LinearLayout>
4) In your new activity class put
setContentView(R.layout.foo);
For creating an activity see this answer
I guess the problem with your xml file is that you had not specified any layout for the activity.

Not so hard to link 2 layouts just do :
#Override
public void onClick(View args0) {
setContentView(R.layout.aardelayout);
}

Change the name from FirstActivity to firstactivity.
Layout does not accept caps.

Related

android:fragments crash on screen Orientation

I've made two XML files one for portrait mode and the other is for landscape
When i start the app it runs normally and after flipping the screen for the second time it crashes and gives this error:java.lang.IllegalStateException: Fragment has not been attached yet.
Eventhough the landscape xml file contains static fragments and the portriat one contains nothing(in order to add the fragments dinamically on further steps).Here is my XML 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">
<fragment
android:name="com.example.alihaidar.fragmentlab.fragList"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/frag_list"
/>
<fragment
android:name="com.example.alihaidar.fragmentlab.fragContent"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:id="#+id/frag_content"
/>
</LinearLayout>
and this is mainActivity it contains nothing :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
You need to make a validation to check if the Fragment is attached to its parent using isAdded() method:
if (!isAdded()){
return; //Not attached
}else{
//Attached!
}
isAdded() Return true if the fragment is currently added to its
activity.
Another option, define in your activity the property:
<activity
android:configChanges="orientation|keyboardHidden|screenSize">
to avoid the destruction of the parent activity when you rotate the device.

android programmatically using shape

I am trying to run MainActivity which extends View but its not running. Do i have to use MainActivity extends Activity to run the application because i want to use draw() method in MainActivity class.Can i use it directly or i have to use View instead of activity?.
Here is my code
public class MainActivity extends View {
public MainActivity(Context context) {
super(context);
}
Main.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
Yes your main activity (and any other) MUST extend Activity.
If you want to make your own, custom control, just write it (extend view or some of it's descendatns) and put it into your main layout as any other control.
In your custom control you can override onDraw() method if you want.
Take a look at the docs:
Custom controls
Try something like:
class MyActivity extends Activity {
...
#Override
protected void onCreate {
...
setContentView(R.layout.main);
....
}
}
After you setContentView, you can reference it by calling findViewById(<LinearLayout's id>). In your layout you can place the view that you want to do the drawing
Your activity should extend Actvity and you have get your draw image as view which is a object of class that extends View
I hope this will be usefull to you.

changes in main.xml are not reflected in the android app in netbeans

I am new to android apps. I am using the netbeans 7.0.1 IDE to develop android apps. I have written the following code in the main java file:
package com.test.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class helloworld extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t1=new TextView(this);
t1.setText("hello world..!!!!");
setContentView(t1);
}
}
This was working fine.
I edited the main.xml file to display a textfield and button as follows:
<?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"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"/>
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"/>
</LinearLayout>
Of course I have added all the corresponding strings in strings.xml. But when I try to run my app these weren't displaying... :( . I mean the same string that was displayed previously was being displayed.
Can anybody figure out what is the mistake??
Remove below lines from onCreate method of your activity
TextView t1=new TextView(this);
t1.setText("hello world..!!!!");
setContentView(t1);
Because you are setting the contentView from the main.xml and then again you are creating the TextView dynamic and setting that TextView as a contentView. so you are getting the static string "hello world..!!!!"
Edit
There is a spelling mistake in your android:oreintation line which you are using for set orientation. use "android:orientation" instead.
remove setContentView(t1); from your code
public void setContentView (int layoutResID)
Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
public void setContentView (View view)
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy.
from developer.android.com
Since you called it two times, the second call did override the first view.
Right Clicck your project and clean it ..
uninstall your app from device or emulator.....
then again reinstall .....
i hope it will work my friend

The LinearLayout returned is null in the subclass of PreferenceActivity

I have a SettingsActivity which is a subclass of PreferenceActivity in my library project
The oncreate method looks like this
addPreferencesFromResource(R.xml.preferences);
setContentView(R.layout.main);
The preference.xml file has structure like element PreferenceScreen -> PreferenceCategory -> Preference
My main main.xml Looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!--Listview will be replaced by preferences -->
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The above class SettingsActivity is subclassed in another project and Looks like this
public class SettingsActivityFree extends SettingsActivity
{
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//...
// layout returned will be null ..
LinearLayout layout = (LinearLayout)findViewById(R.layout.main);
// Add the adView to it
layout.addView(adView);
}
The problem is I am trying to get LinearLayout from the parent class so that I can add my stuff in it, but for some reason it returns null , the reason I have LinearLyout in the SettingsActivity class is because I want to put some ads etc at the top of preferences and without putting ad specific code in the Library project
Please advise If I am missing something here,
Thanks
First you need to give your LinearLayout an id using android:id="#+id/whatever". Then you can search for it using findViewById(R.id.whatever).
You should really check the developer site. You are not doing it correctly. Look up the documentation on ListView and ListActivity. Learn how it works before using it.
Also as a hint:
You don't do this (LinearLayout)findViewById(R.layout.main);
When retrieving a view, you will find it in R.id.(whatever) not in R.layout.(whatever)

List View and ListActivity question in Android

I am trying to use List View and List activity. My code is very simple, here are my java and xml files. The MyRecords.java file is:
public class MyRecords extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myrecords);
.
.
.
myrecords.xml file is:
<?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:padding="10dp"
android:id="#+id/myrecords">
</TextView>
and I have added this to the Manifest file:
<activity android:name=".MyRecords"
android:label="#string/myrecords_title" >
</activity>
in my main activity page I have a button corresponding to MyRecords.
The problem is that as soon as I press the button the application force closes.
I appreciate any help in letting me know what is wrong with this code.
I must mention that when instead of ListActivity, I had activity, there was no problem with the code.
Thanks for the help.
TJ1
A ListActivity needs to be supplied a layout that contains a ListView with the correct id (I think it's android:id/list).

Categories

Resources