Java TextView Onclick() returning expression error - java

I am really new to java and I came across the following 2 errors error: illegal start of expression public void registerClick(View v) and error: class, interface, or enum expected }
I don't understand what I am doing wrong. I have watched extensive tutorials on Youtube and gone to the Android Studio docs but I can not find out what is wrong with my program. I am trying to go from a signup screen to a register screen if a certain text view is clicked. My code is down below.
Main Activity.java:
package com.example.gooddeed;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Config Stuff
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//end of config stuff
public void registerClick(View v)
{
TextView tv= (TextView) findViewById(R.id.register);
}
}
}
Activity Main.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="235dp"
android:background="#drawable/blue_bg" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="36.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_border_rounded_cornwe">
<!-- INPUT -->
<EditText
android:id="#+id/email_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/username_icon"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_regular"
android:hint="Email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textCursorDrawable="#drawable/cursor_color"
android:textSize="12sp" />
<!-- ICON -->
<ImageView
android:id="#+id/username_icon"
android:layout_width="14.7dp"
android:layout_height="10.3dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="#drawable/email" />
</RelativeLayout>
<!-- Spacer Button -->
<!-- Spacer Button -->
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="14dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="12.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_border_rounded_cornwe">
<!-- INPUT -->
<EditText
android:id="#+id/password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_regular"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textCursorDrawable="#drawable/cursor_color"
android:textSize="12sp" />
<!-- ICON -->
<ImageView
android:id="#+id/icon"
android:layout_width="14.7dp"
android:layout_height="10.3dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="#drawable/password" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="18.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_fill__rounded_color"
android:fontFamily="#font/poppins_medium"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/forgotpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:fontFamily="#font/poppins_medium"
android:text="FORGOT PASSWORD?"
android:textColor="#1566e0"
android:textSize="10.5sp" />
<TextView
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:fontFamily="#font/poppins_medium"
android:text="Don't have an account? Make one for free!"
android:textColor="#1566e0"
android:textSize="13sp"
android:clickable="true"
android:onClick="registerClick"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="13.7dp"
android:gravity="center"
android:orientation="horizontal"/>
</LinearLayout>
</RelativeLayout>
Thank you to everyone that helps!

You should have registerClick as a separate method inside the activity because Java does not support directly nested methods
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Config Stuff
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//end of config stuff
}
public void registerClick(View v) {
TextView tv= (TextView) findViewById(R.id.register);
}
}

Related

The button is not visible which is going to connect another activity in android studio

my friends and I are creating an android app for our project and we are new to this and if you think the code is repeated then please post the link.
We have referred from following YouTube video- https://youtu.be/wbsWVtCdiW4
For now we are creating a basic structure for our app. The following is the code entered activity_main.xml which is for button which should connect to other activity but after running the code on emulator and in my phone the app shows a blank screen.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="157dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="119dp"
android:layout_marginLeft="119dp"
android:layout_marginTop="251dp"
android:layout_marginEnd="97dp"
android:layout_marginRight="97dp"
android:layout_marginBottom="323dp"
android:background="#android:color/holo_blue_light"
android:onClick="startdbapp"
android:text="start db app"
android:textColor="#F8000000"
android:visibility="visible"
tools:visibility="visible" />
</RelativeLayout>
this is the MainActivity.java
package com.login.sqlitedemoapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startdbapp(View view) {
new DbManager(this);
startActivity(new Intent(this, InsertData.class));
}
}
this is my 2nd activity file named activity_insert_data.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".InsertData">
<EditText
android:id="#+id/t1"
android:layout_width="242dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="94dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="99dp"
android:layout_marginEnd="75dp"
android:layout_marginRight="80dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:hint="Enter_Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/t2"
android:layout_width="242dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="89dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="194dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginBottom="492dp"
android:ems="10"
android:hint="Enter_Email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/t3"
android:layout_width="241dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="88dp"
android:layout_marginLeft="88dp"
android:layout_marginTop="290dp"
android:layout_marginEnd="82dp"
android:layout_marginRight="82dp"
android:layout_marginBottom="396dp"
android:ems="10"
android:hint="Enter_Course"
android:inputType="textPersonName" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="68dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="76dp"
android:layout_marginLeft="76dp"
android:layout_marginTop="395dp"
android:layout_marginEnd="62dp"
android:layout_marginRight="62dp"
android:layout_marginBottom="268dp"
android:onClick="addRecord"
android:text="Insert Record" />
</RelativeLayout>
This it the Main activity named InsertData.java
package com.login.sqlitedemoapp;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class InsertData extends AppCompatActivity
{
EditText t1,t2,t3;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert_data);
t1=(EditText)findViewById(R.id.t1);
t2=(EditText)findViewById(R.id.t2);
t3=(EditText)findViewById(R.id.t3);
}
public void addRecord(View view)
{
DbManager db = new DbManager(this);
String res = db.addRecord(t1.getText().toString(), t2.getText().toString(),
t3.getText().toString());
Toast.makeText(this, res, Toast.LENGTH_LONG).show();
t1.setText("");
t2.setText("");
t3.setText("");
}
}
I would recommend you to use a constraint layout. Relative layout is old and most of the developers avoid them. Also, check the constraints of the button.

