Not able to dynamically add items to RelativeLayout one below other - android

I'm facing a problem in adding items one below the other for relativeLayout created dynamically. I'm not able to see the first element added to it . Everytime I'm able to see only the last element added to it.
I'm providing here with this mail java source code & xml file . Please help me in sorting out this issue:
DynamicRelativeLayoutActivity.java
==================================
package com.andr.rlayout;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class DynamicRelativeLayoutActivity extends Activity {
/** Called when the activity is first created. */
RelativeLayout rLayout;
ScrollView sview;
RelativeLayout dynamiclayout;
LinearLayout horizontalllayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rlayout);
sview = (ScrollView)findViewById(R.id.slayout);
dynamiclayout = new RelativeLayout(DynamicRelativeLayoutActivity.this);
dynamiclayout.setBackgroundColor(Color.WHITE);
sview.addView(dynamiclayout);
RelativeLayout.LayoutParams lp_btn;
horizontalllayout = new LinearLayout(DynamicRelativeLayoutActivity.this);
horizontalllayout.setOrientation(android.widget.LinearLayout.HORIZONTAL);
TextView tv = new TextView(DynamicRelativeLayoutActivity.this);
tv.setText("Hi");
horizontalllayout.addView(tv);
lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp_btn.addRule(RelativeLayout.BELOW);
dynamiclayout.addView(horizontalllayout, lp_btn);
horizontalllayout = new LinearLayout(DynamicRelativeLayoutActivity.this);
horizontalllayout.setOrientation(android.widget.LinearLayout.HORIZONTAL);
TextView tv1 = new TextView(DynamicRelativeLayoutActivity.this);
tv1.setText("Hello");
horizontalllayout.addView(tv1);
lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp_btn.addRule(RelativeLayout.BELOW);
dynamiclayout.addView(horizontalllayout, lp_btn);
}
}
rlayout.xml
===========
<?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="wrap_content"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/slayout"
>
</ScrollView>
</RelativeLayout>
AndroidManifest.xml
-==================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andr.rlayout"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".DynamicRelativeLayoutActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

//add id to your horizontalllayout view
horizontalllayout.setId(100);
//then call
lp_btn.addRule(RelativeLayout.BELOW,horizontalllayout.getId());
//finally at end add to your scroll view
sview.addView(dynamiclayout);

Related

How to make two activities?

I'm new in Android studio and I'm trying to make two activities in my application but it don't works.
I don't know what I'm doing wrong, I think it's about the first options I put in my intent.
I'm new and I would like a simple solution or if you can explain me what I have to do it will be fine :)
This is my first activity (MainActivity):
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mPasserelle = null;
public final static String AGE = "com.myapplis.multiactivite.AGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPasserelle = (Button) findViewById(R.id.passerelle);
mPasserelle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent secondeActivite = new Intent(MainActivity.this,IntentExample.class);
secondeActivite.putExtra(AGE,24);
startActivity(secondeActivite);
}
});
}
}
The second activity(IntentExample) :
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class IntentExample extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.seconde_activite);
Intent i = getIntent();
int age = i.getIntExtra(MainActivity.AGE,0);
TextView resultat= (TextView)findViewById(R.id.resultat);
resultat.setText("Le résultat est : "+age);
}
}
This is my first layout (activity_main), just a simple button to push to get the second activity:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dunomade.multiactivite.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/passerelle"/>
</LinearLayout>
And now this is my second layout(seconde_activite), just a simple text to show me I'm on the second activity:
<?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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/resultat"
android:text="" />
</LinearLayout>
And to finish, this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplis.multiactivite">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".IntentExample"
android:label="seconde acivite">
</activity>
</application>
</manifest>
When I'm running the app, MainActivity is good, but when I try to click on the button the AVD show me "Have you declared this activity in your AndroidManifest ?". I declared it on my manifest but I think the problem is in my MainActivity.
Please help me, I think for you it's really simple but I can't solve it, I tryed many way but I already got the same error.
Thank's for read and for answer =)
I advise that you use the name of package before the name of your activity, like this (some devices need this, like Moto G) :
<activity android:name="your.package.IntentExample "></activity>
And instead use:
public class IntentExample extends Activity
use:
public class IntentExample extends AppCompatActivity

admob does not show up in android

I'm a beginner,I'm trying to set an admob add in my app.But I have some errors witch I can't manage.
XmlFile
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.google.ads.AdView android:id="#+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</RelativeLayout>
Manifest File.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admob"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application
android:allowBackup="true" android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:theme="#style/AppTheme" >
<activity
android:name="com.example.admob.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.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|scre enSize|smallestScreenSize" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
Java Code:
package com.example.admob;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity
{
private static final String MY_AD_UNIT_ID = null;
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
RelativeLayout main = (RelativeLayout)findViewById(R.id.main);
main.addView(adView);
adView.loadAd(new AdRequest());
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.addTestDevice("TEST_DEVICE_ID");
}
#Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
}
when Activity start this type error occur onFailedToReceivedAd(InvalidAdRequest).
I don't know what is the problem.
You are not loading the AdRequest which has the test devices registered. Change your code to:
//....
main.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.addTestDevice("TEST_DEVICE_ID");
adView.loadAd(adRequest);
In your relativelayout that contains the adview, are you appending the content layout after the adview. If so, what are the android:layout_width and android:layout_height values. If you are using fill_parent/match_parent for both, then your adview will never show.
The trick is to change the relativelayout to the linearlayout. Then for the content layout within this linearlayout, set its android:layout_width="fill_parent" and android:layout_height="0dp". Add a new attribute android:layout_weight="1" for the content layout. What these does is to ask the content layout to accomodate the remaining space of linearlayout after the adview has occupied its place. Hope this helps.

