Translate Animation is not working perfectly in dynamic view - android

In this project, I want to move TextView's clone from top TextView place to bottom TextView place when button is click.
I got a problem in using Translate Animation. I want to move a view to exact position(Like in Zapya).
In Zapya, when user select item and choose "send", the gridview item's clone move to user icon.
So, I use Translate Animation to move view. And I created a view dynamically to show item's clone.
Using Translate Animation works with view that is initially created in xml but not with view that is dynamically created.
The code is here
top = (TextView) findViewById(R.id.textviewtop);
bottom = (TextView) findViewById(R.id.textviewbottom);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int []from = new int[2];
int []to = new int[2];
from[0] = (int) top.getX();
from[1] = (int) top.getY();
to[0] = (int) bottom.getX();
to[1] = (int) bottom.getY();
ViewGroup vg = (ViewGroup) top.getParent();
view = new TextView(getApplicationContext());
view.setWidth(top.getWidth());
view.setHeight(top.getHeight());
view.setX(from[0]);
view.setY(from[1]);
view.setText(top.getText());
view.setVisibility(View.VISIBLE);
view.setTextColor(Color.BLACK);
view.setId(0x23454657);
vg.addView(view);
TranslateAnimation anim = new TranslateAnimation( 0, 0 , from[1], to[1] );
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
}
});
and xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textviewtop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Move"/>
<TextView
android:id="#+id/textviewbottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="#string/hello_world" />
</RelativeLayout>
"view" show only starting and ending time. But not in between. How can I solve that.
Please tell if there's any other way. I will thanks a ton.

Increase the duration Time, may be that is a problem
anim.setDuration(4000);

Related

ImageButton increase hit area

I'm new in Android development and I need to increase the hit area for two buttons grouped inside a LinearLayout
<LinearLayout
android:id="#+id/buttonsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp">
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/button_close_animation"
android:onClick="closeActivity"
android:layout_weight="1"/>
<ImageButton
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/sendbutton_inactive"
android:onClick="onSendMessage"
android:layout_weight="1"
android:layout_marginLeft="30sp"/>
</LinearLayout>
I want to increase the hit area for the back button in the left and for send button in the right.
I've tried by adding padding to each button but the image stretched and I don't want that... Also I've tried to use TouchDelegate starting with the back button
Rect delegateAreaBack = new Rect();
ImageButton delegateBack = (ImageButton) findViewById(R.id.backButton);
delegateBack.getHitRect(delegateAreaBack);
delegateAreaBack.left -= 2600;
TouchDelegate expandedAreaBack = new TouchDelegate(delegateAreaBack, delegateBack);
if(View.class.isInstance(delegateBack.getParent()))
((View) delegateBack.getParent()).setTouchDelegate(expandedAreaBack);
but the hit area didn't increased... What am I doing wrong?
ImageButton button = (ImageButton) findViewById(R.id.backButton);
button .post( new Runnable() {
public void run() {
final Rect rect = new Rect();
button.getHitRect(rect);
rect.top -= 100; // increase top hit area
rect.left -= 100; // increase left hit area
rect.bottom += 100; // increase bottom hit area
rect.right += 100; // increase right hit area
button .setTouchDelegate( new TouchDelegate( rect , button));
}
});
Dana, it seems like you need to increase the size of the LinearLayout.
Here is how it looked like: https://image.prntscr.com/image/mG6IHwS4Tw25kb_6CY5Tqw.png
Here is how I made it look like https://image.prntscr.com/image/v8Hl0CrJQ3qwZOQoZMdvLg.png by changing the height to 200dp for experiment purposes.
So in all here is how made your XML look like:
<LinearLayout
android:id="#+id/buttonsLayout"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:paddingTop="5sp"
android:layout_marginTop="152dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="114dp"
android:layout_marginStart="114dp">
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#mipmap/ic_launcher"
android:onClick="closeActivity"
android:layout_weight="1"/>
<ImageButton
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#mipmap/ic_launcher"
android:onClick="onSendMessage"
android:layout_weight="1"
android:layout_marginLeft="30sp"/>
</LinearLayout>
Afterwards, I used this simple code to experiment a bit with the problem and if you notice, the clickable area beneath the send button has increased.
public class MainActivity extends AppCompatActivity {
ImageButton button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (ImageButton) findViewById(R.id.backButton);
button2 = (ImageButton) findViewById(R.id.sendButton);
View parent = findViewById(R.id.root); //Your Layout ID
parent.post(new Runnable() {
#Override
public void run() {
Rect delegateArea = new Rect();
ImageButton delegate = button2;
delegate.getHitRect(delegateArea);
delegateArea.bottom += 260;
TouchDelegate expandedArea = new TouchDelegate(delegateArea, delegate);
// give the delegate to an ancestor of the view we're
// delegating the
// area to
if (View.class.isInstance(delegate.getParent())) {
((View) delegate.getParent())
.setTouchDelegate(expandedArea);
}
}
});
}
}
Now if you notice on my second screenshot, I have increased the gap of the liner layout on the bottom and on the top therefore that means if you want this to work you need to increase the area of the layout around the buttons.
In all I did these changes to your code just to experiment with what works and which direction you should take to solve your problem. Hope this will help you.
Credit goes to #Strider as well from his post on Increase the clickable area of the button

