Customizing a DialogFragment - android

The first image is what I want. The second is what I get
I've this class to create a dialog
import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;
public class AlertFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
// Set Dialog Icon
.setIcon(R.drawable.androidhappy)
// Set Dialog Title
.setTitle("Alert DialogFragment")
// Set Dialog Message
.setMessage("Alert DialogFragment Tutorial")
// Positive button
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
// Negative Button
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do something else
}
}).create();
}
}
In my view
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
.......
final FragmentManager fm = getSupportFragmentManager();
.......
AlertFragment alertdFragment = new AlertFragment();
// Show Alert DialogFragment
alertdFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
alertdFragment.show(fm, "Alert Dialog Fragment");
This is my style file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:spinnerItemStyle">
#style/spinnerItemStyle
</item>
<!-- For each individual Spinner list item once clicked on -->
<item name="android:spinnerDropDownItemStyle">
#style/spinnerDropDownItemStyle
</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="spinnerItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:padding">0dp</item>
<item name="android:textSize">#dimen/fld_txt_size</item>
</style>
<style name="spinnerDropDownItemStyle">
<item name="android:padding">0dp</item>
<item name="android:textSize">#dimen/fld_txt_size</item>
</style>
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="android:textColor">#color/btn_text</item>
</style>
<style name="MyDialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
<item name="android:headerDividersEnabled">true</item>
</style>
</resources>
I've posted all the style to see if there's anything going wrong. Regardless that I would like the separator between the header and the body of the dialog I do not understand why the buttons do not appear.
Edit:
I read this
Missing buttons on AlertDialog | Android 7.0 (Nexus 5x)
and I realized that i need to define a style for my dialog.
Infact if i set this
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>
<item name="android:buttonBarStyle">#style/Widget.AppCompat.ActionButton</item>
</style>
and then initialize dialog in this way
AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this,R.style.dialog_theme);
I can see change of color and text. But wich is property to show title, separator and buttons?

if you want a customized layout. You need to create an instance of Dialog. i.e. Dialog dialog = new Dialog; and set the contentview for that dialog: dialog.setContentView(R.layout.my_customized_dialog_layout);
you can then create buttons inside that layout and initialize them.

Related

Android app running on emulator not on real device

