scroll activity page when key board is open android - java

I want that whent the soft key board is open the activity page will scrolled.
To do this I added scroll View in 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"
tools:context="com.example.trying.MainActivity"
>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="#+id/LayoutBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" >
<requestFocus />
</EditText>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
and changed the realativeLayout height in code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
int x=getActionBarHeight();//in this line I get the actionBar height programicaly the function is in the end of page
RelativeLayout l= (RelativeLayout)findViewById(R.id.LayoutBackground);
l.getLayoutParams().height = (height-x);//the layout size = size of full screen - size of actionBar
}
//I saw this function in stackOverflow site. function to get the actionBar height
public int getActionBarHeight() {
TypedValue tv = new TypedValue();
getApplicationContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
return actionBarHeight;
}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trying"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The idia is that the layout heigt is depended of screen size (all devices), and action bar heigt too.
My problem is that when I run the app, the background is small.
the it's It seems that the action bar size fell twice,

Add the following line of code to your manifest for the particular activity
android:windowSoftInputMode="adjustResize"

You don't need to add ScrollView in your layout. Remove it from xml. Then in AndroidManifest.xml, add the following lines in your activity:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden"
android:windowSoftInputMode="adjustPan|stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Related

Could not identify launch Activity: Default Activity wasn't found

I'm new to Android and I have tried to create a new View.
But I get this error on run
Could not identify launch Activity: Default Activity not found
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<com.bamlineapps.dungeonexplorer.DungeonExplorer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dungeonView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</com.bamlineapps.dungeonexplorer.DungeonExplorer>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Here is my android manifest as I think that the error comes from here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.bamlineapps.dungeonexplorer">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Your manifest file must list all activities you want to use and the one you want to be your entry point needs to have right intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
I'm guessing that you've forgot to
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas); // <- this line
// Your code here
}
And I can only guess because there's some important data missing in the question - Could you please attach the crash Logs/errors printed on console on the question?
Update
Try null checking your variables and initializing stuff in the constructor of your class like so:
public class YourView extends ImageView { // I don't know; provide more info
private Bitmap chibiSprite;
public YourView(Context context) {
super(context);
chibiSprite = BitmapFactory.decodeResource(getResources(), R.drawable.chibi_sprite);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(canvas != null)
drawCharacter(canvas);
}
public void drawCharacter(Canvas canvas) {
// Customize these three
float location_x = 100F;
float location_y = 100F;
Paint custom_paint = null;
canvas.drawBitmap(chibiSprite, location_x, location_y, custom_paint);
}
}

java lang runtime exception :unable start activity component info

hi sir I got this error when my app was launched. When I run my project unfortunately project was stop. This error is coming my log cat. When click on app unfortunately app was stop msg came.
I put one text view on activity_xml. When click on navigate to another page but here do not display my first page. There is problem to on mainactivity.java and activity main.xml
error is java.lang.runtime Exception: Unable to start activity component Info{com.example.b/com.example.b.MainActivity}: java. Lang.NullPointerException
**mainactivity.java**
andheri = (TextView) findViewById(R.id.Andheri1);
andheri.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Context context = null;
// TODO Auto-generated method stub
Intent intent = new Intent(context,andheri.class);
startActivity(intent);
}
});
**activity_xml**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/untitled1"
>
<TextView
android:id="#+id/Andher1"
android:layout_width="80dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FFE4B5"
android:capitalize="characters"
android:gravity="center"
android:text="Andheri"
android:textColor="#000000"
android:textColorHint="#0000CD"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
**mainifest file.**
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.b.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.example.b.andheri"
android:label="#string/app_name" >
</activity>
</application>
You should change this
andheri = (TextView) findViewById(R.id.Andheri1);
to
andheri = (TextView) findViewById(R.id.Andher1);
Your TextView id is id/Andher1 and you're trying to find it as #+id/Andheri1. So you got NPE.
In the intent you send from main activity, you should set the context to this, not null
and change the id of textview , you missed an 'i'

Swapping screens with button. Crashes

