android:label problem in AndroidManifest (can't set title) - android

When I tried to give different titles in AndroidManifest.xml for each activity, app shows first title for all children activities instead off given titles. I'm new in coding so sorry for wrong explanation of my problem. The code is as following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.miwok">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NumbersActivity"
android:label="#string/category_numbers" />
<activity
android:name=".FamilyActivity"
android:label="#string/category_family" />
<activity
android:name=".ColorsActivity"
android:label="#string/category_colors" />
<activity
android:name=".PhrasesActivity"
android:label="#string/category_phrases" />
</application>
</manifest>
How can I solve it?
Update: to make it more clear I'm adding my MainActivity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
TextView numbers = (TextView) findViewById(R.id.numbers);
TextView colors = (TextView) findViewById(R.id.colors);
TextView family = (TextView) findViewById(R.id.family);
TextView phrases = (TextView) findViewById(R.id.phrases);
assert numbers != null;
numbers.setOnClickListener(this);
assert colors != null;
colors.setOnClickListener(this);
assert family != null;
family.setOnClickListener(this);
assert phrases != null;
phrases.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.numbers:
Intent numberIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(numberIntent);
break;
case R.id.colors:
Intent colorsIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(colorsIntent);
break;
case R.id.family:
Intent familyIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(familyIntent);
break;
case R.id.phrases:
Intent phrasesIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(phrasesIntent);
break;
}
}
}
Second update:
When I copied Intents for each activity, I forgot to change activity names, so problem was ot in title but in opened activity.

You can name it in code:
by
actionbar.setTitle("title here");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!MyApplication.getInstance().isTablet())
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_settings);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getString(R.string.settings));
....
}

So when I copied MainActivity.java from course example, all titles start working normally. The difference is that I used 'switch (view.getId())' instead of creating new onClick for each activity. I still didn't understand why it doesn't work, but hope I will learn it with more experience.
Summary:
Using 'switch' statement for activities can cause problems (for ones without knowledge,like me) in children activities titles.

Related

No Activity found to handle Intent

I know that there is a lot of questions related to this issue, but none of them solve my problem. I made a quiz app with 50 questions fragments. In the final question (Question 50 fragment) I call the score Activity to show the score. In this Activity I created a "Play Again" button, which calls the Main Activity again to start the game. But everytime I click on "Play Again" button, my app crashes.
Logcat error: 04-03 15:34:43.638 26316-26316/com.example.moresche.englishqigame E/AndroidRuntime: FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.moresche.englishqigame.MainActivity }
scoreActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
Typeface mTypeface = Typeface.createFromAsset(getAssets(),"chlo.TTF");
TextView myTextview = (TextView)findViewById(R.id.txtla);
myTextview.setTypeface(mTypeface);
Typeface m2Typeface = Typeface.createFromAsset(getAssets(),"chlo.TTF");
TextView m2yTextview = (TextView)findViewById(R.id.txtla1);
m2yTextview.setTypeface(m2Typeface);
initControls();
}
public void initControls() {
TextView final_score = (TextView) findViewById(R.id.textView103);
TextView final_scoreqi = (TextView) findViewById(R.id.textView104);
TextView lvlqi = (TextView) findViewById(R.id.textViewzzz);
Button btnxd = (Button)findViewById(R.id.btnx);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
...
btnxd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
app_preferences.edit().clear().commit();
Intent intent1 = new Intent ("com.example.moresche.englishqigame.MainActivity");
startActivity(intent1);
}
});
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moresche.englishqigame">
<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"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".scoreActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.example.moresche.englishqigame.scoreActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You are calling your intent in the wrong way.
Intent intent1 = new Intent ("com.example.moresche.englishqigame.MainActivity");
It should be something along the lines of:
Intent intent1 = new Intent(scoreActivity.this,MainActivity.class);
http://developer.android.com/training/basics/firstapp/starting-activity.html
Although that will only taking you to the page not play the game again.

Unfortunately the app has stopped. What can I do to fix this?

I am programming an app. There is no errors. I can run the app, but when I press the button it says:
Unfortunately the app has stopped.
What can I do?
Here is my activity code:
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 =(Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
private void button1Click()
{
startActivity (new Intent ("com.example.cp3.tutorial.Class2"));
}
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
button1Click();
}
}
and the manifest:
<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.cp3.tutorial.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.cp3.tutorial.Class2"
android:label="#string/title_activity_class2" >
</activity>
</application>
</manifest>
Change button1Click() like this:
private void button1Click()
{
startActivity (new Intent (MainActivity.this, Class2.class));
}
startActivity (new Intent ("com.example.cp3.tutorial.Class2"));
here in your code! you refer to a package to be opend. if you want to open an activity on button click use this code:
Intent intent = new Intent(firstActivity.this, secoundActivity.class);
startActivity(intent);
put this on your click listener, instead of firstActivity put your current activity and secoundActivity put your activity name you want to be opened.
Hope this help you.

Activity not started