Translate Animation incorrect view move

I have 3 view objects(essentially ImageView's) with in a LinearLayout that in turn is within a RelativeLayout , The views are aligned within the LinearLayout as follows:
1 - 2 - 3 - -
The 3 views are wrapped under the Linear layout as demonstrated above.
Basically the idea is to get the side views(1st and 3rd views) move with animation to the place of the middle view onClick() .(so if 3rd view is clicked it should move to middle view's place and the middle moves right to the 3rd view's place.)
I implemented the translate animation as such that the 3rd view when clicked over 100dp to left and 2nd view moves to right, happens fine, but when I apply the translate animation on the onClick() of 1st view to make it move to right and 2nd to move to left then the 2nd and 3rd views both move collectively!! , This means the views are mapped next to each other automatically by the Android!!(note: that's why I used a LinearLayout so that alignment problems don't occur like 2nd view is aligned_below 1st view and aligned_left of 3rd view problems. )
also another problem is that though the views make a move land on another position that was originally belonging to the other view before the animated movement they still maintain the original view ID ?? For.Example that when 3rd view moves left to 2nd views place and 2nd mvoes to 3rd views place the onClick() still is mapped to their original location's?
XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
<RelativeLayout
android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#138AF8" >
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#138AF8" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/cost" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/cost" />
<ImageView
android:id="#+id/ImageView02"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/cost" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Those 3 ImageViews with the names of ImageView1, ImageView01, ImageView02 are the one's I'm trying to animate.
Animation code:
final TranslateAnimation moveLefttoRight = new TranslateAnimation(0,
100, 0, 0);
final TranslateAnimation moveRightToLeft = new TranslateAnimation(0,
-100, 0, 0);
moveRightToLeft.setDuration(1000);
moveLefttoRight.setDuration(1000);
moveRightToLeft.setFillAfter(true);
moveLefttoRight.setFillAfter(true);
final ImageView image1 = (ImageView) findViewById(R.id.imageView1);
final ImageView image2 = (ImageView) findViewById(R.id.ImageView01);
final ImageView image3 = (ImageView) findViewById(R.id.ImageView02);
image1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(getApplicationContext(), "image1 clicked",
Toast.LENGTH_SHORT).show();
}
});
image2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(getApplicationContext(), "image2 clicked",
Toast.LENGTH_SHORT).show();
image2.startAnimation(moveLefttoRight);
image1.startAnimation(moveRightToLeft);
}
});
image3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(getApplicationContext(), "image3 clicked",
Toast.LENGTH_SHORT).show();
image2.startAnimation(moveLefttoRight);
image3.startAnimation(moveRightToLeft);
}
});
Translate animation doesn't actually move the view, it just makes it look like that. You have to set the params before the end of the animation so that the button moves as well.example
with the other issue can you post your xml layout file? do you use weights in the imageviews?
I ended up using ObjectAnimator builtin animating API and this moves the view's position permanently to the co-ords. requested.

Android - TextSwitcher - Change TextView