Android emulator is working when running the application but on a real device the app gets an error from the action bar. I am using android bumble bee and the device is a Samsung s21
I already have NoActionBar as well as windowNoTitle true and windowActionBar false
Here is my error
Process: com.example.wonderfinder, PID: 14427
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wonderfinder/com.example.wonderfinder.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4166)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4312)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2571)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8741)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:581)
at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:183)
at com.example.wonderfinder.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:8578)
at android.app.Activity.performCreate(Activity.java:8557)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4312) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2571) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:226) 
at android.os.Looper.loop(Looper.java:313) 
at android.app.ActivityThread.main(ActivityThread.java:8741) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 
2022-11-16 13:17:46.765 14427-14427/? I/Process: Sending signal. PID: 14427 SIG: 9
Here is my theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.WonderFinder" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryVariant">#color/primaryDarkColor</item>
<item name="android:colorPrimaryDark">#color/primaryColor</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/primaryColor</item>
<item name="colorSecondaryVariant">#color/primaryColor</item>
<item name="colorOnSecondary">#color/black</item>
<item name="android:textColorHint">#color/textColor</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="android:navigationBarColor">#color/primaryColor</item>
<!-- Customize your theme here. -->
</style>
<style name="ActionBarTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!-- Primary brand color. -->
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryVariant">#color/primaryDarkColor</item>
<item name="android:colorPrimaryDark">#color/primaryColor</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/primaryColor</item>
<item name="colorSecondaryVariant">#color/primaryColor</item>
<item name="colorOnSecondary">#color/black</item>
<item name="android:textColorHint">#color/textColor</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="android:navigationBarColor">#color/primaryColor</item>
<!-- Customize your theme here. -->
</style>
</resources>
Here is my main activity
package com.example.wonderfinder;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.wonderfinder.databinding.ActivityMainBinding;
import com.example.wonderfinder.databinding.NavDrawerLayoutBinding;
import com.example.wonderfinder.databinding.ToolbarLayoutBinding;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
//Declare all variables as private variables with their respective types
private NavDrawerLayoutBinding navDrawerLayoutBinding;
private ActivityMainBinding activityMainBinding;
private ToolbarLayoutBinding toolbarLayoutBinding;
private FirebaseAuth firebaseAuth;
private TextView txtName, txtEmail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
navDrawerLayoutBinding = NavDrawerLayoutBinding.inflate(getLayoutInflater());
setContentView(navDrawerLayoutBinding.getRoot());
//Set the variables to their respective values
activityMainBinding = navDrawerLayoutBinding.mainActivity;
toolbarLayoutBinding = activityMainBinding.toolbar;
setSupportActionBar(toolbarLayoutBinding.toolbar);
firebaseAuth = FirebaseAuth.getInstance();
//Toggle the drawer with open and close
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, navDrawerLayoutBinding.navDrawer, toolbarLayoutBinding.toolbar,
R.string.open_navigation_drawer,
R.string.close_navigation_drawer
);
navDrawerLayoutBinding.navDrawer.addDrawerListener(toggle);
toggle.syncState();
NavController navController = Navigation.findNavController(this, R.id.fragmentContainer);
NavigationUI.setupWithNavController(navDrawerLayoutBinding.navigationView, navController);
View headerLayout = navDrawerLayoutBinding.navigationView.getHeaderView(0);
txtName = headerLayout.findViewById(R.id.txtHeaderName);
txtEmail = headerLayout.findViewById(R.id.txtHeaderEmail);
//Call getUserData method
getUserData();
}
//Close the nav the drawer on back pressed
#Override
public void onBackPressed() {
if (navDrawerLayoutBinding.navDrawer.isDrawerOpen(GravityCompat.START))
navDrawerLayoutBinding.navDrawer.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
}
//Method to populate the users username and email in the nav drawer
private void getUserData() {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users")
.child(firebaseAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
UserModel userModel = snapshot.getValue(UserModel.class);
txtName.setText(userModel.getUsername());
txtEmail.setText(userModel.getEmail());
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
I'm going to take a wild stab in the dark here and assume that you are using dark mode (night mode) on your device, but normal (day mode) on the emulator. Am I right??
See some of the answers in this related question: This Activity already has an action bar supplied by the window decor?

How to Transparent in Custom Alert Dialog in android

I created the custom alert dialog for my project. When I click the button, the custom alert dialog is opened but a white background appears on my custom alert dialog. How can I make it transparent?
.java:
package com.example.bskes.customalertdialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button adbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adbtn = (Button) findViewById(R.id.btnCustomAD);
adbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.rtr_mem_profile_layout, null);
builder.setPositiveButton("Thank You", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "It's My Pleasure", Toast.LENGTH_SHORT).show();
}
});
builder.setView(view);
builder.show();
}
});
}
}
You should use this ,this will help to show transparent background of dilog
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
First line show the transparent layer on background and second line provide the actual size of the your window view.
it help me, use it
define what follows in the styles.xml file
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
and pass it as argument to the AlertDialog constructor
AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this, R.style.CustomDialog);
Or programmatically, through the Dialog instance you can call
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))
Add this to your styles.xml file
<style name="MyDialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">#android:color/transparent</item>
</style>
Add this to your java file :
Dialog dialog = new Dialog(this, R.style.MyDialog);

onClick event of button not working android

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();
}
});
}
}

Toolbar functional, but text/settings icon not visible in Fragment on Kit Kat