Text view doesn't show after button click - Android

I have a problem with Text View and Button in Android Studio. I would like to update my text view when i clik a button but it doesn't work.
This is my code:
activity_distance_to_run.xml
<?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"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="25dp">
<TextView
android:id="#+id/Distancerun2"
android:layout_width="126dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:text="Distance to ....."
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/distancetorun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:text="run"
app:layout_constraintBaseline_toBaselineOf="#+id/distancetoride"
app:layout_constraintEnd_toStartOf="#+id/distancetoride"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/distancetoride"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_marginTop="32dp"
android:text="ride a bicycle"
app:layout_constraintEnd_toStartOf="#+id/distancetoswim"
app:layout_constraintStart_toEndOf="#+id/distancetorun"
app:layout_constraintTop_toBottomOf="#+id/Distancerun2" />
<Button
android:id="#+id/distancetoswim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="swim"
app:layout_constraintBaseline_toBaselineOf="#+id/distancetoride"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/distancetoride" />
<Button
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:text="I did it !!"
app:layout_constraintBaseline_toBaselineOf="#+id/no"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="116dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="I don't want to do it !!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="23dp"
android:layout_marginEnd="25dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="221dp"
android:autoText="true"
android:clickable="true"
android:enabled="false"
android:freezesText="false"
android:text="Elo ziomek"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/no"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
distancetorun.java
package pl.agcode.sqliteexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class distance_to_run extends Activity {
Button distancetorun;
TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distance_to_run);
distancetorun = (Button) findViewById(R.id.distancetorun);
txtView = (TextView) findViewById(R.id.textView1);
distancetorun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText("NewText");
}
});
}
}
Thanks for help I will try to repair it but don't know how.
This is very important to repair this for me. :)
Looks all good except textview1 color.
Add below line for the textview1 in layout xml
<TextView
android:id="#+id/textView1"
android:textColor="#android:color/black"
After launching the app
After clicking RUN button

How to open fragments containing webViews in another activity on button click from fragments of another activity?