I have an existing TextView (in code it's name - quote) and I want to change it's text with fancy animation.
It seems the only way to create an text-changing animation is to use TextSwitcher.
I try to use this code:
quoteSwitcher = (TextSwitcher)findViewById(R.id.quote_switcher);
quoteSwitcher.addView(quote);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
quoteSwitcher.setInAnimation(in);
quoteSwitcher.setOutAnimation(out);
quoteSwitcher.setText("Example text");
And this code throws an exception:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
What can I do? I just want to change TextView text with animation.
Full code:
protected void initWidgets() {
quote = (TextView)findViewById(R.id.quote); // Афоризм
/* Начальное значение афоризма */
quote.setText(getRandomQuote());
/* Плавная анимация смены афоризмов */
quoteSwitcher = (TextSwitcher)findViewById(R.id.quote_switcher);
quoteSwitcher.removeAllViewsInLayout();
quoteSwitcher.addView(quote);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
quoteSwitcher.setInAnimation(in);
quoteSwitcher.setOutAnimation(out);
quoteSwitcher.setText(getRandomQuote());
}
XML:
<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" tools:context=".MainActivity"
android:orientation="vertical"
android:focusableInTouchMode="false"
android:id="#+id/main_layout">
<TextSwitcher
android:id="#+id/quote_switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextSwitcher>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/logotype_layout">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/logotype"
android:layout_gravity="center_horizontal"
android:src="#drawable/logotype"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/main_logotext"
android:id="#+id/logotext"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="55sp" />
<TextView
android:fontFamily="sans-serif-thin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/main_quote_0"
android:id="#+id/quote"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textStyle="italic" />
Your problem is that the quote TextView already has the parent i.e. LinearLayout.
You can try setting the custom Factory to your TextSwitcher
quoteSwitcher.setFactory(new ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
myText.setTextColor(Color.BLUE);
return myText;
}
});
and you can remove the quote TextView from the xml file and also remove quoteSwitcher.addView(quote);. For more detail check this blog.
the problem in your code is that you are adding a textview from your xml which already have a parent view. Use the following code. In it i am creating a textview at runtime with no existing parent and adding it to textWatcher which will solve your issue.
mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
mSwitcher.setFactory(new ViewFactory() {
public View makeView() {
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
myText.setTextColor(Color.BLUE);
return myText;
}
});
// Declare the in and out animations and initialize them
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
// set the animation type of textSwitcher
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);

On animation, part of listview is not showing

