Button is being disabled after clicking once.I want to reuse it - android

In a part of my program There is a button named "imageButton_sound"in xml file "activity_exam_class_1".I want to play a sound clicking this button.First when i click it, it play sound but from the second time it doesn't work.
I initialize this xml file in"Exam_class_1.java" file .
The sequence of my program is -first I will press the"imageButton_sound" then press any other button of the same file like "imageButton_a","imageButton_b""imageButton_c"...........etc.
Then after fulfilling some condition the program will go to either "success_exam.xml" or "fail_exam.xml"
file both of which contain a button .After pressing the button it will go back to the "activity_exam_class_1",and then I will press"imageButton_sound"again and the process will be continued .
here are my code :
Exam_class_1.java:
package com.example.alphabet_school;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
public class Exam_class_1 extends Activity implements View.OnClickListener{
ImageButton im_a,im_b,im_c,im_d,im_e,im_f;
ImageButton im_success,im_fail;
ImageButton sound;
MediaPlayer joy_sound,wrong_sound,letter_sound;
int exam_sound,success=0,fail=0;
//int[] keepSound={R.raw.a,R.raw.b,R.raw.c,R.raw.d,R.raw.e,R.raw.f};
int ks=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam_class_1);
sound=(ImageButton) findViewById(R.id.imageButton_sound);
im_a=(ImageButton) findViewById(R.id.imageButton_a);
im_b=(ImageButton) findViewById(R.id.imageButton_b);
im_c=(ImageButton) findViewById(R.id.imageButton_c);
im_d=(ImageButton) findViewById(R.id.imageButton_d);
im_e=(ImageButton) findViewById(R.id.imageButton_e);
im_f=(ImageButton) findViewById(R.id.imageButton_f);
for(int i=0;i<6;i++){
sound.setOnClickListener(this);
im_a.setOnClickListener(this);
im_b.setOnClickListener(this);
im_c.setOnClickListener(this);
im_d.setOnClickListener(this);
im_e.setOnClickListener(this);
im_f.setOnClickListener(this);
}
// setContentView(R.layout.success_exam1);
//
// im_success = (ImageButton) findViewById(R.id.im_button_success);
// im_success.setOnClickListener(this);
//
//
// setContentView(R.layout.fail_exam1);
// im_fail=(ImageButton) findViewById(R.id.im_button_fail);
// im_fail.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.exam_class_1, menu);
return true;
}
public void onClick( View v) {
// TODO Auto-generated method stub
int opt= v.getId();
if(opt==R.id.imageButton_sound){
if(ks==0){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==1){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==2){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==3){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==4){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==5){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
}
else{
if((opt == R.id.imageButton_a && ks==1)||(opt == R.id.imageButton_b && ks==2)||
(opt == R.id.imageButton_c && ks==3)||(opt == R.id.imageButton_d && ks==4)||
(opt == R.id.imageButton_e && ks==5)||(opt == R.id.imageButton_f && ks==6)){
setContentView(R.layout.success_exam1);
joy_sound = MediaPlayer.create(this,R.raw.joy);
joy_sound.start();
}
else{
setContentView(R.layout.fail_exam1);
wrong_sound = MediaPlayer.create(this,R.raw.wrong);
wrong_sound.start();
}
}
}
public void click_success_fail(View v){
setContentView(R.layout.activity_exam_class_1);
}
}
activity_exam_class_1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageButton
android:id="#+id/imageButton_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="#null"
android:src="#drawable/exam_c" />
<ImageButton
android:id="#+id/imageButton_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:contentDescription="#null"
android:src="#drawable/exam_a" />
<ImageButton
android:id="#+id/imageButton_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="#null"
android:src="#drawable/exam_d" />
<ImageButton
android:id="#+id/imageButton_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton_f"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:contentDescription="#null"
android:src="#drawable/audio_icon"
/>
<ImageButton
android:id="#+id/imageButton_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton_e"
android:layout_alignLeft="#+id/imageButton_d"
android:contentDescription="#null"
android:src="#drawable/exam_f" />
<ImageButton
android:id="#+id/imageButton_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton_a"
android:layout_alignLeft="#+id/imageButton_sound"
android:layout_marginLeft="44dp"
android:contentDescription="#null"
android:src="#drawable/exam_e" />
<ImageButton
android:id="#+id/imageButton_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton_c"
android:layout_alignRight="#+id/imageButton_sound"
android:layout_marginRight="47dp"
android:contentDescription="#null"
android:src="#drawable/exam_b" />
</RelativeLayout>
success_exam.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tick1">
<ImageButton
android:id="#+id/im_button_success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/next"
android:contentDescription="#null"
android:onClick="click_success_fail"
/>
</RelativeLayout>
fail_exam.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wrong1" >
<ImageButton
android:id="#+id/im_button_fail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/try_again"
android:contentDescription="#null"
android:onClick="click_success_fail"
/>
</RelativeLayout>

