I am making an android app and I want it so that when someone clicks a button in the main menu activity, they are taken to the corresponding activity. At the moment my code is:
button in menu xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dwight Quotes"
android:id="#+id/DwightButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="openNewActivity"
android:background="#EAD383"
android:enabled="false" />
the menu java:
public void openNewActivity(View view) {
// Do something in response to button
Intent intent = new Intent(this, Dwight.class);
startActivity(intent);
when I click the button nothing happens. Can someone explain what I need to do?
Remove android:enabled="false" from your code.
Related
I am trying to make a clickable phone number button with an icon in my app. I was checking for some references and found google maps implementation good. How can I achieve this in my app?
I have tried the Image Button view but that does not solve the problem. I have put 'onClick' attribute for text & image views, but the button animation isn't there and both text & image icon does not look together.
Please guide me as to what view/s we have to use to achieve the result as in the image and how to get that animation on click of the button. Or is there any better way to achieve this?
I am aware of intents, so that part is clear.
If you can let me know how to make that phone number copied to the clipboard automatically on hold of that button, that would be really great.
Try this code
Change spacing dimens according to your use and also change icon.
<LinearLayout
android:id="#+id/callButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/spacing_small"
android:padding="#dimen/spacing_small"
android:clickable="false"
app:srcCompat="#drawable/ic_download"
android:tint="#color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/amaranth"
android:gravity="center_vertical"
android:paddingLeft="#dimen/spacing_xxhuge"
android:paddingTop="#dimen/spacing_medium"
android:paddingBottom="#dimen/spacing_medium"
android:text="000 0000 000"
android:textColor="#color/grey_70"
android:textSize="#dimen/textsize_large" />
</LinearLayout>
and set on click listener on callButton. use below code in java code.
And also i have added a code to copy phone number directly on click event. You have to save text in clipboard.
LinearLayout callButton = findViewById("callButton");
callButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
// You have to get text from phoneNumber textview. and set it to clipboard.
clipboard.setPrimaryClip(clip);
}
});
Some questions:
Do you have any experience with Android development?
If so, do you have anything up and running?
I'm gonna assume you do have experience but you're asking before you start coding anything. There are many ways to implement this, the way that would be easiest would be to have a custom listview (here's a simple and easy tutorial for that) and use an item in the listview to display a phone number. Each listview item has a setOnItemLongClickListener which you can use and inside it use the ClipboardManager to copy or use an intent to the phone calling service.
list.setOnItemLongClickListener(new OnItemLongClickListener() { //list is my listView
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int pos, long id) {
//Whatever you wanna do
ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
}
});
I believe this is what you want to achieve
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Your other layouts-->
<TextView
android:background="?selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:padding="#dimen/activity_vertical_margin"
android:drawableStart="#drawable/ic_phone"
android:drawablePadding="16dp"
android:text="The mobile number here"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
android:background="?selectableItemBackground"
This will add the default system animation of ripple(or anything) on click.
Also android:focusable=" and android:clickable="true" is necessary for this to work.
If you want to customize the click events, you better be using selectors in the background of your view.
For the 'Copy to Clipboard' feature you can refer to the other answers.
Happy Coding!
i have a TextView or Button when i click on any View or TextView android app getting closed like entered into background mode. why this is happened i don't know.
please help me out of this bug.
Here is my xml code:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginRight="#dimen/default_margin"
android:visibility="visible"
android:background="#drawable/text_cta"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin_5"
android:layout_marginRight="#dimen/margin_5"
android:textAlignment="center"
android:gravity="center"
android:fontFamily="#font/sf_regular"
android:text="Next"
android:textColor="#color/colorOrange"
android:textSize="#dimen/text_20" />
</LinearLayout>
Here is My click actions:
#OnClick({R.id.ll_profile, R.id.tv_all})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.ll_profile:
//callProfileAPI();
startActivity(new Intent(context, ProfileActivity.class).putExtra("isFrom", ""));
break;
case R.id.tv_all:
startActivity(new Intent(context, ViewAllClubNewsActivity.class).putExtra("isFrom", "Home"));
break;
}
}
i'm really annoying for this error, i don't find any answers for this.
i tried with below line added in my view:
android:splitMotionEvents="false"
but i doesn't event work.
below is the stack trace coming everytime:
06-09 12:06:49.403 7048-8137/com.bocawestcc.cobaltmemberapp E/FA: Unable to use remote or local measurement implementation. Please register the AppMeasurementService service in the app manifest
Please help me...
I'm making a simple app and when I press a button that has an onClick() it crashes. I'm trying to open another activity on the button click.
Here's my onClick method from MainActivity.java:
public void gameActivity(View view){
Intent intent = new Intent(MainActivity.this, GameActivity.class);
MainActivity.this.startActivity(intent);
}
Here's my Button code from activity_main.xml:
<Button
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f01c00"
android:onClick="gameActivity"
android:padding="16dp"
android:text="Play"
android:textColor="#FFF" />
Let me know what you think's the problem. Thanks
Well, I tried different things like moving around the startActivity(), but nothing happened. I deleted the activity I wanted to start and made a new one. That worked. I don't know what happened, but it's solved.
GameActivity must be registered in the AndroidManifest.
Been stuck on this for about an hour now - May be an easy fix but I just cannot figure it out.
I have a button on the start screen, which once clicked will move to another activity.
This is from the xml:
<ImageButton
android:id="#+id/btn_text_scan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/scan_button_description"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#null"
android:gravity="center"
android:scaleType="centerInside"
android:onClick="textExtract"
android:src="#drawable/ic_btn_text" />
and this is from the manifest:
<activity
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTop"
android:name=".OCRActivity">
</activity>
And from the class I want to move from, I use this code to move to the other activity. (However, even by removing this code, I still get this problem - so probably not this)
public void textExtract(View view)
{
Intent intent = new Intent(ScannerActivity.this, OCRActivity.class);
startActivity(intent);
}
I can provide the code for the activities, but I dont think this is needed
Any help would be appreciated!
Dan
EDIT
LogCat error
http://i.imgur.com/zK43neM.png
You have to call super.onCreate(bundle); as the first line in your onCreate(Bundle bundle) method. bundle might be named something else, but it's whatever is passed into this method.
The same goes for other lifecycle methods in an Activity. For example, the first line in your onPause method should be a call to super.onPause().
Try this:
Intent intent = new Intent(getApplicationContext(), OCRActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
Try and remove that android:background="#null" line in your XML and see if that works.
I've been racking my brain for the past two days trying to get a button to work.
Basically I want my button to call 'add a password function'.
If you like to view my entire code, its at github.com/servingbaby/UPM-Epassafe .
Here is my main.xml (The button shows up succesfully however when pushing it nothing happens)
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:gravity="center"
android:text="#string/no_accounts" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center"
android:text="#string/add_account" android:id="#+id/add" android:minWidth="125dip"></Button>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Here is my AndroidManifest slimmed down to the parts I believe are where I am going wrong.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.epassafe.upm"
android:versionCode="3"
android:versionName="1.2">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="16"/>
<application android:icon="#drawable/upm"
....>
<activity android:name=".AppEntryActivity">
</activity>
<activity android:name=".FullAccountList">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResults" />
</activity>
<activity android:name=".AddEditAccount">
</activity>
</application>
</manifest>
Here is my MainActivity.Java, where I believe I should be calling the correct code.
public abstract class MainActivity extends Activity implements OnClickListener{`
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button countButton = (Button) findViewById(R.id.add);
countButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AddEditAccount.class);
MainActivity.this.startActivity(intent);
}
});
}
}
But pushing the button nothing happens like I expect. No errors or force close, so Im really at a loss at the moment. Thankyou!
**Edit
Thank you all how attempt to help me. I have tried many of the suggested solutions and still haven't figured it out just yet. I have uploaded my files to github with some of the attempted edits I have made as commented. Basically I am just trying to figure out how to add a button and get it to run the necessary code to add an account. (The original code works well, but I would like to improve upon it. Sorry my competence level is still low, but trying to learn my best :) I would really appreciate if anyone could help me with this! Thank you^^
**Edit 2
Again Thankyou all for the advice and suggestions. I figured out a simple solution. I ended up using a Menu Wrapper to create a button click event, and also it turned out I was editing the wrong Java document, when I was suppose to be trying to add the correct code to another one that seem to almost be doing the same thing. Learn something new everyday.
I don't understand why you're implementing OnClickListener to your activity. Remove the implements OnClickListener from your activity. It should work fine.
But since you're doing this for don't-know-what reason, you should make your button clickable like this.
countButton.setOnClickListener(this);
public void onClick(View view)
{
Intent intent = new Intent(MainActivity.this,AddEditAccount.class);
MainActivity.this.startActivity(intent);
}
First of all you dont need to implement OnClickListener to set a a listner in your button. But maybe you have it for another reason, if not, remove it.
You are declaring wrong the names of the id of your xml i think, take a look at this:
Difference between "#id/" and "#+id/" in Android
May be because of the #android:id instead of #+id.
use this way call Intent
Intent intent = new Intent(MainActivity.this, AddEditAccount.class);
startActivity(intent);
look at your list view
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
you have given width and height as fill parent so your ListView is on your button so on the screen when you click on the button you are not actually clicking on the button. when you click a button the button color changes for a sec that is not happening in your case see it for your self. so remove the listview or set the height below the button and then check your button click will work