I want to create a pdf reader with android studio. I added a pdf file to assets folder. I used seekbar. But code doesn't show anything. What's wrong?
I share all prog
Mainactivity.java
package enespolat.polat.com.kuranahmethusrev;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.pdf.PdfRenderer;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import uk.co.senab.photoview.PhotoViewAttacher;
public class MainActivity extends AppCompatActivity {
LinearLayout pagerSection;
SeekBar pageSeekbar;
ImageView pageView;
TextView tvCounter;
PdfRenderer.Page sayfa;
PhotoViewAttacher pva;
int pageCount=0;
int currPage=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageSeekbar=(SeekBar)findViewById(R.id.sbPager);
pagerSection=(LinearLayout)findViewById(R.id.llPageCounter);
pageView=(ImageView)findViewById(R.id.iv);
tvCounter=(TextView)findViewById(R.id.tvCounter);
render();
pva = new PhotoViewAttacher(pageView);
pva.setMaximumScale(10.0f);
pageSeekbar.setMax(605);
pageSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b)
{
currPage=i;
showCurrentPageIndicator();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
currPage=seekBar.getProgress();
render();
}
});
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (pageView != null)
pageView.invalidate();
}
void showCurrentPageIndicator()
{
tvCounter.setText(""+(currPage+1));
pagerSection.setVisibility(View.VISIBLE);
pagerSection.postDelayed(new Runnable() {
#Override
public void run() {
pagerSection.setVisibility(View.GONE);
}
},700);
}
private void render()
{
try {
File dlFolder = new File(Environment.getExternalStorageDirectory(), "Download");
File pdfFile = new File(dlFolder, "kuran.pdf");
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(pdfFile,ParcelFileDescriptor.MODE_READ_ONLY));
sayfa = renderer.openPage(currPage);
Bitmap bitmap=Bitmap.createBitmap(sayfa.getWidth(),sayfa.getHeight(), Bitmap.Config.ARGB_8888);
Matrix m=pageView.getImageMatrix();
m.setScale(1.0f, 1.0f);
Rect rect=new Rect(0,0,sayfa.getWidth(),sayfa.getHeight());
sayfa.render(bitmap,rect,m,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
//img.setImageMatrix(m);
pageView.setImageBitmap(bitmap);
pageView.invalidate();
/**/
sayfa.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
layout folder
<?xml version="1.0" encoding="utf-8" ?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="#+id/activity_main" android:layout_width="match_parent" android:background="#FFFFFF" android:layout_height="match_parent">
<ImageView android:id="#+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="#+id/ly" />
- <LinearLayout android:background="#88333333" android:layout_width="match_parent" android:paddingRight="5dp" android:paddingLeft="5dp" android:id="#+id/ly" android:paddingBottom="12dp" android:paddingTop="12dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true">
<SeekBar android:id="#+id/sbPager" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
- <LinearLayout android:visibility="gone" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="40dp" android:background="#88333333" android:id="#+id/llPageCounter" android:layout_centerVertical="true" android:layout_centerHorizontal="true">
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="#FFFFFF" android:textStyle="bold" android:id="#+id/tvCounter" android:text="355" android:textSize="40sp" android:gravity="center" />
</LinearLayout>
</RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8" ?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="enespolat.polat.com.kuranahmethusrev">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <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>
</application>
</manifest>
Related
im doing a simple android camera application that capture one picture then display it. the capturing part is working however i cant display it on the imageview.there wasnt any error but in one of the logcat stated this
Bitmap too large to be uploaded into a texture (3120x4208, max=4096x4096)
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
public class MainActivity extends Activity {
Button button;
ImageView imageView;
static final int CAM_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getfile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent,CAM_REQUEST);
}
});
}
private File getfile(){
File folder = new File("sdcard/camera_app");
if(!folder.exists()){
folder.mkdir();
}
File image_file = new File (folder, "cam_image.jpg");
return image_file;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
}
activity_main.xml
<?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: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="com.example.lala.camera.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Capture Image"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:layout_width="350dp"
android:layout_height="300dp"
android:id="#+id/image_view"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
/>
</LinearLayout>
AndroidManifest.xml
<
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amirul.camera">
<uses-feature android:name="android.hardware.camera2"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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>
</application>
</manifest>
Your image is too large you have to resize it.
Try this:
imageView.setImageDrawable(resize(Drawable.createFromPath(path)));
private Drawable resize(Drawable image) {
Bitmap bitmap = ((BitmapDrawable)image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap , WIDTH, HEIGHT, false);
return new BitmapDrawable(getResources(), bitmapResized);
}
I am fresher to android. My task is to open a pdf file in android device. I searched a tutorials and tried it. But I could not open a pdf file. I dont know the error where I did?. Please help me anyone.
activity_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"
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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp"
android:text="open" />
</RelativeLayout>
MainActivity.java
package com.example.pdftest;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#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;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File pdffile=new File(Environment.getExternalStorageDirectory(),"/sdcard/abc.pdf");
try
{
if(pdffile.exists())
{
Uri path=Uri.fromFile(pdffile);
Intent objintent=new Intent(Intent.ACTION_VIEW);
objintent.setDataAndType(path, "application/pdf");
objintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objintent);
}
else
{
Toast.makeText(MainActivity.this,"File Not Found",Toast.LENGTH_SHORT).show();
}
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this,"No viewer application Found",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pdftest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.pdftest.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>
You have to remove the root from the second parameter in File class constructor. When you're instantiating File with File(String dirPath, String name) then the second parameter must contain just the file name and the first the directory where the file is located.
With this correction your code should work :
File pdffile=new File(Environment.getExternalStorageDirectory(),"abc.pdf");
I am trying to read my GPS position. It works on emulator Nexus S API 23
But when install it on Deices with 4.1.1 or 5.0.1 versions it stopped before working. i need to know that if the problem because of the code or because of the different versions between devices:
this is my code:
Main Activity.java
package com.example.user.getgps1;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static final long meter=1;
static final long mill=1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager manager=(LocationManager) this.getSystemService(context.LOCATION_SERVICE);
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, meter, mill, new locationlist(this));
}
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,meter,mill,new locationlist(this));
}
ProgressDialog dialog;
Context context;
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void bulocation(View view) {
context = this;
dialog = new ProgressDialog(MainActivity.this);
dialog.show();
dialog.setMessage("Getting Last Location");
LocationManager locationmanager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, meter, mill, new locationlist(this));
}
Location lastknown = locationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastknown==null)
lastknown=locationmanager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
TextView t1=(TextView) findViewById(R.id.textView1);
TextView t2=(TextView) findViewById(R.id.textView2);
t2.setText(Double.toString(lastknown.getLatitude()));
t1.setText(Double.toString(lastknown.getLongitude()));
dialog.dismiss();
}
}
activity_main
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView1"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="bulocation"/>
</RelativeLayout>
Android_manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.getgps1" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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>
</application>
</manifest>
locationlist.java
package com.example.user.getgps1;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
public class locationlist implements LocationListener {
Context context;
public locationlist(Context context)
{
this.context=context;
}
#Override
public void onLocationChanged(Location location) {
Toast.makeText(context,"log:"+Double.toString(location.getLongitude())+", lat: "+Double.toString(location.getLatitude()),Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(context,"GPS is Changed",Toast.LENGTH_LONG).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(context,"GPS is on",Toast.LENGTH_LONG).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(context,"GPS is OFF",Toast.LENGTH_LONG).show();
}
}
what is the problem
Looking at the logcat you posted as answer.
Your call to getLastKnownLocation() is returning null.
On a real device you cant assume to always get a location,
Hope this helps.
My project has showing red highlighted text I need my project fixed
am not sure what has caused this to happen any suggestions.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="scrape2.myapplication" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="scrape2.myapplication.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=".MainActivity2Activity"
android:label="#string/title_activity_main_activity2" >
</activity>
</application>
</manifest>
<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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/parse_html"
android:id="#+id/html_content"
android:textSize="16sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="119dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="html"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_x="186dp"
android:layout_y="1dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/parse_html"
android:id="#+id/textView1"
android:textSize="16sp"
android:singleLine="false"
android:layout_marginTop="107dp"
android:layout_below="#+id/html_content" />
<ListView
android:id="#+id/listView2"
android:layout_width="355dp"
android:layout_height="421dp"
android:layout_x="20dp"
android:layout_y="118dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button"
android:layout_toStartOf="#+id/button"
android:layout_x="264dp"
android:layout_y="-1dp" />
</RelativeLayout>
I am not sure why its doing that please share me some tip thanks.
I tried to clean the project , also clicked the rebuild still no luck
activity class
package scrape2.myapplication;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import static scrape2.myapplication.Constants.FIFTH_COLUMN;
import static scrape2.myapplication.Constants.FIRST_COLUMN;
import static scrape2.myapplication.Constants.FOURTH_COLUMN;
import static scrape2.myapplication.Constants.SECOND_COLUMN;
import static scrape2.myapplication.Constants.THIRD_COLUMN;
public class MainActivity extends ActionBarActivity {
private ArrayList<HashMap<String, String>> list;
public int count = 0;
public String temp;
private Document htmlDocument;
private String htmlPageUrl = "http://en.masjidway.com/masjid/2804/prayer";
private TextView parsedHtmlNode;
private TextView textViewwww;
private String htmlContentInStringFormat;
private String dayys;
private String amarrdatta;
private String duhr_val1;
private String asr_val1;
private String maghreb_val1;
private String isha_val1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parsedHtmlNode = (TextView)findViewById(R.id.html_content);
textViewwww = (TextView)findViewById(R.id.textView1);
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, MainActivity2Activity.class);
MainActivity.this.startActivity(myIntent);
}
});
Button htmlTitleButton = (Button)findViewById(R.id.button);
htmlTitleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JsoupAsyncTask jsoupAsyncTask = new JsoupAsyncTask();
jsoupAsyncTask.execute();
}
});
w(ListView)findViewById(R.id.listView2);
String zuhrr =duhr_val1;//2
w
Your RelativeLayout doesn't belong in AndroidManifest.xml. You should create a new xml file for every layout.
I think i have found a solution i have done many checks and for some reason the project plays up not sure why this is the test i have done at first
from relatetive layout changed only this name to linerlayout
then pressed
clean project
rebuild
run project then once it compiles the file opens the emulator or phone press cancle.
then close the project and open it , it fixes it.
also you can just leave it as reletive layout and do the remaining steps it will fix it.
am not sure why it does that.
Hi I have a basic app for logging in with hardcoded credentials, and I want it to take you to another activity called Welcome. Here is my main activity:
package com.example.numericlogin;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity{
private EditText login_key=null;
private Button login=null;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login_key = (EditText)findViewById(R.id.editText1);
login = (Button)findViewById(R.id.login);
}
public void login(View view){
if(login_key.getText().toString().equals("123456")){
Toast.makeText(getApplicationContext(), "Login Successful!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,Welcome.class));
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
{
login.setEnabled(false);
}
}
}
#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;
}
}
Here is the main activity's 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: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="com.example.numericlogin.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/login_key" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView1"
android:ems="10"
android:digits="0123456789"
android:inputType="number|textPassword"
android:maxLength="6"
android:password="true"
/>
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="#string/login" />
</RelativeLayout>
Here is the second activity I want it to switch to when logged in correctly:
package com.example.numericlogin;
import android.view.View;
import android.widget.ImageView;
public class Welcome {
public void welcome(View v){
ImageView picture = (ImageView)
setContenView(R.layout.fragment_main);
}
private ImageView setContenView(int fragmentMain) {
// TODO Auto-generated method stub
return null;
}
}
Here is the xml for the above activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:src="#drawable/welcome" />
</RelativeLayout>
And here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.numericlogin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
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.numericlogin.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=".Welcome"/>
</application>
</manifest>
At the moment when I launch it it brings up the first activity and allows you to enter input in the login area, but when you click the login button nothing happens. There are no errors in the code and here is the logcat output:
07-01 14:11:15.313: W/IInputConnectionWrapper(23128): showStatusIcon on inactive InputConnection
This is how you switch between activities:
In MainActivity.java:
Intent go = new Intent(this, Welcome.class);
startActivity(go);
Your second activity class must extend Activity
public class Welcome extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.childact);
}
}
And you must register all activities in manifest file.
You are missing this:
<activity
android:name="com.example.testingproj.Welcome"
android:label="#string/app_name">
</activity>
I think you forgot set onClickListener for the login button or set attribute onClick with value 'login' in the properties panel in graphical layout preview.
final Button login= (Button) findViewById(R.id.login);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform code validation
}
});