slm;I think you should RE-Initialise the counter "int Ks=0;"at the end of your first part of game. try to "RE-Initialise" like this:
else{
if((opt == R.id.imageButton_a && ks==1)||(opt == R.id.imageButton_b && ks==2)||
(opt == R.id.imageButton_c && ks==3)||(opt == R.id.imageButton_d && ks==4)||
(opt == R.id.imageButton_e && ks==5)||(opt == R.id.imageButton_f && ks==6)){
Ks=0;
setContentView(R.layout.success_exam1);
joy_sound = MediaPlayer.create(this,R.raw.joy);
joy_sound.start();
}
else{ Ks=0;
setContentView(R.layout.fail_exam1);
wrong_sound = MediaPlayer.create(this,R.raw.wrong);
wrong_sound.start();
}

Related

How to load multiple images on the same imageview by clicking a button?

Basically, I want to have an AlertDialog in which multiple images will load on the same ImageView. By clicking the NEXT button it will get the next image bitmap from bitmap list and finally will load on that ImageView. Same case for PREV button. It will take previous bitmap and will load the image on the same ImageView.
But the problem is after loading the first image it does not load the next image. If I click the next button then it takes the next bitmap but the imageview.setImageBitmap(bitmap) does not work.
How to remove or clear the previous image to place the next photos?
The XML file are given below :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/question"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:text="#string/question"
android:textSize="14sp"
android:textColor="#color/light_black"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/divider"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
app:cardElevation="0dp">
<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:id="#+id/selected_place_images"
android:scaleType="centerCrop"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/divider"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="47dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/previous"
android:text="#string/previous_page"
android:textColor="#color/black"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"
android:text="#string/next_page"
android:textColor="#color/black"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
The java code are given below :
private void showDialogOfImages() {
Log.d(TAG,"showDialogOfImages : showing places images");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = this.getLayoutInflater();
View customLayout = inflater.inflate(R.layout.custom_displaying_selected_place_images,null);
mSelectedPlaceImages = (ImageView) customLayout.findViewById(R.id.selected_place_images);
nextPage = (TextView) customLayout.findViewById(R.id.next);
previousPage = (TextView) customLayout.findViewById(R.id.previous);
displayPhoto();
nextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentPhotoIndex++;
displayPhoto();
}
});
previousPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentPhotoIndex--;
displayPhoto();
}
});
builder.setView(customLayout);
builder.show();
}
private void displayPhoto() {
if (mCurrentPhotoIndex < mSelectedLocationPhotosBitmap.size()) {
Bitmap bitmap = mSelectedLocationPhotosBitmap.get(mCurrentPhotoIndex);
Toast.makeText(context,""+mCurrentPhotoIndex+" : "+bitmap,Toast.LENGTH_LONG).show();
mSelectedPlaceImages.setImageBitmap(bitmap);
setButtonVisibility();
}
}
private void setButtonVisibility() {
if(mCurrentPhotoIndex == 0 && mSelectedLocationPhotosBitmap.size() == 1){
nextPage.setEnabled(false);
nextPage.setClickable(false);
nextPage.setTextColor(getResources().getColor(R.color.divider));
previousPage.setEnabled(false);
previousPage.setClickable(false);
previousPage.setTextColor(getResources().getColor(R.color.divider));
}
else if (mCurrentPhotoIndex == 0 && mSelectedLocationPhotosBitmap.size() > 1){
nextPage.setEnabled(true);
nextPage.setClickable(true);
nextPage.setTextColor(getResources().getColor(R.color.black));
previousPage.setEnabled(false);
previousPage.setClickable(false);
previousPage.setTextColor(getResources().getColor(R.color.divider));
}
else if (mCurrentPhotoIndex == mSelectedLocationPhotosBitmap.size()-1 && mSelectedLocationPhotosBitmap.size() > 1){
nextPage.setEnabled(false);
nextPage.setClickable(false);
nextPage.setTextColor(getResources().getColor(R.color.divider));
previousPage.setEnabled(true);
previousPage.setClickable(true);
previousPage.setTextColor(getResources().getColor(R.color.black));
}
else{
nextPage.setEnabled(true);
nextPage.setClickable(true);
nextPage.setTextColor(getResources().getColor(R.color.black));
previousPage.setEnabled(true);
previousPage.setClickable(true);
previousPage.setTextColor(getResources().getColor(R.color.black));
}
}
You need to clear the previous image from imageview and then you can set the new on top.
Do as below
private void displayPhoto() {
if (mCurrentPhotoIndex < mSelectedLocationPhotosBitmap.size()) {
Bitmap bitmap = mSelectedLocationPhotosBitmap.get(mCurrentPhotoIndex);
Toast.makeText(context,""+mCurrentPhotoIndex+" : "+bitmap,Toast.LENGTH_LONG).show();
mSelectedPlaceImages.setImageBitmap(null)
mSelectedPlaceImages.setImageBitmap(bitmap);
setButtonVisibility();
}
}
Try this:
mSelectedPlaceImages.destroyDrawingCache();
mSelectedPlaceImages.setImageBitmap(bitmap);
mSelectedPlaceImages.buildDrawingCache();

