I'm quite new to android programming and I made my first project yesterday, a cocktail bible app. It runs perfectly but the problem is I'v loaded loads of classes and xml's to store the cocktails data. I was just wondering is there any simpler methods I could use to do away with all my unsuitable classes of the cocktails. Any help would be greatly appreciated.
Menu class
package com.drunktxtapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[] = {"Bloody_Mary", "Capirinha", "Cosmopolitan", "Cuba_Libre", "Daiquiri", "Mai_Tai", "Manhattan", "Margarita", "Martini", "Mint_Julep", "Mojito", "Old_Fashoned", "Pina_Colada", "Screwdriver", "Singapore_Sling", "Tom_Collins", "Whiskey_Sour", "White_Russian"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cocktailType = classes[position];
try{
Class<?> ourClass = Class.forName("com.drunktxtapp." + cocktailType);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
Example of one of my cocktail classes. I have many of these.
package com.drunktxtapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Bloody_Mary extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bloody_mary);
Button b1 = (Button) findViewById(R.id.bYoutube);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=Alt-ehDc3fc")));
}
});
}
}
One of my xml's for which I have many for each cocktail.
<?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"
android:background="#drawable/beer"
android:id="#+id/bloody_mary" >
<TextView
android:textStyle="bold"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Bloody Mary"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/bloodymary" />
<TextView
android:textStyle="bold"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Ingredients"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="40ml vodka \n\ 120ml tomato juice \n\ 5ml lemon or lime juice \n\ 5ml worcestershire sauce \n\ 2 dashes tabasco \n\ salt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
<TextView
android:textStyle="bold"
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:text="Preparation"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Squeeze the liquid out of the horseradish, then shake ingredients well with cracked ice in a chilled cocktail shaker, then strain into a Collins glass with 2 or 3 ice cubes in it; add a pinch of salt and a grind or two of fresh pepper, to taste. Garnish, if necessary, with a stalk of celery."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
<Button
android:id="#+id/bYoutube"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="YouTube Clip"
android:textSize="20dp" />
</LinearLayout>
My manifest with some parts removed
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.drunktxtapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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=".CocktailMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.CocktailMenu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.Menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Bloody_Mary"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.Bloody_Mary" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You will need just one Activity and one xml Layout for all types of cocktails. (Assume that all cocktails has same layout, just the text and image are different.)
Instead of getting the class like your current approach, you will pass a parameter in the start intent.
Intent ourIntent = new Intent(Menu.this, CocktailDetailActivity.class);
ourIntent.putExtra("whichcocktail", "bloodymary");
startActivity(ourIntent);
In the destination activity's onCreate method ( Let's say CocktailDetailActivity):
// Get the param back
String cocktail = getIntent().getStringExtra("whichcocktail");
textView1.setText(cocktail);
Now you have "bloodymary" display on your textview.
In real life, you would want to pass an index to your database / array.
Then in CocktailDetailActivity, you will get the info by this index and display the correct cocktail.
All you need is two activities (ListActivity and Activity to display Cocktail detail) & two xml layouts (one for each activity).
To work with this :
1. Put all your cocktail images in drawable folder(set the name of images same as the cocktail name)
2. Start the cocktail activity when user selects an cocktail from list using Intent and pass the name of the cocktail as extra (or any other detail).
Intent intent = new Intent(Menu.this, CocktailDetailActivity.class);
ourIntent.putExtra("cocktail_name","bloodymary");
startActivity(intent);
3. In CocktailActivity's onCreate() method get the extras from intent object
String cocktailName = getIntent().getStringExtra("cocktail_name");
textView1.setText(cocktailName);
4.To set the image use the cocktailName value (if stored with same name)
ImageView imgView1 = (ImageView)findViewById(R.id.imageView1);
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.cocktailName));
Related
Eg. "http://www.john.com/" + "textview content from activity"
I've tried a lot of different coding, but I can't make it work. The closest I come finding the solution is a code for Google Search Query, and I've tried to modificate the code for my needs, but it doesn't work.
I have a textview named "article" which value is fetched from a database. Let's say that the output value becomes "4545", so when I click a button I want the browser to open and start at this URL "http://www.john.com/4545".
I would really appriciate the help! Thanks in advance!
MainActivity.java
package com.example.lindstreamerss.androidsqlitesearch;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textViewInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInput = (TextView) findViewById(R.id.article);
public void onSearchClick(View v) {
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = textViewInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
layout_item.xml
<TextView
android:id="#+id/article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="28dp"
android:fontFamily="#font/regular"
android:gravity="center_vertical|start"
android:text="4545"
android:textAllCaps="false"
android:textSize="19sp">
<requestFocus />
</TextView>
<Button
android:id="#+id/imageButton"
android:background="#color/white"
android:fontFamily="#font/regular"
android:drawableLeft="#drawable/ic_web"
android:paddingLeft="25dp"
android:textSize="15sp"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onSearchClick"
android:paddingRight="40dp"
android:text="Visa artikel" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lindstreamerss.androidsqlitesearch">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.example.lindstreamerss.androidsqlitesearch.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This simply works, on button click perform below code, and make necessary change to make it compatible with you.
String term = textViewInput.getText().toString();
String myUrl = "http://www.john.com/4545"+term;
String url = "https://developer.android.com/"+"jetpack";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
Solution if you want to import from other XML:
View inflatedView = getLayoutInflater().inflate(R.layout.YOURLAYOUT, null);
TextView text = (TextView) inflatedView.findViewById(R.id.YOURID); String
escapedQuery = text.getText().toString(); Uri uri =
Uri.parse("https://www.YOURWEBSITE.com/" + escapedQuery); Intent intent = new
Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
Solution if you're using a ViewHolder (in my case RecyclerView:
String example = ((TextView) recyclerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.YOURID)).getText().toString();
Uri uri = Uri.parse("https://www.YOURWEBSITE.com/" + example);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Happy coding ;)
I am working in Eclipse on an android app which starts with a splash screen (suspecting Memory Leak here after some research done). The splash screen goes into the main menu consisting of just 7 buttons which lead to some Java Applet. The whole app was working perfectly until I changed the 7 dummy (ImageButton) png images to the finalized 7 png images. These png images have an average of 10KB and don't think they are the cause of the problem (since they're this small), even though this problem started after I changed these png images of the ImageButtons.
Honestly I don't know where to start apart from resizing the images again, cause they are designed a bit large as size in pixels (not memory) to fit different devices. But I think there is another solution to this problem which I as a beginner cannot figure out. I hope someone can help me with this problem :)
Here is the code:
Manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.letsfly.tryp"
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"
android:debuggable="true" >
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.letsfly.tryp.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Testing"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.letsfly.tryp.TESTING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypOne"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypTwo"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPTWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypThree"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPTHREE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypFour"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPFOUR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypFive"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPFIVE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Splash XML.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="600dp"
android:layout_height="800dp"
android:layout_gravity="center"
android:layout_marginTop="-180px"
android:contentDescription="#string/button1"
android:padding="0px"
android:src="#drawable/trypsplash" />
</LinearLayout>
Splash Java.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle waitFiveSeconds) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(waitFiveSeconds);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMenu = new
Intent("com.letsfly.tryp.MAINACTIVITY");
startActivity(openMenu);
}
}
};
timer.start();
}
}
MainActivity Java (where the buttons are).
package com.letsfly.tryp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton button1;
ImageButton button2;
ImageButton button3;
ImageButton button4;
ImageButton button5;
ImageButton button6;
ImageButton button7;
#Override
protected void onCreate(Bundle savedInstanceState) {
//replace yourActivity.this with your own activity or if you declared a context you can write context.getSystemService(Context.VIBRATOR_SERVICE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (ImageButton) findViewById(R.id.imageButton1);
button2 = (ImageButton) findViewById(R.id.imageButton2);
button3 = (ImageButton) findViewById(R.id.imageButton3);
button4 = (ImageButton) findViewById(R.id.imageButton4);
button5 = (ImageButton) findViewById(R.id.imageButton5);
button6 = (ImageButton) findViewById(R.id.imageButton6);
button7 = (ImageButton) findViewById(R.id.imageButton7);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypOne = new Intent("com.letsfly.tryp.TRYPONE");
startActivity(goToTrypOne);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypTwo = new Intent("com.letsfly.tryp.TRYPTWO");
startActivity(goToTrypTwo);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypThree = new Intent("com.letsfly.tryp.TRYPTHREE");
startActivity(goToTrypThree);
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypTwo = new Intent("com.letsfly.tryp.TRYPFOUR");
startActivity(goToTrypTwo);
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypFive = new Intent("com.letsfly.tryp.TRYPFIVE");
startActivity(goToTrypFive);
}
});
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#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;
}
}
MainActivity XML.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/button1"
android:src="#drawable/button1"
android:layout_weight="1"
android:layout_gravity="center"/>
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/button2"
android:src="#drawable/button2"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/button3"
android:src="#drawable/button3"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/button4"
android:src="#drawable/button4"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/button5"
android:contentDescription="#string/button5"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/button6"
android:src="#drawable/button6"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/button7"
android:src="#drawable/button7"
android:layout_weight="1"
android:layout_gravity="center" />
</LinearLayout>
Being large in pixels actually is the issue, as Android will need to inflate the compressed pngs into uncompressed bitmaps in memory to display them.
Instead of making large pngs that scale down to accomodate different size/density devices, instead use drawable folders with different qualifiers:
Images for mdpi devices: /res/drawable-mdpi
Images for hdpi devices: /res/drawable-hdpi
Images for xhdpi devices: /res/drawable-xhdpi
Images for xxhdpi devices: /res/drawable-xxhdpi
When you request an image (say, R.drawable.myImage), it will look for a myImage file in the folder that has the density qualifier that best matches the device. This lets you automatically make sure that small density devices are using images with a smaller memory footprint.
More information here:
http://developer.android.com/guide/practices/screens_support.html
I am writing a basic application which contains two activities. Both contain a TextView showing the title and the first one contains an EditText in which the user types a message and clicks on a button on its side, the second activity is launched which shows the message the user types.
It has the following problem:
1.When I click on the button, the app stops saying "Unfortunately Write n Display and stopped.", rather than launching the second activity at all.
The Logcat can be found here: enter link description here since adding it to the question exceeded the limit.
CODE OF FIRST ACTIVITY: -
package com.practice.myfirstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
//import android.view.Menu;
public class MainActivity extends Activity {
public static final String key_name="com.practice.firstApp.key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent= new Intent(this, SecondActivity.class);
EditText editText=(EditText) findViewById(R.id.EditText1_MainActivity);
String key_value= editText.getText().toString();
intent.putExtra(key_name, key_value);
startActivity(intent);
}
}
LAYOUT OF FIRST ACTIVITY: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/TextView1_MainActivity"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/title_MainActivity"
android:textStyle="bold"/>
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/TextView1_MainActivity"
android:layout_toRightOf="#id/EditText1_MainActivity"
android:text="#string/Button_MainActivity"
android:onClick="sendMessage"/>
</RelativeLayout>
CODE OF SECOND ACTIVITY: -
package com.practice.myfirstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent= getIntent();
String intent_value= intent.getStringExtra(MainActivity.key_name);
TextView textView= new TextView(this);
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
}
}
LAYOUT OF SECOND ACTIVITY: -
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/title_SecondActivity"
android:textStyle="bold"/>
<TextView
android:id="#+id/TextView2_SecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
STRINGS RESOURCE FILE:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Write n Display</string>
<string name="action_settings">Settings</string>
<string name="title_MainActivity">WRITE</string>
<string name="EditText_MainActivity">Your Message here</string>
<string name="Button_MainActivity">Send</string>
<string name="title_SecondActivity">DISPLAY</string>
</resources>
ANDROID MANIFEST FILE: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.practice.myfirstapp1"
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"
android:debuggable="true" >
<activity
android:name="com.practice.myfirstapp1.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.practive.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
To solve your first issue: put below code to your textview of both xml files.
Reason: You had not put the android:layout_centerHorizontal="true" in your TextView
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/title_MainActivity"
To solve your second issue:Change LAYOUT OF FIRST ACTIVITY:
Reason: android:layout_width is "0dp". it should be wrap_content or fill_parent or match_parent
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
To:
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content" //you can put, fill_parent or match_parent
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
To solve your third issue: change your CODE OF FIRST ACTIVITY: -
Reason: You had put editText=(EditText) findViewById(R.id.EditText1_MainActivity); in sendMessage(View view). send message contains a View so whenever you initiate any component in this method, it will search that component on this particular view which is incorrect. you can initiate your components in any method which does not contain any view parameter.if you want to initiate any component in particular view then you have to initiate it like editText=(EditText)view.findViewById(R.id.EditText1_MainActivity);. But in your case edittext is in main view so you need to remove it from sendMessage(View view).
public class MainActivity extends Activity {
public static final String key_name="com.practice.firstApp.key";
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.EditText1_MainActivity);
}
public void sendMessage(View view){
Intent intent= new Intent(this, SecondActivity.class);
String key_value= editText.getText().toString();
intent.putExtra(key_name, key_value);
startActivity(intent);
}
}
Change your second Activity
Reason: TextView textView= new TextView(this); is not correct way to initialize any component of your xml view.
TextView textView= new TextView(this);
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
To:
TextView textView;
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
you can solve your problem like this
1.replace
android:gravity="center_horizontal" by android:layout_gravity="center_horizontal".
2.Give layout_width as wrap_content for edittext of first activity xml
3.Make your sendMessage method public not private
To get the logcat In Eclipse, Goto Window-> Show View -> Other -> Android-> Logcat.
OR
Write LogCat in Quick Access edit box in your eclipse window (top right corner, just before Open Prospective button). And just select LogCat it will open-up the LogCat window in your current prospec
change private void sendMessage(View view) to public void sendMessage(View view) (private to public)
LAYOUT OF FIRST ACTIVITY:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/TextView1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/title_MainActivity"
android:textStyle="bold"/>
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button_MainActivity"
android:onClick="sendMessage"/>
LAYOUT OF SECOND ACTIVITY: -
<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:orientation="horizontal"
android:gravity="center_vertical"
tools:context=".SecondActivity">
<TextView
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/title_SecondActivity"
android:textStyle="bold"/>
<TextView
android:id="#+id/TextView2_SecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Logcat shows the following:
Caused by: android.content.ActivityNotFoundException: Unable to find
explicit activity class
{com.practice.myfirstapp1/com.practice.myfirstapp1.SecondActivity};
have you declared this activity in your AndroidManifest.xml?
You did define SecondActivity in manifest, but the package name is wrong ( android:name="com.practive.myfirstapp1.SecondActivity" should be ndroid:name="com.practice.myfirstapp1.SecondActivity"
).
<activity
android:name="com.practice.myfirstapp1.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.practive.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
It should be
<activity
android:name="com.practice.myfirstapp1.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.practice.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
i have prepared a entry screen for my program and where i have put an edittext and also ImageViews.Now the problem is that m able to type the text inside the edittext through the virtual keyboard but the problem is that after i m done with my text then i m not able to come out of the edittext,the cursor remains at the edittext box only.
i have a imageView below to be clicked to proceed with the form but it is nt clickable at that momen..please help!!!
my code is
my xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/begin_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170dp"
android:layout_marginTop="220dp"
android:src="#drawable/begin" />
<ImageView
android:id="#+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="30dp"
android:src="#drawable/enter_name" />
<ImageView
android:id="#+id/roof"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/roof" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_marginTop="180dp"
android:ems="8"
android:inputType="textMultiLine"
android:imeOptions="actionDone"/>
</FrameLayout>
my main java class
package game.pack;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.*;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Begin extends Activity implements View.OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.entry);
ImageView begin = (ImageView) findViewById(R.id.begin_);
begin.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.begin_:
setContentView(new SushiMain(this));
break;
default:
break;
}
}
}
my android manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="game.pack"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".FrontScreen"
android:screenOrientation="landscape"
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=".Begin"
android:screenOrientation="landscape"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="begingame" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Sushitap"
android:screenOrientation="landscape"
android:label="#string/app_name" >
<intent-filter>
<action android:name="gameover" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
use View.OnKeyListener for EditText to remove Focus from EditText when ENTER KEY PRESSED after typing in EditText :
ditText.setOnKeyListener(onSoftKeyboardDonePress);
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
// code to hide the soft keyboard
EditTexst.clearFocus();
EditTexst.requestFocus(EditText.FOCUS_DOWN);
}
return false;
}
};
Try this.
editTextId.clearFocus();
editTextId.requestFocus(EditText.FOCUS_DOWN); // assuming that you want to move the focus to the next View Element. If you want it to go to the previous View, use FOCUS_UP
Can you try putting this in layout file for edittext?
android:focusableInTouchMode="true"
If your app requires an edit text and an image view next to it..then here's the code u can use.
In layout.xml u can add this code for edittext with image
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white2" >
<EditText
android:id="#+id/et_search"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:drawableRight="#drawable/search_20"
android:ems="10"
android:hint="#string/search_brdNm" />
</LinearLayout>
and in your java class you can have this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_asset);
mEt_search = (EditText) findViewById(R.id.et_search);
mEt_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String str_searchTxt = mEt_search.getText().toString().trim();
String str_channelNm = AppConfigurationUtils.getChannelName();
if (str_searchTxt.length() == 0) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.ENTER_MANDATORY),
Toast.LENGTH_LONG).show();
return;
}
}
});
}
HEY guys my problem is solved!!!
actually i was moving from some other view to this view and in the first view i was calling the present class activity by its layout Id so in this case the imageview was not behaving as a normal button but as just a view
so in the first view i used an intent to call the class of the activity which i hav posted above and this successfully completed my task!!!
Thank you everyone for your time and efforts...much appreciated!!!!
THANK YOU!!!
You need to link your edittext in your xml to your code. In your oncreate method add
EditText edittext= (EditText ) findViewById(R.id.editText2);
Im a newbie in android development and thats why im asking you experts such a simple problem
I created a table view and it is working fine.
I tried to move to a new view when clicking on the first cell and for that i created a new file Newview.java.
But when clicking on the first cell app is crashing and showing the message "Unfortunately HelloTableLayout has stopped".
Can anyone please help me with this.
HelloTableLayoutActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class HelloTableLayoutActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView name = (TextView)findViewById(R.id.label);
name.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent nameActivity =new Intent();
nameActivity .setClass(getApplicationContext(), Newview.class);
startActivity(nameActivity);
}
});
}
}
Main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<TableRow>
<TextView
android:layout_column="1"
android:text="#string/name"
android:padding="3dip" />
<TextView
android:text="#string/initial"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="#string/hometown"
android:padding="3dip" />
<TextView
android:text="#string/state"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
Newview.java
import android.app.Activity;
import android.os.Bundle;
public class Newview extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
}
}
HelloTableLayout Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sweans.tb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
<activity
android:label="#string/app_name"
android:name=".HelloTableLayoutActivity">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewviewActivity"
android:label="#string/app_name"
>
</activity>
</application>
</manifest>
foo.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
Can anyone please help me where im going wrong in my code and why the app is crashing.
Thanks in advance.
Please change the following code .
Intent nameActivity =new Intent();
nameActivity .setClass(getApplicationContext(), Newview.class);
startActivity(nameActivity);
with the below code.
Intent nameActivity =new Intent(HelloTableLayoutActivity.this,Newview.class);
startActivity(nameActivity);
and define your Newview class in android manifest file.
<activity android:name=".Newview" > </activity>
Because as you call Newview from your main class and you declare NewviewActvity class in android manifest file , so when you click on textview at that time android finds the Newview class in android manifest file, because it not present in that manifest file your application goest to crash.