Making imagebuttons glow on touch in android - java

I am making a launcher for android, where it is only usable in landscape. Half of the screen is buttons, not apps, but buttons, and half is simply some clocks and stuff. I want my buttons to glow for like for a second or something once i touch them, which is followed by an animation to show a different screen. I only need help with the glow. Please help me and thanks so much!
This is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</LinearLayout>
<GridView
android:id="#+id/gridView1"
android:layout_width="284dp"
android:layout_height="wrap_content"
android:numColumns="3" >
</GridView>
I would also like to know how i can add the imagebuttons to my gridview
my java code:
package com.mysoftware.mysoftwareos.launcher;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
LinearLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Import views
mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
//Setup onClickListener for the buttons
}
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event) {
if ((KeyCode == KeyEvent.KEYCODE_BACK)) {
//Do nothing to prevent the back button to kill the launcher
return true;
}
return super.onKeyDown(KeyCode, event);
}
public void onClick(View src) {
switch(src.getId()) {
}
}
}

Related

How can I make Floating Action Button rotate on every click

I want the fab to rotate each time i click on it. But after trying, i achieved the rotation effect. But the problem i'm having is that it only work once. i.e on app startup.
Please help, i'm a beginner in this.
Below is the complete code i used.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:orientation="horizontal"
tools:context=".MainActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:backgroundTint="#color/design_default_color_background"
app:fabCradleMargin="15dp"
app:menu="#menu/app_bar_layout"
app:fabCradleVerticalOffset="20dp"
app:fabCradleRoundedCornerRadius="40dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_generate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#1D6837"
android:src="#drawable/generate"
app:layout_anchor="#id/bottom_app_bar"
android:contentDescription="generate"
android:clickable="true"
app:fabCustomSize="80dp"
app:maxImageSize="45dp"
android:layout_marginBottom="20dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import static androidx.core.view.ViewCompat.animate;
public class MainActivity extends AppCompatActivity {
private FloatingActionButton generateFab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateFab = findViewById(R.id.fab_generate);
generateFab.setOnClickListener ( new View.OnClickListener() {
#Override
public void onClick(View v) {
final OvershootInterpolator interpolator = new OvershootInterpolator();
animate(generateFab).
setInterpolator(interpolator).
setListener(null).
rotation(360f).
withLayer().
setDuration(300).
withStartAction(null).
start();
}
});
}
}
Basically, I added onClickListener to my floating action button inside the onCreate method
Try this:
animate(generateFab).
setInterpolator(interpolator).
setListener(null).
rotation(generateFab.getRotation() + 360f).
withLayer().
setDuration(300).
withStartAction(null).
start();
Notice the line rotation(generateFab.getRotation() + 360f).
You rotate the view 360 degrees on the first click so this changes the view elements rotation attribute to 360, and because its already 360 (after the first rotation) it wont rotate again. So instead you want to keep adding 360 degrees to the current views rotation and it will work.
Update:
As per comment by #AHoneyBustard you could instead of rotation(...) use:
rotationBy(360f)

Android Relative layout button crashes app

I'm making an app, now i want to make a button with an image and some text on it.
But every time I click the button my app crashes, android studio does compile however.
This is how i my xml code to make the layout of the screen.
<?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">
<RelativeLayout
android:id="#+id/buttonStartTest"
style="#android:style/Widget.Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<ImageView... />
<TextView.../>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java code:
package com.example.user.ballbounceapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout StartTest = findViewById(R.id.buttonStartTest);
StartTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TestActivity();
}
});
}
public void TestActivity(){
Intent intent = new Intent(MainActivity.this,TestActivity.class);
startActivity(intent);
}
};
You may have forgot to declare your TestActivity inside your manifest.
More about activities in manifest here : https://developer.android.com/guide/topics/manifest/activity-element
So you will need something like this :
<activity android:name="TestActivity">
If the crash persists, the problem might be in your TestActivity 's OnCreate method.

How to implement TvView on Android TV?

