I want to create a pop up in my Android app as shown in the image in the above link. I am building a quiz app, in which I want to navigate between a set of questions. How can I get a pop up as shown?
You can create it with an AlertDialog, doing something like this:
First of all, create a layout where you put what you want to be shon on the AlertDialog. In this case, I'll show the first 5-numbers row (for example, this file layout/example.xml)
layout/example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/tv1"
android:layout_gravity="center_horizontal"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/tv2"
android:layout_gravity="center_horizontal"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/tv3"
android:layout_gravity="center_horizontal"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/tv4"
android:layout_gravity="center_horizontal"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/tv5"
android:textSize="30dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
Then invoque this with an AlertDialog, just like this:
In your activity
LayoutInflater factory = LayoutInflater.from(this);
final View myCustomView = factory.inflate(R.layout.example, null);
final TextView tv1 = (TextView) myCustomView.findViewById(R.id.tv1);
final TextView tv2 = (TextView) myCustomView.findViewById(R.id.tv2);
final TextView tv3 = (TextView) myCustomView.findViewById(R.id.tv3);
final TextView tv4 = (TextView) myCustomView.findViewById(R.id.tv4);
final TextView tv5 = (TextView) myCustomView.findViewById(R.id.tv5);
/* Set the listeners */
tv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
tv2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
tv3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
tv4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
tv5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Goto").setView(textEntryView); /*Remember to use #string */
alert.show();
Related
I am trying to create a dynamic view where I will keep a button to add view to my LinearLayout. The view is getting added successfully.
I have a Spinner, and a count field with (-) (+) buttons to increase decrease the value.
ISSUE: When I am increasing the value of the 2nd or 3rd row, the 1st row value is getting increased.
Ex: If we try to increase the number of pants, the Shirt count is getting increased. And the Pant count is unchanged(0).
View from simulator for better understanding:
field.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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Spinner
android:id="#+id/type_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/itemTypes"
android:gravity="right"/>
<Button
android:id="#+id/decrease"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/round_button"
android:onClick="decreaseInteger"
android:text="-" />
<TextView
android:id="#+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textStyle="bold"/>
<Button
android:id="#+id/increase"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/round_button"
android:onClick="increaseInteger"
android:text="+"/>
</LinearLayout>
</LinearLayout>
item_quantity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_quantity_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Spinner
android:id="#+id/type_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/itemTypes"
android:gravity="right"/>
<Button
android:id="#+id/decrease"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/round_button"
android:onClick="decreaseInteger"
android:text="-" />
<TextView
android:id="#+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textStyle="bold"/>
<Button
android:id="#+id/increase"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/round_button"
android:onClick="increaseInteger"
android:text="+"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add more item"/>
<Button
android:id="#+id/add_field_button"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/round_orange_button"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:onClick="onAddField"
android:textColor="#FFF"
android:text="+"/>
</LinearLayout>
</LinearLayout>
JavaCode
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.laundry_srf_blank);
parentLinearLayout = (LinearLayout) findViewById(R.id.item_quantity_view);
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.item, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
public void increaseInteger(View view) {
minteger = minteger + 1;
display(minteger);
}
public void decreaseInteger(View view) {
minteger = minteger - 1;
display(minteger);
}
private void display(int number) {
TextView displayInteger = (TextView) findViewById(
R.id.integer_number);
displayInteger.setText("" + number);
}
Please help!!!
Your problem is that you looking for R.id.integer_number in the root layout in private void display(int number) method and it always returns the first view what can find (in your case a Shirt field). So you need to look for R.id.integer_number locally in the current ViewGroup where was onClick() method invoked.
Refactor your code like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.laundry_srf_blank);
parentLinearLayout = (LinearLayout) findViewById(R.id.item_quantity_view);
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.item, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
public void increaseInteger(View view) {
TextView displayInteger = (TextView) view.findViewById(
R.id.integer_number);
Integer minteger = Integer.valueOf(displayInteger.getText().toString());
minteger++;
displayInteger.setText(String.valueOf(minteger));
}
public void decreaseInteger(View view) {
TextView displayInteger = (TextView) view.findViewById(
R.id.integer_number);
Integer minteger = Integer.valueOf(displayInteger.getText().toString());
minteger--;
displayInteger.setText(String.valueOf(minteger));
}
I want to ask how to add a textview and an edittext once I click a button? My button to add a textview and edittext is the "Button addStaff"
Here is my code:
_12_EventAssign.java
public class _12_EventAssign extends AppCompatActivity {
LinearLayout linearL;
TextView tvdept1;
EditText etdept1;
TextView tvheadofdept1;
EditText etheadofdept1;
View lineview;
Button addStaff;
LinearLayout staff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__12__event_assign);
getSupportActionBar().setTitle("Add Event: Information");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
linearL = (LinearLayout)findViewById(R.id.ev_assign_linearlay);
tvdept1 = (TextView)findViewById(R.id.textv_dept1);
etdept1 = (EditText)findViewById(R.id.editT_dept1);
tvheadofdept1 = (TextView)findViewById(R.id.textv_headofdept1);
etheadofdept1 = (EditText)findViewById(R.id.editT_headofdept1);
lineview = findViewById(R.id.view1);
// staff = (LinearLayout)findViewById(R.id.add_staff);
addStaff = (Button)findViewById(R.id.btnaddstaff);
addStaff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearL.addView(tvdept1);
}
});
}
}
Here is my xml file
activity__12__event_assign.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ortegapatriciaa.enventer._12_EventAssign"
android:background="#color/colorWhite"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/ev_assign_linearlay"
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="vertical"
android:id="#+id/add_staff">
<TextView
android:id="#+id/textv_dept1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:text="Department"
android:textColor="#color/colorBlack"
android:textSize="23dp" />
<EditText
android:id="#+id/editT_dept1"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="7dp"
android:background="#color/colorGray"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="#+id/textv_headofdept1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Head of the Department"
android:textColor="#color/colorBlack"
android:textSize="23dp" />
<EditText
android:id="#+id/editT_headofdept1"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="7dp"
android:background="#color/colorGray"
android:ems="10"
android:inputType="textPersonName" />
<View
android:id="#+id/view1"
android:layout_width="330dp"
android:layout_height="1dip"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:background="#color/colorBlack" />
</LinearLayout>
<Button
android:id="#+id/btnaddstaff"
android:layout_width="130dp"
android:layout_height="45dp"
android:text="Add Staff"
android:textStyle="bold"
android:textAllCaps="false"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:height="60dp"
android:background="#color/colorWhite"
android:drawablePadding="6dp"
android:drawableLeft="#drawable/ic_person_add_black_24dp"
android:gravity="left|center"
android:padding="6dp"
android:textColor="#000"
android:textSize="18dp"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/btnsave"
android:layout_width="250dp"
android:layout_height="40dp"
android:text="Save"
android:textAllCaps="false"
android:textSize="22dp"
android:background="#color/buttonColor"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</ScrollView>
I hope you could help me. Thank you!
This Works Perfectly...
You can change lparams according to your need
LinearLayout linearLayout =(LinearLayout)findViewById(R.id.yourLinearLayoutID);
Button yourButton = (Button)findViewById(R.id.yourButtonID);
yourButtonID.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutParams layoutparam = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView textview=new TextView(this);
textview.setLayoutParams(lparams);
textview.setText("Your Text Here"); //For TextView
this.linearLayout.addView(textView);
EditText editText=new EditText(this);
editText.setLayoutParams(lparams);
editText.setText("Your Text Here"); //For EditText
this.linearLayout.addView(editText);
}
});
You can not add existing view to layout. You must create new TextView and add that:
addStaff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(getApplicationContext());
tv.setBackgroundColor(Color.GRAY);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
linearL.addView(tv, params);
}
});
Try this way
addStaff = (Button)findViewById(R.id.btnaddstaff);
addStaff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(this)
tv.setText("yourtext")
EditText et = new Edittext(this)
et.setText("yourtext")
linearL.addView(tv);
linearL.addView(et);
}
});
I made a stopwatch using chronometer with four buttons but when i use the visibility modes to make stop and pause button appear they overlap.... Pls explain why...Below is the code.....
Assume the layout file with buttons in relative layout...
public class StopWatchFragment extends Fragment {
Chronometer chronometer;
Button startStopWatch;
Button stopStopWatch;
Button resetStopWatch;
Button pauseStopWatch;
Button resumeStopWatch;
private long lastPause;
//RelativeLayout relativeLayout;
private int check = 0;
public StopWatchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false);
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch);
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch);
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch);
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch);
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch);
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout);
pauseStopWatch.setVisibility(View.GONE);
//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams());
startStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startStopWatch.setVisibility(View.GONE);
pauseStopWatch.setVisibility(View.VISIBLE);
if(check == 1){
resumeStopWatch.setClickable(true);
}
else{
resumeStopWatch.setClickable(false);
}
//params.setMargins(16,16,16,16);
//pauseStopWatch.setLayoutParams(params);
}
});
pauseStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
check = 1;
lastPause = SystemClock.elapsedRealtime();
chronometer.stop();
}
});
resumeStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
chronometer.start();
resumeStopWatch.setClickable(false);
}
});
stopStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
resumeStopWatch.setClickable(false);
}
});
resetStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
resumeStopWatch.setClickable(false);
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
}
});
return rootView;
}
}
This is the layout file pls refer for this....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the way buttons work when click on start button
You are hiding the button, but you are still listening with the OnClickListener. Try adding:
pauseStopWatch.setOnClickListener(null);
Then, when you make your button visible:
pauseStopWatch.setOnClickListener(whatever);
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:visibility="gone"
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</LinearLayout>
</LinearLayout>
In the above, the pause button will be set to Gone. You make it visible later when user clicks start button
Popup Library
I have to display my own header in popup and it
here + sign and back arrow is header i want to exactly like this
you can use Costume Dialog like below
public class dialogMesssageType {
Dialog dialog;
Context m_context;
Activity act;
public void showDialog(Activity activity, Context context, final String titre, final String message, final int type) {
dialog = new Dialog(activity);
m_context = context;
act = activity;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog_message_type);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(android.R.color.transparent)));
TextView txt = (TextView) dialog.findViewById(R.id.txt);
TextView titre_id = (TextView) dialog.findViewById(R.id.txt_titre);
TextView ok = (TextView) dialog.findViewById(R.id.confirmation);
ImageView img_type(ImageView)dialog.findViewById(R.id.img_type);
ImageView close = (ImageView) dialog.findViewById(R.id.close);
txt.setText(message);
titre_id.setText(titre);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#drawable/rounded_gray"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/blue_neigra"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="32dp"
android:id="#+id/img_type"
/>
<TextView
android:id="#+id/txt_titre"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:gravity="center"
android:text="Warning"
android:textColor="#039470"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="18dp"
android:gravity="center"
android:text="Please fill in all fields"
android:textColor="#color/white"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:background="#color/blue_neigra"></View>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:id="#+id/confirmation"
android:textColor="#color/blue_neigra"
android:textSize="20sp"
android:text="Ok"/>
</LinearLayout>
<ImageView
android:id="#+id/close"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:background="#drawable/selector_blue_gray_cercle"
android:padding="12dp"
android:src="#drawable/close_blue" />
</RelativeLayout>
i removed Animation .
I have a Relative layout as:
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/i_pay"
android:textAllCaps="false"
android:textColor="#ffffff" />
</RelativeLayout>
I want my relative layout to perform onCLickListener but does not work.
I am able to do the setonclicklistener but it works only with the textview part and not for the image.
btniPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//for ipay
}
});
This works for the text part only but not for the image. Any idea what I might be missing.
Alternatively, you can add android:clickable="false" in your button xml attribute.
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/i_pay"
android:textAllCaps="false"
android:clickable="false"
android:textColor="#ffffff" />
</RelativeLayout>
may be you can find some good example thats use with imageview and buttons but the easy way i did it by using adding TextView instead of images and buttons.
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:clickable="true"
android:gravity="right">
<TextView
android:id="#+id/englishheading1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/urduheading1"
android:text="English text"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/urduheading1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:text="urdu text1"
android:textColor="#ffffff"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:background="#ffffff" />
</RelativeLayout>
Here is java code to perform onClick.
RelativeLayout layout1 = (RelativeLayout) view.findViewById(R.id.layout1);
layout1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});
see my code easily run change your code:
.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:id="#+id/btn"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher"
android:textAllCaps="false"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
.java file
public class MainActivity extends Activity {
TextView tv;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
}
});
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_SHORT).show();
}
});
}
}
Just mark android:clickable="true" in your RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_id"
android:clickable="true">
Using ButterKnife lib make bind here:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml_name);
ButterKnife.bind(this);
}
#OnClick(R.id.activity_id)
void onActivityClick() {
// do smth ...
}