setOnItemClickListener does not work

I'm having some problems to figure out how I can implement '.setOnItemClickListener' in a GitHub sample project for Android.
I want to select the cells of a list view, but I can get it to work. The github project is this:
https://github.com/schrockblock/android-table-view
I'm trying to add the listener in my main activity but I can't. Here is the code:
public class MainActivity extends ActionBarActivity {
public String[] items={};
WifiHelper wifiHelper;
List<ScanResult> list;
//public WifiManager pWifiManager;
ListView tableView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 取得WifiManager对象
//pWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiHelper = new WifiHelper(this);
//open wifi
wifiHelper.openWifi(new WifiHelper.openWifiCallback() {
#Override
public void openSuccess() {
//open success
}
#Override
public void openFail() {
//open fail
}
});
//scan wifi
list = wifiHelper.startScan();
tableView = (ListView)findViewById(R.id.table_view);
TableViewDataSource tvds = new TableViewDataSource(this){
#Override
protected int numberOfSections() {
return 3;
}
#Override
protected String titleForHeaderInSection(int section) {
if(section==0)
return "\nGENERAL";
else if(section==1)
return "Pull down to refresh networks information ot tap 'Scan' button in the top bar of the screen.\n\nNETWORKS";
else
return "Networks under -75dB (RSSI) may have connectivity problems. Try to be as near as you can of the network.\n\nOnly supported networks can use the exploit feature of this app. You can use iWepPRO as your wifi manager application";
}
#Override
protected int numberOfRowsInSection(int section){
if(section==0)
return 3;
else if(section==1)
return list.size();
else
return 1;
}
#Override
protected DefaultCell cellForRowAtIndexPath(JIndexPath indexPath) {
if(indexPath.section==0) {
DefaultCell cell = new DefaultCell();
cell.hashIBencryption = true;
cell.imageButtonImage = R.drawable.emptywhite2px;
switch (indexPath.row){
case 0:
cell.textLabelText = "WiFi";
cell.hasDisclosureIndicator = false;
cell.hasDisclosureIndicator2 = false;
cell.hasSwitch = true;
cell.hasSwitchOn = true;
break;
case 1:
cell.textLabelText = "Auto-Scan";
cell.hasDisclosureIndicator = false;
cell.hasDisclosureIndicator2 = false;
cell.hasSwitch = true;
cell.hasSwitchOn = false;
break;
case 2:
cell.textLabelText = "Information";
cell.hasDisclosureIndicator = true;
cell.hasDisclosureIndicator2 = false;
break;
default:
break;
}
return cell;
}else if(indexPath.section==1){
SubtitleCell cell1 = new SubtitleCell();
cell1.hasDisclosureIndicator = false;
cell1.hasDisclosureIndicator2 = true;
cell1.hashIBencryption = true;
//cell1.textLabelText = "network name " + indexPath.row;
ScanResult r = list.get(indexPath.row);
//String ssidName = r.SSID;
cell1.textLabelText = r.SSID;
//cell1.detailTextLabelText = "signal " + indexPath.row;
cell1.detailTextLabelText = "" + r.level;
//cell1.detailTextLabelText = "" + calculateSignalStength(pWifiManager, r.level);
cell1.imageButtonImage = R.drawable.deviceaccesssecure;
return cell1;
}else if(indexPath.section==2){
SubtitleCell cell2 = new SubtitleCell();
cell2.hashIBencryption = true;
cell2.imageButtonImage = R.drawable.emptywhite2px;
cell2.textLabelText = " ";
cell2.hasDisclosureIndicator2 = false;
cell2.hasDisclosureIndicator = false;
cell2.hasEmptyCell = true;
return cell2;
}
return null;
}
};
tableView.setAdapter(tvds);
}
I have read and test some samples coding the setOnItemClickListener after the 'setAdapter()' function, but it does not work.
I 'm trying to set the cell selection to show a Toast with some text in order to see if it works, but I don't know what I'm doing wrong.
Can anyone help me with some tips??
Thank you very much in advance.
These are my two layouts:
Cell Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="0dp"
android:background="#ffdedede">
<TextView
android:id="#+id/header_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textSize="12dp"
android:textColor="#android:color/darker_gray"
android:padding="0dp"
android:visibility="gone"
android:layout_marginLeft="20dp"
android:typeface="sans"
android:layout_marginRight="20dp"
android:elegantTextHeight="false" />
<RelativeLayout
android:id="#+id/rl2"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_below="#+id/header_text_view"
android:padding="0dp"
android:background="#android:color/white">
<RelativeLayout
android:id="#+id/rl3"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:background="#android:color/white">
<TextView android:id="#+id/title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cell Title"
android:textSize="18sp"
android:textColor="#000"
android:background="#0fff"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingBottom="2dp"
android:typeface="sans"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/ibencryption"
android:layout_toEndOf="#+id/ibencryption" />
<TextView
android:id="#+id/detail_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cell Subtitle"
android:paddingLeft="5dp"
android:paddingBottom="5dp"
android:textColor="#555"
android:background="#0fff"
android:textSize="14sp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/ibencryption"
android:layout_toEndOf="#+id/ibencryption" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ibencryption"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:background="#android:color/white"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:adjustViewBounds="false" />
</RelativeLayout>
<ImageView
android:id="#+id/disclosure_indicator"
android:src="#drawable/nextitem"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginRight="20dp"
android:cropToPadding="true"/>
<ImageView
android:id="#+id/disclosure_indicator_2"
android:src="#drawable/actionabout"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginRight="20dp"
android:cropToPadding="true"/>
<Switch
android:id="#+id/switch_indicator"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginRight="20dp"
android:cropToPadding="true"/>
</RelativeLayout>
And the main activity 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"
tools:context=".MainActivity" >
<ListView
android:id="#+id/table_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Thank you!!
This is how you can provide a OnItemClickListener :
tableView.setOnItemClickListener(new OnItemClickListener(){
void onItemClick(AdapterView<?> parent, View view, int position, long id){
switch(position){
default: Toast.makeToast(MainActivity.this, "Some crappy text",Toast.LENGTH_LONG ).show();
}
}
}};
Generally :
First of all put all your View#findViewById(int) code to the top of your OnCreate method just below your Activity#setContentView(int) call.
Then try to scan in your WifiManager#openSuccess() callback..
Try to refactor your code and replace all your if,else if and else blocks which check against an integer with switch blocks.
And please do not return null EVER.