In my OnClick event attached to an imageview, I want to open a new activity with a screensized imageview on it to show the picture.
This is the event:
public void onClick(View arg0) {
PhotoList pli = (PhotoList) arg0.getTag();
Intent i = new Intent(getBaseContext(), PhotoActivity.class);
i.putExtra("photo", pli.Photo);
i.putExtra("PhotoId", pli.id);
getBaseContext().startActivity(i);
}
This code is being executed when I click on the image, but nothing happens.
The onCreate of PhotoActivity (which is never executed):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
getActionBar().setDisplayHomeAsUpEnabled(true);
Bundle extras = getIntent().getExtras();
photo = extras.getByteArray("photo");
ViewId = extras.getInt("View");
PhotoId = extras.getInt("PhotoId");
if (photo.length > 0) {
ImageView iv = (ImageView) findViewById(R.id.pvPhoto);
Bitmap mBitmap = BitmapFactory.decodeByteArray(photo, 0, photo.length);
iv.setImageBitmap(mBitmap);
}
The manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="enn.com.sitefinder.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PhotoActivity"
android:label="#string/title_activity_photo" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="enn.com.sitefinder.MainActivity" />
</activity>
</application>
When I accidently connected the OnClick eventlistener to the linear layout, the activity did start.
Why is the activity not shown?
Solved it by passing on a extra integer in the extras, allowing the PhotoActivity class to find the image in the arraylist.
This instead of passing the bitmap byte array which is too much for the bundle.
you do not need to use getBaseContext.startActivity
just use startActivity(intent);

Nothing happens on startActivity()

I have 2 activities with a few buttons etc. I want to start new activity. I have done everything the tutorial said, but the 2nd activity does not start!
case R.id.btnIstorija:
Intent i = new Intent (this,KlasaPrikazBaze.class);
startActivity(i);
break;
It should start my 2nd activity
public class KlasaPrikazBaze extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prikazbaze);
TextView TV = (TextView)findViewById(R.id.tvSQLinfo);
KlasaBaze info = new KlasaBaze(this);
info.open();
String podatak = info.DohvatiPodatak();
info.close();
}
}
However nothing happens.
Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".TalentiFinalActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".KlasaPrikazBaze"></activity>
</application>

How to change title of Activity in Android?

I am using
Window w = getWindow();
w.setTitle("My title");
to change title of my current Activity but it does not seem to work.
Can anyone guide me on how to change this?
Try setTitle by itself, like this:
setTitle("Hello StackOverflow");
Just an FYI, you can optionally do it from the XML.
In the AndroidManifest.xml, you can set it with
android:label="My Activity Title"
Or
android:label="#string/my_activity_label"
Example:
<activity
android:name=".Splash"
android:label="#string/splash_activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you want it one time & let system handle the rest (not dynamic) then do like this in your manifest file:
<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_full" > //This is my custom title name on activity. <- The question is about this one.
<intent-filter android:label="#string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
setTitle(getResources().getText(R.string.MyTitle));
There's a faster way, just use
YourActivity.setTitle("New Title");
You can also find it inside the onCreate() with this, for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("My Title");
}
By the way, what you simply cannot do is call setTitle() in a static way without passing any Activity object.
This worked for me.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
getActivity().setTitle("My Title");
//...
}
If you have multiple activities, you can set it like this in AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NumbersActivity"
android:label="#string/category_numbers"
android:theme="#style/category_numbers" />
<activity
android:name=".FamilyActivity"
android:label="#string/category_family"
android:theme="#style/category_family" />
<activity
android:name=".ColorsActivity"
android:label="#string/category_colors"
android:theme="#style/category_colors" />
<activity
android:name=".PhrasesActivity"
android:label="#string/category_phrases"
android:theme="#style/category_phrases" />
<activity
android:name=".ExperimentActivity"
android:label="#string/category_experiment"
android:theme="#style/category_experiment" />
</application>
I'm using Android Studio 3.0.1.
WIth an Activity:
setTitle("Title Text");
Inside a fragment:
getActivity().setTitle("Title Text");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
this.setTitle("Title name");
}
If you want to set title in Java file, then write in your activity onCreate
setTitle("Your Title");
if you want to in Manifest then write
<activity
android:name=".MainActivity"
android:label="Your Title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In Kotlin, this way:
this.title = resources.getText(R.string.fast_learning)
I have a Toolbar in my Activity and a Base Activity that overrides all Titles. So I had to use setTitle in onResume() in the Activity like so:
#Override
protected void onResume() {
super.onResume();
toolbar.setTitle(R.string.title);
}
The code helped me change the title.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
ActivityName.this.setTitle("Your Activity Title");}
Inside a MainActivity:
public class act1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
setTitle("First Activity");
}
}
setTitle("Whatever apps");
in MainActivity.java is simplier
I would say.
If you want to change Title of activity when you change activity by clicking on the Button. Declare the necessary variables in MainActivity:
private static final String TITLE_SIGN = "title_sign";
ImageButton mAriesButton;
Add onClickListener in onCreate() and make new intent for another activity:
mTitleButton = (ImageButton) findViewById(R.id.title_button);
mTitleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
SignActivity.class);
String title_act = getText(R.string.simple_text).toString();
intent.putExtra("title_act", title_act);
startActivity(intent);
finish();
}
});
SecondActivity code in onCreate():
String txtTitle = getIntent().getStringExtra("title_act");
this.setTitle(txtTitle);
If you're using onCreateOptionsMenu, you can also add setTitle code in onCreateOptionsMenu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
setTitle("Neue Aktivität");
return true;
}
setTitle("Welcome Activity");

Categories

Resources