I have researched the questions thoroughly, but could not yet find the answer. Also, my excuses for my poor english since I am not a native speaker.
The problem: in my android layout we have a status_text with a listview below the status_text. When the status_text is touched, we animate a 'move down' on the status_text and listview so that only the first of the listview row is still on screen. The listview is now still usable.
When the status_text is touched again, we move the status_text and listview up so that the listview uses half of the screen.
The problem we are facing is that during the 'move up' only the first row is animated. After the 'move up' the other rows suddenly appear.
What we would like to have is a 'move up' where the previously hidden rows slide onto the screen.
The layout:
We are using this layout (slightly simplified to focus on the problem at hand):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_declareren_choose_verzekerden"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Dummy anchor to put top of listview in the middle of the screen -->
<RelativeLayout
android:id="#+id/anchor"
style="#style/anchor_status_container"
android:layout_centerVertical="true" >
</RelativeLayout>
<!-- Example image -->
<ImageView
android:id="#+id/my_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footer"
android:adjustViewBounds="true"
android:contentDescription="#string/image_description"
android:scaleType="centerCrop"
android:src="#drawable/empty" />
<!-- Clickable text which moves up and down on click -->
<RelativeLayout
android:id="#+id/status_container"
style="#style/status_container"
android:layout_alignTop="#id/anchor"
android:background="#color/white" >
<TextView
android:id="#+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/spacing_sml"
android:layout_alignTop="#id/status_container" />
</RelativeLayout>
<!-- Listview which moves up and down with the status_container -->
<RelativeLayout
android:id="#+id/listView_container"
style="#style/padding_content_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/status_container"
android:background="#color/white" >
<ListView
android:id="#+id/mylistView"
style="#style/myListviewStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:divider="#null"
android:dividerHeight="0dp" />
</RelativeLayout>
<!-- Footer with buttons -->
<RelativeLayout
android:id="#+id/footer_button_container"
style="#style/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button_again"
style="#style/btn_secondary"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/opnieuw"
android:visibility="gone" />
<Button
android:id="#+id/button_next"
style="#style/btn_primary"
android:layout_width="wrap_content" />
</RelativeLayout>
</RelativeLayout>
And the code (again a bit simplified to show only the problem at hand. Some fade-in/out and rotations are removed):
// The code
#Override
public void onClick(View view)
{
int viewId = view.getId();
if (viewId == R.id.status_container)
{
// Someone clicked the text, move the statusbar (and so the listview) up or down
if (this.viewIsInUpperPosition)
{
startStatusAnimation();
}
}
}
private void startStatusAnimation()
{
if (animationIsRunning)
{
return;
}
setAnimationIsRunning(animValues.START);
// 0. Initialisation
final View statusContainer = (View) getView().findViewById(R.id.status_container);
final View listContainer = (View) getView().findViewById(R.id.listView_container);
final ListView listView = (ListView) getView().findViewById(R.id.myListView);
final View footerButtonContainer = (View) getView().findViewById(R.id.footer_button_container);
// 1. Calculate distance for animation
if (toggleViewDistance == 0)
{
int listViewContainerHeight = listContainer.getHeight();
int footerHeight = footerButtonContainer.getHeight();
int spaceForListView = listViewContainerHeight - footerHeight;
toggleViewDistance = spaceForListView;
}
// 2. Decide if the movement is up or down
float translationDistance = (viewIsInUpperPosition) ? toggleViewDistance : 0 - toggleViewDistance;
// 3. Create the animation
TranslateAnimation yMove = new TranslateAnimation(0, 0, 0, translationDistance);
yMove.setDuration(animValues.ANIMATION_Y_DURATION);
yMove.setInterpolator(new AccelerateDecelerateInterpolator());
// Do here something with scaling and rotating of other objects, not relevant for the question on StackOverflow
// 4. Actions after animation
yMove.setAnimationListener(new AnimationListener()
{
#Override
public void onAnimationEnd(Animation arg0)
{
// Fade de listView in als je van onderen naar boven animeert
if (!viewIsInUpperPosition)
{
// Do some fading, outside scope of question
}
// Create layout after the animation
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) statusContainer.getLayoutParams();
if (viewIsInUpperPosition)
{
// View was previously in upper position, now put the statusbar aligned with the footer
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ABOVE, footerButtonContainer.getId());
}
else
{
// View was previously in bottom position, so put it under the anchor
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_TOP, R.id.anchor);
}
}
statusContainer.setLayoutParams(params); // Set the new layout params
viewIsInUpperPosition = !viewIsInUpperPosition;
setAnimationIsRunning(animValues.END);
}
#Override
public void onAnimationRepeat(Animation arg0)
{
// Empty
}
#Override
public void onAnimationStart(Animation arg0)
{
// empty
}
});
// 5. Start the animation
statusContainer.startAnimation(yMove);
listContainer.startAnimation(yMove);
}
Any advice on how to have the rows of the listview 'slide in' on the screen? Much appreciated!
I figured it out. So I am answering my own question in case someone stumbles upon this question.
What needs to be done is that the listview is drawn off-screen. This can be forced by calling the measure- and layout-methods with the off-screen coordinates of the listview.
This fixed it for my code:
// 5a. Draw the listview off-screen
if (translationDistance < 0)
{
// Do this only when the listview is sliding up, e.g. sliding the window in.
int listViewContainerVerticalPos = listContainer.getTop(); // De positie van de listview
// The required height of the listview
int listContainerHeight = (int) Math.abs(translationDistance) + statusContainer.getHeight();
int measureWidth = View.MeasureSpec.makeMeasureSpec(listContainer.getWidth(), View.MeasureSpec.EXACTLY);
int measureHight = View.MeasureSpec.makeMeasureSpec(listContainerHeight, View.MeasureSpec.EXACTLY);
listContainer.measure(measureWidth, measureHight);
listContainer.layout(0, listContainerVerticalPos, listContainer.getMeasuredWidth(), listContainerVerticalPos
+ listContainerHeight);
}

How to dynamically add a button to a popup window?