How to create a Button in the camera view in vuforia?

I am using Vuforia AR sdk and want to create a button on the camera preview on the screen.
I cannot figure out where and how to add the button.
I have edit the camera_overlay_udt.xml like this.. In my layout design i have placed back button and listview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/header"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#android:color/transparent"
android:src="#drawable/back" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Swipart"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/arcstarButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:src="#drawable/star_button" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/favListingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headerLayout"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ListView
android:id="#+id/favlist"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="7dp"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#color/overlay_bottom_bar_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1" >
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/overlay_bottom_bar_separators" />
<ImageButton
android:id="#+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/content_desc_camera_button"
android:onClick="onCameraClick"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/camera_button_background" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/bottom_bar"
android:background="#color/overlay_bottom_bar_separators" />
</RelativeLayout>
after that please Edit that ImageTargets.java class
private void addOverlayView(boolean initLayout) {
// Inflates the Overlay Layout to be displayed above the Camera View
LayoutInflater inflater = LayoutInflater.from(this);
mUILayouts = (RelativeLayout) inflater.inflate(
R.layout.camera_overlay_udt, null, false);
mUILayouts.setVisibility(View.VISIBLE);
// If this is the first time that the application runs then the
// uiLayout background is set to BLACK color, will be set to
// transparent once the SDK is initialized and camera ready to draw
if (initLayout) {
mUILayouts.setBackgroundColor(Color.TRANSPARENT);
}
// Adds the inflated layout to the view
addContentView(mUILayouts, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Gets a reference to the bottom navigation bar
mBottomBar = mUILayouts.findViewById(R.id.bottom_bar);
// Gets a reference to the Camera button
mCameraButton = mUILayouts.findViewById(R.id.camera_button);
mCameraButton.setVisibility(View.GONE);
favButton = (ImageButton) mUILayouts.findViewById(R.id.arcstarButton);
listview = (ListView) mUILayouts.findViewById(R.id.favlist);
backButton = (ImageButton) mUILayouts.findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
finish();
}
});
listview.setVisibility(View.GONE);
galleryList = SendFile.getFavourites();
if (galleryList != null) {
gridviewAdapter = new GridviewAdapter(ImageTargets.this);
listview.setAdapter(gridviewAdapter);
}
favButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (galleryList != null && galleryList.size() > 0) {
if (listview.getVisibility() == View.GONE) {
listview.setVisibility(View.VISIBLE);
} else {
listview.setVisibility(View.GONE);
}
} else {
Toast.makeText(ImageTargets.this, "Favourites not fond",
Toast.LENGTH_LONG).show();
}
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paramAdapterView,
View paramView, int positon, long paramLong) {
SendFile.setFavourite(galleryList.get(positon));
Intent intent = new Intent(ImageTargets.this,
LoadingScreen.class);
Bundle bundle = new Bundle();
bundle.putInt("x", x_Axis);
bundle.putInt("y", y_Axis);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
showDialogHandler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Server Response: " + aResponse, Toast.LENGTH_SHORT)
.show();
showAlertDialog(aResponse);
} else {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Not Got Response From Server.", Toast.LENGTH_SHORT)
.show();
}
};
};
loadingDialogHandler.captureButtonContainer = mUILayouts
.findViewById(R.id.camera_button);
mUILayouts.bringToFront();
}
They showing there layouts using handlers
Start you camera preview in a normal way. Place a layout on top of it with transparent background like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff000000"
android:layout_height="match_parent">
<ImageView
android:id="#+id/start_image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="#drawable/scan_image"/>
</RelativeLayout>
In java file, you can add this layout like this:
private View mStartupView;
mStartupView = getLayoutInflater().inflate(
R.layout.startup_screen, null);
// Add it to the content view:
addContentView(mStartupView, new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
This way you will get to see your button on top of camera preview. Hope it helps
You can add buttons in cameraoverlay layout which is in layout folder and you can initialize buttons in initAR function which is in mainactivity.
Step 1: Add the button in the camera_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
style="#android:style/Widget.ProgressBar"
android:id="#+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="51dp"
android:text="Button" />
</RelativeLayout>
Step 2: Edit the ImageTargets.java class
private static final String LOGTAG = "ImageTargets";
private Button b1;
Step 3: Modify the initApplicationAR() function of ImageTargets.java class
private void initApplicationAR()
{
// Create OpenGL ES view:
int depthSize = 16;
int stencilSize = 0;
boolean translucent = Vuforia.requiresAlpha();
mGlView = new SampleApplicationGLView(this);
mGlView.init(translucent, depthSize, stencilSize);
mRenderer = new ImageTargetRenderer(this, vuforiaAppSession);
mRenderer.setTextures(mTextures);
mGlView.setRenderer(mRenderer);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setVisibility(View.GONE);
}
});
}
Now lay back and watch your button disappear on a click!
Although it's a long time since the post.. yet I found one article.. wherein you can have the desired thing..
Ref: https://medium.com/nosort/adding-views-on-top-of-unityplayer-in-unityplayeractivity-e76240799c82
Solution:
Step1: Make a custom layout XML file (vuforia_widget_screen.xml). For example, button has been added.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/unity_player_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#null"
android:text="#string/welcome" />
</FrameLayout>
Step 2: Make following changes in the UnityPlayerActivity.java
Replace "setContentView(mUnityPlayer);" with
setContentView(R.layout.vuforia_widget_screen);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
-> For anyone, who will face the issue in future. :)