I've read over a fair few other threads regarding this issue, but have not been able to work out my problem. On Android 5.x my app works fine. But when testing on Android 4.4 Kit Kat I have an issue.
Problem:
- My toolbar/action bar is functional, and still works however the activity name text and the hamburger menu item does not display. I have attached images below to demonstrate the problem.
I have a main activity, with a nav bar and each item on the nav bar loads a new fragment. I am using the below setup for fragment definition.
public class <ClassName> extends android.support.v4.app.Fragment {}
I have attached my 2 x styles XML files & my main fragment file, but i'm not sure if that is where the problem lies. I have also tried removing all content in the layout on the affected screen but that hasn't helped either. This is my first time trying to get layout issues fixed across older version of Android so i'm a tad lost.
Do I need to create a new toolbar object?
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 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">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/whiteSystem</item>
<item name="tabTextColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
</style>
</resources>
v21/styles.xml
<resources>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/whiteSystem</item>
<item name="tabTextColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
</style>
</resources>
Fragment class which is having the issue
import android.app.Activity;
//import android.app.Fragment;
//import android.app.FragmentManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ParentFragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
private View myFragmentView; //this will enable 'findviewbyid' to be accessed
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.parentFragment, container, false);
viewPager = (ViewPager) myFragmentView.findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdaptor(getChildFragmentManager(), getActivity()));
tabLayout = (TabLayout) myFragmentView.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
return myFragmentView;
}
private class CustomAdaptor extends FragmentPagerAdapter {
private String fragments[] = {"1", "2", "3", "4"};
public CustomAdaptor(FragmentManager fragmentManager, Activity activity) {
super(fragmentManager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
case 3:
return new FragmentFour();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}

Fading Action Bar Shows Null PointerException in Android Activity

Am trying to add fading action bar in my android activity but it shows null pointer exception. below i added themes, styles, manifest and everything.
Here is my code
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.manuelpeinado.fadingactionbar.FadingActionBarHelper;
import com.xxx.xxx.R;
import com.xxx.xxx.cache.ImageLoader;
import com.xxx.xxx.header.Header;
public class List extends Activity
{
ListView listView;
private MyAdapter adapter;
Context context;
public static int click_position;
public static String name="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FadingActionBarHelper helper = new FadingActionBarHelper()
.actionBarBackground(R.drawable.ab_background)
.headerLayout(R.layout.header)
.contentLayout(R.layout.list);
setContentView(helper.createView(this));
helper.initActionBar(this);
manifest.xml
<activity
android:name=".List"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:icon="#drawable/icon"
android:label="List"
android:theme="#style/AppTheme.TranslucentActionBar" >
</activity>
styles.xml
<resources>
<style name="Widget" />
<style name="Widget.ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse" />
<style name="Widget.Light" />
<style name="Widget.Light.ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid" />
<style name="Widget.ActionBar.Transparent">
<item name="android:background">#android:color/transparent</item>
</style>
<style name="Widget.Light.ActionBar.Transparent">
<item name="android:background">#android:color/transparent</item>
</style>
</resources>
themes.xml
<resources>
<style name="AppTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/Widget.ActionBar</item>
</style>
<style name="AppTheme.TranslucentActionBar">
<item name="android:actionBarStyle">#style/Widget.ActionBar.Transparent</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="AppTheme.Light.TranslucentActionBar" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/Widget.Light.ActionBar.Transparent</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
</resources>
Here is my Log:
04-06 12:00:55.860: E/AndroidRuntime(23504): Caused by: java.lang.NullPointerException
04-06 12:00:55.860: E/AndroidRuntime(23504): at com.xxx.xxx.List.onCreate(List.java:35)
Your java code is not complete...there is no include of ActionBar.
So you need an actionBar declared with the methods :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Create menu display from the layout XML file menu_main.xml
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.menu , menu );
//Get actionbar as object
ActionBar actionbar = getSupportActionBar();
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//define action bar action here
return true;
}
Then if you are using android.support.v7.app.ActionBar, you need to change the include
import com.manuelpeinado.fadingactionbar.FadingActionBarHelper;
by
import com.manuelpeinado.fadingactionbar.extras.actionbarcompat.FadingActionBarHelper;

Categories

Resources