My navigation drawer doesn't show any items. I changed the background to white, theme has changed but still no items showing up. I am also using the frame layout inside drawer and linear layouts. No errors or messages. What am I doing wrong?
EDIT: I oversimplified my work, but still nothing. No items..
EDIT2: After deleting more I made some progress, now I see that, in preview, my drawer_menu shows up like options menu on top right corner even though in the postLoginactivity.xml file preview shows what I aimed to do. Still no items on emulator.
EDIT3: I have Cutt my code in halve, still can't see what did I do wrong.
activity_post_login.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<!-- burdan sonra komple sıkıntı gibi?-->
<LinearLayout
android:id="#+id/llayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Light" >
<TextView
android:id="#+id/textView"
android:layout_width="326dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:accessibilityLiveRegion="assertive"
android:paddingStart="100dp"
android:paddingEnd="100dp"
android:text="#string/deneme"
android:textSize="30sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v7.widget.Toolbar>
<!-- android:layout_height=?attr/actionBarSize-->
<FrameLayout
android:id="#+id/mainFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="parent"
tools:layout_editor_absoluteX="0dp"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
>
<ExpandableListView
android:id="#+id/list_sliderMenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:paddingTop="176dp"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="1dp"
/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
nav_header.xml
<?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="176dp"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:padding="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher_round"/>
</LinearLayout>
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<!--tools:showIn="navigation_view"-->
<group android:checkableBehavior="single"
android:id="#+id/menu_list">
<item android:title="#string/satisYonetimi"
android:id="#+id/satis_yonetimi"/>
<item
android:id="#+id/insanKaynaklari"
android:title="#string/insanKaynaklari" />
<item
android:id="#+id/muhasebeYonetimi"
android:title="#string/muhasebeYonetimi" />
<item
android:id="#+id/aktiviteYonetimi"
android:title="#string/aktiviteYonetimi" />
<item
android:id="#+id/destekYonetimi"
android:title="#string/destekYonetimi" />
</group>
</menu>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="#style/AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
v21\styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="#style/AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
Plus if anyone thinks problem might be somewhere else here are category.java(expandable list), subcategory.java (future sub menus will be implemented ) and and my activity for the drawer to be displayed(under implementation)
category.java
public class Category {
private String cat_name;
private int cat_code;
public void setCat_name(String cat_name) {
this.cat_name = cat_name;
}
public String getCatName(){
return cat_name;
}
public void setCat_code(int cat_code) {
this.cat_code = cat_code;
}
public int getCat_code() {
return cat_code;
}
}
subcategory.java
public class SubCategory {
private String subcat_name;
private String subcat_code;
public void setSubCatName(String subcat_name) {
this.subcat_name = subcat_name;
}
public String getSubCatName(){
return subcat_name;
}
public void setSubCatCode(String subcat_code) {
this.subcat_code = subcat_code;
}
public String getSubCatCode() {
return subcat_code;
}
}
postLogingActicity.java
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import java.util.ArrayList;
public class PostLoginActivity extends AppCompatActivity {
ActionBar bar;
ViewPager viewPager;
FragmentPagerAdapter fpAdapter;
Fragment mMainScreen;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ExpandableListView mCategoryList;
private ArrayList<Category> category_name = new ArrayList<Category>();
private ArrayList <ArrayList<Category>> subcategory_name = new ArrayList
<ArrayList<Category>>();
private ArrayList<Integer> subCatCount = new ArrayList<Integer>();
int previousGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_login);
//Toolbar toolbar =findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
//mDrawerLayout = findViewById(R.id.drawer_layout);
//ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
//mDrawerLayout, toolbar,
// R.string.navigation_drawer_open,
//R.string.navigation_drawer_close);
//mDrawerLayout.addDrawerListener(toggle);
//toggle.syncState();
//this.getCatData();
//Utils.deleteStringFromSp(this, "email");
}
public void onBackPressed(){
if(mDrawerLayout.isDrawerOpen(GravityCompat.START))
mDrawerLayout.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
}
}
your main activity must be as follows
<android.support.v4.widget.DrawerLayout 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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
And activity_main_drawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_menu7"
android:icon="#drawable/ic_school_black_24dp"
android:title="XXXX" />
<item
android:id="#+id/nav_menu11"
android:icon="#drawable/ic_thumb_up_black_24dp"
android:title="XXXX" />
<item
android:id="#+id/nav_menu12"
android:icon="#drawable/ic_insert_drive_file_black_24dp"
android:title="XXXX" />
<item
android:id="#+id/nav_menu6"
android:icon="#drawable/ic_developer_mode_black_24dp"
android:title="XXXX" />
</group>
</menu>
In Kotlin Navigation Menu
// Navigate Menu
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.nav_menu7 -> {
val intent = Intent(this, Computer::class.java)
startActivity(intent)
}
R.id.nav_menu6 -> {
val menu6 = Menu6()
val fragmentManager = supportFragmentManager
fragmentManager.beginTransaction().replace(R.id.fragment, menu6).addToBackStack(null).commit()
}
R.id.nav_menu12 -> {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://XXXXX"))
startActivity(intent)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
I hope this solve your problem
Menu works with Activity,Fragment,URL
You've missed to reference the menu of the navigation view
As you named it "drawer_menu_xml", then the modification would be:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"/>
According to How to style PopupMenu? you cannot set a popupmenu style directly, which seems contrary to https://developer.android.com/reference/android/widget/PopupWindow.html
It states you can specify a style in the PopupMenu constructor. According to http://blog.http417.com/2014/06/styling-popupmenu.html it looks like the attributes I need to specify are "android:popupBackground" and "android:dropDownWidth". However, the following just removes the default style and doesn't apply my desired color or width:
styles.xml:
<style name="popup">
<item name="android:popupBackground">#color/material_blue_grey_800</item>
<item name="android:dropDownWidth">350dp</item>
</style>
ActivityA.java
public class ActivityA extends AppCompatActivity implements
PopupMenu.OnMenuItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
findViewById(R.id.popupBtn).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View button) {
PopupMenu popupMenu = new PopupMenu(ActivityA.this,
button, Gravity.RIGHT, 0, R.style.popup);
}
}
#Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
}
activity_a.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=".activities.ActivityA">
<Button
android:id="#+id/popupBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Press to open menu"/>
</RelativeLayout>
You cannot apply a style programmatically but you can do it with a theme: https://developer.android.com/guide/topics/ui/themes.html#ApplyAStyle a similar answer can be found here: https://stackoverflow.com/a/2016344/936269
Add this to your AppTheme ..
<item name="android:popupMenuStyle">#style/popup</item>
<item name="popupMenuStyle">#style/popup</item>
Good luck!
You can declare the styles in your theme like below:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Other styles -->
<!-- PopUpMenu styles -->
<item name="popupMenuStyle">#style/Widget.App.PopupMenu</item>
</style>
<!-- Widget PopUpMenu Style-->
<style name="Widget.App.PopupMenu" parent="Widget.AppCompat.Light.PopupMenu">
<!-- Whatever Styles Put Here-->
</style>
I'm trying to implement Guillotine Menu by yalantis in my app.
I've put one button in the guillotine activity and have made a class for the guillotine activity. In that class I'm trying to implement the listener for the button .I've also a put a toast in that function but that's not getting displayed either.
Here is the guillotine activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/guillotine_background"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/guillotine_background"
android:layout_margin = "0dp"
android:divider="#666"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
style="#style/Toolbar"
app:contentInsetStart="0dp">
<ImageView
android:id="#+id/guillotine_hamburger"
style="#style/ImageView.Hamburger"
android:src="#drawable/ic_menu" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="#+id/profile_group"
style="#style/LinearLayout.GuillotineItem"
android:layout_marginTop="#dimen/guillotine_medium_margin">
<ImageView
style="#style/ImageView.GuillotineItem"
android:src="#drawable/ic_profile" />
<Button
style="#style/TextView.GuillotineItem"
android:text="MAIN PAGE"
android:background="#color/guillotine_background"
android:id="#+id/b1"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/feed_group"
style="#style/LinearLayout.GuillotineItem">
<ImageView
style="#style/ImageView.GuillotineItem"
android:src="#drawable/ic_feed" />
<TextView
style="#style/TextView.GuillotineItem"
android:text="#string/feed" />
</LinearLayout>
<LinearLayout
android:id="#+id/activity_group"
style="#style/LinearLayout.GuillotineItem">
<ImageView
style="#style/ImageView.GuillotineItem"
android:src="#drawable/ic_activity_active" />
<TextView
style="#style/TextView.GuillotineItem.Selected"
android:text="#string/activity" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/guillotine_divider_height" />
<LinearLayout
android:id="#+id/settings_group"
style="#style/LinearLayout.GuillotineItem">
<ImageView
style="#style/ImageView.GuillotineItem"
android:src="#drawable/ic_settings" />
<TextView
style="#style/TextView.GuillotineItem"
android:text="#string/settings" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
And here is the class:
package manipal.freshers;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import java.io.File;
/**
* Created by Ankit Vimal on 28-02-2016.
*/
public class Guillotine extends AppCompatActivity {
private Button b1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guillotine);
b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show();
Intent i = new Intent(Guillotine.this, MIT_Sports_Club_Activity.class);
startActivity(i);
}
}
);
}
}
Styles.xml:
<resources>
<style name="LinearLayout" />
<style name="LinearLayout.GuillotineItem">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
<item name="android:layout_marginTop">#dimen/guillotine_small_margin</item>
</style>
<style name="TextView" />
<style name="TextView.ToolbarTitle">
<item name="android:gravity">center</item>
<item name="android:textSize">24sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TextView.GuillotineItem">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">#dimen/guillotine_small_margin</item>
<item name="android:textSize">#dimen/guillotine_item_text_size</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:textColor">#android:color/white</item>
</style>
<style name="TextView.GuillotineItem.Selected">
<item name="android:textColor">#color/selected_item_color</item>
</style>
<style name="Toolbar">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="android:minHeight">?attr/actionBarSize</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#color/guillotine_background</item>
</style>
<style name="ImageView" />
<style name="ImageView.ContentItem">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
</style>
<style name="ImageView.Hamburger" parent="Widget.AppCompat.ActionButton">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_marginBottom">#dimen/abc_action_bar_overflow_padding_start_material</item>
</style>
<style name="ImageView.GuillotineItem">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_marginLeft">#dimen/guillotine_large_margin</item>
</style>
</resources>
Themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.Guillotine"/>
<style name="Theme.Guillotine" parent="Theme.AppCompat.NoActionBar">
<item name="android:color">#color/guillotine_background</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorPrimaryDark">#color/guillotine_background</item>
<item name="android:colorPrimary">#color/guillotine_background_dark</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
</style>
</resources>
MainActivity.java:
package manipal.freshers;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.yalantis.guillotine.animation.GuillotineAnimation;
public class MainActivity extends AppCompatActivity {
private static final long RIPPLE_DURATION = 250;
#InjectView(R.id.toolbar)
Toolbar toolbar;
#InjectView(R.id.root)
FrameLayout root;
#InjectView(R.id.content_hamburger)
View contentHamburger;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private TextView t;
int []imageArray={R.drawable.manipal1,R.drawable.manipal2,R.drawable.manipal3,R.drawable.manipal4,R.drawable.manipal5};
private ImageView i1;
private final Handler handler = new Handler();
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
i1=(ImageView) findViewById(R.id.imageView1);
t = (TextView) findViewById(R.id.textView3);
t.setMovementMethod(new ScrollingMovementMethod());
if(toolbar!= null){
if (android.os.Build.VERSION.SDK_INT >= 11){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
}}
View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.guillotine, null);
root.addView(guillotineMenu);
new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.guillotine_hamburger), contentHamburger)
.setStartDelay(RIPPLE_DURATION)
.setActionBarViewForAnimation(toolbar)
.setClosedOnStart(true)
.build();
Runnable runnable = new Runnable() {
int i=0;
public void run()
{
i1.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 3000); //for interval...
}
};
handler.postDelayed(runnable,0);
}
}
package com.example.vinaymaneti.button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
});
}
}
Im showing a popup alert activity, when select a notification from tray.
But on top it's showing the App name (MCP), with improper alignment.
I don't want the top part, just a normal dialog.
Manifest:
<activity
android:name=".activity.AlertDialogActivity"
android:configChanges="orientation|locale|screenSize|uiMode|fontScale"
android:excludeFromRecents="true"
android:theme="#style/AlertDialogTheme" />
Style:
<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Alert activity:
public class AlertDialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
this.setFinishOnTouchOutside(false);
String message = getIntent().getExtras().getString("message");
TextView tvPushMessage = (TextView) findViewById(R.id.tvAlertMessage);
tvPushMessage.setText(message);
Button btnPushOk = (Button) findViewById(R.id.btnAlertOk);
btnPushOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#drawable/background_round_rectangle"
android:padding="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/tvAlertMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />;
<Button
android:id="#+id/btnAlertOk"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="#id/tvAlertMessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#drawable/btn_use"
android:text="#string/ok"
android:textColor="#color/white" />
</RelativeLayout>`
Also tried:
requestWindowFeature(Window.FEATURE_NO_TITLE);
and:
getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert)
before setContentView()
Just Add Following code in style.xml
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
or You can hide it Programmatically.
getSupportActionBar().hide();
Try this code (without using "android:")
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
I used this:
In AppCompatActivity onCreate() add following code:
setTitle("")
and Add The following code in style.xml
<style name="PopupWindowTheme" parent="#style/Theme.AppCompat.Dialog.Alert">
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">#style/AnimBottom</item>
</style>
Add requestWindowFeature in this way may be work proper.
First Call Super after add it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.add__favourite_layout);
}
I was wondering if someone could help me out. I am trying to create a custom AlertDialog. In order to do this, I added the following line of code in styles.xml
<resources>
<style name="CustomAlertDialog" parent="android:Theme.Dialog.Alert">
<item name="android:windowBackground">#drawable/color_panel_background</item>
</style>
</resources>
color_panel_background.9.png is located in drawable folder. This is also available in Android SDK res folder.
The following is the main activity.
package com.customdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class CustomDialog extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTheme(R.style.CustomAlertDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("HELLO!");
builder .setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//dialog.cancel();
}
});
AlertDialog alertdialog = builder.create();
alertdialog.show();
}
}
In order to apply the theme to an AlertDialog, I had to set the theme to the current context.
However, I just can't seem to get the app to show customized AlertDialog. Can anyone help me out with this?
In Dialog.java (Android src) a ContextThemeWrapper is used. So you could copy the idea and do something like:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
And then style it like you want:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>
</resources>
I was having this AlertDialog theme related issue using sdk 1.6 as described here: http://markmail.org/message/mj5ut56irkrkc4nr
I solved the issue by doing the following:
new AlertDialog.Builder(
new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
I have written an article in my blog on how to configure the layout of an AlertDialog with XML style files. The main problem is that you need different style definitions for different layout parameters. Here is a boilerplate based on the AlertDialog style of Holo Light Platform version 19 for a style file that should cover a bunch of the standard layout aspects like text sizes and background colors.
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
...
<item name="android:alertDialogTheme">#style/MyAlertDialogTheme</item>
<item name="android:alertDialogStyle">#style/MyAlertDialogStyle</item>
...
</style>
<style name="MyBorderlessButton">
<!-- Set background drawable and text size of the buttons here -->
<item name="android:background">...</item>
<item name="android:textSize">...</item>
</style>
<style name="MyButtonBar">
<!-- Define a background for the button bar and a divider between the buttons here -->
<item name="android:divider">....</item>
<item name="android:dividerPadding">...</item>
<item name="android:showDividers">...</item>
<item name="android:background">...</item>
</style>
<style name="MyAlertDialogTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
</style>
<style name="MyAlertTextAppearance">
<!-- Set text size and color of title and message here -->
<item name="android:textSize"> ... </item>
<item name="android:textColor">...</item>
</style>
<style name="MyAlertDialogTheme">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowTitleStyle">#style/MyAlertDialogTitle</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowIsFloating">true</item>
<item name="android:textAppearanceMedium">#style/MyAlertTextAppearance</item>
<!-- If you don't want your own button bar style use
#android:style/Holo.Light.ButtonBar.AlertDialog
and
?android:attr/borderlessButtonStyle
instead of #style/MyButtonBar and #style/MyBorderlessButton -->
<item name="android:buttonBarStyle">#style/MyButtonBar</item>
<item name="android:buttonBarButtonStyle">#style/MyBorderlessButton</item>
</style>
<style name="MyAlertDialogStyle">
<!-- Define background colors of title, message, buttons, etc. here -->
<item name="android:fullDark">...</item>
<item name="android:topDark">...</item>
<item name="android:centerDark">...</item>
<item name="android:bottomDark">...</item>
<item name="android:fullBright">...</item>
<item name="android:topBright">...</item>
<item name="android:centerBright">...</item>
<item name="android:bottomBright">...</item>
<item name="android:bottomMedium">...</item>
<item name="android:centerMedium">...</item>
</style>
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#color/colorAccent</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#color/teal</item>
</style>
new AlertDialog.Builder(new ContextThemeWrapper(context,R.style.AlertDialogCustom))
.setMessage(Html.fromHtml(Msg))
.setPositiveButton(posBtn, okListener)
.setNegativeButton(negBtn, null)
.create()
.show();
You can directly assign a theme when you initiate the Builder:
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.MyAlertDialogTheme);
Then customize your theme in your values/styles.xml
<!-- Alert Dialog -->
<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorBackground">#color/alertDialogBackground</item>
<item name="android:windowBackground">#color/alertDialogBackground</item>
</style>
I was struggling with this - you can style the background of the dialog using android:alertDialogStyle="#style/AlertDialog" in your theme, but it ignores any text settings you have. As #rflexor said above it cannot be done with the SDK prior to Honeycomb (well you could use Reflection).
My solution, in a nutshell, was to style the background of the dialog using the above, then set a custom title and content view (using layouts that are the same as those in the SDK).
My wrapper:
import com.mypackage.R;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAlertDialogBuilder extends AlertDialog.Builder {
private final Context mContext;
private TextView mTitle;
private ImageView mIcon;
private TextView mMessage;
public CustomAlertDialogBuilder(Context context) {
super(context);
mContext = context;
View customTitle = View.inflate(mContext, R.layout.alert_dialog_title, null);
mTitle = (TextView) customTitle.findViewById(R.id.alertTitle);
mIcon = (ImageView) customTitle.findViewById(R.id.icon);
setCustomTitle(customTitle);
View customMessage = View.inflate(mContext, R.layout.alert_dialog_message, null);
mMessage = (TextView) customMessage.findViewById(R.id.message);
setView(customMessage);
}
#Override
public CustomAlertDialogBuilder setTitle(int textResId) {
mTitle.setText(textResId);
return this;
}
#Override
public CustomAlertDialogBuilder setTitle(CharSequence text) {
mTitle.setText(text);
return this;
}
#Override
public CustomAlertDialogBuilder setMessage(int textResId) {
mMessage.setText(textResId);
return this;
}
#Override
public CustomAlertDialogBuilder setMessage(CharSequence text) {
mMessage.setText(text);
return this;
}
#Override
public CustomAlertDialogBuilder setIcon(int drawableResId) {
mIcon.setImageResource(drawableResId);
return this;
}
#Override
public CustomAlertDialogBuilder setIcon(Drawable icon) {
mIcon.setImageDrawable(icon);
return this;
}
}
alert_dialog_title.xml (taken from the SDK)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/title_template"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="6dip"
android:layout_marginBottom="9dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="6dip"
android:paddingRight="10dip"
android:src="#drawable/ic_dialog_alert" />
<TextView android:id="#+id/alertTitle"
style="#style/?android:attr/textAppearanceLarge"
android:singleLine="true"
android:ellipsize="end"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView android:id="#+id/titleDivider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:scaleType="fitXY"
android:gravity="fill_horizontal"
android:src="#drawable/divider_horizontal_bright" />
</LinearLayout>
alert_dialog_message.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="12dip"
android:paddingLeft="14dip"
android:paddingRight="10dip">
<TextView android:id="#+id/message"
style="?android:attr/textAppearanceMedium"
android:textColor="#color/dark_grey"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip" />
</ScrollView>
Then just use CustomAlertDialogBuilder instead of AlertDialog.Builder to create your dialogs, and just call setTitle and setMessage as usual.
For Custom Dialog:
just call super(context,R.style.<dialog style>) instead of super(context) in dialog constructor
public class MyDialog extends Dialog
{
public MyDialog(Context context)
{
super(context, R.style.Theme_AppCompat_Light_Dialog_Alert)
}
}
For AlertDialog:
Just create alertDialog with this constructor:
new AlertDialog.Builder(
new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
I guess it cannot be done. At least not with the Builder. I'm working with 1.6 and the Implementation in Builder.create() is:
public AlertDialog create() {
final AlertDialog dialog = new AlertDialog(P.mContext);
P.apply(dialog.mAlert);
[...]
}
which calls the "not-theme-aware" constructor of AlertDialog, which looks like this:
protected AlertDialog(Context context) {
this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
There is a second constructor in AlertDialog for changing themes:
protected AlertDialog(Context context, int theme) {
super(context, theme);
[...]
}
that the Builder just doesn't call.
If the Dialog is pretty generic anyway, I'd try writing a subclass of AlertDialog, calling the second constructor and use that class instead of the Builder-mechanism.
Better way to do this use custom dialog and customize according your needs here is custom dialog example.....
public class CustomDialogUI {
Dialog dialog;
Vibrator vib;
RelativeLayout rl;
#SuppressWarnings("static-access")
public void dialog(final Context context, String title, String message,
final Runnable task) {
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
dialog.setCancelable(false);
TextView m = (TextView) dialog.findViewById(R.id.message);
TextView t = (TextView) dialog.findViewById(R.id.title);
final Button n = (Button) dialog.findViewById(R.id.button2);
final Button p = (Button) dialog.findViewById(R.id.next_button);
rl = (RelativeLayout) dialog.findViewById(R.id.rlmain);
t.setText(bold(title));
m.setText(message);
dialog.show();
n.setText(bold("Close"));
p.setText(bold("Ok"));
// color(context,rl);
vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
n.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
vib.vibrate(15);
dialog.dismiss();
}
});
p.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
vib.vibrate(20);
dialog.dismiss();
task.run();
}
});
}
//customize text style bold italic....
public SpannableString bold(String s) {
SpannableString spanString = new SpannableString(s);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0,
spanString.length(), 0);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
// spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0,
// spanString.length(), 0);
return spanString;
}
}
Here is xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
>
<RelativeLayout
android:id="#+id/rlmain"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#569CE3" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dip"
android:layout_marginTop="10dip" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Are you Sure?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="13dip" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/relativeLayout1"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_below="#+id/relativeLayout1"
android:layout_marginTop="5dip" >
</RelativeLayout>
<ProgressBar
android:id="#+id/process"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="3dip"
android:layout_marginTop="3dip" />
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/relativeLayout2"
android:layout_below="#+id/relativeLayout2"
android:layout_toLeftOf="#+id/process" >
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="13dip"/>
</RelativeLayout>
<Button
android:id="#+id/next_button"
android:layout_width="90dip"
android:layout_height="35dip"
android:layout_alignParentBottom="true"
android:textColor="#drawable/button_text_color"
android:background="#drawable/blue_button"
android:layout_marginBottom="5dp"
android:textSize="10dp"
android:layout_alignRight="#+id/relativeLayout3"
android:text="Okay" />
<Button
android:id="#+id/button2"
android:text="Cancel"
android:textColor="#drawable/button_text_color"
android:layout_width="90dip"
android:layout_height="35dip"
android:layout_marginBottom="5dp"
android:background="#drawable/blue_button"
android:layout_marginRight="7dp"
android:textSize="10dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/next_button"
/>
</RelativeLayout>
Anyone trying to do this within a Fragment (using the support library i.e. pre API 11) should go with this:
public class LoadingDialogFragment extends DialogFragment {
public static final String ID = "loadingDialog";
public static LoadingDialogFragment newInstance() {
LoadingDialogFragment f = new LoadingDialogFragment();
return f;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
StyleAlertDialog adb = new StyleAlertDialog(getActivity(), R.style.Your_Style);
adb.setView(getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_layout, null));
return adb;
}
private class StyleAlertDialog extends AlertDialog {
protected StyleAlertDialog(Context context, int theme) {
super(context, theme);
}
}
}
#Rflexor gave me the nudge to extend AlertDialog and expose the constructor thanks
Arve Waltin's solution looks good, although I haven't tested it yet. There is another solution in case you have trouble getting that to work.... Extend AlertDialog.Builder and override all the methods (eg. setText, setTitle, setView, etc) to not set the actual Dialog's text/title/view, but to create a new view within the Dialog's View do everything in there. Then you are free to style everything as you please.
To clarify, as far as the parent class is concerned, the View is set, and nothing else.
As far as your custom extended class is concerned, everything is done within that view.
I"m not sure how Arve's solution would work in a custom Dialog with builder where the view is inflated via a LayoutInflator.
The solution should be to insert the the ContextThemeWrapper in the inflator through cloneInContext():
View sensorView = LayoutInflater.from(context).cloneInContext(
new ContextThemeWrapper(context, R.style.AppTheme_DialogLight)
).inflate(R.layout.dialog_fingerprint, null);
You can override the default theme used by DialogFragments spawned by an activity by modifying the activity's theme's attributes....
set the activity's theme in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"> <!-- set all Activity themes to your custom theme -->
.....
</application>
</manifest>
in the values/styles.xml, override the item used to determine what theme to use for spawned DialogFragments
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
<!-- override the default theme for DialogFragments -->
<item name="android:dialogTheme">#style/AppTheme.Dialog</item>
</style>
.....
</resources>
in the values/styles.xml, define and configure the theme you want to use for DialogFragments
<?xml version="1.0" encoding="utf-8"?>
<resources>
.....
<!--
configure your custom theme for DialogFragments...
-->
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.MinWidth">
<!-- override the default theme for DialogFragments spawned by this DialogFragment -->
<item name="android:dialogTheme">#style/AppTheme.Dialog</item>
<!--
OPTIONAL: override the background for the dialog...i am using a dark theme,
and for some reason, there is no themes for dialogs with dark backgrounds,
so, i made my own.
-->
<item name="android:windowBackground">#drawable/dialog__window_background</item>
<!--
add the title to the dialog's theme. you can remove it later by using
DialogFragment.setStyle()
-->
<item name="android:windowNoTitle">false</item>
<item name="windowNoTitle">?android:windowNoTitle</item>
</style>
.....
</resources>
OPTIONAL: if you use a dark theme, and overrode android:windowBackground like i did in AppTheme.Dialog, then add a drawable/dialog__window_background.xml file with the contents:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="?dialogCornerRadius" />
<solid android:color="?android:colorBackground" />
</shape>
</inset>
It can done simply by using the Builder's setView(). You can create any view of your choice and feed into the builder. This works good. I use a custom TextView that is rendered by the dialog builder. I dont set the message and this space is utilized to render my custome textview.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Description");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();