ListView is invisible in Activity

I've a one ListView as shown below, when the program is first executed,then it is visible
later automatically invisible.
This layout is visible when User clicks the Button, before that the Visibility is GONE
<RelativeLayout
android:id="#+id/comments_block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:visibility="gone" >
<TextView
android:id="#+id/comments_subHeadding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Type your comment"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/red" />
<EditText
android:id="#+id/user_commetns"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_below="#+id/comments_subHeadding"
android:layout_marginTop="5dip"
android:background="#color/white"
android:imeOptions="actionSend"
android:inputType="textLongMessage" />
</RelativeLayout>
This Layout contains ListView Here I'm facing the problem.
<LinearLayout
android:id="#+id/listview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/comments_block"
android:background="#color/ios_blue"
android:orientation="vertical">
<ListView
android:id="#+id/comments_list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dip"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent" >
</ListView>
</LinearLayout>
Activity Code
#Override
public void onClick(View v)
{
if (v == add_comment_button)
{
if(show)
{
comments_block.setVisibility(View.VISIBLE);
show = false;
return;
}
else
{
comments_block.setVisibility(View.GONE);
show = true;
}
}
else if (v == back_comments_button)
{ finish(); }
}
In your code you are doing
android:layout_height="wrap_content", if there are no item in listview then its height will be 0.
So please if you will use match_parent instead of wrap_content
android:layout_height="match_content"
android:layout_weight="1"
Then your listview will be visible.
I hope it will work.
I found the solution for your problem.....
You are just showing and hiding the comments_block only....
you try this one...I hope it will help you definitely...All the bset
#Override
public void onClick(View v)
{
if (v == add_comment_button)
{
if(show)
{
comments_block.setVisibility(View.VISIBLE);
listview_layout.setVisibility(View.GONE);
show = false;
return;
}
else
{
comments_block.setVisibility(View.GONE);
listview_layout.setVisibility(View.VISIBLE);
show = true;
}
}
else if (v == back_comments_button)
{ finish(); }
}

