I am new bie in android.
Below is my senerio what i have and what i actually need.
I have one activity where i have declare one custom view which contains a set of button which click event display respective fragments.
This custom view will be appear on top of each fragment.
Now suppose on first button of custom view i am display fragment a fragment which display list-view..
on click on list-view it show another fragment i.e detail fragment
In detail fragment.. i have one button where i need to show an fragment overlay on main activity ... as on full screen.. how can i achive this?
In my opinion easiest way lead to this behavior is use Activity Overlay or DialogFragment. Example:
Activity Overlay
MainActivity.java
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.buttonShow);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
MainActivity.xml
<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"
android:id="#+id/mainLayout"
tools:context=".MainActivity"
android:background="#android:color/holo_orange_dark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Activity"
android:textSize="30dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:text="show"
android:id="#+id/buttonShow"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
SecondActivity.java
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
SecondActivity.xml
<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"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".SecondActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
Styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="TransparentFloatingActivity" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arturszymanski.test" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:theme="#style/TransparentFloatingActivity"
android:name=".SecondActivity"
android:label="#string/title_activity_second" >
</activity>
</application>
</manifest>
DialogFragment
MyDialogFragment.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyDialogFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FullScreen Fragment"
android:textSize="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
MyDialogFragment.java
public class MyDialogFragment extends DialogFragment
{
private int margin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
margin = 10;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my_dialog, container, false);
}
#Override
public void onResume() {
int dialogHeight = MainActivity.displayMetrics.heightPixels - (margin * 2) - MainActivity.StatusBarHeight;
int dialogWidth = MainActivity.displayMetrics.widthPixels - (margin * 2);
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
super.onResume();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
{
public static DisplayMetrics displayMetrics;
public static int StatusBarHeight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
StatusBarHeight = getStatusBarHeight();
Button button = (Button) findViewById(R.id.buttonShow);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getFragmentManager(), "Dialog");
}
});
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
Other Files
Other files are the same as in example above.
If i follow your question than, You should use a RelativeLayout as overlay in main activity's layout. and set visibility according to your need.
layout_activity_main.xml
<RelativeLayout
android:width="match_parent"
android:height="match_parent">
<LinearLayout>
// your toolbar
// You fragment container
// your main layout goes here
</LinearLayout>
<RelativeLayout
android:id="#+id/overlay"
android:width="match_parent"
android:height="match_parent">
</RelativeLayout>
</RelativeLayout>
like this :
<LinearLayout id = "#+id/container">
<ToolBar id ="#+id/toolbar>
</ToolBar>
<FrameLayout id ="#+id/main_containt"/>
</LinearLayout>
getSupportFragmentManager().add(new Fragment(),R.id.container).commit();
use container as the container of fragment;
Related
I tried to make a transition between the two activities using a shared TextView as shown in Google I/O 2016, but it works strangely since the text doesn't seem to fit into some container and its edges get cut off when the animation plays, though nothing should interfere with its scaling. I was putting it in other containers and tried different combinations, but the result was exactly the same. That said, when I go back from the second activity to the first, everything works as it should. A class for scaling text, TextResize.java, has also been added to the project and included in shared element transition set between two activities.
How can I fix incorrect text scaling animation when navigating to second activity?
Video of how it works now (in slow motion): link
Main Activity:
public class MainActivity extends AppCompatActivity {
private TextView textView;
public static final String SHARED_TEXT_SIZE = "textSize";
public static final String PADDING = "textPadding";
public static final String TEXT_COLOR = "textColor";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView1);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent sharedIntent = new Intent(MainActivity.this, SharedActivity.class);
sharedIntent.putExtra(SHARED_TEXT_SIZE, textView.getTextSize());
sharedIntent.putExtra(PADDING,
new Rect(textView.getPaddingLeft(),
textView.getPaddingTop(),
textView.getPaddingRight(),
textView.getPaddingBottom()));
sharedIntent.putExtra(TEXT_COLOR, textView.getCurrentTextColor());
Pair[] pairs = new Pair[1];
pairs[0] = new Pair<View, String>(textView, "textTransition");
ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, pairs);
startActivity(sharedIntent, activityOptions.toBundle());
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView1"
android:layout_alignParentTop="true"
android:transitionName="textTransition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:text="Hello World!" />
</RelativeLayout>
Second Activity:
public class SharedActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared);
final TextView textView = findViewById(R.id.textView1);
setEnterSharedElementCallback(new SharedElementCallback() {
private float targetTextSize;
private ColorStateList targetTextColors;
private Rect targetPadding;
#Override
public void onSharedElementStart(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
super.onSharedElementStart(sharedElementNames, sharedElements, sharedElementSnapshots);
targetTextSize = textView.getTextSize();
targetTextColors = textView.getTextColors();
targetPadding = new Rect(textView.getPaddingLeft(),
textView.getPaddingTop(),
textView.getPaddingRight(),
textView.getPaddingBottom());
textView.setTextColor(getIntent().getIntExtra(MainActivity.TEXT_COLOR, Color.BLACK));
float textSize = getIntent().getFloatExtra(MainActivity.SHARED_TEXT_SIZE, targetTextSize);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
Rect padding = getIntent().getParcelableExtra(MainActivity.PADDING);
assert padding != null;
textView.setPadding(padding.left, padding.top, padding.right, padding.bottom);
}
#Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize);
if (targetTextColors != null) {
textView.setTextColor(targetTextColors);
}
if (targetPadding != null) {
textView.setPadding(targetPadding.left, targetPadding.top,
targetPadding.right, targetPadding.bottom);
}
}
});
}
}
activity_shared.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SharedActivity">
<TextView
android:id="#+id/textView1"
android:transitionName="textTransition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:textSize="36sp" />
</RelativeLayout>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowSharedElementEnterTransition">
#transition/shared_main_detail
</item>
</style>
</resources>
shared_main_detail.xml:
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<transitionSet>
<targets>
<target android:targetId="#id/textView1" />
</targets>
<transition class="com.example.sharedanimation.TextResize" />
<changeBounds />
</transitionSet>
<recolor>
<targets>
<target android:targetId="#android:id/statusBarBackground" />
<target android:targetId="#android:id/navigationBarBackground" />
</targets>
</recolor>
</transitionSet>
I am new to android. So, I have been playing with fragments to understand how they work.
Firstly, I made a calciKeyboard class which extends Fragment
Code:
public class calciKeyboard extends Fragment {
#Nullable
Button b;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
GridLayout gridLayout=(GridLayout) inflater.inflate(R.layout.calci_keyboard,container,false);
for(int i=0;i<gridLayout.getChildCount();i++){
b=(Button)gridLayout.getChildAt(i);
if (b != null) {
b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
}
}
return gridLayout;
}
}
calciKeyboard.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="4"
android:columnCount="4"
>
Buttons inside....
</GridLayout>
advance_mix
public class advance_mix extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advance_length);
}
}
advance_length
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.tilak.myfirstapplication.calciKeyboard"
android:id="#+id/calciKeyboard" />
</GridLayout>
go_advance.java
findViewById(R.id.redirectLength).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(go_advance.this,advance_mix.class);
startActivity(i);
}
}
);
go_advance class contains the intent which starts the advance_mix activity, which further lays out the advance_length layout file.
The problem is when redirect_length button is clicked it shows me a white screen.
Why is it happening?
You should return View .
View getRootView = inflater.inflate(R.layout.calci_keyboard,container,false);
GridLayout gridLayout=(GridLayout)getRootView.findViewById(R.id._gridLAYOUT);
for(int i=0;i<gridLayout.getChildCount();i++){
b=(Button)gridLayout.getChildAt(i);
if (b != null) {
b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
}
return getRootView ;
XML
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/_gridLAYOUT">
<fragment android:layout_width="match_parent" //Change this to match_parent
android:layout_height="match_parent" //Change this to match_parent
android:name="com.example.tilak.myfirstapplication.calciKeyboard"
android:id="#+id/calciKeyboard" />
</GridLayout>
OK. Try to use "android.support.v4.app.Fragment" rather than "android.app.Fragment" and second think 'Button b;' declare before #Nullable
Sorry for my English.
I'm trying to use a second activity in Android, but it doesn't load. It's like the code jump it. Below, you can see the code. Thanks.
1- First Activity
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
});
}
2- Second Activity
public class activity_janela1 extends Activity
{
public void OnCreate(Bundle saveInstaceState)
{
super.onCreate(saveInstaceState);
setContentView(R.layout.activity_janela1);
}}
3- First layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btenviardados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
4- Second layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutFormulario"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Im screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
5- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.seven.reader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name="com.example.seven.reader.activity_janela1" >
</activity>
</application>
</manifest>
You have created a method within your oncreate. Remove that and leave it in onCreate.
///public void addListenerOnButton() {
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
});
You are misunderstanding how to use methods and scope. I recommend you do a read up on these things.
The problem is with your method addListenerOnButton().
You could define a method which will be executed when button is clicked via xml just add onClick attribute where you have defined your button.
As:-
<Button
android:id="#+id/btenviardados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to another screen"
android:onClick="click" />
And define click method in MainActivity.java
as:-
public void click(View view)
{
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
Or you can add OnClickListener in you onCreate method of MainActivity.
Try this, may help you:
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(getApplicationContext, activity_janela1.class);
startActivity(intent);
finish();
System.out.println("ebadasdadas");
}
});
}
}
I placed a Button in a layout
here is my xml code
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#ff0000"/>
and wrote the following code in activity:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
}
public void clkBtn(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
When I run this code, I am getting white blank screen (without any button). Can any one tell me what is wrong with my code?
Update your first.xml under res->layout folder 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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clkBtn"
android:text="Click Me" />
</LinearLayout>
To get the Button event you need to first register your Button for that. The code which you have written is right but to invoke that method you have to add the property android:onClick="clkBtn" in your layout file.
OR
If you don't want to use this way then you can also explicitly invoke the event by registering your Button in your class as below:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
And also to launch your activity make sure have added your activity as launcher in your manifest file as below .
<activity android:label="#string/app_name"
android:name="Basic" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is My Answer. In XML file Button should be like this:
<Button
android:id="#+id/ID of you button "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name To Display Button name" />
In the MainActivity OR In any activity (in which you have called XML):
public class MainActivity extends Activity {
public void oncreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout."Your XML file in which Button is.");
Button btn = (Button)findViewById(R.id."Your button Id");
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do your onclick program here
}
});
}
}
I have a question about xml parser in android. I am creating button information in the xml. I am getting its height,width,color and so on. I am creating all buttons in a function. Now i want to do the following. When i click the buttons, new android page opens. As far as i know i will do it by creating new activities.
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
I am writing this on my function which i create all buttons.when i do this it does not work.can somebody help me?
In your Activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
View btnTapOnMe = findViewById(R.id.btnTapOnMe);
btnTapOnMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
}
}
view_main.xml :
<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" >
<Button
android:id="#+id/btnTapOnMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap on Me" />
</RelativeLayout>
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourpackage"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.yourpackage.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.yourpackage.MainActivity2"/>
</application>
</manifest>
Should work like a charm
UPDATE:
Don't ever use AbsoluteLayout it is deprecated
This class was deprecated in API level 3.
Use FrameLayout, RelativeLayout or a custom layout instead.
First of all change it to Frame or Relative layout.
Here what i get in my example below :
public class MainActivity extends FragmentActivity {
public void AddAllButtons() {
AbsoluteLayout Layout = (AbsoluteLayout) findViewById(R.id.layout1);
Button btn = new Button(this);
btn.setText("Test case");
Layout.addView(btn);
AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btn.getLayoutParams();
absParams.x = 100;
absParams.y = 100;
absParams.width = 150;
btn.setLayoutParams(absParams);
btn.setBackgroundColor(Color.GREEN);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SearchActivity.class);
startActivity(i);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
AddAllButtons();
}
}
and here is xml file :
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="#+id/btnTapOnMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap on Me"/>
<AbsoluteLayout
android:id="#+id/layout1"
android:layout_below="#+id/btnTapOnMe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</AbsoluteLayout>
</RelativeLayout>