I have tried to set an OnClickListener to my imageButton but it didn't work. I've seen other questions about the topic but none of the ansqers work for me. Am I doing something wrong?
MainActivity.java
public void Button()
{
View.OnClickListener listSet = new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
}
};
boton= (ImageButton) findViewById(R.id.imageButton7);
boton.setOnClickListener(listSet);
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.xxxx.xxx.MainActivity"
android:orientation="vertical"
android:weightSum="1"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
>
<WebView
android:id="#+id/webV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
</WebView>
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#android:color/background_light"
android:src="#drawable/ic_gear"
tools:background="#android:color/background_light"
tools:ignore="ContentDescription" />
</RelativeLayout>
Set clickable property of ImageButton to true
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:background="#android:color/background_light"
android:src="#drawable/ic_gear"
tools:background="#android:color/background_light"
tools:ignore="ContentDescription" />
Try this :
ImageButton ib = (ImageButton) ad.findViewById(R.id.imageButton7);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_SHORT).show();
}
});
try:
boton= (ImageButton) findViewById(R.id.imageButton7);
boton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(this, "settings", Toast.LENGTH_LONG).show();
}
};);
Related
I'm a beginner in android app making and I'm trying to do an app for a project. I found this tutorial and I'm currently trying to put to apps together in android studio. Both are reminders apps, however the second one (the food one), the FAB is not working it register the touch but when it does it says APP keeps stopping. If anybody can help me I'll appreciated.
First Reminder .java
public class MedicineActivity extends AppCompatActivity {
#BindView(R.id.compactcalendar_view)
CompactCalendarView mCompactCalendarView;
#BindView(R.id.date_picker_text_view)
TextView datePickerTextView;
#BindView(R.id.date_picker_button)
RelativeLayout datePickerButton;
#BindView(R.id.toolbar)
Toolbar toolbar;
#BindView(R.id.collapsingToolbarLayout)
CollapsingToolbarLayout collapsingToolbarLayout;
#BindView(R.id.app_bar_layout)
AppBarLayout appBarLayout;
#BindView(R.id.contentFrame)
FrameLayout contentFrame;
#BindView(R.id.fab_add_task)
FloatingActionButton fabAddTask;
#BindView(R.id.coordinatorLayout)
CoordinatorLayout coordinatorLayout;
#BindView(R.id.date_picker_arrow)
ImageView arrow;
private MedicinePresenter presenter;
private SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd", /*Locale.getDefault()*/Locale.ENGLISH);
private boolean isExpanded = false;
public ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medicine);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
imageButton = (ImageButton) findViewById(R.id.image2button);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent= new Intent(MedicineActivity.this,dashboard_screen.class);
startActivity(intent);
}
});
mCompactCalendarView.setLocale(TimeZone.getDefault(), /*Locale.getDefault()*/Locale.ENGLISH);
mCompactCalendarView.setShouldDrawDaysHeader(true);
mCompactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
#Override
public void onDayClick(Date dateClicked) {
setSubtitle(dateFormat.format(dateClicked));
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateClicked);
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (isExpanded) {
ViewCompat.animate(arrow).rotation(0).start();
} else {
ViewCompat.animate(arrow).rotation(180).start();
}
isExpanded = !isExpanded;
appBarLayout.setExpanded(isExpanded, true);
presenter.reload(day);
}
#Override
public void onMonthScroll(Date firstDayOfNewMonth) {
setSubtitle(dateFormat.format(firstDayOfNewMonth));
}
});
setCurrentDate(new Date());
MedicineFragment medicineFragment = (MedicineFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame);
if (medicineFragment == null) {
medicineFragment = MedicineFragment.newInstance();
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), medicineFragment, R.id.contentFrame);
}
//Create MedicinePresenter
presenter = new MedicinePresenter(Injection.provideMedicineRepository(MedicineActivity.this), medicineFragment);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.medicine_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_stats) {
Intent intent = new Intent(this, MonthlyReportActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public void setCurrentDate(Date date) {
setSubtitle(dateFormat.format(date));
mCompactCalendarView.setCurrentDate(date);
}
public void setSubtitle(String subtitle) {
datePickerTextView.setText(subtitle);
}
#OnClick(R.id.date_picker_button)
void onDatePickerButtonClicked() {
if (isExpanded) {
ViewCompat.animate(arrow).rotation(0).start();
} else {
ViewCompat.animate(arrow).rotation(180).start();
}
isExpanded = !isExpanded;
appBarLayout.setExpanded(isExpanded, true);
}
}
First Reminder XML File
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay"
app:expanded="false"
app:layout_behavior="com.gautam.medicinetime.utils.ScrollingCalendarBehavior">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:contentScrim="?attr/colorPrimary"
app:titleEnabled="false"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:statusBarScrim="?attr/colorPrimaryDark">
<LinearLayout
android:id="#+id/compactcalendar_view_container"
android:layout_width="match_parent"
android:layout_height="250dp"
android:paddingTop="?attr/actionBarSize"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="1.0">
<com.github.sundeepk.compactcalendarview.CompactCalendarView
android:id="#+id/compactcalendar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:compactCalendarBackgroundColor="?attr/colorPrimary"
app:compactCalendarCurrentDayBackgroundColor="#FFC107"
app:compactCalendarCurrentSelectedDayBackgroundColor="#BBDEFB"
app:compactCalendarTextColor="#fff"
app:compactCalendarTextSize="12sp" />
</LinearLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
style="#style/ToolbarStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:id="#+id/date_picker_button"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?android:selectableItemBackground"
android:gravity="center_vertical"
android:clickable="true"
android:focusable="true"
android:orientation="vertical">
<TextView
android:id="#+id/date_picker_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/date_picker_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/date_picker_text_view"
android:layout_toRightOf="#id/date_picker_text_view"
app:srcCompat="#drawable/ic_arrow_drop_down"
tools:ignore="ContentDescription,RtlHardcoded" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/design_default_color_background">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/image2button"
android:layout_width="48dp"
android:layout_height="50dp"
android:background="#drawable/roundbutton"
android:src="#drawable/menu_icon"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.976" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/sixteen_dp"
app:fabSize="normal"
app:layout_anchor="#+id/relativeLayout2"
app:layout_anchorGravity="end|bottom"
app:srcCompat="#drawable/ic_add" />
<FrameLayout
android:id="#+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="674dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Second Remider .java
public class FoodActivity extends AppCompatActivity {
FloatingActionButton mCreateRem;
RecyclerView mRecyclerview;
ArrayList<Model> dataholder = new ArrayList<Model>();
//Array list to add reminders and display in recyclerview
myAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food);
mRecyclerview = (RecyclerView) findViewById(R.id.recyclerView_food);
mRecyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mCreateRem = (FloatingActionButton) findViewById(R.id.create_reminder);
//Floating action button to change activity
mCreateRem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), FoodAddReminder.class);
startActivity(intent);
//Starts the new activity to add Reminders
}
});
Cursor cursor = new dbManager(getApplicationContext()).readallreminders();
//Cursor To Load data From the database
while (cursor.moveToNext()) {
Model model = new Model (cursor.getString(1), cursor.getString(2), cursor.getString(3));
dataholder.add(model);
}
adapter = new myAdapter(dataholder);
mRecyclerview.setAdapter(adapter);
//Binds the adapter with recyclerview
}
#Override
public void onBackPressed() {
finish();
//Makes the user to exit from the app
super.onBackPressed();
}
}
Second Reminder XML file
<?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=".FoodActivity"
android:id="#+id/Food_Container">
<androidx.appcompat.widget.Toolbar
android:id="#+id/FoodToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/yellow_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="What's on you firdge?" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView_food"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:visibility="invisible"
app:layout_constraintBaseline_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/FoodToolbar"
tools:layout_editor_absoluteX="-4dp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="No food reminder added\n + Add now"
android:textAlignment="center"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/recyclerView_food"
app:layout_constraintStart_toStartOf="#+id/recyclerView_food"
app:layout_constraintTop_toBottomOf="#+id/FoodToolbar"
app:layout_constraintVertical_bias="0.523"
android:visibility="gone"
/>
<ImageView
android:layout_width="379dp"
android:layout_height="46dp"
android:src="#drawable/food_icon"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/FoodToolbar"
app:layout_constraintVertical_bias="0.966"
android:visibility="gone"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/create_reminder"
android:layout_width="65dp"
android:layout_height="56dp"
android:src="#drawable/ic_baseline_add_24"
app:backgroundTint="#color/yellow_light"
app:layout_anchorGravity="right|bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.928"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961" />
</androidx.constraintlayout.widget.ConstraintLayout>
You can not use applicationcontext in here, but if you want you need to add flags to the intent. So easier method for you to use "this" instead:
mCreateRem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(this, FoodAddReminder.class);
startActivity(intent);
//Starts the new activity to add Reminders
}
});
We can only "guess" why, but "not working" and "app keeps stopping" could mean a lot of things and you didn't provide any Log information or an Exception stacktrace. Its also hard to guess what FoodAddReminder.class is, if its an activity, make sure its declared in your AndroidManifest.xml
mCreateRem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(this, FoodAddReminder.class); // <-- is this an activity?
startActivity(intent);
//Starts the new activity to add Reminders
}
});
Check your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.my.project">
<application >
<activity
android:name=".FoodAddReminder"/> <!-- here -->
</application>
</manifest>
If that is not the issue, I suspect that using getApplicationContext(), you're having this crash log
AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this
really what you want?
You should then tell the system you want to start a new task,
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), FoodAddReminder.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
But I would suggest not doing it, instead use your FoodActivity as your calling activity to FoodReminder in this case, and pay attention to this, it should be prefixed with FoodActivity otherwise it will refer to its enclosing anonymous OnClickListener instance.
#Override
public void onClick(View v) {
Intent intent = new Intent(FoodActivity.this, FoodAddReminder.class);
startActivity(intent);
}
If none of these solved your "not working" issue, please provide a more specific Log information and reduce your code to something that is copy-and-paste-able.
I have a problem dealing with FloatingActionButton with sub menu. i need to add actions when the sub menu is clicked. I've just got this FloatingActionButton code from the net, i want to put actions in it, thanks for the help.
this is my FloatingActionButton:
im using com.github.clans:fab:1.6.2 library
this is my onCreateView code:
FloatingActionButton fabsnail = v.findViewById(R.id.fabsnailtrail);
FloatingActionButton fabprox = v.findViewById(R.id.fabproximity);
FloatingActionButton fabnav = v.findViewById(R.id.fabnavigations);
fabsnail.setOnClickListener(this);
fabprox.setOnClickListener(this);
fabnav.setOnClickListener(this);
fabsnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(),"Snail Trail!" , Toast.LENGTH_SHORT);
}
});
fabprox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(),"Proximity!" , Toast.LENGTH_SHORT);
}
});
fabnav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(),"Navigations!" , Toast.LENGTH_SHORT);
}
});
And this is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<com.github.clans.fab.FloatingActionMenu
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="false"
android:layout_marginBottom="11dp"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
fab:menu_animationDelayPerItem="55"
fab:menu_backgroundColor="#android:color/transparent"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#a21e23"
fab:menu_colorPressed="#a21e23"
fab:menu_colorRipple="#99d4d4d4"
fab:menu_labels_style="#style/MenuLabelsStyle"
fab:menu_fab_size="normal"
fab:menu_icon="#mipmap/negative"
fab:menu_labels_colorNormal="#333"
fab:menu_labels_colorPressed="#444"
fab:menu_labels_colorRipple="#66efecec"
fab:menu_labels_cornerRadius="3dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_hideAnimation="#anim/fab_slide_out_to_right"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="8dp"
fab:menu_labels_position="right"
fab:menu_labels_showAnimation="#anim/fab_slide_in_from_right"
fab:menu_labels_showShadow="true"
fab:menu_labels_singleLine="false"
fab:menu_labels_textColor="#f2f1f1"
fab:menu_labels_textSize="15sp"
fab:menu_openDirection="up"
fab:menu_shadowColor="#66aff198"
fab:menu_shadowRadius="4dp"
fab:menu_shadowXOffset="1dp"
fab:menu_shadowYOffset="4dp"
fab:menu_showShadow="true">
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fabsnailtrail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/snailtrail"
fab:fab_label=" Snail Trail "
fab:fab_colorNormal="#a21e23"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fabproximity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/proximity"
fab:fab_label=" Proximity "
fab:fab_colorNormal="#a21e23"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fabnavigations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/navigations"
fab:fab_label=" Navigations "
fab:fab_colorNormal="#a21e23"
fab:fab_size="mini" />
</com.github.clans.fab.FloatingActionMenu>
</RelativeLayout>
You should add show method in Toast .
Try to use this .
Toast.makeText(getContext(),"Navigations!" , Toast.LENGTH_SHORT).show();
If i'm click OK how to choose pick a value one service in radio button but different value.
void OpenDialogService() {
closeLyt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
cancelTxt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
okTxt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
Bundle b = activity.getIntent().getExtras();
if(b == null)
b = new Bundle();
b.putString("service_id", "1");
startActivity(new Intent(activity, Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
}
});
dialog.show();
}
In this line for primary service :
b.putString("service_id", "1")
How i make conditional if service other give value :
b.putString("service_id", "2")
This is code snippet how you can achieve this with the help of RadioGroups
Your dialog layout should look like this :-
<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"
android:orientation="vertical"
tools:context="com.example.test.MainActivity" >
<TextView
android:id="#+id/headerTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#ff669900"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="Pick Service"
android:textColor="#FFF"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_margin="10dp"
android:layout_marginTop="32dp" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/primaryTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:checked="true"
android:text="Primary Service"
android:textSize="18sp" />
<TextView
android:id="#+id/primaryContentTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/primaryTxt"
android:text=" -Dusting \n -Sweeping \n -Washing \n -Cleaning " />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="#+id/service_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/service_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" />
</RadioGroup>
<TextView
android:id="#+id/otherTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/primaryContentTxt"
android:layout_marginTop="10dp"
android:checked="true"
android:gravity="left"
android:text="Other Service(Soon)"
android:textSize="18sp" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/closeLyt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:weightSum="2" >
<TextView
android:id="#+id/cancelTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:padding="5dp"
android:text="CANCEL"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/okTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:padding="5dp"
android:text="OK"
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
And you can inflate this layout in custom dialog box and with the help of radio buttons you can choose service type
int serviceNumber = 0;
void OpenDialogService() {
final Dialog dialog = new Dialog(this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pop_new_order2);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
TextView headerTxt = (TextView) dialog.findViewById(R.id.headerTxt);
TextView okTxt = (TextView) dialog.findViewById(R.id.okTxt);
TextView cancelTxt = (TextView) dialog.findViewById(R.id.cancelTxt);
TextView primaryTxt = (TextView) dialog.findViewById(R.id.primaryTxt);
TextView otherTxt = (TextView) dialog.findViewById(R.id.otherTxt);
TextView primaryContentTxt = (TextView) dialog.findViewById(R.id.primaryContentTxt);
LinearLayout closeLyt = (LinearLayout) dialog.findViewById(R.id.closeLyt);
// Radio Buttons
final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = radio.findViewById(checkedId);
int index = radio.indexOfChild(radioButton);
Toast.makeText(getApplicationContext(), "service" +index, 500).show();
serviceNumber = index+1;
}
});
headerTxt.setText("Pick Services");
//TODO change color here
// headerTxt.setBackgroundResource(R.drawable.bg_dialog_header_success);
// closeLyt.setBackgroundResource(R.color.selector_close_alert_dialog_success);
closeLyt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
cancelTxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
okTxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Bundle b = getIntent().getExtras();
if (b == null)
b = new Bundle();
b.putString("service_id", String.valueOf(serviceNumber));
Toast.makeText(getApplicationContext(), "service " + String.valueOf(serviceNumber), 500).show();
//TODO Handle Activity Transition Here
// startActivity(new Intent(MainActivity.this,
// Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
// overridePendingTransition(R.anim.push_left_in,
// R.anim.push_left_out);
}
});
dialog.show();
}
Result
You can get the selected item id of RadioGroup using, radioGroup.getCheckedRadioButtonId() and use if-else to choose different strings.
int id = radioGroup.getCheckedRadioButtonId();
if(id == radioButton1)
{
b.putString("service_id", "1");
}
else if(id == radioButton1)
{
b.putString("service_id", "2");
}
I'm trying to create an application with 5 buttons on the main page. For some reason the ListenerOnButton is not working and the app will close before showing xml buttons layout.
Here's my MainActivity:
public class MainListActivity extends Activity {
ImageButton news;
ImageButton weather;
ImageButton counter;
ImageButton fakoi;
ImageButton gyalia;
ImageButton uvindex;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
addListenerOnButton();
}
public void addListenerOnButton() {
news = (ImageButton) findViewById(R.id.news);
weather = (ImageButton) findViewById(R.id.weather);
fakoi = (ImageButton) findViewById(R.id.fakoi);
gyalia = (ImageButton) findViewById(R.id.gyalia);
uvindex = (ImageButton) findViewById(R.id.uvindex);
news.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), MyOrasisNews.class);
startActivity(intent);
}
});
weather.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
gyalia.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), GyaliaActivity.class);
startActivity(intent);
}
});
fakoi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), FakoiActivity.class);
startActivity(intent);
}
});
uvindex.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), UvMainActivity.class);
startActivity(intent);
}
});
}
}
And this is the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:background="#drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:text="News"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/news"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/weather"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:text="Weather"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/weather"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/uvindex"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Uv Index"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/uvindex"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/fakoi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="S Fakwn"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sfakon"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/gyalia"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="S gyalion"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sgyalion"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
</TableLayout>
</LinearLayout>
Any help will be appreciated.
you defined Buttons in your xml but you are trying to cast them to ImageButton. change type of your ImageButtons to Button.
You try to cast Button to ImageButton.
In your XML file change the Button to ImageButton or in your activity change the ImageButton to Button.
I am making a game that requires multiple views within an Activity, and i decided to use Viewflipper to do.
The thing is. I need to have 3 views in in the viewflipper, and the LAST view will transfer back to the first one.
My problem is that the buttons are acting weird, and they are either not going to the next, or skipping the third view. I tried to put vf.setDisplayedChild(R.Id.gamescreen1); at the last view but then the whole thing breaks down.
Thanks for all answers in advance, i have been stuck with this problem for DAYS! and yes, i know that i am a noob :(
[SOURCECODE]
public class GameActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_game);
final ViewFlipper vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
//SCREEN 1
Button btnSTART = (Button) findViewById(R.id.btnSTART);
//SCREEN 2
Button btnALT1 = (Button) findViewById(R.id.btnALT1);
Button btnALT2 = (Button) findViewById(R.id.btnALT1);
//SCREEN 3
Button btnALT3 = (Button) findViewById(R.id.btnALT1);
Button btnALT4 = (Button) findViewById(R.id.btnALT1);
//screen 1
btnSTART.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
//screen 2 // Either button will go to view 3
btnALT1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
btnALT2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
//screen 3 // Either button will go back to view 1
btnALT3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
btnALT4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
}
}
[XML]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="435dp" android:gravity="top">
<ListView android:layout_width="fill_parent" android:id="#+id/list1" android:layout_height="184dp" android:layout_weight="0.53"></ListView>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:gravity="bottom|center" android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:id="#+id/btnSTART" android:layout_width="200dp" android:text="#string/btnstart"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/list2"></ListView>
</LinearLayout>
<LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="alt1" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT1"></Button>
<Button android:text="alt2" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT2"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
</LinearLayout>
<LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="alt3" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT3"></Button>
<Button android:text="alt4" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT4"></Button>
</LinearLayout>
</LinearLayout>
try this
flipper.setDisplayedChild(1);
.
.to
.
flipper.setDisplayedChild(3);
if(flipper.getCurrentView() == 3)
{
flipper.setDisplayedChild(1);
}
setDisplayChild takes an integer parameter that is the zero based index of the child to display NOT the id of the child to display. A bit confusing I know.