cannot catch a click event on custom android view - java

I have this xml:
<com.me.view.button.SwipeableButton
android:id="#+id/mainMenuSwipeableButton"
android:layout_width="250dp"
android:layout_height="62dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
app:button="#drawable/main_menu_button"
android:visibility="visible" />
and:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/icon_menu_press" /> <!-- pressed -->
<item android:drawable="#drawable/icon_menu" /> <!-- default -->
</selector>
why isn't my click caught when i have this code?
setCloseToolTipClickListener(getMainLayout().findViewById(R.id.mainMenuSwipeableButton), tooltipView);
private void setCloseToolTipClickListener(View view, final LinearLayout tooltip) {
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tooltip.setVisibility(View.GONE);
}
});
}

Related

Android custom dialog button styles not applied

I wish to create a simple dialog for the user with 2 buttons as follows:
Dialog Layout (dialog_layout.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="match_parent"
android:layout_margin="8dp"
android:orientation="vertical">
<Button
android:id="#+id/btn_select_choice_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Choice"
android:theme="#style/secondary_button_normal" />
<Button
android:id="#+id/btn_select_choice_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Choice"
android:theme="#style/secondary_button_normal" />
</LinearLayout>
secondary_button_normal:
<style name="secondary_button_normal" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/button_secondary_normal_background</item>
<item name="android:textColor">#color/button_secondary_normal_text</item>
<item name="android:textSize">#dimen/button_textSize</item>
<item name="android:padding">#dimen/button_padding</item>
</style>
Activity's onCreate:
final Dialog selection = new Dialog(this);
selection.setContentView(R.layout.dialog_layout);
Button selectFirstChoice = (Button)selection.findViewById(R.id.btn_select_choice_1);
Button selectSecondChoice = (Button)selection.findViewById(R.id.btn_select_choice_2);
selectFirstChoice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
selection.dismiss();
}
});
selectSecondChoice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
selection.dismiss();
}
});
selection.setTitle("Some Title");
selection.setCancelable(false);
selection.show();
The preview is alright:
Preview
It works well on Nougat, but when I run it on Lollipop (5.0 and 5.1.1), the buttons are without styling, although the same button styling worked on activity buttons on Lollipop:
App
I wonder what could be going wrong, I also tried moving the Dialog into a DialogFragment but I faced the same behavior.
Found the following solution:
In my Styles.xml, I added:
<style name="dialog_button" parent="Theme.AppCompat.Light.Dialog">
<item name="colorButtonNormal">#color/button_secondary_normal_background</item>
<item name="android:textColor">#color/button_secondary_normal_text</item>
<item name="android:textSize">#dimen/button_textSize</item>
<item name="android:padding">#dimen/button_padding</item>
</style>
And in my custom dialog layout, I used that style as a theme for my buttons:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_margin="8dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_select_student"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/student"
android:theme="#style/dialog_button" />
<Button
android:id="#+id/btn_select_tutor"
android:layout_width="match_parent"
android:text="#string/tutor"
android:layout_height="wrap_content"
android:theme="#style/dialog_button" />
</LinearLayout>

How to make a custom ProgressDialog Android? (Not cleaned dimensions of a standard ProgressDialog)

Help me create a custom ProgressDialog. I did so:
custom_progressdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/progressdialog"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Загрузка...
"
android:textStyle="bold"
android:textColor="#android:color/darker_gray"
android:layout_marginTop="8dp"/>
</LinearLayout>
progressdialog.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#d4ffffff" />
<corners android:radius="10dp" />
<padding android:left="15dp" android:top="15dp" android:right="15dp" android:bottom="15dp"/>
</shape>
And my Java file:
pd = new ProgressDialog(getActivity());
pd.show();
pd.setContentView(R.layout.custom_progressdialog);
pd.setCancelable(false);
I get this:
Why is this happening? What adjustments to make?
I have get:
Here is my class for creating custom progress dialog
package yourpackage.abc;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomProgress extends Dialog implements OnDismissListener {
public CustomProgress(Context context) {
super(context);
}
public CustomProgress(Context context, int theme) {
super(context, theme);
}
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
AnimationDrawable spinner = (AnimationDrawable) imageView
.getBackground();
spinner.start();
}
}
public void setMessage(CharSequence message) {
if (message != null && message.length() > 0) {
findViewById(R.id.message).setVisibility(View.VISIBLE);
TextView txt = (TextView) findViewById(R.id.message);
txt.setText(message);
txt.invalidate();
}
}
public static CustomProgress show(Context context, String message,
boolean indeterminate, boolean cancelable) {
CustomProgress dialog = new CustomProgress(context, R.style.ProgressHUD);
dialog.setTitle("");
dialog.setContentView(R.layout.anim_hud);
if (message == null || message.length() == 0) {
dialog.findViewById(R.id.message).setVisibility(View.GONE);
} else {
TextView txt = (TextView) dialog.findViewById(R.id.message);
txt.setText(message);
}
dialog.setCancelable(cancelable);
// dialog.setOnCancelListener(cancelListener);
dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.2f;
dialog.getWindow().setAttributes(lp);
// dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
try {
dialog.show();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return dialog;
}
#Override
public void onDismiss(DialogInterface arg0) {
System.out.println("dismiss is called");
}
}
here is the anim_hud.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="20dp" >
<ImageView
android:id="#+id/spinnerImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#anim/spinner_anim" />
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Message"
android:textColor="#FFFFFF" />
</LinearLayout>
copy this in your style.xml ProgressHUD
<style name="ProgressHUD" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
paste this spinner_anim.xml into your anim folder
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/spinner_0"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_1"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_2"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_3"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_4"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_5"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_6"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_7"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_8"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_9"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_10"
android:duration="60"/>
<item
android:drawable="#drawable/spinner_11"
android:duration="60"/>
</animation-list>
pd = new ProgressDialog(getActivity());
pd.show();
pd.getWindow().setBackgroundDrawableResource(R.color.transparent);
pd.setContentView(R.layout.custom_progressdialog);
pd.setCancelable(false);

