onClick event of button not working android - 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();
}
});
}
}

Related

DatePicker Using Material Design dont show

I am trying to create a button that when clicked opens a calendar using material design with android studio
but clicking it closes the app.
I implement the librery in the gradle
implementation 'com.google.android.material:material:1.3.0-alpha01'
This my code Java
package com.niccode.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.material.datepicker.MaterialDatePicker;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mdatePicker = findViewById(R.id.button);
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
builder.setTitleText("Select");
final MaterialDatePicker materialDatePicker = builder.build();
mdatePicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
}
});
}
}
This is xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
just change your appTheme to materialTheme in styles.xml:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
Try below style
<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Add these -->
<item name="materialCalendarStyle">#style/Widget.MaterialComponents.MaterialCalendar</item>
<item name="materialCalendarFullscreenTheme">#style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen</item>
<item name="materialCalendarTheme">#style/ThemeOverlay.MaterialComponents.MaterialCalendar</item>
</style>

Could not instantiate android.support.design.widget.TextInputLayout

I'm trying to use android.support.design.widget.TextInputLayout but i get this exception
java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
at android.support.design.widget.TextInputLayout.(TextInputLayout.java:193)
at android.support.design.widget.TextInputLayout.(TextInputLayout.java:186)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:465)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:172)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$70(RenderTask.java:659)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
My gradle module file contains:
compile 'com.android.support:design:26.0.1'
compile 'com.android.support:appcompat-v7:26.0.1'
activity-main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.eltobgy.yala.MainActivity">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="TextAppearence.App.TextInputLayout" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/colorAccent</item>
</style>
<style name="label_text">
<item name="android:textSize">19sp</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="text">
<item name="android:textSize">14sp</item>
<item name="android:textColor">#color/gray</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="line">
<item name="android:layout_marginBottom">8dp</item>
<item name="android:layout_marginTop">8dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">#color/colorAccent</item>
<item name="android:shadowColor">#color/colorAccent</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">0</item>
<item name="android:shadowRadius">8</item>
</style>
<!--FIREBASE UI-->
<style name="LoginTheme" parent="FirebaseUI">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorButtonNormal">#color/colorAccent</item>
<item name="colorControlNormal">#android:color/white</item>
<item name="colorControlActivated">#android:color/white</item>
<item name="colorControlHighlight">#android:color/white</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorSecondary">#android:color/white</item>
<item name="android:colorBackground">#color/colorPrimary</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:textColorHint">#android:color/white</item>
</style>
<style name="FirebaseUI.Text">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textColor">#android:color/white</item>
</style>
<style name="FirebaseUI.Text.Link">
<item name="android:textColor">#color/colorSecondary</item>
</style>
<color name="colorSecondary">#FFC300</color>
<color name="gray">#FFF0</color>
</resources>
MainActivity.java
package com.example.eltobgy.yala;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.auth.AuthUI;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.FirebaseUserMetadata;
import com.google.firebase.database.ChildEventListener;
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 java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String ANONYMOUS = "Anonymous";
// Choose an arbitrary request code value
private static final int RC_SIGN_IN = 1;
Boolean flag_registered = false;
// Firebase
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
private ChildEventListener mChildEventListener;
private String mUsername;
private DatabaseReference mDatabaseReference;
private FirebaseDatabase mDatabase;
private User currentUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
mUsername = ANONYMOUS;
mFirebaseAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
currentUser = null;
mDatabaseReference = mDatabase.getReference().child("users");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
// User is signed in
FirebaseUserMetadata metadata = firebaseAuth.getCurrentUser().getMetadata();
onSignedInInitialize(firebaseUser.getDisplayName());
Toast.makeText(MainActivity.this, "You're now signed in. Welcome " + firebaseUser.getDisplayName() + "!", Toast.LENGTH_SHORT).show();
DatabaseReference userDatabaseRefrence = mDatabaseReference.child(firebaseUser.getUid());
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
currentUser = dataSnapshot.getValue(User.class);
if(currentUser.getCurrentType().equals("c")){
//customer
if(currentUser.isCustomerModeActivation()){
//already filled details
//TODO go to next activity
}else{
//didn't fill details
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MainActivity.this, "cancelled", Toast.LENGTH_SHORT).show();
}
};
userDatabaseRefrence.addValueEventListener(valueEventListener);
} else {
// User is signed out
onSignedOutCleanup();
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build());
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setAvailableProviders(providers)
.setTheme(R.style.LoginTheme)
//.setLogo(R.drawable.ic_melomania_blue_light)
.build(),
RC_SIGN_IN);
//RC request code, it's a flag for when we return rom starting the activity for the results.
}
}
};
}
And Invalidate caches/Restart & Rebuilding the project didn't work.
try changing app gradle to
compileSdkVersion 27
targetSdkVersion 27
compile 'com.android.support:design:27.1.0'
implementation 'com.android.support:appcompat-v7:27.1.0'