I'm trying to write a function that opens a popup window, and populates it with button links to all of the previous pictures or recorded audio files the app has recorded. I tried to write the function so that it will work for audio files or images. The goal is to have a pop-up window appear on the screen which has a button for each of the previous files and will open the designated file on click. The app will successfully open the popup window, but it will only be populated by the auto-defined button (back) which will close the popup window. However, white space appears underneath the back button, which increases the height of the scroll view and allows for scrolling within the pop-up window but no buttons actually appear.
Initially, my problems came from not including pw.getContentView() (pw is my popupwindow) for each of my layout elements, but I'm not sure what it does exactly so that may be a part of the problem.
This is the function that is called to open the popup window.
private void display_media(int check, String header_text){
//The check variable is a 1 or 0 depending on if it's an image or an audio.
// header_text is a simply text to replace the default header.
try{
LayoutInflater inflater = (LayoutInflater) NewPlan_Two.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.files_layout, (ViewGroup) findViewById(R.id.show_media));
pw = new PopupWindow(layout,width/4,height/3,true);//width and height are predefined values
Button close_button = (Button) layout.findViewById(R.id.back_scroll);
close_button.setOnClickListener(close_view);
TextView header =(TextView) pw.getContentView().findViewById(R.id.previous_files);
header.setText(header_text);
LinearLayout l_l = (LinearLayout) pw.getContentView().findViewById(R.id.scroll_linear);
for(String media_file: files){ // this is defined and explained below
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button new_button = new Button(this);
if(check==0){
new_button.setId(image_id_count);
image_id_count++; // this is initialized to 3000
button_id = new_button.getId();
}
else{
new_button.setId(audio_id_count);
audio_id_count++; // this is initialized to 2000
button_id = new_button.getId();
}
new_button.setText(media_file);
l_l.addView(new_button,params);
if(check==0)
button_two= (Button) pw.getContentView().findViewById(button_id);
else
button_two = (Button) pw.getContentView().findViewById(button_id);
button_two.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
pw.dismiss();
// this will be replaced with code to open the image or audio, but I haven't written it yet.
}
});
}
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}catch(Exception e){
e.printStackTrace();
}
}
To parse the audio or image files, I use this function. It is successfully getting the names of the files. It is called with either "mp4" or "png" as the argument.
private void parse_Files(String fileType){
files = new ArrayList<String>();
File folder = new File(abs_path); // abs_path is the path to the folder with the files
File[] list_of_files = folder.listFiles();
for(int count = 0; count < list_of_files.length;count++){
String file_name = list_of_files[count].getName();
String[] parsed_list = file_name.split("\\.");
if(parsed_list[1].equals(fileType))
files.add(parsed_list[0]);
}
}
Finally here is the xml file that opens as the popup window.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/show_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding = "15dp"
android:background="#DCDDF7">
<TextView
android:id="#+id/previous_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/previous_files"
android:textSize="20sp"/>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:background="#FFFFFF" >
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scroll_linear">
<Button
android:id="#+id/back_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="18sp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Thanks!
Edit:
I tried to change it to a list view, but I am getting the same issue. It opens a pop-up and there are x buttons corresponding to x associated files, but none of the buttons have text or do anything on click. What am I missing?
Here is the revised code:
private void display_media(int check, String header_text){
try{
display_media_array=new String[files.size()];
display_media_array=files.toArray(display_media_array);
LayoutInflater inflater = (LayoutInflater) NewPlan_Two.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.files_layout, null);
pw = new PopupWindow(layout,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
ListView listView = (ListView) layout.findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, display_media_array));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
pw.dismiss();
}
});
Button close_button = (Button) layout.findViewById(R.id.back_scroll);
close_button.setOnClickListener(close_view);
TextView header =(TextView) pw.getContentView().findViewById(R.id.previous_files);
header.setText(header_text);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}catch(Exception e){
e.printStackTrace();
}
}
And here is the new xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/show_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DCDDF7"
android:orientation="vertical"
android:padding="15dp" >
<TextView
android:id="#+id/previous_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/previous_files"
android:textSize="20sp" />
<ListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</ListView>
<Button
android:id="#+id/back_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="18sp" />
</LinearLayout>
Any help is appreciated, thanks!

Categories

Resources