I have image-buttons in one of the fragments of an activity and want to open corresponding fragments containing webViews on button clicks in another activity.
I am a beginner so please do give appropriate codes for this.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.administrator.hiha.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar">
</include>
<fragment
android:id="#+id/upperFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.administrator.hiha.Upper_main_Fragment"
android:layout_below="#id/toolbar"
>
</fragment>
<fragment
android:layout_below="#id/upperFragment"
android:id="#+id/lowerFragment"
android:layout_width="match_parent"
android:layout_height="140dp"
android:name="com.example.administrator.hiha.Lower_main_Fragment"
>
</fragment>
<fragment
android:layout_below="#id/lowerFragment"
android:id="#+id/Bottom_Most_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.administrator.hiha.BottomMostFragment"
tools:layout="#layout/fragment_bottom_most">
</fragment>
<View
android:id="#+id/hLastRow"
android:layout_centerHorizontal="true"
android:layout_below="#id/Bottom_Most_Fragment"
android:layout_width="300dp"
android:layout_height="2dp"
android:background="#EEEEEE"
/>
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hLastRow"
android:layout_centerHorizontal="true"
android:text="Designed \u0026 Developed by me © 2016."/>
</RelativeLayout>
</ScrollView>
MainActivity.java
package com.example.administrator.hiha;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
//Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar=(Toolbar)findViewById(R.id.toolbar);
}
}
fragment_lower_main.xml
<FrameLayout 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="com.example.administrator.hiha.Lower_main_Fragment">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/about_board"
android:id="#+id/about_img_btn"
android:onClick="onAboutBoardClick"
/>
<TextView
android:id="#+id/text_about_img_btn"
android:text="About Board"
android:layout_below="#id/about_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/sarasvati"
android:id="#+id/sarasvati_img_btn"
android:layout_toRightOf="#id/about_img_btn"
android:onClick="onSarasvatiClick"
/>
<TextView
android:layout_toRightOf="#id/text_about_img_btn"
android:id="#+id/text_sarasvati_img_btn"
android:text="Sarasvati"
android:layout_below="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/scientific_evidences"
android:id="#+id/scientific_evidences_img_btn"
android:onClick="onScientificEvidencesClick"
/>
<TextView
android:layout_toRightOf="#id/text_sarasvati_img_btn"
android:id="#+id/text_scientific_evidences_img_btn"
android:text="Scientific EVidences"
android:layout_below="#id/scientific_evidences_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/digital_library_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/affltd_organization"
android:id="#+id/affltd_oragnization_img_btn"
android:onClick="onAffltdOrganizationClick"
/>
<TextView
android:layout_toRightOf="#id/text_digital_library_img_btn"
android:id="#+id/text_affltd_oragnization_img_btn"
android:text="Affiliated Organizations"
android:layout_below="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/tender"
android:id="#+id/tender_img_btn"
android:onClick="onTenderClick"
/>
<TextView
android:layout_toRightOf="#id/text_affltd_oragnization_img_btn"
android:id="#+id/text_tender_img_btn"
android:text="Tenders"
android:layout_below="#id/tender_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
Lower_Main_Fragment.java
package com.example.administrator.hiha;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Lower_main_Fragment extends Fragment {
ImageButton about_img_btn;
ImageButton sarasvati_img_btn;
ImageButton scientific_evidences_img_btn;
ImageButton digital_library_img_btn;
ImageButton affltd_oragnization_img_btn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView=inflater.inflate(R.layout.fragment_lower_main_,container,false);
//cast image buttons
about_img_btn=(ImageButton)getView().findViewById(R.id.about_img_btn);
sarasvati_img_btn=(ImageButton)getView().findViewById(R.id.sarasvati_img_btn);
scientific_evidences_img_btn=(ImageButton)getView().findViewById(R.id.scientific_evidences_img_btn);
digital_library_img_btn=(ImageButton)getView().findViewById(R.id.digital_library_img_btn);
affltd_oragnization_img_btn=(ImageButton)getView().findViewById(R.id.affltd_oragnization_img_btn);
return rootView;
}
//onclick methods of image buttons
public void onAboutBoardClick(View view){
}
}
These are the files in which i want that if i click on about_img_btn then it should open a fragment containing a webView in another activity. The same should be repeated for all the buttons.
Please define the further code that i have to use and mention the files where i have to use it. Thanks!
try this:
in your fragment_lower_main xml:
Give an id to the Framelayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<FrameLayout
android:id="+#id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<--Rest of the code -->
/>
</LinearLayout>
Now in code open new Fragment during button click using:
about_img_btn=(ImageButton)rootView.findViewById(R.id.about_img_btn);
about_img_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AboutUS fragment = new AboutUS();
Replace_fragment(fragment); //pass the fragment u want to replace
}
});
public void ReplaceFragment(Fragment r_fragment) {
FragmentManager fragmentManager = getActivity.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, r_fragment).commit();
}

