Contextual Actionbar with DialogFragment - android

I've been trying to implement a contextual action bar along with a dialog fragment.
Similar to the downloads widget in android.
I've tried to set android:windowActionModeOverlay to be true in the theme.
But it doesnt seem to work. Is there any way I can achieve it??

The downloads window that you have in your screenshot is actually an Activity using the #android:style/Theme.Holo.Dialog theme which makes it look like a dialog. To achieve the same look as the downloads window, your Activity need only use the same theme.
You can set this theme in your manifest like so:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Example implementation excluding string and drawable resources.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mceley.dialog.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.mceley.dialog.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
findViewById(R.id.context_button).setOnClickListener(this);
}
#Override
public void onClick(View v) {
ExampleMode mode = new ExampleMode();
startActionMode(mode);
}
public class ExampleMode implements ActionMode.Callback {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
}
}
main_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<Button android:id="#+id/context_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/show_context_bar" />
</LinearLayout>
main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Result:

I don't think you can add ActionBar in DialogFragment.
But We can Try that using Activity As Dialog.
I have tried this with ActionBarSherlock and I have added R.style.Sherlock___Theme_Dialog as my Activity's theme but it looks like this:
After doing Above thing i understud, we can't Add ActionBar in Dialog or DialogFragment.

Related

How we start an activity in MainActivity which extends AppCompatActivity [duplicate]

This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 4 years ago.
I used startActivity() method but it is not working.I think this method is not working because my MainActivity extends app compat actvity.
So please help me to start activity in an app compat activity.
I try various times but when I start my apk and click on button then app crashes.
Here my code
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E96050"/>
<TableRow><Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainButton"
android:text="Open Second Activity"
android:onClick="yas"/></TableRow> </TableLayout>
MainActivity.java
package com.mycompany.myapp;
import android.app.Activity;
import android.os.*;
import android.support.v7.app.*;
import
android.support.v7.widget.Toolbar;
import android.view.*;
import android.widget.Toast;
import android.content.Intent;
import android.support.v7.app.*;
public class MainActivity extends
AppCompatActivity {
#Override
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
private void CreateMenu(Menu menu) {
MenuItem mnu1 = menu.add(0,0,0,"item1");
{
mnu1.setIcon(R.drawable.ic_launcher);
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
public void yas(View vv) {
Intent iiii = new
Intent(MainActivity.this,SecondAct.class);
startActivity(iiii);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="secondActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="SecondActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
use this
Intent intent=new Intent(this,Target_Activity.class);
startActivity(intent);
And open your manifest file add this line
<activity android:name=".Target_Activity"
android:parentActivityName=".MainActivity">
</activity>
I hope you try this code..
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Please add second activity into android manifest file.
<activity android:name=".SecondActivity"/> // define second activity name.
add this code in android manifest file..
<activity
android:name=".SecondActivity"
android:label="secondActivity" >
</activity>

Display App Icon in action bar under api 11

I'm writing simple tutorial app and for some reason my App icon is not showing up (I want to use it as navigate up button instead of the arrow).
As stated in the topic, I want to do it on Api level 11, however all the sollutions I've found rely on setIcon method.
getActionBar().setIcon(R.drawable.ic_launcher); //only on API >= 14
The fragment preview in Android studio 1.0.2 does show the icon, however the emulator does not, so I'd expect the resource is ok. Also, the search and delete icons are working like a charm. I've tried setting this both as Icon and a logo to no avail.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yamba"
android:versionCode="1"
android:versionName="1.0"
>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light"
android:logo="#drawable/ic_launcher"
>
<activity
android:parentActivityName=".MainActivity"
android:name=".StatusActivity"
android:label="#string/app_name" >
</activity>
<!--- The labe is what's displayed at the taskbar!-->
<activity
android:parentActivityName=".MainActivity"
android:name=".SettingsActivity"
android:label="#string/title_activity_settings" >
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_settings.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: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="com.example.yamba.SettingsActivity">
<TextView android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
menu_settings.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.yamba.SettingsActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
SettingsActivity.java
package com.example.yamba;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SettingsActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null){
SettingsFragment fragment = new SettingsFragment();
getFragmentManager().beginTransaction().add(android.R.id.content, fragment, fragment.getClass().getSimpleName()).commit();
setContentView(R.layout.activity_settings);
// getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
// only for API>=14
// getActionBar().setDisplayShowHomeEnabled(true);
// getActionBar().setIcon(R.drawable.ic_launcher);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_settings, menu);
// return true;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id){
case R.id.action_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
case R.id.action_tweet:
startActivity(new Intent(this, com.example.yamba.StatusActivity.class));
return true;
default:
return false;
}
}
}
SettingsFragment.java
package com.example.yamba;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
/**
* Created by me on 19-01-2015.
*/
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences prefs;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
public void onStart(){
super.onStart();
prefs= PreferenceManager.getDefaultSharedPreferences(getActivity());
prefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
}
}
I've stumbled upon suggestion that I may be able to define custom style based on one of the existing styles, but can't quite figure out how to approach it.
If you need any other file, let me know and thanks for your time.
since it's API 11..
did you try using the support libraries?
try switching
getActionBar().setIcon(R.drawable.ic_launcher);
with :
getSupportActionBar().setIcon(R.drawable.ic_launcher);
tell me if it works and we'll continue from there :)

Android Orientation tag error

I am using eclipse android
I want to know about the orientantion changes ,when orientation changes gget a log message in logid ,Below is our code
package com.example.orientation;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d("Android","orientation changes"+newConfig.orientation);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orientation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.orientation.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
here we don't get the message in the log cat .I am trying to log the integer value of the orientation .there is no error in the code
Use
<activity
android:name="com.example.orientation.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize">
Instead of this....
<activity
android:name="com.example.orientation.MainActivity"
android:label="#string/app_name" >
android:configChanges="orientation|screenSize">
see the difference

Manifest merging in Android Studio

Is there any way to use ADT's manifest merging feature (manifestmerger.enabled=true in project.properties) in Android Studio?
1. create sample project
2. add new module
3. Module Setting
4. Remove files in App Module
Move app/~~~/values/style.xml to common/~~~/values/style.xml
edit 1.
<!-- app/~~~/AndroidManifest.xml -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.susemi99.manifestmergerforandroidstudio" >
<application >
<activity
android:name="kr.susemi99.common.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
edit 2.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"> <!-- add this line -->
<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>
edit 3.
package kr.susemi99.common;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings)
{
Toast.makeText(getApplicationContext(), "test toast", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
See all http://susemi99.kr/2368

Android - Spinning circle appears in Action Bar

So I have an action bar I am trying to learn and when I implement it I see this white spinning circle left of my first menu item. Why is this being shown here?
Source:
package com.example.lookingfor;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.atcAbout:
break;
case R.id.atcContact:
break;
case R.id.atcRate:
break;
}
return true;
}
}
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/atcAbout"
android:showAsAction="ifRoom"
android:title="About"/>
<item
android:id="#+id/atcRate"
android:showAsAction="ifRoom"
android:title="Rate this app"/>
<item
android:id="#+id/atcContact"
android:showAsAction="ifRoom"
android:title="Contact Us"/>
</menu>
Main Layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lookingfor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<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"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is a screenshot (I cannot post images here yet)
http://imgur.com/RaTTP
How can I get rid of this spinning circle before I publish the app? It is an ugly and annoying distraction to the app itself.
Thanks Y'all!
Try commenting out the line requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); in your onCreate() callback.

Categories

Resources