Edit: I realized that the code posted below works flawlessly in Nexus 6 (shamu) and Galaxy Tab A, both with android 7.0! And it does not show on Moto G5 Plus (android 7.0).
It's a simple question. My ProgressBar won't show up. At frist I was trying directly at my LoginActivity, but I wasn't having success. Things that i tried:
Put ProgressBar directly inside the layout
Put ProgressBar inside a layout
Set ProgressBar propriety indeterminate to true
Set the visibility of progressbar and parent layout to VISIBLE
Create a separated .xml file and call a Dialog (in this case, if I put a textview inside the text will appear but the progressbar won't)
Change the proprieties layout_width and layout_height
A combination of all the steps above
Last I tryed to create this simple blank activity showing only the progress bar...
activity_test.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="context_name_here">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="visible" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
test.java:
package package_name_here;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class test extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
What's the problem? I suspect that is something about the resources... the image of the default progressbar or version of android...
any ideas?
Thanks
I figured out what was the problem.
The Animator duration scale have to be ON at Developer options from device:
Developer options -> Developer duration scale -> 1x (ON)
Try with hard-coded dimension for the width and height for ProgressBar, also check your Colors that might be the same as activity default, also check by removing the style attribute once, or try with a different style for ProgressBar.
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.textView);
// Start long running operation in a background thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
Define XML:
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="20dp"
android:indeterminate="false"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1" />
<ProgressBar
android:id="#+id/progressBar_cyclic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:minWidth="50dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
First you need to declare the progress dialog
private ProgressBar progressBar;
then in your onCreate
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar= new ProgressBar(test.this);
progressBar.show();
to dismiss the dialog or cancel it you can use
progressBar.cancel(); or progressBar.dismiss();
Related
I want to toggle visibility of some elements. I have read some Q/A that the main problem was doing this in main thread but I am running the code in runOnUiThread but not works yet:
TextView progress;
TextView registrationFailed;
Button tryAgainRegistration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
sp = this.getSharedPreferences("settings", this.MODE_PRIVATE);
progress = (TextView) findViewById(R.id.progress);
registrationFailed = (TextView) findViewById(R.id.registrationFailed);
tryAgainRegistration=(Button) findViewById(R.id.tryAgainRegistration);
Setstat(0); // For test
}
public void Setstat(final Integer a){
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e("progress",a.toString()); // It shows 0 and 1 correctly
if (a == 1) {
progress.setVisibility(View.VISIBLE);
registrationFailed.setVisibility(View.GONE);
tryAgainRegistration.setVisibility(View.GONE);
} else {
progress.setVisibility(View.GONE);
registrationFailed.setVisibility(View.VISIBLE);
tryAgainRegistration.setVisibility(View.VISIBLE);
}
}
});
}
for more details this is the UI xml:
<TextView
android:id="#+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="Registeration in progress"
android:visibility="visible" />
<TextView
android:id="#+id/registrationFailed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="Registeration in progress"
android:visibility="gone" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Try again"
android:id="#+id/tryAgainRegistration"
android:visibility="gone"
/>
Try running this in on create.
Just to see if it is because of the threads or not:
findViewById(R.id.yourView).setVisibility(View.GONE);
The runOnUiThread method needs a reference to the activity.
Otherwise it doesn't know which one the UI thread is.
You might get a warning about memory leaks. If you are worried about that, google for WeakReference.
((Activity)context).runOnUiThread(new Runnable() {
My specific problem was that I had created the XML view using constrainLayout but modified it manually without removing original settings of constrainLayout. So I guess the layout was not well recognized by system Ui.
In fact I made a mistake by manually renaming android.support.constraint.ConstraintLayout to LinearLayout as the root element.
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Registration"
tools:showIn="#layout/activity_registration">
I am working on splash screen for my android app. The screen starts from my MainActivity.java, in which onCreate() method contains the following code. Also the same activity class is loading maps in its onResume() method and in it i m loading a new layout which is activity_main.xml. That xml has black screen and no background screen.
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.loading_screen);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView = (TextView) findViewById(R.id.textView1);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus + "/"
+ progressBar.getMax());
}
});
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
compass = new CompassSensor(this, compassHandler);
}
my layout for loading_screen.xml is as follow.
loading_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash_screen_loading" xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/progressBar1"
android:layout_marginLeft="15dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
The problem i m facing is that only black screen shows during app loading. I tried to debug the problem but all my efforts were useless. Plz help.
You should not attempt this in onCreate, if you expect visual changes. Nothing is shown to user during onCreate. I am aware of Thread.sleep(200).
You may want to try doing it in your onResume, just after calling super.onResume. The way you described it, R.layout.loading_screen is not even supposed to be shown.
Also, if you're using AsyncTask to load maps, Developers lets you know how to publish progress. Take a look at this: AsyncTask. Maybe you could add this prior work (with progress on R.layout.loading_screen) to asynchronous task, too.
I have rating bar widget in my layout and set custom style in layout.
I want to set more that 10 stars in my rating bar, i want to display rating bar stars in two line, as in single line it cut off.
Here is my layout.
<RatingBar
style="#style/starRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="10"
android:stepSize="1" />
Any help is appreciated.
As far as I know, there is no way to linebreak a single RatingBar, so here is what I suggest:
Make TWO RatingBars and set their OnRatingBarChangeListeners to interact with one another.
I wrote up a quick example to show you exactly what I mean:
activity_my.xml
This would be your layout file. I used the default name when creating a new project in AS.
Use RelativeLayout.
<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=".MyActivity">
<RatingBar
android:id="#+id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="10"
android:stepSize="1" />
<RatingBar
android:id="#+id/bar2"
android:layout_below="#id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="4"
android:stepSize="1" />
</RelativeLayout>
MyActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;
public class MyActivity extends Activity {
private RatingBar ratingBar1;
private RatingBar ratingBar2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
setOnChangeListeners();
}
public void setOnChangeListeners(){
ratingBar1 = (RatingBar) findViewById(R.id.bar1);
ratingBar2 = (RatingBar) findViewById(R.id.bar2);
ratingBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
ratingBar2.setRating(0);
ratingBar1.setRating(v);
}
});
ratingBar2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
ratingBar1.setRating(10);
ratingBar2.setRating(v);
}
});
}
}
As you can see: we use a helper function: setOnChangeListeners() to initialize ratingBar1 and ratingBar2. After that we set onRatingChanged for both bars. For the first line bar made of 10 stars, we make sure ratingBar2 is set to 0. For the second bar, if it is changed, ratingBar1 must be set to 10 so it will be full.
Try it out! Hope it works for you!
Rating bar can't break in two line so add rating bar layout dynamically
int noOfStarts = 8;
int noOfRatingLine = (noOfStarts/5)+1;
LinearLayout[] linear = new LinearLayout[noOfRatingLine];
RatingBar[] ratingBarNames = new RatingBar[noOfRatingLine];
for(int i =0;i<noOfRatingLine;i++)
{
linear[i] = (LinearLayout)this.getLayoutInflater().inflate(R.layout.app_ratingbar, null);
ratingBarNames[i] = (RatingBar)linear[i].findViewById(R.id.rating);
if(noOfStarts<5){
ratingBarNames[i].setNumStars(noOfStarts);
}
else{
ratingBarNames[i].setNumStars(5);
}
ratingBarNames[i].setStepSize(1);
noOfStarts = noOfStarts-5;
ratingbarLayout.addView(linear[i]);
}
XML layout app_ratingbar.xml for inflate
<RatingBar
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RatingBar>
I set an imageview to change my activity onClick, for some reason it crashes each time I click the imageview on my device. Any ideas? I feel as if this is a very general answer that I am missing. Also if you have any reading material you could link related to my mistake it would help me greatly. Thank you!
Main Activity
package com.gcomcode.oakgang;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
ImageView bioicon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView musicicon = (ImageView) findViewById(R.drawable.musicicon);
ImageView galleryicon = (ImageView) findViewById(R.drawable.gallery);
ImageView connecticon = (ImageView) findViewById (R.drawable.connecticon);
ImageView contacticon = (ImageView) findViewById (R.drawable.contacticon);
ImageView eventsicon = (ImageView) findViewById (R.drawable.eventsicon);
}
public void bioClick() {
bioicon = (ImageView) findViewById(R.drawable.bioicon);
bioicon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent bioIntent = new Intent(MainActivity.this, BioActivity.class);
startActivity(bioIntent);
}
});
}
}
and here is the corresponding layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context="com.gcomcode.oakgang.MainActivity"
tools:ignore="MergeRootFrame" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:src="#drawable/oakganglogo"
/>
<android.support.v7.widget.Space
android:id="#+id/space1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="144dp"
android:layout_marginTop="216dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp" >
<ImageView
android:id="#+id/bioicon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/bioicon"
android:onClick="bioClick" />
<android.support.v7.widget.Space
android:id="#+id/space3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/musicicon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/musicicon" />
<ImageView
android:id="#+id/galleryicon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/gallery" />
<android.support.v7.widget.Space
android:id="#+id/space2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_gravity="bottom" >
<ImageView
android:id="#+id/eventsicon"
android:layout_width="100dp"
android:layout_height="85dp"
android:src="#drawable/eventsicon" />
<ImageView
android:id="#+id/contacticon"
android:layout_width="100dp"
android:layout_height="85dp"
android:src="#drawable/contacticon" />
<ImageView
android:id="#+id/connecticon"
android:layout_width="100dp"
android:layout_height="85dp"
android:src="#drawable/connecticon" />
</LinearLayout>
This is the activity I am trying to open
package com.gcomcode.oakgang;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class BioActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bio_activity);
}
}
Change this:
public void bioClick() {
to this:
public void bioClick(View view) {
because in order for an onClick method to be called from the xml (layout file), that onClick method has to take in exactly one parameter, a View parameter. (The view that gets passed into the method is whichever view called it - whichever view was pressed. This is useful if you want more than one view to call the same onClick method, because then you can use the view variable to figure out which view called it, and respond accordingly.)
Now, another problem with your code:
If you make that change I wrote, then it will probably stop crashing, because now it will be able to find the right onclick method. But the first time you click on bioicon, it won't take you to a new activity. You will find that you have to click it TWICE to make it go to a new activity. That is because the first time you click it, it runs the bioClick method. But the only thing that method does is to change the onClick method for the ImageView from "bioClick" to the new internal method. If you want the activity to change the FIRST time you click the bioicon ImageView, change your method to look like this:
public void bioClick() {
Intent bioIntent = new Intent(MainActivity.this, BioActivity.class);
startActivity(bioIntent);
}
This part of the code:
bioicon = (ImageView) findViewById(R.drawable.bioicon);
bioicon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent bioIntent = new Intent(MainActivity.this, BioActivity.class);
startActivity(bioIntent);
}
});
is how you set an onClickListener programmatically, in code. If you set an onClickListener in your xml layout file (by using android:onClick="bioClick"), then you do not need to set your onClickListener programmatically as well. Do one or the other, but not both.
While I am at it, I will also point out that unless you have more code in your onCreate method of your first activity than you showed here, there is absolutely no point to doing this:
ImageView musicicon = (ImageView) findViewById(R.drawable.musicicon);
ImageView galleryicon = (ImageView) findViewById(R.drawable.gallery);
ImageView connecticon = (ImageView) findViewById (R.drawable.connecticon);
ImageView contacticon = (ImageView) findViewById (R.drawable.contacticon);
ImageView eventsicon = (ImageView) findViewById (R.drawable.eventsicon);
You only need to "get" your views into variables if you plan on doing something with them afterward. (Like setting Listeners on them, changing text, or whatever.)
I am making a really simple android application that will display a picture after a button is clicked. I have searched for days and tried everything I can think of but cant seem to get it to work. I have been able to host the picture online and link to it but I want the content available offline too. Please help, sorry for the stupid question.
Update:
It is a fixed image and in my drawable resource. Here is the current code I am using to display the image from a URL. What changes should I make to to display that same image from my drawable resource?
JAVA
public class StandingOrders extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void buttonClick (View image)
{
Uri uri = Uri.parse("my url");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
}
}
XML
<Button android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="170px"
android:text="The Button"
android:layout_gravity="center"
android:clickable="true"
android:onClick="buttonClick">
</Button>
If it's a fixed image, include it as a drawable resource and use an ImageView to display it. If it's an image to download, you can do that in a separate thread and store it in a file or an SQLite data base, then again use an ImageView to see it.
Add an ImageView to your xml layout and call setImageResource from onClick.
setImageResource(R.drawable.yourImage);
Or you could set the image in your layout and make the ImageView hidden until you click the button. See setVisibility(View.GONE)
Check out this answer: https://stackoverflow.com/a/4896272/458968
In your case, the uri should be
Uri.parse("android.resource://com.company.app/"+R.drawable.my_image);
You can try to implement the method, which I am posting below. I have done something more or less similar to your requirement.
Method:
//method to zoom images
public void zoomImage(String imageUrl)
{
AlertDialog.Builder builder;
Context mContext = ExamActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
//to close dialog
ImageView close_dialog = (ImageView) layout.findViewById(R.id.imageView_custom_dialog_close);
close_dialog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
alertDialog.dismiss();
}
});
//to show image
WebView wv=(WebView) layout.findViewById(R.id.show_image_webView);
wv.getSettings().setBuiltInZoomControls(true);
wv.setInitialScale(200);
wv.loadUrl(imageUrl);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
the imageUrl is the url to the webpage containing the image. If you want to show a local image then just create a simple html page containing the image and use the local url.
Try this:-
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageToDisplay = (ImageView) findViewById(R.id.imageToDisplay);
Button btnShowImage = (Button) findViewById(R.id.btnShowImage);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imageToDisplay.setVisibility(View.VISIBLE);
imageToDisplay.setImageResource(R.drawable.imagelogo);
}
});
}
}
In layout imageToDisplay's visibility is gone.
try following example
you can download "android-query-0.22.10.jar" file from following url.
http://www.java2s.com/Code/Jar/a/Downloadandroidquery02210jar.htm
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.addbuttondynamic.AddButtonDynamic" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Image" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
and Main.java
import com.androidquery.AQuery;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Main extends Activity {
Button btnLoad;
ImageView ivImage;
AQuery aQuery;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLoad=(Button)findViewById(R.id.button1);
ivImage=(ImageView)findViewById(R.id.imageView1);
aQuery=new AQuery(this);
btnLoad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//load image from /res folder.
ivImage.setImageResource(R.drawable.ic_launcher);
//load image from url.
aQuery.id(ivImage).image("you URL");
// if ivImage is not working in aQuery.id() then use R.id.imageView1.
}
});
}
}
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.imagechange.MainActivity"
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:text="#string/khilan"
android:onClick="btnOnClick"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="800px"
android:layout_height="500px"
android:layout_marginTop="70dp"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnOnClick (View view)
{
String imagename = "khilan";
int res = getResources().getIdentifier(imagename, "drawable", getPackageName());
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(res);
}
}
Create a layout with image and button, make the image hidden and onClick set the visibility to VISIBLE.
Here is what the XML should look like:
<?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">
<ImageView
android:id="#+id/image"
android:src="#drawable/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Image" />
</RelativeLayout>
and the following is the Java code
ImageView image = findViewById(R.id.image);
Button button = findViewById(R.id.got_it_button);
image.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image.setVisibility(View.VISIBLE);
}
});
Good luck :)