Android : Managing two buttons

I have a problem concerning setting a listener that would execute different action based on the clicked button.
The compiler doesn't detect any error on the syntax, but the problem is that when I simulate my app on the emulator, it pops up the window asking for "Force Close".
Here is my Java file :
package tp.imc.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class IMCCalculatorActivity extends Activity {
EditText poids,taille;
Button boutton1,boutton2;
TextView text1;
Boolean k=true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
poids=(EditText) findViewById(R.id.poids);
taille=(EditText) findViewById(R.id.taille);
boutton1 =(Button) findViewById(R.id.boutton1);
text1=(TextView) findViewById(R.id.text1);
boutton1.setOnClickListener(clicker);
boutton2.setOnClickListener(clicker);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener clicker = new View.OnClickListener(){
public void onClick (View v){
if( v == boutton1 )
{
String a,b;
Double r;
a=poids.getText().toString();
b=taille.getText().toString();
Double zero=Double.parseDouble(b);
a=poids.getText().toString();
b=taille.getText().toString();
if (zero==0)
{
text1.setText("Erreur ! Division par 0.");
}
else {
r=(Double.parseDouble(a))/(Double.parseDouble(b)*Double.parseDouble(b));
if (r<16.5) text1.setText("Famine.");
else if (r>16.5 && r<18.5) text1.setText("Maigreur.");
else if (r>=18.5 && r<25) text1.setText("Coplulence nomrale.");
else if (r>=25 && r<30) text1.setText("Surpoids.");
else if (r>=30 && r<35) text1.setText("Obésité modérée.");
else if (r>=35 && r<40) text1.setText("Obésité sévère.");
else if (r>=40) text1.setText("Obésité morbide ou massive.");
}
}
else if( v == boutton2 )
{
poids.setText("");
taille.setText("");
text1.setText("");
}
}
};
}
And here is my XML file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/poids"
android:textColor="#FF0000"
android:gravity="center"
/>
<EditText
android:id="#+id/poids"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/poids"
android:lines="1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/taille"
android:textColor="#FF0000"
android:gravity="center"
/>
<EditText
android:id="#+id/taille"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/taille"
android:lines="1"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/metre"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/centimetre"
/>
</RadioGroup>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mega"
android:checked="false"
/>
<Button
android:id="#+id/boutton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/compute" />
<Button
android:id="#+id/boutton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/raz" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/resultat"
/>
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text"
/>
</LinearLayout>
You don't define boutton2, it is null. Add this:
boutton2 = (Button) findViewById(R.id.boutton2);
boutton2.setOnClickListener(clicker);
Since boutton1 and boutton2 don't have common code between them you could simply create two different listeners.
Lastly, this line doesn't look right.
if( v == boutton1 )
You are trying to compare a View to a Button which may not work. Try this:
if( v.getId() == boutton1.getId() )

Categories

Resources