Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

How to put if statement inside button's OnClickListener in Android

Course Queries Syllabus Queries
Max. 2000 characters
Replies

usr_profile.png
Harsh Vashishth

User

( 4 months ago )

 


I am working on my major project and I want my button to open different intents based on the string array value obtained. I used if and else if statements inside the OnClickListener of the button but it won't click anymore. Please help me.

This is my XML file:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:cardCornerRadius="3dp"
    app:cardElevation="2dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/spacing_large">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/spacing_middle"
        android:text="Syllabus"
        android:textAppearance="@style/TextAppearance.AppCompat.Title" />

    <Button
        android:id="@+id/buttonpdf"
        style="@style/Widget.AppCompat.Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="Open Book" />

</LinearLayout> 

And this is the Java file for the same activity:

public class ActivityBookDetails extends AppCompatActivity {

    public static final String EXTRA_OBJCT = "com.app.sample.recipe.OBJ";

    private Book book;
    private FloatingActionButton fab;
    private View parent_view;

    Button button;
    String[] obj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_book_details);
        parent_view = findViewById(android.R.id.content);
        button = (Button)findViewById(R.id.buttonpdf);


        book = (Book) getIntent().getSerializableExtra(EXTRA_OBJCT);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fabToggle();
								 								 
							usr_profile.png
							
Raman Tripathi

User

( 4 months ago )

You probably shouldn't try to add conditionals inside the OnClickListener, instead the simplest thing to do would be to move your conditional logic to another method for example.

@Override
public void onClick(View view){
    goToNextView();
}

private void goToNextView(){
    if (title_subjects[0] == "Soft Computing") {
        Intent intent = new Intent(ActivityBookDetails.this, pdfviewactivity.class);
        startActivity(intent);
    }
    // else if {}
    // else {}
}

As cricket_007 already suggested there are nicer ways than using if/else if statements. If you plan to add a lot more options maybe consider using an enum or map.

public enum Subjects {
    SOFT_COMPUTING("Soft Computing", pdfviewactivity.class),
    WEB_ENGINEERING("Web Engineering", pdfweben.class),
    NETWORK_MANAGEMENT("Wireless Network", pdfnetwork.class),
    WIRELESS_NETWORK("Network Management", pdfwireless.class);

    private String name;
    private Class clazz;

    Subjects(String name, Class clazz){
        this.name = name;
        this.clazz = clazz;
    }

    public static Class getClass(String title_subject) {
        for(Subjects subject: Subjects.values()) {
            if (subject.name.equals(title_subject)) {
                return subject.clazz;
            }
        }
        return null;
    }
}

private void goToNextView() {

    //Alternative to conditionals using Enum
    Class theClassToGoTo = Subjects.getClass(title_subjects[0]);
    Intent intent = new Intent(ActivityBookDetails.this, theClassToGoTo);
    startActivity(intent);

   //Alternative to conditionals using HashMap
    Map<String, Class> subject_map = new HashMap<>();
    subject_map.put("Soft Computing", pdfviewactivity.class);
    subject_map.put("Web Engineering", pdfweben.class);
    subject_map.put("Wireless Network"
			
        

what's your interest