Phone won't play the on-click sound - java

I am currently developing an android application in which purpose is to play a large variety of every-day sounds on click.
I have 3 activities : home, animaux and transports.
Home is fine, animaux is fine (it plays all the sounds without any problems).
But I want to integrate admob on Transports acivity. Ads display well, but when I cluck on the sounds buttons, nothing happens. If you could help me understand my mistake and how to fix it, it would be very nice !
Here is transports.java
public class Transports extends Activity {
private AdView adView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transports);
// Create the adView
adView = new AdView(this, AdSize.BANNER, "xxxxxx"
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout1);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
private MediaPlayer mPlayer = null;
/** Called when the activity is first created. */
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transports);
Button btn_sound_sncf2 = (Button) findViewById(R.id.sncfnew);
btn_sound_sncf2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
playSound(R.raw.sncf2);
}
});
Button btn_sound_sncf1 = (Button) findViewById(R.id.sncf1);
btn_sound_sncf1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
playSound(R.raw.sncf1);
}
});
}
#Override
public void onPause() {
super.onPause();
if(mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
}
private void playSound(int resId) {
if(mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer = MediaPlayer.create(this, resId);
mPlayer.start();
}
}
And here is the transports.xml code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical"
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=".Transports" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxx"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
android:gravity="top" >
</com.google.ads.AdView>
<Button android:id="#+id/sncfnew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/sncf1"
android:layout_below="#+id/sncf1"
android:layout_marginTop="38dp"
android:text="#string/sncfnew" />
<Button android:id="#+id/sncf1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1"
android:text="#string/sncf1" />
</LinearLayout>
</RelativeLayout>
I wonder if the problem isn't in my xml file : I have a lot of difficulties to maker a clear interface with the linearlayout required by admob.

You aren't actually setting the listeners. You have that code in a method:
public void onCreate1(Bundle savedInstanceState) {
that is never called. Move that code to the actual onCreate() method and it should work fine.

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;
}
}
}

NullPointerException when trying to open ZXingScannerView

I'm new to Android development so please bear with me.
I have the code inside an activity that implements qr scanning. Basically there is an activity before this segments of codes, but its just clicking of a button to go here. and whenever I do that, my app crashes and that is the error that returns. The idea of the app is once the user click the button, it will first display a qr scanner, once it has been scanned, it will now call this XML File
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_scan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/scanbg"
tools:context=".ScanActivity">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="62dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question Here"
android:textSize="22dp"
android:id="#+id/questionhere"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/questionhere"
android:hint="Answer"
android:id="#+id/answerhere"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Correct or not indicator"
android:id="#+id/indicator"
android:layout_below="#id/answerhere"
android:textSize="18dp"/>
<!--this textview will be programmed to be changed as correct or not-->
<!--if answer == correct then "correct!" else "wrong answer"-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Point(s):"
android:id="#+id/userpoints"
android:layout_below="#id/indicator"
android:textSize="18dp"/>
<Button
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/userpoints"
android:layout_alignParentEnd="true"
android:text="Go"
android:textSize="14dp"
android:background="#B0DAD5"
android:id="#+id/gosagot"/>
</RelativeLayout>
and Here is the java file
public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
Button validateanswer;
ProgressDialog progress;
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
validateanswer = (Button)findViewById(R.id.gosagot); //Im having error here
validateanswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
progress.setMessage("Connecting...");
progress.setCancelable(false);
progress.show();
EditText theanswer = (EditText)findViewById(R.id.answerhere);
String sagotdito = theanswer.getText().toString();
RequestFactory.confirmanswer(ScanActivity.this, sagotdito, new RequestCallback<Boolean>() {
#Override
public void onSuccess(Boolean response) {
runOnUiThread(new Runnable() {
#Override
public void run() {
finish();
//load new question
progress.dismiss();
}
});
}
#Override
public void onFailed(String message) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(ScanActivity.this, "Wrong Answer. Try Again!", Toast.LENGTH_LONG).show();
progress.dismiss();
}
});
}
});
}
});
}
continuation...
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(final Result result) {
Log.w("handleResult",result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText answertoQuestion = new EditText(this);
answertoQuestion.setHint("answer");
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
setContentView(R.layout.activity_scan);
TextView j = (TextView)findViewById(R.id.questionhere);
EditText k = (EditText)findViewById(R.id.answerhere);
String n = k.getText().toString();
j.setText(result.getText());
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
please help me.
If you check the examples of ZXing...
In your activity XML, you can add a region for the QR scanner.
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Grab that and add the scanner view there.
public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_simple_scanner);
ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
mScannerView = new ZXingScannerView(this);
contentFrame.addView(mScannerView);
}
Your error is that you can't findViewById on your button when you replaced the content view with some other view that doesn't have the ID you want to find.
And even if you "switched" findViewById and setContentView around, your code wouldn't crash, but you would no longer have a button in the content view, so having the click action would be pointless.
The problem is that the button is a part of activity_scan and you set setContentView(mScannerView) to other layout.You are bound to get this error as the activity does not have a refernce to the view(button) you are refering.
What you can do is make the mScannerView a part of your main layout and then get it with findViewById();

Change Layouts Dynamically in 1 Activity

