Make image height continually increase when screen is touched - android

I have an image within a relative layout and a linear layout below the image. This image is transparent so it looks like nothing is there.
My code adds 3 text views to the linear layout.
I have it so that when I move my finger on the screen it enlarges height of the image, thus giving the impression that the linear layout has moved down the screen.
I want to get it so that when the screen is being touched, so even if the finder is still, the image gets bigger, making it look like the image is moving down the screen. When the finger is removed the image stops enlarging.
I've been trying to create a thread to run onTouchEvent for about 2 weeks now, but my code just seems to crash the app.
Could someone amend my code / add this functionality so I can see where I'm going wrong. I'm hoping I'm just not putting the thread in the correct location or i'm not stopping the thread correctly which is causing it to crash.
My Layout
<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"
android:orientation="vertical"
android:id="#+id/LinearLayout"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/Spacer"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="Spacer"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/Letters"
android:orientation="horizontal"
android:layout_below="#id/Spacer"
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" >
</LinearLayout>
</RelativeLayout>
My Activity
package com.example.zz;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
protected int splashTime = 3000;
TextView tv1;
ImageView spacer;
String[] name = {"Hello","Rich","You","Hero"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] letters = {"A","B","C"};
String letter = "";
String nextLetters = "";
for( int i = 0; i < letters.length; i++)
{
letter = letters[i];
nextLetters = nextLetters + letter;
// Create a textView in code
LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);
View linearLayout = findViewById(R.id.Letters);
TextView tv1 = new TextView(this);
tv1.setText(letter);
tv1.setLayoutParams(paramsExample);
tv1.setGravity(Gravity.CENTER);
((LinearLayout) linearLayout).addView(tv1);
}
}
#Override
public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
spacer = (ImageView) findViewById(R.id.Spacer);
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)spacer.getLayoutParams();
head_params.height = 400;
spacer.setLayoutParams(head_params);
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I am just using the launch icon as the image.
Any help would easy my hair loss, thanks in advance.

You can set up a boolean that is false. If ACTION_MOVE is called you set it true and when ACTION_UP is called you set it false again.
Then you call run every x milliseconds by using the handler
implement runnable and assign the handler
private Handler handler = new Handler();
In on create:
handler.postDelayed(this,TIME);
Then call run:
#Override
public void run(){
if (yourboolean == true){
spacer = (ImageView) findViewById(R.id.Spacer);
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)spacer.getLayoutParams();
head_params.height += 100;
spacer.setLayoutParams(head_params);
}
handler.postDelayed(this,TIME);
}

Related

is there a way to dynamicaly add a widget on top of another widget

i'm trying to create a widget called DecoView dynamicaly every time i press a button.
if i just put two deco views in the XML i can see them both and they both look fine on ontop of the other.
but when i try to add it dynamicly in the code, i only create the first one,
and all the rest just aren't get created
what am i missing here ?
can someone help me with this ?
my mainActivity.java
package com.example.shay_v.dynamicdecoviewexample;
import android.graphics.Color;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.events.DecoEvent;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button mainMenuButton;
int widgetInteger = 1;
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//pointing to display
mainMenuButton = (Button) findViewById(R.id.button);
mainMenuButton.setOnClickListener(this);
//points to the linear layout in the xml
ll = (LinearLayout)findViewById(R.id.mainMenu_mainLayout);
}
private void createDecoViewWidget (int i) {
//adds params to the linear layout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//deco view widget
DecoView decoViewWidget = new DecoView(this);
//adding to view
decoViewWidget.setId(i);
ll.addView(decoViewWidget, params);
//decoViewWidget.configureAngles((int) (Math.random() * 360) + 1, (int) (Math.random() * 100));
//Create data series track
SeriesItem seriesItem = new SeriesItem.Builder(Color.argb(255, (int) (Math.random()*255), (int) (Math.random()*255), (int) (Math.random()*255)))
//third controller is end point
.setRange(0, 100, 0)
.setLineWidth(60f)
.setInset(new PointF(120f, 120f))
.build();
int series1Index = decoViewWidget.addSeries(seriesItem);
decoViewWidget.addEvent(new DecoEvent.Builder((float) (Math.random() * 100)).setIndex(series1Index).setDelay(1000).build());
}
//button listener
#Override
public void onClick(View v) {
createDecoViewWidget (widgetInteger);
widgetInteger++;
}
}
my activity_main.xml >
<?xml version="1.0" encoding="utf-8"?>
<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.shay_v.dynamicdecoviewexample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<LinearLayout
android:orientation="vertical"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_centerVertical="true"
android:id="#+id/mainMenu_mainLayout"
android:layout_centerHorizontal="true"></LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
anyone ?
When you are creating the Views they are being added into a LinearLayout. This type of layout will position the views one after the other, so the second view is being drawn off the visible area of the screen.
As you want all views to be drawn on top of each other you should use a RelativeLayout.