Customizing a DialogFragment

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.

Define TabLayout style in theme

I have two different styles of TabLayout in my app:
<style name="TabLayoutPower" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="TabLayoutFree" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/black</item>
<item name="tabTextColor">#android:color/black</item>
</style>
How can I define the default TabLayout style for a theme? I cannot find any info which item name should I use to build my theme. I'd like to add the TabLayout the same way I added my listview:
<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/black</item>
<item name="colorPrimaryDark">#color/black</item>
<item name="colorAccent">#color/main_red</item>
<item name="android:windowBackground">#color/main_bg_light</item>
<item name="android:listViewStyle">#style/MyListView</item>
</style>
For Support Library TabLayout, you can set tabStyle attribute in your theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="tabStyle">#style/AppTheme.TabLayout</item>
</style>
<style name="AppTheme.TabLayout" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
If you wan to use 2 different styles for your TabLayout based on theme of your application then you should define your style inside attrs.xml
here is the Sample code for that
First you create an attrs.xml file ( Like this )
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="textColor" format="reference|color" />
<attr name="backColor" format="reference|color" />
<attr name="myTabStyle" format="reference" />
</resources>
define various theme as per your requirements inside styles.xml
Note: i have used 3 different themes for demo
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="tabStyle">#style/TabLayoutPower</item>
<item name="textColor">#android:color/white</item>
<item name="backColor">#color/colorPrimary</item>
</style>
<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#android:color/holo_green_dark</item>
<item name="colorPrimaryDark">#android:color/white</item>
<item name="colorAccent">#android:color/holo_blue_dark</item>
<item name="textColor">#android:color/white</item>
<item name="tabStyle">#style/TabLayoutFree</item>
<item name="backColor">#FF00</item>
</style>
<style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#ff00</item>
<item name="colorPrimaryDark">#ff0</item>
<item name="colorAccent">#0a91d4</item>
<item name="tabStyle">#style/TabLayoutNew</item>
<item name="textColor">#FF00</item>
<item name="backColor">#android:color/white</item>
</style>
<style name="TabLayoutPower" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="TabLayoutFree" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/holo_blue_bright</item>
<item name="tabTextColor">#android:color/holo_blue_bright</item>
</style>
<style name="TabLayoutNew" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/holo_green_dark</item>
<item name="tabTextColor">#android:color/holo_green_dark</item>
</style>
</resources>
Now use this style in your TabLayout like this inside layout.xml files
Use like this style="?myTabStyle" it will select style based on current theme of your application
<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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnThemeOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme One" />
<Button
android:id="#+id/btnThemeTwo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme Two" />
<Button
android:id="#+id/btnThemeThree"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme Three" />
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="?myTabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:background="#color/colorAccent"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
sample code of TabsActivity to change theme
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class TabsActivity extends AppCompatActivity {
PrefManager prefManager;
private TabLayout tabLayout;
private ViewPager viewPager;
private Button btnThemeOne, btnThemeTwo, btnThemeThree;
#Override
protected void onCreate(Bundle savedInstanceState) {
prefManager = new PrefManager(this);
getTheme().applyStyle(prefManager.getTheme(), true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
btnThemeOne = findViewById(R.id.btnThemeOne);
btnThemeOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme one", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme);
recreate();
}
});
btnThemeTwo = findViewById(R.id.btnThemeTwo);
btnThemeTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme two", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme2);
recreate();
}
});
btnThemeThree = findViewById(R.id.btnThemeThree);
btnThemeThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme three", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme3);
recreate();
}
});
viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new OneFragment(), "TWO");
adapter.addFragment(new OneFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
PrefManager class to save theme info
import android.content.Context;
import android.content.SharedPreferences;
public class PrefManager {
// Shared preferences file name
private final String PREF_NAME = "theme-pref";
private final String THEME = "theme";
SharedPreferences pref;
SharedPreferences.Editor editor;
public PrefManager(Context context) {
pref = context.getSharedPreferences(PREF_NAME, 0);
editor = pref.edit();
}
public int getTheme() {
return pref.getInt(THEME, R.style.AppTheme);
}
public void setTheme(int themeId) {
editor.putInt(THEME, themeId);
editor.commit();
}
}
you can check the output video of above code
https://www.youtube.com/watch?v=uup072IDGd0
You can find that name in Android theme -
<item name="android:tabWidgetStyle">#style/Your.Style</item>
<style name="Base.Widget.Design.TabLayout" parent="">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/black</item>
<item name="colorPrimaryDark">#color/black</item>
<item name="colorAccent">#color/main_red</item>
<item name="android:windowBackground">#color/main_bg_light</item>
<item name="android:listViewStyle">#style/MyListView</item>
<item name="android:tabWidgetStyle">#style/Base.Widget.Design.TabLayout</item>
</style>

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