I have made a CustomTypeDialog class and what I want is to use EditText which is not in the active layout. I get a nullpointer exception when I try to click one of the buttons, which I think is because they are not in the active layout. Can you help me solve this? The dialog is called in an activity from another class.
package dk.droidrun.droidrunapp;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class CustomTypeDialog extends Dialog {
ImageButton routeType;
EditText txtType;
Button imageRun, imageBike, imageWalk;
public CustomTypeDialog(final Context context) {
super(context);
this.setContentView(R.layout.customtype_dialog);
routeType = (ImageButton)findViewById(R.id.saveRoute_activityType);
txtType = (EditText)findViewById(R.id.saveRoute_typeTxt);
imageRun = (Button)findViewById(R.id.dialog_btn1);
imageBike = (Button)findViewById(R.id.dialog_btn2);
imageWalk = (Button)findViewById(R.id.dialog_btn3);
setTitle("Select activity type");
show();
imageRun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});
imageBike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Bike");
routeType.setBackgroundResource(R.drawable.track_bike);
}
});
imageWalk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Walk");
routeType.setBackgroundResource(R.drawable.track_walk);
}
});
}
}
This is my customtype_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".AutoMode"
android:background="#color/black" >
<RelativeLayout
android:id="#+id/dialog_relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_above="#+id/dialog_relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/dialog_btn1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/track_run"
android:layout_alignRight="#+id/dialog_relativeLayout1"
android:layout_alignTop="#+id/dialog_relativeLayout1"
/>
<Button
android:id="#+id/dialog_btn2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/track_bike"
android:layout_alignTop="#+id/dialog_relativeLayout1"
android:layout_toRightOf="#+id/dialog_btn1"
/>
<Button
android:id="#+id/dialog_btn3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/dialog_btn2"
android:background="#drawable/track_walk"
/>
</RelativeLayout>
</RelativeLayout>
saveroutes.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"
android:orientation="vertical"
android:background="#color/black"
tools:context=".SaveRouteActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="#string/saveRoute"
android:textColor="#color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Enter a name for the route" />
<EditText
android:id="#+id/saveRoute_nameRoute"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="#string/saveRoute_name"
android:textColor="#color/white"
android:background="#4e4751"
android:inputType="textPersonName" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Describe your route" />
<EditText
android:id="#+id/saveRoute_desciptionTxt"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="#string/saveRoute_description"
android:textColor="#color/white"
android:background="#4e4751"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Activity type (e.g. running)"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal" >
<EditText
android:id="#+id/saveRoute_typeTxt"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="17dip"
android:ems="10"
android:layout_marginLeft="30dp"
android:hint="#string/saveRoute_type"
android:textColor="#color/white"
android:background="#4e4751" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/saveRoute_activityType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/track_walk" />
</LinearLayout>
<Button
android:id="#+id/saveRoute_saveBtn"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#color/white"
android:text="#string/saveRoute_savebutton" />
<Button
android:id="#+id/saveRoute_cancelBtn"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#color/white"
android:text="#string/saveRoute_cancel" />
</LinearLayout>
You cannot access views from another layout (your activity) using findViewById within the dialog view.
You need to add a callback listener for when the buttons on your dialog are clicked:
public interface OnDialogClickListener {
void onDialogImageRunClick();
}
public class CustomTypeDialog extends Dialog {
private final OnDialogClickListener listener;
public CustomTypeDialog(final Context context, OnDialogClickListener listener) {
this.listener = listener;
}
....
imageRun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onDialogImageRunClick();
}
);
}
Then when you create your dialog in your Activity where you have access to the view:
new CustomTypeDialog(context, new CustomTypeDialog.OnDialogClickListener() {
#Override
public void onDialogImageRunClick() {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});
Related
knowing that there is no errors, only MainActivity shows without homebutton................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
MainActivity.java
package com.example.AppCalculator;
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;
public class MainActivity extends AppCompatActivity {
EditText etfirstvalue,etsecondvalue;
Button btnadd,btnsubs,btnmultiply,btndivide;
Double num1,num2;
TextView tvresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etfirstvalue=findViewById(R.id.etfirstvalue);
etsecondvalue=findViewById(R.id.etsecondvalue);
btnadd=findViewById(R.id.btnadd);
btndivide=findViewById(R.id.btndivision);
btnmultiply=findViewById(R.id.btnmultiply);
btnsubs=findViewById(R.id.btnsubs);
tvresult=findViewById(R.id.tvresult);
Clicklistener();
}
public void Clicklistener(){
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1+num2;
tvresult.setText(String.valueOf(result));
}
});
btnsubs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1-num2;
tvresult.setText(String.valueOf(result));
}
});
btnmultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1*num2;
tvresult.setText(String.valueOf(result));
}
});
btndivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1/num2;
tvresult.setText(String.valueOf(result));
}
});
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#FFFFFF"
android:backgroundTint="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Simple Calculator"
android:textSize="25sp" />
<EditText
android:id="#+id/etfirstvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter First Value"
android:inputType="number" />
<EditText
android:id="#+id/etsecondvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Second Value"
android:inputType="number" />
<TextView
android:id="#+id/tvresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Result"
android:textColor="#color/purple_500"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btnadd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="ADD" />
<Button
android:id="#+id/btnsubs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Subs" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btnmultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Multiply" />
<Button
android:id="#+id/btndivision"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Divide" />
</LinearLayout>
</LinearLayout>
homebutton.java
package com.example.AppCalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class homebutton extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homebutton);
Button btncalc=findViewById(R.id.btncalc);
btncalc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(homebutton.this, MainActivity.class);
startActivity(intent);
}
});
}
}
homebutton.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".homebutton"
tools:ignore="ExtraText">
<Button
android:id="#+id/btncalc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Calculator" />
</LinearLayout>
You need to clarify your question more. However I believe you meant homebutton should start first and after you click calculate it should take you to the second one.
If that's the case, then you need to define your start activity from the AndroidManifest because currently your app is starting on MainActivity.java
I am try to save my roll no and name in main activity but when my screen is rotated the activity data has been lose ,
finally i fixed it , below i adding source code
consider any layout , here i am using this layout structure
In activity_xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/rollnoID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Rollno : "
android:textSize="25dp" />
<EditText
android:hint="Roll Number"
android:id="#+id/rollnoID_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:ems="8"
android:inputType="number"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/nameID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Name : "
android:textSize="25dp" />
<EditText
android:hint="Student Name"
android:id="#+id/nameID_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:ems="8"
android:inputType="textCapWords"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<Button
android:onClick="saveData"
android:id="#+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save this Data"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:gravity="center"
android:hint="~RollNo~"
android:textColor="#color/colorPrimary"
android:id="#+id/display_rollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
<TextView
android:layout_marginTop="20dp"
android:id="#+id/display_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="~Name~"
android:textColor="#color/colorPrimary"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
In MainActivity :
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 android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String KEY_ROLL = "rollno_key";
private static final String KEY_NAME = "name_key";
int rollno;
String name;
EditText roll_et , name_et;
TextView roll_tv , name_tv;
Button saveButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
roll_et = (EditText) findViewById(R.id.rollnoID_edit);
name_et = (EditText) findViewById(R.id.nameID_edit);
roll_tv = (TextView) findViewById(R.id.display_rollno);
name_tv = (TextView) findViewById(R.id.display_name);
if (savedInstanceState != null){
String save_RollNO = savedInstanceState.getString(KEY_ROLL);
roll_tv.setText(save_RollNO);
String save_Name = savedInstanceState.getString(KEY_NAME);
name_tv.setText(save_Name);
} else {
Toast.makeText(getApplicationContext(),"Entry your data",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onSaveInstanceState(Bundle saveData) {
saveData.putString(KEY_ROLL , roll_et.getText().toString());
saveData.putString(KEY_NAME , name_et.getText().toString());
super.onSaveInstanceState(saveData);
}
public void saveData(View v){
roll_tv.setText(roll_et.getText().toString().trim());
name_tv.setText(name_et.getText().toString().trim());
}
}
I am trying to create radio buttons that allows the users to select one of the three options. Once selected, the option should have a border, while the others do not.
Here is an image of what I am trying to create:
I'm not sure how to go about this. Any suggestions are greatly appreciated.
You could create a group of objects (which are the options) and programmatically ensure that only one of them can be selected.
selection could be presented as you wish it would be, I'd give a code example that imitates approximately the image you've attached. here's the structure:
XML file: CardViews with base of invisible FrameLayout (will be functioning as stroke), above the base is the cards' content
Java Class: setChecked(MaterialCardView selected) method that selects the chosen card (making it's 'stroke' visible) and checkedCard() that returns the selected card
so here's the code:
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"
android:background="#color/parentBackground"
tools:context=".MainActivity">
<com.google.android.material.card.MaterialCardView
android:id="#+id/public_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/public_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="visible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card1Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_public" />
<TextView
android:id="#+id/card1Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card1Icon"
android:text="Public Profile"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card1Label"
android:layout_alignStart="#id/card1Label"
android:text="Everyone can see your account. \nIncluding non followers."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/friends_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/public_card"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/friends_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card2Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_friends" />
<TextView
android:id="#+id/card2Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card2Icon"
android:text="Friends Only"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card2Label"
android:layout_alignStart="#id/card2Label"
android:text="Connections nly. Only your friends \nand friends of friends can see your account."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/private_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/friends_card"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/private_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card3Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_private" />
<TextView
android:id="#+id/card3Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card3Icon"
android:text="Private"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card3Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card3Label"
android:layout_alignStart="#id/card3Label"
android:text="Only you can see your account."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.google.android.material.card.MaterialCardView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public MaterialCardView cPublic, cFriends, cPrivate;
public FrameLayout sPublic, sFriends, sPrivate;
public ArrayList<MaterialCardView> cards;
public ArrayList<FrameLayout> strokes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = new ArrayList<>();
strokes = new ArrayList<>();
cPublic = findViewById(R.id.public_card);
cFriends = findViewById(R.id.friends_card);
cPrivate = findViewById(R.id.private_card);
cards.add(cPublic);
cards.add(cFriends);
cards.add(cPrivate);
sPublic = findViewById(R.id.public_stroke);
sFriends = findViewById(R.id.friends_stroke);
sPrivate = findViewById(R.id.private_stroke);
strokes.add(sPublic);
strokes.add(sFriends);
strokes.add(sPrivate);
cPublic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cPublic);
}
});
cFriends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cFriends);
}
});
cPrivate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cPrivate);
}
});
}
public void setChecked(MaterialCardView selected) {
int index = cards.indexOf(selected);
FrameLayout stroke = strokes.get(index);
stroke.setVisibility(View.VISIBLE);
for (FrameLayout s : strokes) {
if (!s.equals(stroke)) {
s.setVisibility(View.INVISIBLE);
}
}
}
public MaterialCardView checkedCard() {
int index = 0;
for (FrameLayout s : strokes) {
if (s.getVisibility() == View.VISIBLE) {
index = strokes.indexOf(s);
}
}
return cards.get(index);
}
}
Hope it could help you (:
I'm really new in coding for Android, just started to learn. I have some image buttons to change values displayed in textboxes and I don't know how to do this. I tried some tests for buttons but I have some errors which I can't repair.
Message from eclipse is:
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
and
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
for setOnClickListener and OnClickListener
I looked at this thread but still I don't know what to do
setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SequencerActivity) back.setOnClickListener(this);
Except that, I want to change the Medium text fields with the buttons. Perhaps by the declared integers.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
int vs,vh,ws,wh,wth,wts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.imageButton1);
b1.setOnClickListener(new OnClickListener()
{
protected void onClick(View v)
{
Toast.makeText(ApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});
}
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:background="#012e53"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context="pl.tmalachowski.truewindcalc.MainActivity" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="26dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:orientation="vertical"
android:textColor="#bdc3c7" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center" >
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_width="62dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView2"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/ves2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView3"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rel2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/podst" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/arrow_up_float" />
</LinearLayout>
I think you are using your onClickListener wrong.
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});
Please try this usage. Hope it helps
Declare like this ImageButton b1; instead of Button b1;
then change to like this following code
b1=(ImageButton) findViewById(R.id.imageButton1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
}
});
main activity.java
package abhilmohan.blogspot.com;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
EditText amount1;
EditText amount2;
EditText amount3;
EditText amount4;
Button calculate;
double w=0;
double x=0;
double y=0;
double z=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar= getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
}
public void initcontrols() {
amount1=(EditText)findViewById(R.id.editText1);
amount2=(EditText)findViewById(R.id.editText2);
amount3=(EditText)findViewById(R.id.editText3);
amount4=(EditText)findViewById(R.id.editText4);
calculate=(Button)findViewById(R.id.button1);
}
public void calculate() {
w=Double.parseDouble(amount1.getText().toString());
x=Double.parseDouble(amount2.getText().toString());
y=w/12;
amount3.setText(Double.toString(y));
z=w*x/100;
amount4.setText(Double.toString(z));
}
public void gotoactivity (View v) {
Intent intent = new Intent(this,ResultPage.class);
calculate();
startActivity(intent);
}
am not getting result while calling calculator() void method on button click.i want my results to be published in two textviews created in reulst_page layout
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:gravity="left"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="abhilmohan.blogspot.com.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_marginRight="40dp"
android:text="#string/ctc"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="-15dp"
android:inputType="number"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:padding="20dp"
android:paddingBottom="50dp"
android:textColor="#000000" />
<TextView
android:id="#+id/textView2"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="-30dp"
android:text="#string/TDS"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="-70dp"
android:background="#drawable/rounded_edit_text"
android:inputType="number"
android:ems="10"
android:padding="20dp"
android:paddingBottom="50dp" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:background="#drawable/button_style"
android:text="#string/ok"
android:textColor="#ffffff"
android:onClick="gotoactivity" />
</LinearLayout>
</RelativeLayout>
resultpage.java
package abhilmohan.blogspot.com;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
public class ResultPage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_page);
getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar bar= getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
result_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_marginRight="40dp"
android:text="#string/amount"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="-15dp"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:inputType="number"
android:padding="20dp"
android:paddingBottom="50dp"
android:textColor="#000000"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="#+id/textView2"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="-30dp"
android:text="#string/tdsamount"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="-70dp"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:inputType="number"
android:padding="20dp"
android:paddingBottom="50dp"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button2"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:background="#drawable/button_style"
android:text="#string/rate"
android:textColor="#ffffff" />
</LinearLayout>
At the moment you are executing calculate() in the MainActivity.java second activity called ResultPage doesn't exist, therefore you can't change it's view (editText3and editText4).
In order to pass data to another activity you should fill your Intent with some extra data and then in your ResultPage activity's onCreate you would get underlying extras.
EDIT
MainActivity.java
public void gotoactivity (View v) {
calculate();
Intent intent = new Intent(this, ResultPage.class);
intent.putExtra("AMOUNT_3", y);
intent.putExtra("AMOUNT_4", z);
startActivity(intent);
}
ResultPage.java inside onCreate
Bundle extras = getIntent().getExtras();
if (extras != null) {
int amount3 = extras.getInt("AMOUNT_3");
int amount4 = extras.getInt("AMOUNT_4");
}