How to fix blurry imageView in Android app?

I have an activity, which only has an image. Which is way too blurry. How do I fix its blur and get it back sharp? I have not added any special things to it. And I actually want the image to cover at least half the screen. It is just the image which has become blurred and I can't figure out how to fix it since 5 hours.
My SorryAnim.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:background="#0C090A"
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.dhairyadev.sorry.SorryAnim" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scale="centerCrop"
android:src="#drawable/pls" />
My SorryAnim.java:
package com.dhairyadev.sorry;
import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class SorryAnim extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sorry_anim);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sorry_anim, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.about_dev:
Intent intent = new Intent (getApplicationContext(), AboutDev.class);
startActivity (intent);
}
return false;
}
}
Looking for helpful answer ASAP. :)
The only thing that may make it blure is that you are stretching it to match_parent
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Or the image is assets is just blurred.
Try to remove the scaling and see if it is changing anything.
Your ImageView height and width are set to match_parent. Unless your drawable happens to be exactly the size of the screen, it will be stretched, i.e. interpolated. Try changing height and width to wrap_content to see the image in it's original form.

Android - Show image from res/drawable onClick

First of all, I wanna say I've been seeking for an answer on the Forum and I found didn't match for what I wanted. Basically, what I want is: when the user clicks on one of the images previously "specified" on the .xml file, a new image is displayed on the center of the screen that is not "specified" on the .xml file. I wrote "specified" cause idk if it's the correct way to refer to this.
EDIT: there was no need to not specify the image previously, all I needed was to set "gone" for visibiity. This code is working exactly how I wanted (ty guys):
Main.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class Principal extends Activity {
ImageView cuia1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
cuia1 = (ImageView) findViewById(R.id.cuia1);
cuia1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ImageView cuia1grande = (ImageView) findViewById(R.id.cuia1grande);
cuia1grande.setVisibility(1);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.principal, menu);
return true;
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
android:id="#+id/tabelaCuias"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center">
<TextView
android:id="#+id/selecionaCuia"
android:text="Selecione a cuia"
android:textStyle="bold">
</TextView>
<ImageView
android:id="#+id/cuia1"
android:src="#drawable/cuia1">
</ImageView>
<ImageView
android:id="#+id/cuia2"
android:src="#drawable/cuia2">
</ImageView>
<ImageView
android:id="#+id/cuia3"
android:src="#drawable/cuia3">
</ImageView>
<ImageView
android:id="#+id/cuia4"
android:src="#drawable/cuia4">
</ImageView>
</TableRow>
</TableLayout>
<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:visibility="gone"
android:id="#+id/cuia1grande"
android:src="#drawable/cuia1grande">
</ImageView>
Is there any reason you don't want to "specify" the image in your layout file? You could place it there and not display it (visibilty="gone"), and then show/hide it when you deem fit.
Here's what I'd do:
Make your layout a RelativeLayout instead of a TableLayout (this will make things easier for showing the image in the center)
Place your TableLayout within the wrapping RelativeLayout
Define an ImageView as the last child within the wrapping RelativeLayout, set centerInParent="true", visibilty="gone"
In your onClick method, simply set its visibility as visible.
If you really don't want to define the ImageView in the layout, then you can create it programmatically:
Follow the same steps 1-2 as before
Capture the reference to the wrapping RelativeLayout in the code
In the onClick method, create the ImageView programatically, specifying the centerInParent="true" via the code (let me know if you want an example on how to do this & I'll edit the answer with a code sample).
Add the new view to the RelativeLayout via myRelativeLayout.addView(myImageView);
Hope this helps :)
public class Principal extends Activity {
ImageView cuia1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
cuia1 = (ImageView) findViewById(R.id.cuia1);
//set invisible
cuia1 .setVisibility(View.INVISIBLE);
cuia1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//show image on the center of screen
//set image
cuia1.setImageResource(R.drawable.cuia1);
// set visible
cuia1 .setVisibility(View.VISIBLE);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.principal, menu);
return true;
}
}
import import android.view.View;
Cheerz!

