<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="horizontal"
android:padding="10dp"
android:paddingTop="8dp"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/view"
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/accent"/>
<TextView
android:id="#+id/m_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF6600"
android:textSize="30dp"
android:layout_toRightOf="#id/view"
android:text="hey th"
android:textStyle="bold" />
<Button
android:id="#+id/m_delete"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/m_name"
android:layout_weight="0"
android:text="Delete" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I have put a button inside like this and this is shown in recycler view.
The problem I am facing is that the button click only works if I long press on the button else if i click normally then intent is called.
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MetricsDAO dao = new MetricsDAO(context);
dao.deleteMetrics(metrices.get(position).getEventName());
dao.close();
Intent i = new Intent(context, HealthGraph.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
// ((Activity) context).finish();
}
});
I am doing this inside on onBindViewHolder()..
What is it that i am doing wrong or missing?
Related
I am trying to implement a setOnClickListener method on CardView but I keep getting a null exception whenever I switch orientation of device (works fine if I start by either portrait or landscape mode):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.navajoinc.navajoinc.navfeedbackapp/com.navajoinc.navajoinc.navfeedbackapp.feedBackCardView}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
My Java Class is:
public class feedBackCardView extends FragmentActivity {
CardView neutralCustomer;
CardView happyCustomer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_back_card_view);
neutralCustomer = findViewById(R.id.neutralCustomer);
happyCustomer = findViewById(R.id.happyCard);
neutralCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(feedBackCardView.this, thank_you_positive.class);
startActivity(intent);
}
});
happyCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(feedBackCardView.this, thank_you_positive.class);
startActivity(intent);
}
});
}
public void unsatisfiedCutomerSelectionMethod(View v){
singleChoiceClass unsatisfied_customer_dialog = new singleChoiceClass();
unsatisfied_customer_dialog.show(getFragmentManager(), "unsatisfied_customer_dialog");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main_menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.contactus:
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:800.525.5097"));
startActivity(callIntent);
break;
case R.id.visitsite:
Intent siteIntent = new Intent(Intent.ACTION_VIEW);
siteIntent.setData(Uri.parse("http://navajoinc.com"));
startActivity(siteIntent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
Activity 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=".feedBackCardView"
android:padding="10dp"
android:background="#color/colorWhiteSmoke"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:layout_gravity="center"
android:text="Please tap a response :"
android:textColor="#color/colorLightGrey" />
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:clickable="false"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/happyCard"
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/happy_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Happy Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="I have had a happy experience"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:id="#+id/neutralLayout"
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/neutralCustomer"
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/neutral_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Neutral Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="My experience was neutral"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:onClick="unsatisfiedCutomerSelectionMethod"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/angry_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Unsatisfied Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="My experience was unpleasant"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I have tried many solutions on Stackoverflow and other websites but none of it has worked ? Could you please help with this issue?
I've tried the code and it's working. Even after rotation. However, I changed the implementation of the onclick methods to simply show a toast rather that launch an intent since you didn't provide the classes. I also ommited all the code related to the menu.
Also, why are you setting the clickable=true property on your card view if you are setting an onClickListener within your code? It's redundant. Also, I don't see any reason to assert clickable=false in your <LinearLayout> tags.
I don't think you are providing enough information to locate the error, or it's related to the classes that haven't been provided. Is the app crashing simply on rotation, or after you try clicking on something? Are you sure the crash is related to rotation, and not an attempt to launch a new activity?
I am new to android development. I want to include the same horizontal Scroll view in all android activities.I have defined the layout and Onclick events in one separate activity and extended that class with other activites.But the onclick events are not working
here is my Base Activity
public class Footer extends AppCompatActivity implements View.OnClickListener {
private ImageView img_school, img_group, img_news, img_schemes, img_jobs, img_gallery, img_goddess, img_services, img_census, img_address, img_abt_us;
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imgs_school:
Intent i = new Intent(Footer.this, SchoolActivity.class);
startActivity(i);
break;
case R.id.imgs_galary:
Intent gi = new Intent(Footer.this, GalleryActivity.class);
startActivity(gi);
break;
case R.id.imgs_events:
Intent ni = new Intent(Footer.this, EventsActivity.class);
startActivity(ni);
break;
case R.id.imgs_abtus:
break;
case R.id.imgs_jobs:
Intent ji = new Intent(Footer.this, JobsActivity.class);
startActivity(ji);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_footer);
initAll();
img_gallery.setOnClickListener(this);
img_school.setOnClickListener(this);
img_news.setOnClickListener(this);
img_jobs.setOnClickListener(this);
img_abt_us.setOnClickListener(this);
}
public void initAll() {
img_school = findViewById(R.id.imgs_school);
img_news = findViewById(R.id.imgs_events);
img_jobs = findViewById(R.id.imgs_jobs);
img_gallery = findViewById(R.id.imgs_galary);
img_abt_us = findViewById(R.id.imgs_abtus);
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:paddingLeft="#dimen/_20sdp"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_school"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/school"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="School"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_events"
android:clickable="true"
android:src="#drawable/news"
android:layout_gravity="center"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Events"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_jobs"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/jobs"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Jobs"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:paddingRight="#dimen/_50sdp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_galary"
android:clickable="true"
android:src="#drawable/gallery"
android:layout_gravity="center"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Gallery"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:paddingRight="#dimen/_50sdp"
android:layout_height="wrap_content">
<ImageView
android:layout_gravity="center"
android:id="#+id/imgs_abtus"
android:clickable="true"
android:src="#drawable/abt_us"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="About Us"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
And i am extending this activity with other classes
such as
public class SchoolActivity extends Footer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
}
}
And its activity_school xml
<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.evoqis.manovaru.SchoolActivity">
<include layout="#layout/activity_footer"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
But the onclick events are not happening.
Thanks in adavance
Here is the problem:
In subclass you call:
super.onCreate(savedInstanceState);
Which calls:-
setContentView(R.layout.activity_footer); //which creates a new views
initAll(); //which finds views by id
img_gallery.setOnClickListener(this); //setting listeners.
and then in subclass you call:
setContentView(R.layout.activity_school); which creates new view, and overrides all the listeners you have set in superclass.
How to fix it
Move
img_gallery.setOnClickListener(this);
img_school.setOnClickListener(this);
img_news.setOnClickListener(this);
img_jobs.setOnClickListener(this);
img_abt_us.setOnClickListener(this);
Inside public void initAll(), and in subclass do this:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
initAll();
Also you can remove onCreate() method from superclass.
the issue is whithin 3 imageviews don't display toast and also i tried to log any text whithin its onlick and from anonymous also not working any suggestion?
Screenshot
My XML
<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"
android:background="#mipmap/bg_login"
android:orientation="vertical"
android:paddingBottom="19dp"
android:paddingLeft="10dp"
android:paddingRight="9dp"
tools:context="com.example.mahmoud.blink.LoginFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="217dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="51dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:src="#mipmap/logo_small" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#mipmap/overlay_white"
android:clickable="false"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="#+id/iv_tm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
app:srcCompat="#mipmap/t" />
<ImageView
android:id="#+id/iv_fbm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="31dp"
android:layout_marginRight="31dp"
android:layout_toLeftOf="#+id/iv_tm"
android:layout_toStartOf="#+id/iv_tm"
android:background="#mipmap/f"
android:clickable="true" />
<ImageView
android:id="#+id/iv_googlem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_toEndOf="#+id/iv_tm"
android:layout_toRightOf="#+id/iv_tm"
android:clickable="true"
app:srcCompat="#mipmap/g" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.5dp"
android:layout_marginRight="12dp"
android:drawableLeft="#mipmap/mail2"
android:drawablePadding="10dp"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.5dp"
android:layout_marginRight="12dp"
android:drawableLeft="#mipmap/password"
android:drawablePadding="10dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
</LinearLayout>
<com.wang.avi.AVLoadingIndicatorView
android:id="#+id/avi"
style="#style/AVLoadingIndicatorView.Large"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical|center_horizontal"
app:indicatorColor="#color/colorPrimary"
app:indicatorName="BallClipRotatePulseIndicator" />
</FrameLayout>
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="15dp"
android:background="#mipmap/normal_btn"
android:text="Sign In"
android:textColor="#8b572a" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_forget_pass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/sencondry_btn"
android:text="Forgot Password"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="6dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/sencondry_btn"
android:text="Sign up"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Java Code
in LoginFragment.java
//decleration within the fragment class
ImageView iv_g;
ImageView iv_fb;
ImageView iv_t;
From onCreateView method
View view = inflater.inflate(R.layout.fragment_login, container, false);
iv_fb = (ImageView) view.findViewById(R.id.iv_fbm);
iv_t = (ImageView) view.findViewById(R.id.iv_tm);
iv_g = (ImageView) view.findViewById(R.id.iv_googlem);
iv_fb.setOnClickListener(this);
iv_g.setOnClickListener(this);
iv_t.setOnClickListener(this);
From onClick method
if (v.getId() == R.id.iv_googlem) {
Toast.makeText(context, "google", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MyService.class);
intent.putExtra("type", "google");
context.startService(intent);
} else if (v.getId() == R.id.iv_fbm) {
Toast.makeText(context, "facebook", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MyService.class);
intent.putExtra("type", "facebook");
context.startService(intent);
} else if (v.getId() == R.id.iv_tm) {
Toast.makeText(context, "twitter", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MyService.class);
intent.putExtra("type", "twitter");
context.startService(intent);
}
the 3 buttons works right , but the 3 ImageViews don't triggerd when clicked
Try this syntax and see if it works :
iv_fb.setOnClickListener(new view.OnClickListener{ public void onClick()
{
Toast.makeText(context, "google", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MyService.class);
intent.putExtra("type", "google");
context.startService(intent);
} });
I am using a layout inflater instead of menu inflater in which the layout gets inflated in center of the page instead of showing at the right corner.
I have tried changing the gravity to right but it didn't works.
Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="#color/primary"
android:orientation="vertical"
android:layout_alignParentEnd="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linshare"
android:paddingTop="5dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:background="#drawable/share"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="Share"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:id="#+id/lininvite"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:background="#drawable/invite"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="Invite Friends"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linrate"
android:paddingTop="5dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:background="#drawable/rate"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="Rateus"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:id="#+id/linhelp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:background="#drawable/help"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="Help"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Code:
private void menz() {
LayoutInflater layoutInflater = LayoutInflater.from(Additionone.this);
View promptView = layoutInflater.inflate(R.layout.menuinflate, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Additionone.this);
alertDialogBuilder.setView(promptView);
final LinearLayout share = (LinearLayout) promptView.findViewById(R.id.linshare);
final LinearLayout invite = (LinearLayout) promptView.findViewById(R.id.lininvite);
final LinearLayout rate = (LinearLayout) promptView.findViewById(R.id.linrate);
final LinearLayout help = (LinearLayout) promptView.findViewById(R.id.linhelp);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Future of Learning http://www.pencilruler.com/ ");
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
invite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
i.putExtra(Intent.EXTRA_SUBJECT, "QUIZ APP");
i.putExtra(Intent.EXTRA_TEXT, "You have to try this quiz app");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "NO EMAIL CLIENTS INSTALLED", Snackbar.LENGTH_LONG);
snackbar.show();
}
}
});
help.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(Additionone.this,HelpAct.class);
startActivity(i);
Additionone.this.finish();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Screenshot:
You need to set your Alert Dialog Builder Alignment as you are prompting your view in it.
Using setGravity(Gravity.TOP | Gravity.RIGHT);
Refer this.
Window window = alertDialogBuilder.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.TOP | Gravity.RIGHT);
NOTE: You can change Gravity of Alert Dialog as per your choice.
my app contains "Settings" activity which contains 2 imagebuttons "back" and "about", whenever i press any of these buttons i get:
"couldnt save which view has focus because the focused view android.widget.LinearLayout#4055a9e0 has no id"
the xml code for my_settings:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll2"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="15" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="8dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/settings" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|end"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/tb1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="#string/sett1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/border2"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/ll_set_back"
android:layout_margin="3dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
**<ImageButton
android:id="#+id/st_ibt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_btn"
android:src="#drawable/back_ic" />**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/ll_set_about"
android:layout_margin="3dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
**<ImageButton
android:id="#+id/st_ibt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_btn"
android:src="#drawable/about_ic" />**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
dont pay attention to the "scrollview" (i dont think the problem is there), just look at the end of the code for the 2 imagebuttons..
and this the java code for Settings:
public class Settings extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_settings);
//code...
ImageButton goBack=(ImageButton)findViewById(R.id.st_ibt1);
ImageButton about=(ImageButton)findViewById(R.id.st_ibt2);
goBack.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Settings.this, MainActivity.class);
startActivity(intent);
//finish();
}
});
about.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Settings.this, About.class);
startActivity(intent);
//finish();
}
});
}
so, how to fix this? i dont understand what this error means.. any suggestions
I think the problem is :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical" >
You enable focus to this LinearLayout, but you didn't provide any id for it.