-2

I have made a string in java by combining two other strings but how can I use that string in my xml file in android studio. I am new to android development.

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:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:padding="24dp">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start"
            android:text="Hello"
            android:textColor="#114a60"
            android:textSize="36sp"
            android:textStyle="bold" />

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:text="Login to continue"
            android:textSize="20dp" />


    <EditText
            android:id="@+id/activity_main_usernameEditText"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/roundedittext"
            android:hint="CNIC"
            android:inputType="text"
            android:paddingLeft="15dp"
            android:textColorHint="#aeaeae" />


    <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/etPasswordLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/roundedittext"
            card_view:passwordToggleEnabled="true">

            <EditText

                android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="63dp"
                android:background="@drawable/roundedittext"
                android:hint="Write password"
                android:inputType="textPassword"
                android:textColorHint="#aeaeae" />
        </com.google.android.material.textfield.TextInputLayout>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="forgot password?" />

    <androidx.appcompat.widget.AppCompatButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="250dp"
            android:background="@drawable/roundbutton"
            android:text="Login"
            android:textColor="#ffffff" />

    <TextView
            android:id="@+id/Sign"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            />


</LinearLayout>

My MainActivity.java

package com.example.loginapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity
{

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

        TextView textView = findViewById(R.id.Sign);

        String String1 = getString(R.string.DontHaveAcc);
        String String2 = getString(R.string.Sign_Up);

        String Merged;
        Merged = String1 + " " + String2;

        textView.setText(Merged);
    }
}

Ive tried multiple different variations which i have seen on here but i am unable to get the text to show up in the view. please explain it in simplet terms since i am pretty new to programming

1 Answers1

1

Let just assume that below is the String you generated in java

String mString = "Your generated STRING!!!";

If I understand your question correctly you want to set this String to your xml design TextView , if that's the case you must already have a TextView in your design UI(your xml) where you want to display . If you already done with these than you code like this -

TextView textView = findViewById(R.id.youXmlTextViewID);
textView.setText(mString);

After this you will be able to see your generated String to your xml TextView. Since your a beginner at Android these is the most basic way to set your java String to your design XML.

If this answer works for you , mark the answer as Accepted.

JustGotStared
  • 86
  • 1
  • 11