I've built my first app, and I would like to password protect it. It's fine for me to store the password in the Java files and the method needs to be as simple as possible because i have no experience of java or even xml before this app. I've had a few attempts and failed so I was hoping someone can help me out.
I've created the layout with an EditText field:
<EditText
android:id="#+id/passwordedittext"
android:layout_width="200dp"
android:layout_height="50dp"
android:inputType="textPassword"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp">
<requestFocus />
and a submit button:
<Button
android:id="#+id/submitbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:background="#drawable/bgo"
android:clickable="true"
android:layout_gravity="right|center_horizontal"
android:layout_marginRight="20dp"/>
The Java file:
package com.berry;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class password extends Activity{
MediaPlayer mpbuttonclick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.password);
mpbuttonclick = MediaPlayer.create(this, R.raw.keypress);
Button sumbitButton = (Button) findViewById(R.id.submitbutton);
sumbitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext);
if(passwordEditText.getText().toString()=="MyPasswordHere"){
startActivity(new Intent("com.berry.intro"));
mpbuttonclick.start();
}}});
}}
This part:
if(passwordEditText.getText().toString()=="MyPasswordHere")
is incorrect. It should be
if(passwordEditText.getText().toString().equals("MyPasswordHere"))
When comparing primitive data types (like int, char, boolean) you can use ==, !=, etc.
When comparing objects (like String, Car, etc) you need to use the .equals() method.
See also this page.
It is in no way safe to check your password like that.
There are several ways to easily bypass your code
Calling the activity directly from another App
Reading the disassembled smali code to retrieve the password
Modifying the code using smali to always jump into the codeblock
Solutions available to solve these problems:
Obscure your code (Worst option, but might be enough in most cases)
Comparing the Hashed Password: Much more secure. But should be a salted Hash.
(There is also a more simple to understand explaination for the implementation)
Use a HTTP Request to a server of yours to hide the mechanism behind the password check. (But that requires your app to ask for Networking Permissions)
In the edit text field xml you can add
android:password="true"
Related
I'm trying to add ActionBar-PushToRefresh to my project. I've followed the instructions from Chris Banes Github there: Github ActionBar-PullToRefresh
Here is my view from which I want to enable the PullToRefresh feature:
<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your content, here we're using a ScrollView -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="PULLTOREFRESH THE VIEW"
android:id="#+id/textView1" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ScrollView>
</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
And here is my Activity to display the view:
package com.Test.pulltorefresh;
import android.app.Activity;
import android.os.Bundle;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.Options;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
public class MainActivity extends Activity {
private PullToRefreshLayout mPullToRefreshLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Now find the PullToRefreshLayout to setup
mPullToRefreshLayout = (PullToRefreshLayout) findViewById(R.id.ptr_layout);
// Now setup the PullToRefreshLayout
ActionBarPullToRefresh.from(this)
// Mark All Children as pullable
.allChildrenArePullable()
// Set the OnRefreshListener
//.listener(this)
// Finally commit the setup to our PullToRefreshLayout
.setup(mPullToRefreshLayout);
}
}
When I launch my app, I got the blank view but nothing happens when I'm trying to PushToRefresh my view. Can someone show me how to fix it, I know that I've missed something. Thanks.
I may be a bit partial but...
you could consider using this
Maybe you have already achieved the functionality, but in the code above, you have commented out the line which defines the action on pulling the view.
//.listener(this)
You should uncomment this line and define the listener method, which should be called when the view is pulled.
I was wondering what would happen if I wrote one code in XML and another in Java.
I had this code in my program:
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class ActivityName extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
While in my Manifest I had this:
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar">
</activity>
My application started with no issues but ran the Java code and not what I had in XML. I assume this is true for all XML edits but wanted to make sure, if I had a Java code that is similar to XML with a bit of differences, would it use my XML format or Java code?
It'll always use the Java code, since that's run second. If you're inflating from XML, though, and you inflate after doing some Java stuff (to the layout), then the XML overrides the code you wrote.
The code takes precedence over xml. You can always over-define things from the code you write. As #Cornholio pointed out if you manipulate xml-based configurations from java code it can modify things you have set from java code.
The Android framework however will never overwrite for example the xml layout files you created.
package com.example.one;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView txt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button=(Button)findViewById(R.id.button1);//error line of code
txt1=(TextView)findViewById(R.id.txt);//error line of code
button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
txt1.setText("hello");
}
});
}
#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;
}
}
I have marked the error code of lines but unable to resolve
the .xml is
<EditText
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txt"
android:layout_below="#+id/txt"
android:layout_marginLeft="18dp"
android:layout_marginTop="110dp"
android:text="Button -MYBUT" />
I just want to add a button and a textview in my app beacuse I'm a beginner Android Developer so unable to resolve my issue thanks in advance
Try to use findViewById after you inflate your menu. You are trying to find an element in a view that has not yet be attached to the activity.
This is just fine, check your imports. In android are 2 types of R present.
- R Generated, by your project builder
- R brought by android sdk
I guess that you imported R from android SDK, not generated by your project. That's why eclipse yells with errors at you.
The other situation is, that, the findViewById() method called OnCreateOptionsMenu() callback is trying to reach view that has not been created yet, and returns null.
EDIT. Problems with R might be also associated with weird, not visible and hard to find mistakes in your xml files. When you make huge mistake in some of your xml files, builder returns error and R will not be generated. But in some cases, builder cant find any of your mistakes, but is not generating new R class - this causes your R to be out of date.
I just want to know that about the explanation of this line
but = (Button) findViewById(R.id.but);
findViewById(Id) returns a View and you have to cast that View to whatever UI element you are using.
In order for findViewById(Id) to return a View, you have to assign an Id to each UI element (if you want to use it in java):
<Button
android:id="#+id/myButtonId"
[...]
/>
Now you can do this:
but = (Button) findViewById(R.id.myButtonId);
You should also use the onCreate() method to initialize your UI elements, and not onCreateOptionsMenu().
Eclipse is a Curse try to not use it guys I Prefer NetBeans I love NetBeans so much Hats off for NetBeans!
The Answer of my own Question:
it was a real time fault of my eclipse (a curse) not my fault. The Solution is to restart the IDE or to press the Save All from File Menu of IDE
Visit: https://stackoverflow.com/a/7453201/2277645
I have a hindi content based android app and have used devangiri font from Android API 16 SDK and renamed as hindi.ttf. The text renders fine on API level 16 and 17 but simply tears apart in Android API level 15 and lower.
Is there any why I can fix that without removing support for lower API levels. My code for setting textview is:
import android.app.Activity;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.GestureDetector;
import android.widget.TextView;
public class Prayer extends Activity {
// TextSwitcher vs;
GestureDetector gestureDetector;
static Tips tip = null;
static StringBuilder quesString = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prayer);
TextView tv1 = (TextView) findViewById(R.id.textView1);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/hindi.ttf");
tv1.setTypeface(tf);
tv1.setText("श्री");
tv1.setPaintFlags(tv1.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
Layout XML:
<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"
android:background="#C5C6E1"
tools:context=".Prayer" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>
</RelativeLayout>
Current:
Expected:
I faced exactly similar issue when I was developing an Hindi app. However, the error resolved when I changed the font files.
Also, IMHO the device simply replaces UTF-8 symbols in text with corresponding symbols in font files. In this case, its showing a completely different symbol altogether. So, the problem should be with font files.
Give me your e-mail in case you want to use my font files.
For Hindi support, I backported the DroidSansDevanagari-Regular.ttf font that was added in Jellybean. You can find all the Jellybean Android fonts in you SDK directory under:
/<Android SDK Dir>/platforms/android-16/data/fonts
If you are still seeing tearing on 4.0.2, you can try:
tv1.setPaintFlags(tv1.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
When setting a dynamic textSize use this:
tv1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelOffset(R.dimen.questionTEXT));
I'm trying to implement a splash screen for my webview android app, so that it will display until the page is fully loaded. I was using the code from the accepted answer here but am running into some errors that I'm not sure how to solve. I assume that I'm missing a library of some sort, but I have no idea what.
The errors I'm getting are:
View cannot be resolved to a variable
error: Error: No resource found that matches the given name (at 'src' with value #drawable/vert_loading')
The code where I'm getting the view errors is:
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
findViewById(R.id.webview).setVisibility(View.VISIBLE);
}
});
While my layout xml file is:
<ImageView
android:id="#+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="#drawable/vert_loading"
/>
My assumption is that I might be missing an import of some sort, but I wouldn't know which one. The ones I have are:
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Any help would be greatly appreciated.
Add an drawable folder in your project res from File->New->Folder and put your drawable vert_loading in res->drawable folder. then Clean your project from Project->Clean.