Goal
To create a reduced TvView that displays on top of a WebView.
This is specifically for live TV.
(I've seen numerous solutions that address streaming content, but this is intended for video that's delivered through the coaxial cable.)
Where I'm At Now
The WebView portion works fine, and I've learned that a RelativeLayout is required in order to layer views.
Also, TvView doesn't seem to be recognized, so I'm guessing that I'll probably need to use a SurfaceView instead.
activity_main.xml
MainActivity.java
Questions
I've seen the TvInput, example applications, but it's extremely excessive. I'm not trying to recreate the TvInput service, just leverage on existing framework.
Is it possible to simply call on the existing, TV service that's already on the Android device, and display it in a view?
If it is possible, how is it accomplished?
If it's not possible, what's the simplest method of implementing it? (repository links would be great.)
I've searched all over for answers but can't seem to find anything.
Thank you in advance for any assistance.
Code in Text Format
As requested by tung.
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:id="#+id/main_browse_fragment"
android:name="com.company.app.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.company.app.MainActivity"
tools:deviceIds="tv"
tools:ignore="MergeRootFrame" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TvView
android:id="#+id/TvView"
android:layout_width="816dp"
android:layout_height="404dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout >
MainActivity.java
package com.company.app;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.media.tv.TvView;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
public class MainActivity extends Activity {
private WebView mWebView;
private TvView mTvView;
private WebSettings mWebSettings;
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mWebView = new WebView(this);
mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.loadUrl("http://10.28.98.150:1000/");
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
this.setContentView(mWebView);
mTvView = new TvView(this );
mTvView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
this.setContentView(mTvView);
}
#Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
String channelUrl="content://android.media.tv/channel/"+channelNumber;
mTvView.tune(mInputId, Uri.parse(channelUrl));
input id is available in TV database(refering to which source channel is coming from).
If you pass these info to tvview it will start playing.

Removing duplication in Android Layout and Activity codes

I have a simple Activity/ LinearLayout combination that has two buttons and changes the background colour when each button is clicked. The code is as below
It appears to me that there is a lot of high-level duplication of code. For example, I should not need to define and make variables out of the buttons and the LinearLayout background in my Java file. When I express my intention to couple them, I should be getting them automatically
So, lines like btnBlue = (Button) findViewById(R.id.btnBlue); should not be necessary. A good framework should allow me to use some conventions (like give me a Java variable btnBlue if my resource file #+id/ resource is named as btnBlue, etc
Where can I find a framework that achieves these for me in Android programming?
Thanks
Layout xml
<?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:background="#ff000000"
android:weightSum="1"
android:id="#+id/background">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Linear Layout Tutorial"
android:textColor="#ff33ff"
android:textSize="32sp"
/>
<Button
android:id="#+id/btnGreen"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:text="Change to Green"
android:layout_weight="0.07" />
<Button
android:id="#+id/btnBlue"
android:layout_width="400sp"
android:layout_height="wrap_content"
android:text="Change12 to Blue"
android:layout_weight="0.19" />
</LinearLayout>
Activity Java code
package com.example.xxx.testandroid;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private final String TAG = "TKT";
LinearLayout background;
Button btnGreen, btnBlue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.som_linear);
Log.d(TAG, "onCreate");
background = (LinearLayout) findViewById(R.id.background);
btnBlue = (Button) findViewById(R.id.btnBlue);
btnGreen = (Button) findViewById(R.id.btnGreen);
btnGreen.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// click button code here
background.setBackgroundColor(Color.parseColor("#00ff00"));
}
});
btnBlue.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// click button code here
background.setBackgroundColor(Color.parseColor("#006699"));
}
});
}
}
ButterKnife by Jake Wharton seems a good solution for your problem.

How to focus to the top of newly generated text in textView?

I have a simple jokes application. When you press the button you get new joke. If the previous one was bigger than the screen you can scroll it down. If you go down to the bottom and you go to the next joke, you are transfered to the bottom of the newly generated joke, but i want it to go to the top, and automatically display the start of the joke. How can i do this ?
I assume it would be done via the java code.
Thank you for your time.
Use the scrollTo(int x, int y) method, i like to have ScrollView around my TextView but i think the same thing will work for a TextView only. hope you understand!
Rolf
Example
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="130dp" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="long\n\n\n\n\n\n\n\n long\n\n\n\n\n\n\n very text here!" />
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="joke" />
</LinearLayout>
java:
package org.sample.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class AutoscrollActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button new_joke;
private TextView joke;
private ScrollView scroll;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new_joke = (Button) this.findViewById(R.id.button);
new_joke.setOnClickListener(this);
joke = (TextView) this.findViewById(R.id.text);
scroll = (ScrollView) this.findViewById(R.id.scroll);
}
#Override
public void onClick(View v) {
joke.setText("Long\n\n\n\n\n\n\n\n joke \n\n\n\n\n\n\n\nlong joke joke");
scroll.scrollTo(0, 0);
}
}

Categories

Resources