I have been messing around with this for a few days now and have the code identical to that in the tutorial and yet it still doesn't want to work, Why?
MainActivity.java
package com.example.mdpmk1;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
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.scan);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ScanScreen.class);
startActivity(intent);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen One......"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/scan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
</LinearLayout>
ScanScreen.java
package com.example.mdpmk1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class ScanScreen extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_screen);
}
}
scan_screen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have done it!!"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstapp.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>
</application>
</manifest>
Any help would be lovely, This has relay been annoying me and i know that it is probably a simple mistake.
Add the following in your AndroidManifest.xml
<activity android:name=".ScanScreen" />
within application like so:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstapp.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=".ScanScreen" />
</application>
Here is a quote from the official docs on what the activity tags are for:
Declares an activity (an Activity subclass) that implements part of
the application's visual user interface. All activities must be
represented by elements in the manifest file. Any that are
not declared there will not be seen by the system and will never be
run.
That last sentence is important.
In your case writing <activity android:name=".ScanScreen" /> above </application> will solve your problem.
Whenever you need to use any activity in android you have to mention it in AndroidManifest.xml file. Just giving answer to you won't help you.
You should first read on below links to understand what is intent and intent-filters used for.
http://developer.android.com/reference/android/content/Intent.html
http://developer.android.com/guide/components/intents-filters.html
What is an Intent in Android?
Your app crashes because your not setting or registered the activity in manifest.You haven't added the ScanScreen Activity in your manifest file.
Just add the following to your AndroidManifest.xml :
<activity android:name=".ScanScreen" />
within your application tag like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstapp.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=".ScanScreen" />
</application>

Android Getting Runtime Error While Starting App

I'm trying to launch my app but i'm getting runtime error all the time.
Definition of the program is: App starts when user clicks button , button will be invisible and count down starts after countdown finish Menu.java will open.
Here is my code below:
Main.java
public class Main extends Activity {
TextView countDown;
int counter;
Intent menuIntent;
Button appStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menuIntent = new Intent("com.example.project_21.MENU");
counter = 5;
countDown = (TextView) findViewById(R.id.countDown);
appStartButton = (Button) findViewById(R.id.appStartButton);
appStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.GONE);
while (counter != 0) {
sleep(1000);
counter--;
countDown.setText(counter + " seconds");
}
startActivity(menuIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sleep(long mill) {
try {
Thread.sleep(mill);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project_21"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.project_21.Main"
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.example.project_21.Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.project_21.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_main.xml
<LinearLayout 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:background="#drawable/android2"
android:orientation="vertical"
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=".Main" >
<TextView
android:id="#+id/startsInfo"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:gravity="center"
android:text="#string/welcome"
android:textSize="30sp" />
<TextView
android:id="#+id/countDown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/countDown"
android:textSize="45sp" />
<Button
android:id="#+id/appStartButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/appStartButton"
android:textSize="35sp" />
</LinearLayout>
ERROR LOG
The error message states that you are making a call to system services, before they are available. This is because you are calling them in the onCreate() method. Move the calls you are making in your Menu.java class (not included here, so can't tell you exactly which calls you are making) to another lifecycle method - onCreateView() is probably best for an Activity (onAttach() is a good one for Fragments).

App crashes when it reaches myMapView.getController()

For some reason this VERY simple program crashes when it reaches the getController() method. Its almost like the debugger is trying to tell me something, but its all garble.Believe it or not there isn't a clear explanation or exception given, ha. But it definitely blows up on this line. If I take it out of the code the app will run a basic map. I have tried everything imaginable and I can't figure out the problem. Does anyone have any thoughts? Thanks in advance.
public class Main extends MapActivity {
MapView myMapView;
MapController mc;
GeoPoint point;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
double lon = -79.9;
double lat = 40.4;
myMapView = (MapView) findViewById(R.layout.main);
point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
mc = myMapView.getController();
mc.animateTo(point);
mc.setZoom(13);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
And the main.xml code:
<?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">
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0SN6PntBTxgMIeekPQm2Of1gnlDiHmrTm8GG2xQ"
></com.google.android.maps.MapView>
</LinearLayout>
And Finally, the manifest.xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplyDesign.BarCraw" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Main" 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="com.google.android.maps" />
</application>
</manifest>
You should give your MapView an android:id and feed it to findViewById(). In your code you search for a view with the Id of your whole layout. This can only return null... and give you a NullPointerException when invoking getController() on null.
define id for MapView in xml
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0SN6PntBTxgMIeekPQm2Of1gnlDiHmrTm8GG2xQ"
android:id="#+id/mapView"
></com.google.android.maps.MapView>
and change this line..
myMapView = (MapView) findViewById(R.layout.mapView);

Categories