I wanted the TextView to appear over the Button instead of Under it. How can I do that?

I want the text to appear on the button and not under it.And How can I give more weightage to the TextView rather than the Button background Colour.I was building an App TicTacToe and I came across this Issue.I'm not able to solve it.Can someone please guide me?
Activity(MainActivity.java):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends
AppCompatActivity implements View.OnClickListener {
TextView textone;
Button button,buttonPanel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textone = (TextView)findViewById(R.id.textone);
button = (Button)findViewById(R.id.button);
buttonPanel = (Button)findViewById(R.id.buttonPanel);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button){
textone.setText("This is not Fair");
}
}
}
Layout(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">
<Button
android:id="#+id/buttonPanel"
android:text="#string/button"
android:textSize="40sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f4f18a"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/textone"
android:id="#+id/textone"
android:textSize="45sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button2"
android:id="#+id/button"
android:layout_marginBottom="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
If you have <LinearLayout ... as a parent of your layout, You need to add your <TextView .. and <Button .. like as below
<LinearLayout layout_height=".." layout_width=".." orientation="vertical">
<TextView layout_height=".." layout_width=".." id="#+id/textone"/>
<Button layout_height=".." layout_width=".." id="#+id/button" />
<!-- your other layout view -->
</LinearLayout>
2.If you have <RelativeLayout ... as a parent of your layout, You need to add your <TextView .. and <Button .. as below
<RelativeLayout layout_height=".." layout_width=".." orientation="vertical">
<TextView layout_height=".." layout_width=".." id="#+id/textone"/>
<Button layout_height=".." layout_width=".." id="#+id/button" android:layout_below="#id/textone" />
<!-- your other layout view -->
</RelativeLayout>

Android NullPointer Exception in Activity's onCreate

I have a simple application to multiply two numbers from text boxes and display result in third box. There is no any syntax errors in the code but when I am running an application i get this error: application has stopped unexpectedly.
Here is the java code:
package c.example.rectangle;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
EditText l = (EditText) findViewById(R.id.length);
EditText w = (EditText) findViewById(R.id.width);
TextView a = (TextView) findViewById(R.id.lblarea);
Button b = (Button) findViewById(R.id.calculate);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b.setOnClickListener(this);
}
public void onClick(View v) {
calculateRectangle(l.getText().toString(), w.getText().toString());
}
private void calculateRectangle(String clength, String cwidth){
int area = Integer.parseInt(clength)*Integer.parseInt(cwidth);
b.setText(String.valueOf(area));
}}
And here is my XML file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#8B4513"
android:orientation="vertical" >
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/rect"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/cm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/length"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:ems="10"
android:gravity="center"
android:inputType="number" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2F4F4F"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="#string/breadth"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/width"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="33dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:inputType="number"
android:ems="10"
android:gravity="center"
>
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="fill_parent"
android:layout_marginLeft="100dip"
android:layout_marginRight="100dip"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:layout_marginTop="20dp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:text="#string/area"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblarea"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
Please help.
I know, that you got correct answer, but I just want to explain you why you need to write code as #Mr.Me says.
You describe your View elements in start of class, and trying to initialize them there. It is not correct. Because you have not attached layout file to activity at the moment when constructor will run your initialization of Views objects. As you can see, you are using findViewById() method, but before use it you should call setContentView().
For better understanding, read Activity Lifecycle, pay attention to rendering proccess.
Rearrange your code to look like this:
EditText l;
EditText w;
TextView a;
Button b;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (EditText) findViewById(R.id.length);
w = (EditText) findViewById(R.id.width);
a = (TextView) findViewById(R.id.lblarea);
b = (Button) findViewById(R.id.calculate);

Categories