Button in dialog not behaving as a button when clicked

After create and testing my custom dialog I've noticed that my button does not show any visual sign of change (such as the ripple effect or highlighting) when clicked. Does anyone know what I'm doing wrong and how to resolve this issue?
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<Button
android:id="#+id/world"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:background="#color/green"
android:textColor="#color/white"
android:text="#string/world"
android:textAllCaps="false"
style="#android:style/TextAppearance.Medium"/>
</LinearLayout>
Java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_other_lines) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_hello_world);
dialog.setTitle("Dialog");
Button world = (Button) dialog.findViewById(R.id.world);
world.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
return super.onOptionsItemSelected(item);
}
You override the default onClick effect by putting a background color.
android:background="#color/green"
You can do it by creating a custom background xml file on drawable, like this.
custom_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/Pressed_Color" />
<item android:state_activated="false" android:drawable="#color/green"/>
</selector>
then you call it on the button styling as like this:
android:background="#drawable/custom_background"
when you change your default appearance, you should handle different state if you want like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_sel" android:state_selected="true" />
<item android:drawable="#drawable/button_sel" android:state_pressed="true" />
<item android:drawable="#drawable/button_unsel" />
</selector>
save this as xml drawable and add as android:background

I use TransitionDrawable to change background color but it occure error - NullPointerException

I use following java code to run this,
public class MainActivity extends ActionBarActivity {
int transitionTime = 300;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TransitionDrawable transition = (TransitionDrawable) findViewById(R.id.relativeLayoutMain).getBackground();
transition.startTransition(transitionTime);
}
Please help me to sole this problem.
Hi I'm new to android and what I'm try to do is change layout background color continuously.
Above method trigger error to me, I not found solution to this but I use different approach to do this.
I use timer with TransitionDrawable and here is my working copy of code.
Here I add "drawable" resources,
transition_drawable.xml (this is main resource xml set this resource file to your layer backbround)
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
<item android:drawable="#drawable/color_selector_01" />
<item android:drawable="#drawable/color_selector_02" />
</transition>
color_selector_01.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#919bfa"
android:endColor="#4e5bda"
android:type="linear" />
</shape>
</item>
</selector>
color_selector_02.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#fe5528"
android:endColor="#3c1ed4"
android:type="linear" />
</shape>
</item>
</selector>
In my main layout I set layer background to "transition_drawable"
here is my "activity_main.xml"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#drawable/transition_drawable" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/linkTerms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:singleLine="true"
android:text="#string/linkTerms" />
<TextView
android:id="#+id/and"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#string/and" />
<TextView
android:id="#+id/linkPrivacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="#string/linkPrivacy" />
</LinearLayout>
</ScrollView>
Finlay here is my MainActivity.java file
#TargetApi(Build.VERSION_CODES.HONEYCOMB) #SuppressLint("NewApi") public class MainActivity extends ActionBarActivity {
int transitionTime = 6000;
private RelativeLayout Rlayout;
private TransitionDrawable trans;
final Handler handler_interact=new Handler();//not defined as final variable. may cause problem
View layout_interact;
#SuppressLint("NewApi") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout_interact =(View) findViewById(R.id.relativeLayoutMain);
Rlayout = (RelativeLayout)findViewById(R.id.relativeLayoutMain);
Resources res = this.getResources();
trans = (TransitionDrawable)res.getDrawable(R.drawable.transition_drawable);
Rlayout.setBackgroundDrawable(trans);
BackgroundColoerChangerCallTimer();
}
public void BackgroundColoerChangerCallTimer(){
//creating timer
Timer timer_interact=new Timer();
timer_interact.schedule(new TimerTask() {
#Override
public void run() {
UpdateGUI();
}
}, 3000);
}
private void UpdateGUI() {
handler_interact.post(runnable_interact);
}
//creating runnable
final Runnable runnable_interact = new Runnable() {
public void run() {
//ayout_interact.setBackgroundColor(Color.GREEN);
trans.reverseTransition(transitionTime);
BackgroundColoerChangerCallTimer();
}
};

Button topmost in android

I have layer, it is transparent. I can click on button only, I want to click through layer.
Here's my Manifest:
<activity android:name=".Test" android:theme="#style/Theme.Transparent"/>
Here's Transparent theme style:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Here's my Activity code:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// v I can touch through app, but cant click on button
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
// ^ I can touch through app, but cant click on button
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
}
Here's my test.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" >
<Button android:id="#+id/buttonx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="X" />
</RelativeLayout>
Try this code and set click listener on the linear 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" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll">
<Button android:id="#+id/buttonx" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:text="X" />
</LinearLayout>
</RelativeLayout>
You already do the following here to set the click listener for the X button.
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
Now all you need to do is the same thing but for the Layout which you have created that is transparent. So that when you click the layout in the area that your play button is, it listens to the click, rather than the button, as the button is out of reach.

Categories