Android Activity Breaking

I'm just trying to come up with a skeleton for an activity that uses a ListView as a news feed for a game my class is making.
In this activity there's simply the news feed, and a Refresh button (to update the ListView with the latest news)
My activity_news.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<ListView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="320dp"
>
</ListView>
<Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:text="Refresh"
android:id="#+id/bRef"
/>
My NewsActivty.java:
package com.example.mynews;
import com.example.mynews.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class NewsActivity extends Activity {
Button refresh;
String newsfeed [] = {"latest news", "older news"};
Intent ref = new Intent(this, NewsActivity.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
refresh = (Button) findViewById(R.id.bRef);
ListView newsStuff = (ListView) findViewById(R.id.news);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, newsfeed);
newsStuff.setAdapter(adapter);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(ref);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
}
And my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mynews"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.mynews.NewsActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I built everything up piece by piece and ran it as I finished something new (the layout, the array adapter, etc) and the activity started to break once I added the OnClickListener (emulator tells me "mynews has unfortunately stopped") for my Refresh button. I'm sure there's a better way to accomplish what i'm trying to do, but any assistance with what I have so far would be greatly appreciated.
Did u define a class ref.java?
if yes then you need define that activity(ref) in the manifest file.

How to use third-party jar file in Android

I know this question asked before but any answer helped me.
I want to use SVG but Android can't see SVG jar. I added build path. And I attach library resuorce as libs folder. And also added tag in Android Manifest. But it didn't work.
Regards
Here is my Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.svgexample2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.svgexample2.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>
<uses-library android:name="svg-android" android:required="true" />
</application>
</manifest>
My Main Activity :
package com.example.svgexample2;
import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
RelativeLayout mainLayout;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView myImg;
SVG svgFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout = new RelativeLayout(this);
myImg = new ImageView(this);
myImg.setBackgroundColor(Color.WHITE);
SVG svgFile = SVGParser.getSVGFromResource(getResources(), R.drawable.image);
myImg.setImageDrawable(svgFile.createPictureDrawable());
mainLayout.addView(myImg);
setContentView(mainLayout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Here is my additional svgandroid.xml file :
<?xml version="1.0" encoding="utf-8">
<permissions>
<libraryname="svg-android" file="/home/breed/dev/Android/androidWorkspace/SvgExample2/libs/"/>
</permissions>
AFAIK there is no need that you place a uses-library in your Manifest. The svgandroid.xml should be useless, too. Just place the .jar files in the libs folder from your Android Eclipse Project.

Adwhril sample example giving classnotfound exception

i am having my manifest file like this
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.basic.adwhrilsample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".Invoker" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:value="my sdk key"
android:name="ADWHIRL_KEY"/>
</activity>
</application>
and the below is my main.xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout_main">
<com.adwhirl.AdWhirlLayout
android:id="#+id/adwhirl_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
and my Invoker.java activity
package com.basic.adwhrilsample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import com.adwhirl.AdWhirlLayout;
import com.adwhirl.AdWhirlLayout.AdWhirlInterface;
import com.adwhirl.AdWhirlManager;
import com.adwhirl.AdWhirlTargeting;
public class Invoker extends Activity implements AdWhirlInterface {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);
AdWhirlTargeting.setAge(23);
AdWhirlTargeting.setGender(AdWhirlTargeting.Gender.MALE);
AdWhirlTargeting.setKeywords("online games gaming");
AdWhirlTargeting.setPostalCode("94123");
AdWhirlTargeting.setTestMode(false);
AdWhirlLayout adWhirlLayout = (AdWhirlLayout)
findViewById(R.id.adwhirl_layout);
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
int diWidth = 320;
int diHeight = 52;
int density = (int) getResources().getDisplayMetrics().density;
adWhirlLayout.setAdWhirlInterface(this);
adWhirlLayout.setMaxWidth((int) (diWidth * density));
adWhirlLayout.setMaxHeight((int) (diHeight * density));
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
textView.setText("Below AdWhirlLayout");
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_main);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
layout.addView(adWhirlLayout, layoutParams);
layout.addView(textView, layoutParams);
layout.invalidate();
}
#Override
public void adWhirlGeneric() {
}
}
but i am getting
08-30 10:17:25.194: E/AndroidRuntime(564): java.lang.RuntimeException: Unable to
instantiate activity
ComponentInfo{com.basic.adwhrilsample/com.basic.adwhrilsample.Invoker}:
java.lang.ClassNotFoundException: com.basic.adwhrilsample.Invoker in loader
dalvik.system.PathClassLoader[/data/app/com.basic.adwhrilsample-2.apk]
Can anyone help me out in solving my issue.
Or can anyone provide me a sample working example so that i could follow it
Updated logcat :
i think everything is allright just replace imported package replace this
import android.widget.RelativeLayout.LayoutParams;
by
import android.view.ViewGroup.LayoutParams;
Try deleting ur bin directory of the project and then clean the project build it and run. I think ur class file is not present in the .dex extension it sometimes happens when the files dont get properly compiled. Also check if there is no error of build path.

Categories

Resources