I want to test if the button works on my application.
They are placed inside my first fragment. When I click "All" Button it does not respond at all. My main goal is to load fragment,but in this case I put System.out.println just to test if my button is going to work,but it obviously is not working.
I have tried to make new project and it works,but only inside Main Activity,I want this inside me Exercises_Tab fragment.
I do not see any error logs at all..
public class Exercises_Tab extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View Exercises = inflater.inflate(R.layout.fragment_1,container,false);
Button allbtn = (Button) Exercises.findViewById(R.id.all_button);
allbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Test");
}
});
return Exercises;
}
}
And my XML:
<LinearLayout
android:id="#+id/Linear_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= " Muscles "
android:background="#000000"
android:textColor="#ffffff"
android:textSize="25dp"
android:layout_marginTop="3dp"/>
<HorizontalScrollView
android:id="#+id/scrollViewhorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="-20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"
android:background="#fafafa"
android:text="All"
android:textColor="#696969"
android:id="#+id/all_button"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:id="#+id/second_button"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"
android:background="#fafafa"
android:text="Button 1"
android:textColor="#696969" />
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<FrameLayout
android:id="#+id/Frame_Layout"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimary"
android:layout_below="#+id/Linear_Layout"
>
</FrameLayout>
Perform your entire code inside this function for fragment as it already has a reference to your xml view.
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button allbtn = (Button) view.findViewById(R.id.all_button);
allbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Test");
}
});
}
Simply you can implement the click listener like
public class Exercises_Tab extends Fragment implements OnClickListener {
Button allbtn ;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View Exercises = inflater.inflate(R.layout.fragment_1,container,false);
allbtn = (Button) Exercises.findViewById(R.id.all_button);
allbtn .setOnClickListener(this);
return Exercises ;
}
#Override
public void onClick(View v) {
// implements your things
}
}
I prefer this because it makes your code more readable.
Related
So I am here asking for your kind help.
I am trying to make a TextView visible and gone by touching a single button.
It did work inside a MainActivityJava but when doing the same inside a Fragment Activity, I am facing many troubles.
It seems that the setOnClickListener does not match with fragment but I do not know how to deal with it...
package com.androidbegin.locumatix;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Tab1Fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
return view;
}
Button button = getActivity().findViewById(R.id.buttonTaureauBrute);
TextView textView = getActivity().findViewById(R.id.textView3);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
textView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
};
}
<?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"
android:background="#drawable/wallpaper_selection">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/imageButtonTaureauSon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:scaleType="fitXY"
app:srcCompat="#drawable/prononciation_icone" />
<Button
android:id="#+id/buttonTaureauBrute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5"
android:text="Prendre le taureau par les cornes"
android:textAlignment="center"
android:onClick="onClick"/>
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:gravity="center"
android:visibility="gone"
android:text="Affronter une difficulté avec détermination"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:srcCompat="#drawable/taureau" />
<Button
android:id="#+id/button10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exemples :"
android:textAlignment="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="NATURE :\nverbe"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="REGISTRE :\nstandard"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="NIVEAU :\nB1"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
</LinearLayout>
<Button
android:id="#+id/button14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EXPLICATIONS :" />
<Button
android:id="#+id/button11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="USAGES :" />
<Button
android:id="#+id/button13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lexique :" />
<Button
android:id="#+id/button12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Traductions :" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Try this code
public class Tab1Fragment extends Fragment {
Button button;
TextView textView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
button = getActivity().findViewById(R.id.buttonTaureauBrute);
textView = getActivity().findViewById(R.id.textView3);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if( view.getVisibility() == View.GONE ) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.GONE);
}
}
};
return view;
}
}
I am writing a simple app which is working fine but the issue I have is that I am using a password condition to trigger a button click if entered correctly.
The issue is that my source code is saying 'onButtonClick' is never used and when I manually press that button in the app, it suddenly force closes and crashes. Anyone know what I am doing wrong. I am extending Activity, at the start of the source code. Should I be extending AppCompatActivity?
public class Gvoice extends Activity implements OnClickListener{
ListView lv1;
static final int check = 1111;
Button b1;
Button b_home;
EditText a1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gvoice);
lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
b1 = (Button)findViewById(R.id.GVoice);
a1 = (EditText) findViewById(R.id.editTextHome);
b1.setOnClickListener(this);
//This now handles an automatic press of the bVoice button 1 second after the activity is opened
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
b1.callOnClick();
}
}, 1000);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.BHome) {
String str = a1.getText().toString();
//Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'xxx' which is not case sensitive
if (str.toLowerCase().contains("home")) {
Intent userintent = new Intent(Gvoice.this, PocketSphinxActivity.class);
startActivity(userintent);
} else {
Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
}
}
}
public void onClick(View v){
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
startActivityForResult(i1, check);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
a1.setText((String) lv1.getItemAtPosition(0)); //Get the first phrase in the first row of list view
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
b_home.performClick();
}
}, 500); //Automatically click the 'Blogin' button after 500ms
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Update: Below is the xml file. Please note that onButtonClick has been added to the xml file but still it force closes the app when the button is clicked using the condition statement:
<?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="#ececec">
<ImageView
android:layout_width="100dip"
android:layout_height="100dip"
android:background="#drawable/patient_two"
android:id="#+id/pimage"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Patient Name: Joe Blogs"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Cause of Injury: Car crash"
android:id="#+id/textView2"
android:layout_below="#+id/pimage"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Date of Birth:"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:layout_marginTop="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Gender:"
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_alignStart="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Occupation:"
android:id="#+id/textView5"
android:layout_below="#+id/textView4"
android:layout_alignStart="#+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Address:"
android:id="#+id/textView6"
android:layout_below="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medical History"
android:id="#+id/textView7"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView6"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Heart attack"
android:id="#+id/textView8"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView7"
android:layout_alignStart="#+id/textView6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Arthritis"
android:id="#+id/textView9"
android:layout_below="#+id/textView8"
android:layout_alignStart="#+id/textView8" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tests Completed"
android:id="#+id/textView10"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView9"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="X-Ray"
android:id="#+id/textView11"
android:layout_below="#+id/textView10"
android:layout_alignStart="#+id/textView9"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="#+id/textView12"
android:layout_below="#+id/textView11"
android:layout_alignStart="#+id/textView11" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tests Due"
android:id="#+id/textView14"
android:layout_below="#+id/textView12"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="#+id/textView15"
android:layout_below="#+id/textView14"
android:layout_alignStart="#+id/textView12"
android:layout_marginTop="15dp" />
<ListView
android:layout_width="150dp"
android:layout_height="50dp"
android:id="#+id/lvVoiceReturn1"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter"
android:id="#+id/Blogin1"
android:onClick="onButtonClick"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#+id/bVoice1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
android:id="#+id/bVoice1"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TFusername1"
android:layout_alignParentStart="true"
android:hint="Speech to Text" />
You can 4 method handle button click :
method 1 :
public class Mtest extends Activity {
Button b1;
public void onCreate(Bundle savedInstanceState) {
...
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(myhandler1);
...
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
}
};
}
method 2 :
class MTest extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
...
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(this);
...
}
#Override
public void onClick(View v) {
}
}
method 3 in xml and android:onClick="HandleClick" :
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="HandleClick" />
public class MTest extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void HandleClick(View view) {
}
}
method 4 :
public class MTest extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do stuff
}
});
}
}
The issue was to do with initially not adding onButtonClick in the xml file and also not correctly assigning the button labels in the java file.
lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
b1 = (Button)findViewById(R.id.GVoice);
a1 = (EditText) findViewById(R.id.editTextHome);
c1 = (Button)findViewById(R.id.BHome);
b1.setOnClickListener(this);
The following corrects the issues and everything is working fine now. Hope this can help others in the future
I am using viewPager and fragments to develop an application . In one screen I use DialogFragmentto display an alert message .
My DialogFragment code is below,
public class ErrorFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.error_fragment, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setLayout(350,300);
Button button=(Button)rootView.findViewById(R.id.yesButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Print -"," Ok");
if(getActivity()!=null){
Log.d("Print -"," Ok2");
((LoginActivity)getActivity()).setCurrentItem(4);
Log.d("Print -", " Ok4");
}
Log.d("Print -"," Ok3");
dismiss();
}
});
return rootView;
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
Activity a = getActivity();
if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
In my button action , I have program to open another fragment however nothing is happening except DialogFragment getting closed.
Below is my layout,
<?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/button_border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/errorContent"
android:id="#+id/errorTextView1"
android:textSize="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="30dp"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Space
android:layout_width="match_parent"
android:layout_height="36px"
android:layout_below="#id/errorTextView1"
android:id="#+id/space4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_border_2"
android:id="#+id/yesButton"
android:text=" Yes "
android:layout_below="#+id/space4"
android:textColor="#android:color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20dp"
android:layout_marginLeft="46dp"
android:layout_marginStart="46dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_border_2"
android:id="#+id/noButton"
android:text=" No "
android:layout_below="#+id/space4"
android:textColor="#android:color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20dp"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:layout_alignTop="#+id/yesButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Space
android:layout_width="match_parent"
android:layout_height="36px"
android:layout_below="#id/noButton"/>
</RelativeLayout>
How can I open other fragment by clicking particular button in DialogFragment ?
Even though fragment shifting is not happening the console print inside the button action is getting printed.
I'm creating a small animation app using Android studio. When a button press images should work according to the particular animation.
There's an error with line viewGroup = (viewGroup)findViewById(R.id.ViewGroup);
It shows "Cannot resolve symbol ViewGroup". I typed Alt+Enter and selected a solution. But nothing happened.
public class MainActivity extends Activity {
private ViewGroup viewGroup;
private ImageView imageView, imageView2, imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9,imageView10,imageView11,imageView12;
private Button button,button1;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
imageView= (ImageView) findViewById(R.id.imageView);
imageView2= (ImageView) findViewById(R.id.imageView2);
imageView3= (ImageView) findViewById(R.id.imageView3);
imageView4= (ImageView) findViewById(R.id.imageView4);
imageView5= (ImageView) findViewById(R.id.imageView5);
imageView6=(ImageView) findViewById(R.id.imageView6);
imageView7=(ImageView) findViewById(R.id.imageView7);
imageView8=(ImageView) findViewById(R.id.imageView8);
imageView9=(ImageView) findViewById(R.id.imageView9);
imageView10=(ImageView) findViewById(R.id.imageView10);
imageView11=(ImageView) findViewById(R.id.imageView11);
imageView12=(ImageView) findViewById(R.id.imageView12);
button = (Button)findViewById(R.id.button);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TransitionManager.beginDelayedTransition(viewGroup, new Fade());
fade(imageView, imageView2, imageView3,imageView4,imageView5,imageView6);
}
});
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TransitionManager.beginDelayedTransition(viewGroup, new Slide());
toggle(imageView7,imageView8,imageView9,imageView10,imageView11,imageView12);
}
});}
private static void toggle(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
}
private static void fade(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
Here's my activity_main 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/myAniLayout">
<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_alignParentStart="true"
android:layout_marginBottom="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button1"
android:layout_alignBottom="#+id/button"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#mipmap/ic_launcher"
android:layout_alignTop="#+id/imageView2"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView7"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#android:drawable/star_big_on" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView8"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView9"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView10"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView9"
android:layout_alignStart="#+id/button1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView11"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView8"
android:layout_alignEnd="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView12"
android:src="#android:drawable/star_big_on"
android:layout_above="#+id/button"
android:layout_alignStart="#+id/imageView4" />
Your logcat shows
"Cannot resolve symbol 'ViewGroup'"
viewGroup=(viewGroup)findViewById(R.id.ViewGroup); // (ViewGroup)
viewGroup Not declare in your XML
(viewGroup) must be (ViewGroup) . (viewGroup)
The ViewGroup class is a subclass of the View class. ViewGroup
instances work as containers for View instances to group View
instances together.
http://developer.android.com/intl/es/reference/android/view/ViewGroup.html
I don't see an element ViewGroup in the xml. You need to add it in the same xml or include it.
As per your xml there is not an any id with ViewGroup in your xml code. please give id to your layout with ViewGroup and cast it as below,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
there was also a spelling mistake in your line,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
As they pointed out, you have no R.id.ViewGroup in your layout.
If you really need the view group try this:
viewGroup = (ViewGroup)findViewById(android.R.id.content)
The problem is that you have,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
and you have to change it for this,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
The V must be a capital letter.
Also watch for the id of your Viewgroup in your xml, check if it is R.id.Viewgroup or R.id.viewgroup
I have several Views, text views, and a button that have the android:visibility="invisible" attribute. My goal is to click a button that resides above these 'invisible' widgets, so that these widgets will become visible. I created another java class called 'VisibilityActivity.java" and tried the following method. But for some reason when I run the app, the button doesn't do anything. I don't know what I'm missing.
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VisibilityActivity extends Activity {
private View mVictim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mVictim = findViewById(R.id.horizontalRule1);
mVictim = findViewById(R.id.TextView03);
mVictim = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_background"
android:isScrollContainer="true"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/ratingsInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/yourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp" />
<Button
android:id="#+id/submitRatingButton"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_button"
android:text="#string/submitRating"
android:textColor="#color/black1" />
<View
android:id="#+id/horizontalRule1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/summaryInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1"
android:visibility="invisible" />
<View
android:id="#+id/horizontalRule2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/ourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/overallRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar3"
android:color="#color/black1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="40dp"
android:visibility="invisible" />
<Button
android:id="#+id/saveContinueButton3"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:background="#drawable/custom_button"
android:text="#string/saveContinue"
android:textColor="#color/black1"
android:onClick="onSaveAndContinue3Clicked"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Thanks. Help would be greatly appreciated!
I am updating user936414's answer.
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
if( mText.getVisibility() == View.INVISIBLE )
mText.setVisibility(View.VISIBLE);
else
mText.setVisibility(View.INVISIBLE);
if( mRule1.getVisibility() == View.INVISIBLE )
mRule1.setVisibility(View.VISIBLE);
else
mRule1.setVisibility(View.INVISIBLE);
if( mRule2.getVisibility() == View.INVISIBLE )
mRule2.setVisibility(View.VISIBLE);
else
mRule2.setVisibility(View.INVISIBLE);
}
};
Also you might want to experiment with View.GONE.
findViewById(R.id.ratingBar3).setVisibility(View.VISIBLE);
findViewById(R.id.saveContinueButton3).setVisibility(View.VISIBLE);
you made it invisible view invisible again.. try the above code
Try
public class VisibilityActivity extends Activity {
private TextView mText;
private View mRule1, mRule2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mText= (TextView)findViewById(R.id.horizontalRule1);
mRule1 = findViewById(R.id.TextView03);
mRule2 = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mText.setVisibility(View.VISIBLE);
mRule1.setVisibility(View.VISIBLE);
mRule2.setVisibility(View.VISIBLE);
}
};
}
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(new View.onClickListener)
{
#Override
public void onClick(View v)
{
//Insert your code here
}
}