Android ViewPager not Showing - java

I am trying to make a little app to try out some things for a bigger app I am developing. I want to add a viewpager that displays four images, I developed this viewpager alone and it worked, but when I tried to add it to my other app (with just a couple of buttons) with the tag, it does not show anything and I am really stuck in here. Is possible to add the Viewpager this way? Honestly I don't have that much experience in Java, I usually program only C and Assembler and I will be reaaally glad if you could help me.
This is the xml file with the button and the include:
<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"
android:weightSum="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculo Rapido"
android:id="#+id/btcr"
android:layout_gravity="center_vertical"
android:layout_weight="0.16"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculo Avanzado"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_below="#+id/btcr" />
<include layout="#layout/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:translationZ="15dp" />
Now, the MainActivity, which holds the button activity because I kept the Viewpager inside an Activity that's linked with the viewpager original layout
Button siguiente;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
siguiente = (Button)findViewById(R.id.btcr);
siguiente.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent siguiente = new Intent(MainActivity.this, Main2Activity.class);
startActivity(siguiente);
}
});
}
And lastly, the code for the mainpager main activity, this uses a CustomPagerAdapter saved in another java class. As I told before, this viewpager does work alone, but when I try to implement it here, with the /include tag, it doesn't show anything.
public class MainActivity extends Activity {
CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;
private static int currentPage = 0;
private static int NUM_PAGES = 0;
Button siguiente;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// == Setting up the ViewPager ==
mCustomPagerAdapter = new CustomPagerAdapter(this);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mViewPager.setCurrentItem(currentPage++);
if (currentPage == 5) {
currentPage = 0;
mViewPager.setCurrentItem(currentPage++, false);
}
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 2000, 2000);
}
Sorry if it is too long guys, but I am a little frustrated, I have tried a lot of things but it isn't working yet. Either the buttons do their thing and the ViewPager doesn't work or the ViewPager works but the buttons are dead.
I will reaaally aprecciate your help :)

create a new xml file inside layout folder and name it secondactivity.xml
and put this code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pager">
</android.support.v4.view.ViewPager>
</LinearLayout>
Now inside your Main2Activity.class
change
setContentView(R.layout.activity_main);
to
setContentView(R.layout.secondactivity);
And remove the include from your previous xml

Turns out, what I needed was just the simple solution. I had to put the viewpager on my activity's layout file as another object, just configuring its size and position. And then, on my onCreate I pasted the code of my functioning ViewPager. Quite simple, without requiring another layouts :)

Related

Why SetText not work in OnClick() function

I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
Here is my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}

The activities in android studio don't switch after pressing the button

I've already tried searching the Google on how to switch activities in android studio, even in the official documentation with tutorial. I did it exactly like it was said but I am still unable to get redirected to another activity after clicking a button.
I've entered an onClick name of the method to the button
it looks like this: https://i.imgur.com/pmsztaL.png (can't post images yet)
and this is my MainActivity.class file with said method
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void handleButtonAddNew(View view) {
MainActivity.this.startActivity(new Intent(MainActivity.this, AddItemActivity.class));
}
}
After pressing the button in a phone, the button does nothing.
This is my AddItemActivity.class
public class AddItemActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
}
public void handleButtonRemember(View view) {
finish();
}
}
How come the button doesn't work, what did I do wrong?
EDITS
EDIT: Successfully ran emulator and the buttons did in fact work, the problem lies within my phone, the buttons there don't want to work. Where could be the issue now?
EDIT: XML Layout of MainActivity.class
<?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=".MainActivity">
<Button
android:id="#+id/buttonAddNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="188dp"
android:layout_marginRight="188dp"
android:layout_marginBottom="264dp"
android:text="#string/buttonAdd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="395dp"
android:layout_height="715dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</android.support.constraint.ConstraintLayout>
EDIT: The problem was with scroll view blocking the clickable button so on my phone the button didn't register any clicks, after removing the scrollview the button works normally.
I'm afraid that this button doesn't get any listeners attached.
The programatic approach
From the Official Android Documentation
I'd suggest you use this in your onCreate callback:
Button button = (Button) findViewById(R.id.your_btn_id);
button.setOnClickListener((view) -> { theMethodYouWantToCallHere(view); });
XML approach
Please double check the following using this approach
Your activity is registered in the AndroidManifest.xml
You're including the android namespace to the XML file in the parent container of the layout file. IE: xmlns:android="http://schemas.android.com/apk/res/android"
Make sure you're setting the content view for the activity with setContentView(R.layout.your_layout_file);
Once 2 is satisfied make sure you're using the android namespace. IE: android:onClick="yourMethod"
As you're currently doing: Make sure the onClick callbacked method has the View view parameter; no more, no less
Make sure the onClick function is public in the activity.
And that no other clickable views are covering the view you're registering the onClick listener for :)
Including the namespace: xmlns:android="http://schemas.android.com/apk/res/android"
XML file
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="Click me!" android:onClick="clicked" android:layout_height="wrap_content" android:layout_width="wrap_content"/>
</FrameLayout>
The activity
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clicked(View view){
Toast.makeText(this, "Hey! Im clicked!",Toast.LENGTH_SHORT).show();
}
}

