Charlotte Windels
Charlotte Windels

Reputation: 1

Tabs > fragments with different layout + actions

I'm new to android studio / java language.
I need to set up a pretty straightforward app, but the information I find doesn't let me solve the problem.
Can any of you help :)?

I want to make an app with 3 tabs

I've implemented a (for this use, simplified) code for the first tab.

I know how to code all the three tabs separately, but I don't know how to merge them together in one app with 3 tabs.

I started with the tabs-template given in android studio, but it demands that every tablayout is the same. I've seen a lot of answers how to have different layouts for each tab, but how do I code the different tabs (e.g. setonclicklistener).

Second problem is that every solution uses android and I have androidx, so the imports won't take. And in dependencies I don't find design V7 or anything of that sort.


Mainactivity.java:

package com.example.soloapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
    DecimalFormat numberFormat = new DecimalFormat("#.0");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button calcButton = (Button) findViewById(R.id.calcButton);
        calcButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                EditText editNum = (EditText) findViewById(R.id.editNum);
                TextView resultTextView = (TextView) findViewById(R.id.resultTextView);

                double cysC = Double.parseDouble(editNum.getText().toString());
                double tempResult = Math.pow(cysC,-0.931);
                double resultLong = 70.69 * tempResult;
                String result = numberFormat.format(resultLong);
                resultTextView.setText(result);

            }


        });

    }
}

activy_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintBottom_toTopOf="@+id/resultTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="168dp"
        android:text="Result"
        app:layout_constraintBottom_toTopOf="@+id/calcButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/calcButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="200dp"
        android:text="CALCULATE"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 28

Answers (1)

Vedprakash Wagh
Vedprakash Wagh

Reputation: 3732

I'll add what you need to learn in order to create what you told.

  1. You need to use ViewPager in Android to create swipeable tabs. (If you know WhatsApp, those swipeable three tabs are ViewPager).

    You can learn more about ViewPager here.

  2. For using ViewPagers, you need to learn what Fragments are, Fragments in Android are like small activities(but not activities). You embed these Fragments inside your Activities, so you need to put these Fragments inside the Activity that contains your ViewPager.

You can learn about Fragments here.

Although, the first ViewPager link will be sufficient for you to learn everything that you need to learn about creating Swipeable Tabs.

About the second problem that you mentioned.

According to Migrating to AndroidX,

Androidx only maps the original support library API packages into the androidx namespace.

Basically, they just renamed the package name so that it'd be easy for them to support the updates to libraries.

You can easily find the corresponding androidx package from here.

Migrating to Androidx.

Upvotes: 1

Related Questions