ScrollView with two views, first view filling screen

I would like to have a ScrollView with a LinearLayout containing two views. The first view should occupy the full screen, with the second view initially off the screen (but it can be scrolled to). Is this achievable?
use this code
<ScrollView 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:fillViewport="true"
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=".MainActivity" >
<LinearLayout
android:id="#+id/linlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/f1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#468432" >
</FrameLayout>
<FrameLayout
android:id="#+id/f2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ff8222" >
</FrameLayout>
</LinearLayout>
</ScrollView>
and your activity gose like following
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
LinearLayout layout ;
FrameLayout f1,f2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout)findViewById(R.id.linlayout);
f1=(FrameLayout) findViewById(R.id.f1);
f2=(FrameLayout) findViewById(R.id.f2);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
f1.setLayoutParams(new LayoutParams(width, height));
f2.setLayoutParams(new LayoutParams(width, height));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You don't need a ScrollView, a ViewPager is more suited to your needs. Also, since you want it to be vertical, you should check this out: Directional ViewPager
I had the hardest time once trying to achieve this in the iphone, maybe the ideas will help you. What I had was a background view with a notebook style set of lines, and a view on top of that with text, and of course they had to scroll together. I added both to the ScrollView and just intercepted the "scrollViewDidScroll" for view1 and did a view2.contentOffset=view2.contentOffset so that they stayed 'glued'. i just started on Android so I wouldn't know how to exactly achieve this there, but I think the principle wouldn't be much different. Hope this helps :)

findViewById still return null (newbie)

Hello, i'm new to the android(also java) so i try to create some easy app. This app will have one base layout from xml and the dynamic layout which will be create from code and will be insert into that base layout. But when i use the findViewbyId it won't work, it return as null.
After that i try to insert "setContentView" before trying findViewbyId and also clean project... and it still got NULL ..
Here are my code..
package com.app.something;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
LinearLayout lLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
GridLayout grid = new GridLayout(this);
grid.setColumnCount(5);
for (int i = 0;i<20;i++){
ImageButton ib = new ImageButton(this);
ib.setImageResource(R.drawable.ic_launcher);
ib.setId(i+1);
final int index = i;
ib.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("TAG","The index is" + index);
}
});
grid.addView(ib);
}
lLayout = (LinearLayout)findViewById(R.layout.activity_main);
if(lLayout == null)Log.i("TAG", "It said, NULLL");
else {
lLayout.addView(grid);
setContentView(lLayout);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and this is the base layout activity_main (blank)
<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"
android:background="#ffffff00"
tools:context=".MainActivity" >
</RelativeLayout>
Firt you are trying to cast a RelativeLayout to a LinearLayout. Second you missed the id attribute for your LinearLayout
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#ffffff00"
android:id="#+id/myId"
tools:context=".MainActivity" >
</LinearLayout>
after fix that you can retrieve your layout this way:
lLayout = (LinearLayout)findViewById(R.id.myId);
change
lLayout = (LinearLayout)findViewById(R.layout.activity_main);
to
RelativeLayout lLayout = (RelativeLayout)findViewById(R.id.activity_main);
because activity_main layout conatin RelativeLayout layout as root View instead of LinearLayout and you also need to define android:id for RelativeLayout layout.
or you can do it as using LayoutInflater :
View lLayout = getLayoutInflater().inflate(R.layout.activity_main);
if(lLayout == null)
Log.i("TAG", "It said, NULLL");
else {
lLayout.addView(grid);
setContentView(lLayout);
}
lLayout = (LinearLayout)findViewById(R.layout.activity_main);
1.you have to pass ID for that view, add android:id attribute to relative layout.
2.you dont have any linear layout in your code
3.you have a relative layout cast this to RelativeLayout
you can not use
lLayout = (LinearLayout)findViewById(R.layout.activity_main);
as per the method name it finds the view By id of the view, not the resource id like you are using. you can access a view by an id that is set by using
android:id="#+id/someid"
then from code
lLayout = (LinearLayout)findViewById(R.id.someid);
findViewById searches in your layout, so e.g.:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout_main"
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"
android:background="#ffffff00"
tools:context=".MainActivity" >
</RelativeLayout>
The you can use it:
(RelativeLayout) findViewById(R.id.layout_main);

Categories

Resources