Add button and functionality yo appWidget - android

Ok y'all, I'm trying to learn here. I just learned how to create a basic appWidget and it works so far. Now try as I might, I do not understand how to add an image button to my appWidget. I mean it's been physically added to the xml file named simple_app_widget.xml. What can't figure out is how do I add an onClick event to the SimpleAppWidget.java file? I have tried it like a normal one click event, but AIDE tells me that it doesn't recognize the info being input.
Please don't just do my work for me. I want to learn, but my search skills are lacking and what I do find is irrelevant or not at me skill level.

as you said... to add an image button first must add the image in your drawable folder ( search it in your proyect tree), then go to the layout where want to use it and write your code like this (note : in, android:background =" " must enter the same name of your image copied in drawable folder):
<ImageButton
android:id="#+id/arrow"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="4dp"
android:background="#drawable/arrow"
android:contentDescription="#string/test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.902"
app:layout_constraintStart_toStartOf="parent" />
After it, go to your Activity.class and type your code:
public class MainActivity extends AppCompatActivity {
ImageButton test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here should put what you want to show or where you want to go after click the button.
//For example if you want to go to the next activity use Intents like this:
Intent intent = new Intent(this, MainActivity1.class);
startActivity(intent);
finish();
}
});
}
}

Related

Can I use the same button on multiple Android activities and return to original activity?

I'm new to Android and making a very, very rough sketch of how my app will display information to users. I am making the app but not providing all the text it displays.
What I want to do is have multiple activities with a text box and underneath it have a button using the oncreate method (I believe the correct method) to have the exact same button on each activity linking back to main activity. What I want to do is make it so I don't have to recreate the intent or the button code on every page, just make a single line that does like "display button ID=home" on each activity. Just want to make it as simple as possible because I'll be making a lot of activities with text display.
When I figure it out I'm just going to drop the buttons and use a navigation drawer to access the info but need a draft for the team to start organizing the Information it displays.
I hope explaining what my final product will be can help show what I want to do. Thanks for any help in advance!
Create button programmatically.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button button1 = createButton(this);
layout.addView(button1);
public static Button createButton(Activity a) {
Button b = new Button(a);
b.setLayoutParams(layoutParams);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(a.class.this, MainActivity.class);
startActivity(intent);
}
})
return b;
}

How do i make multiple pages that i can go to in the app with buttons and make the imagebuttons link to these pages?

So this is my first app and I am trying to code and need some help with buttons. After searching for a answer I just couldn't find one that I understood. I want to be able to make different pages for the app and make imagebuttons that link to these pages. This is the very basic code I have at the minute for my button. Please try to explain where to put the code etc.
Thanks in advance.
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:background="#drawable/home_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:nestedScrollingEnabled="true" />
</RelativeLayout>
Since this is your first app, let's start it simple with using only Activities.
You start with a MainActivity, which shall contain your ImageButtons. By clicking on one of these buttons, you will be directed to another activity. If you press the back button, you will arrive back at your MainActivity.
I shall demonstrate some code which shows you how to navigate from one activity to another one. First add the two activities so your AndroidManifest.xml will look something like this:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second_activitity" >
</activity>
If you're using AndroidStudio, it will do this for you when you create a new activity.
Your MainActivity.java will look something like this:
public class MainActivity extends Activity {
//Define your views
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Find your views
imageButton = (ImageButton) findViewById(R.id.image_button);
//Assign a listener to your button
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Start your second activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
Repeat these steps for every Activity you want to add to your application.
For more information, you will find the Android Docs an usefull source. Please check this link out as a start.
Good luck!
I dont think its a question to be questioned ! Though,make your desired button in your main .xml file and the using java access the button and apply the task which you want to perform from that button ..
You use this in .xml to make a button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
/>
Here's java code for accessing this button
private Button button;
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override public void onClick(View view) {
//do what you want here
}
});
}

OnClick to move from more than two activities ?

I'm a beginner in android developing , I tried to make many activities ( screens ) , when clicking the first button it takes me to the second screen , but when I click on the button of the second screen to move to the third screen the app won't work !
This is my XML code :
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:background="#drawable/fal"
android:onClick="kahwah" />
and this is how it is in java :
public void kahwah(View V)
{
Intent i2 = new Intent (this,Third.class);
startActivity(i2);
}
You need to mention your activities in Android manifest file of application tag like:
<activity android:name="com.your_package_name.YourActivity" />
in the application tag.

Android - button suddenly stopped responding to listener

I have a button like this:
<Button
android:id="#+id/logout_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Log out"
/>
and I have a listener like this:
Button logout = (Button)findViewById(R.id.logout_button);
logout.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Logging out. Please wait...", Toast.LENGTH_LONG).show();
sendEmail("Logout Chosen", "From home page, user clicked on logout" );
Intent myIntent = new Intent(ProblemioActivity.this, LogoutActivity.class);
ProblemioActivity.this.startActivity(myIntent);
}
});
I don't remember making too many changes to this, but the button suddenly stopped responding to clicks. Funny thing is on the same page the other buttons all work well still.
Any idea why this might happen?
Thanks!
1)Try rebuilding your code and see if that works.
ECLIPSE: Project>Clean>Your Project
2) Also, check the other button's ID's, and make sure you did not accidentally named them the same (there should only be one button whose ID is "logout_button")

How to go to web page after clicking on custom banner in app?

I have created a custom banner that is a simple rectangle picture that sits on the bottom of the first activity screen after the app is opened. It is being displayed as an ImageView with clickable="true". I have a setOnClickListener ready to go. Just need help with the code that actually takes the user to my website when they click on my custom banner. Also, need to know what permission to ask for to cover this. I would assume it is android.permission.INTERNET. Thanks for your help in advance.
Ok, here is the code that ended up working. Thanks guys.
Inside the xml layout file for this activity:
<ImageView
android:src="#drawable/saas_banner"
android:clickable="true"
android:id="#+id/SaasBannerIMG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
Inside the onCreate method for the activity:
// add a click listener to the SaaS Ad Banner
ImageView img = (ImageView) findViewById(R.id.SaasBannerIMG);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.websitetogoto.com"));
startActivity(intent);
}
});
Do you want it to open up the browser in order to view the webpage?
If you do, then you can try to do an implicit intent.
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yourwebsite.com"));
startActivity(intent);
Put that in your button handler and the browser should load with the webpage.
You just need to set up an intent to start the browser with the required link. See this answer for the details: Sending an Intent to browser to open specific URL .
You do not need a specific permission, since the browsing will be handled by a different app (which does need the INTERNET permission).
This is, of course, unless you want to load the page yourself inside a WebView.

Categories

Resources