I have the same request of this Post and I followed the FoamyGuy's Answer but I don't get the introLayout with the ImageView displaying.
I just want to show my introLayout first and then change it to my WebView.
here is my main.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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/introLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#color/white"
android:visibility="visible"
>
<ImageView android:layout_width="wrap_content"
android:contentDescription="splash screen"
android:id="#+id/splash"
android:src="#drawable/splash"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
</RelativeLayout>
<WebView
android:id="#+id/browser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
/>
</LinearLayout>
in MainActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout introLayout = (RelativeLayout) findViewById(R.id.introLayout);
introLayout.setVisibility(View.GONE);
webview = (WebView)findViewById(R.id.browser);
webview.setVisibility(1);
}
Help?
I don't know if it would make any difference but you could try :
webview.setVisibility (View.VISIBLE);
Instead of :
webview.setVisibility (1);
PS: With View.GONE you make the View not visible and with View.VISIBLE you make the View visible :).
Just because you did the opposite thing in your MainActivity then you discribed.
Your introLayout is set View.GONE, change to
introLayout.setVisibility(View.VISIBLE);
You need to make some logic in your code, when you will hide this "#+id/introLayout" and then show your WebView.
Ok, here is how would i do that if you dont need that layout after few seconds:
ActivityMain
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent startSpalsh = new Intent("com.yourpackagename.secondactivity");
startActivity(startSpalsh);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
finish();
}
}
splash.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/introLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#color/white"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:contentDescription="splash screen"
android:id="#+id/splash"
android:src="#drawable/splash"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
</RelativeLayout>
SecondActivity
public class SecondActivity extends AppCompatActivity {
WebView webView;
TextView txtChk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
webview = (WebView)findViewById(R.id.browser);
}
second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/browser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
</LinearLayout>
First, change de ubication of intro. Set it below of WebView in your layout. WebView and intro don't need to set tag visibility. So... In your onCreate() apply this:
new Handler().postDelayed(new Runnable() { #Override public void run() { introLayout.setVisibility(View.GONE); } }, 2000);
2000 = 2 seconds. Change this like you want.
This code will be later that your initialize WebView and IntroLayout.
Change the LinearLayout by RelativeLayout in your layout how principal container.

How to make one ImageView invisible when another clickable ImageView is pressed?

I tried to make ImageView(bul1) disappear when ImageView(Seethrough) is pressed. I get a nullpointer error when i try to run this code. What is wrong with it?
JAVA code
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
final ImageView view1 = (ImageView) findViewById(R.id.bul1);
seethrough1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(view1.getVisibility() == View.VISIBLE)
{
view1.setVisibility(View.INVISIBLE);
}
}
});
}
XML code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="#drawable/gun"
android:clickable="true"
android:id="#+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet"
android:id="#+id/bul1"
/>
</LinearLayout>
You need to reconcile seethrough's onClickListener with its onClick XML attribute. I'd suggest removing this line from the xml:
android:onClick="next"
and placing the code inside your next method (if you have one)
public void next (View v){
some code
}
behind or before your visibility checking if, whichever suits you more:
#Override
public void onClick(View v) {
//place some code here
if(view1.getVisibility() == View.VISIBLE){
view1.setVisibility(View.INVISIBLE);
}
//or here
}
I think their is problem with out xml code, please try writing xml as follows,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="#drawable/gun"
android:clickable="true"
android:id="#+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet"
android:id="#+id/bul1"
/>
</LinearLayout>
If it return NullPointerExeption, I think your ImageView is Null, because
setContentView(R.layout.activity_main);
and activity_main.xml is not like contents of your post, check name of layout and try again.
I found out that I was supposed to declare the imageviews inside the method instead of before.
like this
public void onClick(View v) {
ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);

Android TextView troubles

Ok so i'm starting to learn Android and i cant append or set the text of the TextView.. i always get a NullPointerException.. anyone help me out?
Field variables
public TextView t = null;
public Button b = null;
My onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
t = (TextView)findViewById(R.id.textView1);
b = (Button)findViewById(R.id.dummy_button);
//t.append("hello again");
setContentView(R.layout.activity_fullscreen);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView,
HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
The listener i decided to put the append in..
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("onClick", "before append");
t.append("ummmm" + System.currentTimeMillis());
Log.e("onClick", "after");
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
Here is the .xml file that i use in layout
<FrameLayout 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="#0099cc"
tools:context=".FullscreenActivity" >
<!--
The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->
<TextView
android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="#string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >
<Button
android:id="#+id/dummy_button"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/dummy_button" />
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="411dp"
android:text="Why Hello There" />
</FrameLayout>
R.java.. it is auto generated however.. here is inside R.java
public static final class id {
public static final int dummy_button=0x7f070002;
public static final int fullscreen_content=0x7f070000;
public static final int fullscreen_content_controls=0x7f070001;
public static final int textView1=0x7f070003;
}
Any help would be amazing! :) i am quite new to Android development
You are missing setContentView(R.layout.your_layout_id);
Add it after super.onCreate and before you are finding any views.
Your OnCreate should be like this,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xml);
t = (TextView)findViewById(R.id.textView1);
b = (Button)findViewById(R.id.dummy_button);
}
It seems that you forget to call setContentView method

Categories