Issues when relative layout implement swipe-to-refresh layout

I'm currently attempting to implement swipe-to-refresh layout into relative layout, but it is extremely insensitive and unstable. When I pull down the screen, it usually either doesn't refresh or it refreshes without progress bar.
<android.support.v4.widget.SwipeRefreshLayout
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.miaor.tutorialweather.MainActivity"
android:id="#+id/refreshLayout"
android:background="#fe970b">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
public class MainActivity extends AppCompatActivity{
public static final String TAG = MainActivity.class.getSimpleName();
#BindView(R.id.TimeString) TextView mTimeLabel;
#BindView(R.id.TemperatureLabel) TextView mTemperatureLabel;
#BindView(R.id.HumidityValue) TextView mHumidityValue;
#BindView(R.id.rainingChanceValue) TextView mChance;
#BindView(R.id.weatherIcon) ImageView mWeatherIcon;
#BindView(R.id.SummaryText) TextView mSummaryText;
#BindView(R.id.refreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
///why do we make a new variable of CurrentWeather
private CurrentWeather mCurrentWeather;
private String apiKey = "6b9448b8e21c2abe2fb623b25554a77c";
private double latitude = 31.230708;
private double longitude = 121.472916;
private String forecastUrl = "https://api.forecast.io/forecast/" + apiKey +
"/" + latitude + "," + longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
///implement swipe-to-refresh feature
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
Log.i(TAG, "onRefresh is working");
getForecast();
}
});
getForecast();
Log.d(TAG, "Main UI code is running!");
}
You can't implement SwipeToRefreshLayout with RelativeLayout as its child. You need to have a scrollable view as its only child. e.g. Listview, RecyclerView, ScrollView. That is the reason why it is not working as expected for you.
That is not the right position.
You need type like this:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/your_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
YOUR RELATIVE LAYOUT HERE
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Your code looks fine, it's weird that it's not working correctly... Maybe it's because of the ButterKnife library? I don't think that's the problem, but it will be worth the shot to get views manually to see if that's the problem
Then, if you need the loading animation to appear/hide you can use mSwipeRefreshLayout.setRefreshing(boolean);
For example in my app I attach the listener first (as you did in your example) and then I use the following code to show the loading animation while I fetch the data.
// Show loading while fetching the first set of data
mainSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mainSwipeRefreshLayout.setRefreshing(true);
}
});
In my fetchData() method I use the following line to hide the animation again when it finishes fetching the data.
// On load complete stop refresh animation
mainSwipeRefreshLayout.setRefreshing(false);
Hope this helps!
Andres.

OnClick doesn't work with LinearLayout

OnClick doesn't work. Nothing happens after clicking on layout. It seems like it is clickable, because layout changes its color, but new layout doesn't open.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/window"
android:layout_width="295dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/editborder"
android:clickable="true"
android:onClick="openBigImage">
Here is more code for Main Activity:
public class MyMapActivity extends FragmentActivity implements LocationListener
{
private Marker marker;
private Hashtable<String, String> markers;
private ImageLoader imageLoader;
private DisplayImageOptions options;
private GoogleMap map;
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);
// Look up the AdView as a resource and load a request.
//AdView adView = (AdView)this.findViewById(R.id.adView);
//adView.loadAd(new AdRequest());
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS)
{ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}
else
{// Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
if (savedInstanceState == null) {
// First incarnation of this activity.
mapFragment.setRetainInstance(true);
}
else
{
// Reincarnated activity. The obtained map is the same map instance in the previous
// activity life cycle. There is no need to reinitialize it.
map = mapFragment.getMap();
}
setUpMapIfNeeded();
}
}
#Override
protected void onResume()
{
super.onResume();
setUpMapIfNeeded();
}
public void openBigImage(View v)
{
setContentView(R.layout.bigpicture);
}
bigpicture.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bigpicture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical">
<fragment
android:id="#+id/minimap"
android:layout_width="200px"
android:layout_height="200px"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
class="com.google.android.gms.maps.SupportMapFragment" />
<ImageView
android:id="#+id/badge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:adjustViewBounds="true" />
</RelativeLayout>
Calling setContentView() multiple times worked in other cases, like menu items "about", "settings" etc.
Tried to make without setContentView. I've put new Layout to the main.xml and made visibility GONE. OnClick method should change visibility to visible, but again nothing happens.
Logcat says "11-25 13:47:28.638: D/GestureDetector(3156): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0" when i'm clicking on linear layout.
Paul,
One thing is close the linear layout with /> .I am assuming that you have followed the map tutorials link and passed all the manifest permissions and other requirements. You might have some reasons to use px. Check if map is being created. Also give some height and background color to your badge image and see if something happens.
I tested your code without map fragment and it worked fine.
Can you post the error log ?
Found. It is a click on InfoWindow, so we should implement onInfoWindowClick.
But first we must add map.setOnInfoWindowClickListener(this); in main activity. Main activity must implement OnInfoWindowClickListener.
I've added new LinearLayout to the main.xml, made it invisible.
Here's code for onInfoWindowClick:
#Override
public void onInfoWindowClick(Marker marker) {
LinearLayout secondLL = (LinearLayout) findViewById(R.id.bigpicture);
int visibility = secondLL.getVisibility();
if(visibility == View.GONE)
{
secondLL.setVisibility(View.VISIBLE);
}
}
I think you can't use onClick attribute.
You have to use setOnClickListener() like that :
LinearLayout layout = (LinearLayout )findViewById(R.id.window);
layout .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
YourActivity.this.setContentView(R.layout.bigpicture);
}
});

How to make an ImageView overlay another View and hide it after a certain period of time?

I'm making a very simple Andriod app and I was wondering if I could get some help with my app.
I would like to show an ImageView over the full ListView (including the action bar) for 3 seconds, and then remove the ImageView (or hide it, anything), to go back to the list view.
How can this be done? I have tried a few things but it either breaks my code or doesn't show anything at all.
Thanks in advance everyone - let me know if you need any further explanation.
EDIT: As per the question below, I'd like the ImageView to be shown as soon as the ListView is shown, for 3 seconds, then disappear.
Allright. What you want is actually quite easy.
Simply create a RelativeLayout that contains a ListView and an ImageView above it.
Then inside your onCreate(...) method, you use a Handler and set the Visibility of the ImageView to GONE after 3 seconds.
Here is the layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/your_image" />
</RelativeLayout>
And inside the onCreate(...) method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
final ImageView iv = (ImageView) findViewById(R.id.imageView1);
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
// EITHER HIDE IT IMMEDIATELY
iv.setVisibility(View.GONE);
// OR HIDE IT USING ANIMATION
hideImageAnimated(iv);
// DONT use both lines at the same time :)
}
}, 3000); // 3 seconds
}
In order to make things a bit smoother, you could use an AlphaAnimation on your ImageView:
public void hideImageAnimated(final ImageView iv) {
Animation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setDuration(1000); // whatever duration you want
// add AnimationListener
alpha.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationEnd(Animation arg0) {
iv.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation arg0) { }
#Override
public void onAnimationStart(Animation arg0) { }
});
iv.startAnimation(alpha);
}

Categories