Im searching this since yesterday. I've got on many Activities some piece of code which displays user name and login. I dont want to copy and paste code in layout into every Activity, but I want something just like user controls in .NET. I've read a lot of topic about custom controls but or I don't understand it or its not possible do to this (I dont belive in that option)
Yes it is possible. Look at this sample. It is a custom Numeric Keyboard or a custom View or a custom user control. Customize it and create your own user control:
import java.util.ArrayList;
import org.mabna.order.R;
import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
// this NumericKeyboard works only for EditTexts which
// have a tag with "usesNumericKeyboard" key and value of "true"
// use tag with "ignoreMeForNumericKeyboardTouchListener" key and
// value "true" for controls you do not want to set its touchListener
public class NumericKeyboard extends LinearLayout implements OnTouchListener {
private View mainView = null;
private EditText currentEditText;
private Button btn0;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private ImageButton btnBackSpace;
private ImageButton btnDeleteAll;
public NumericKeyboard(Context context) {
super(context);
}
public NumericKeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void initialize() {
LayoutInflater layoutInflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater
.inflate(R.layout.view_numeric_keyboard, this);
btn0 = (Button) view.findViewById(R.id.btn0);
btn1 = (Button) view.findViewById(R.id.btn1);
btn2 = (Button) view.findViewById(R.id.btn2);
btn3 = (Button) view.findViewById(R.id.btn3);
btn4 = (Button) view.findViewById(R.id.btn4);
btn5 = (Button) view.findViewById(R.id.btn5);
btn6 = (Button) view.findViewById(R.id.btn6);
btn7 = (Button) view.findViewById(R.id.btn7);
btn8 = (Button) view.findViewById(R.id.btn8);
btn9 = (Button) view.findViewById(R.id.btn9);
btnBackSpace = (ImageButton) view.findViewById(R.id.btnBackSpace);
btnDeleteAll = (ImageButton) view.findViewById(R.id.btnDeleteAll);
btn0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "0");
}
});
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "1");
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "2");
}
});
btn3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "3");
}
});
btn4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "4");
}
});
btn5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "5");
}
});
btn6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "6");
}
});
btn7.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "7");
}
});
btn8.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "8");
}
});
btn9.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "9");
}
});
btnBackSpace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "<");
}
});
btnDeleteAll.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "D");
}
});
if (mainView == null && !this.isInEditMode()) {
setTouchListenerForChildViews();
this.setVisibility(GONE);
}
}
protected void editCurrentText(View v, String character) {
if (currentEditText != null) {
if (character.compareTo("<") == 0) {
String text = currentEditText.getText().toString();
if (text.length() == 0) {
} else if (text.length() == 1) {
currentEditText.setText("");
} else {
text = text.substring(0, text.length() - 1);
currentEditText.setText(text);
}
} else if (character.compareTo("D") == 0) {
currentEditText.setText("");
} else {
String text = currentEditText.getText().toString();
text += character;
currentEditText.setText(text);
}
}
}
// #Override
// protected void onLayout(boolean changed, int l, int t, int r, int b) {
//
// super.onLayout(changed, l, t, r, b);
// }
public void setTouchListenerForChildViews() {
final Activity act = (Activity) getContext();
mainView = act.getWindow().getDecorView()
.findViewById(android.R.id.content);
if (mainView == null)
return;
ArrayList<View> queue = new ArrayList<View>();
queue.add((View) mainView);
while (!queue.isEmpty()) {
View v = queue.remove(0);
if (v instanceof EditText && v.getTag(R.id.usesNumericKeyboard) == Boolean
.valueOf(true)) {
((EditText) v).setInputType(InputType.TYPE_NULL);
((EditText) v).setCursorVisible(true);
v.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
{
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.currentEditText = (EditText) v;
numericKeyboard.setVisibility(View.VISIBLE);
}
}
}
});
}
if (v instanceof NumericKeyboard) {
} else {
if (v.getTag(R.id.ignoreMeForNumericKeyboardTouchListener) != Boolean
.valueOf(true)) {
v.setOnTouchListener(this);
}
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View vChild = vg.getChildAt(i);
queue.add(vChild);
}
}
}
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v instanceof EditText && v.getTag(R.id.usesNumericKeyboard) == Boolean
.valueOf(true)) {
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.currentEditText = (EditText) v;
numericKeyboard.setVisibility(View.VISIBLE);
}
} else {
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.setVisibility(View.GONE);
}
}
return false;
}
static ArrayList<NumericKeyboard> arrNumericKeyboard =
new ArrayList<NumericKeyboard>();
public static void registerNumericKeyboard(
NumericKeyboard numericKeyboard) {
numericKeyboard.initialize();
arrNumericKeyboard.add(numericKeyboard);
}
public static void unregisterNumericKeyboard(
NumericKeyboard numericKeyboard) {
arrNumericKeyboard.remove(numericKeyboard);
}
public static void showForEditText(EditText editText)
{
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.setVisibility(View.VISIBLE);
numericKeyboard.currentEditText = editText;
}
}
public static void hide()
{
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.setVisibility(View.GONE);
}
}
}
its layout as view_numeric_keyboard.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background06"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn7"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="7" >
</Button>
<Button
android:id="#+id/btn8"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="8" >
</Button>
<Button
android:id="#+id/btn9"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="9" >
</Button>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn4"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="4" >
</Button>
<Button
android:id="#+id/btn5"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="5" >
</Button>
<Button
android:id="#+id/btn6"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="6" >
</Button>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="1" >
</Button>
<Button
android:id="#+id/btn2"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="2" >
</Button>
<Button
android:id="#+id/btn3"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="3" >
</Button>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/btnBackSpace"
android:layout_width="50dip"
android:layout_height="50dip"
android:scaleType="fitCenter"
android:src="#drawable/backspace"
android:text="-" />
<Button
android:id="#+id/btn0"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="0" />
<ImageButton
android:id="#+id/btnDeleteAll"
android:layout_width="50dip"
android:layout_height="50dip"
android:scaleType="fitCenter"
android:src="#drawable/remove02"
android:text="-" />
</LinearLayout>
</LinearLayout>
using it in your Activity layout:
<org.mabna.order.ui.NumericKeyboard
android:id="#+id/numericKeyboard1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</org.mabna.order.ui.NumericKeyboard>
Related
I want to make a scientific calculator.
I found some useful code on this site: http://www.androidauthority.com/build-a-calculator-app-721910/
Then i modified it for my purpose.
In this code i found binding method was used so i made the following changes in my IDE as per the instructions on this site: https://developer.android.com/topic/libraries/data-binding/index.html
i made changes in the build.gradle(module app)
and made changes in my MainActivity.java code also.
but the following error occurred:
MainActivity.java
package com.example.anant.scientificcalculator;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import static java.lang.Math.*;
import java.text.DecimalFormat;
//import com.example.anant.scientificcalculator.databinding.MainActivityBinding;
//import android.content.DialogInterface;
//import android.text.TextUtils;
//import android.view.View;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private MainActivityBinding binding; //error in this line cannot resolve MainActivityBinding.
private static final char FACT = '!';
private static final char POWER = '^';
private static final String SQRT = "SQRT";
private static final String SIN = "SIN";
private static final String COS = "COS";
private static final String TAN = "TAN";
private static final String LOG = "LOG";
private static final char LEFTC = '(';
private static final char RIGHTC = ')';
private static final String EXP = "EXP";
private static final String PIE = "PIE";
private static final char ADD = '+';
private static final char SUBTRACT = '-';
private static final char MULTIPlY = '*';
private static final char DIVIDE = '/';
private char CURRENT_ACTION;
private String ADVANCE_ACTION;
private double valueOne = Double.NaN;
private double valueTwo;
private DecimalFormat decimalFormat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
decimalFormat = new DecimalFormat("#.##########");
MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
//error in all the lines below saying cannot resolve button-----(where ---- denotes the specific terms of the button)
binding.buttonDot.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + ".");
}
});
binding.buttonZero.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "0");
}
});
binding.buttonOne.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "1");
}
});
binding.buttonTwo.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "2");
}
});
binding.buttonThree.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "3");
}
});
binding.buttonFour.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "4");
}
});
binding.buttonFive.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "5");
}
});
binding.buttonSix.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "6");
}
});
binding.buttonSeven.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "7");
}
});
binding.buttonEight.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "8");
}
});
binding.buttonNine.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "9");
}
});
binding.buttonLeftC.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "(");
}
});
binding.buttonRightC.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + ")");
}
});
binding.buttonExp.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
computeCalculation();
ADVANCE_ACTION = EXP;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "EXP");
binding.editText.setText(null);
}
});
binding.buttonPie.setOnCLickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
computeCalculation();
ADVANCE_ACTION = PIE;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "PIE");
binding.editText.setText(null);
}
});
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = ADD;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACT;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});
binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPlY;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});
binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVIDE;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});
binding.buttonFact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = FACT;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "!");
binding.editText.setText(null);
}
});
binding.buttonPow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = POWER;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "^");
binding.editText.setText(null);
}
});
binding.buttonSqrt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = SQRT;
binding.infoTextView.setText("SQRT(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonSine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = SIN;
binding.infoTextView.setText("SIN(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonCosine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = COS;
binding.infoTextView.setText("COS(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonTangent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = TAN;
binding.infoTextView.setText("TAN(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonLog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = LOG;
binding.infoTextView.setText("LOG(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});
binding.buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length()-1));
}
else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});
}
private void computeCalculation() {
if(!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);
if(CURRENT_ACTION == ADD)
valueOne = this.valueOne + valueTwo;
else if(CURRENT_ACTION == SUBTRACT)
valueOne = this.valueOne - valueTwo;
else if(CURRENT_ACTION == MULTIPlY)
valueOne = this.valueOne * valueTwo;
else if(CURRENT_ACTION == DIVIDE)
valueOne = this.valueOne / valueTwo;
else if(CURRENT_ACTION == FACT)
{
int i,valueThree = 1;
for(i=(int) valueOne;i>0;i--) //type conversion
{
valueThree *= i;
}
}
else if(CURRENT_ACTION == POWER)
valueOne = Math.pow(valueOne,valueTwo);
else if(ADVANCE_ACTION == SQRT)
valueOne = Math.sqrt(valueOne);
else if(ADVANCE_ACTION == SIN)
valueOne = Math.sin(valueOne);
else if(ADVANCE_ACTION == COS)
valueOne = Math.cos(valueOne);
else if(ADVANCE_ACTION == TAN)
valueOne = Math.tan(valueOne);
else if(ADVANCE_ACTION == LOG)
valueOne = Math.log(valueOne);
else if(ADVANCE_ACTION == PIE)
valueOne = Math.PI;
else if(ADVANCE_ACTION == EXP)
valueOne = Math.E;
}
else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
}
catch (Exception e){}
}
}
}
this is my xml file
main_activity.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:id="#+id/main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anant.scientificcalculator.MainActivity">
<TextView
android:id="#+id/infoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textSize="30sp" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/infoTextView"
android:enabled="false"
android:gravity="bottom"
android:hint="0"
android:inputType="numberDecimal"
android:lines="2"
android:maxLines="2"
android:textAlignment="textEnd"
android:textColor="#android:color/black"
android:textSize="40sp" />
<Button
android:id="#+id/buttonFact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:text="#string/buttonFact"
android:textSize="20sp" />
<Button
android:id="#+id/buttonPow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_toRightOf="#id/buttonFact"
android:text="#string/buttonPow"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSqrt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_toRightOf="#id/buttonPow"
android:text="#string/buttonSqrt"
android:textSize="20sp" />
<Button
android:id="#+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/buttonSqrt"
android:layout_below="#id/editText"
android:text="#string/buttonClear"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonFact"
android:text="#string/buttonSine"
android:textSize="20sp" />
<Button
android:id="#+id/buttonCosine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonPow"
android:layout_toRightOf="#id/buttonSine"
android:text="#string/buttonCosine"
android:textSize="20sp" />
<Button
android:id="#+id/buttonTangent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonSqrt"
android:layout_toRightOf="#id/buttonCosine"
android:text="#string/buttonTangent"
android:textSize="20sp" />
<Button
android:id="#+id/buttonLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonClear"
android:layout_toRightOf="#id/buttonTangent"
android:text="#string/buttonLog"
android:textSize="20sp" />
<Button
android:id="#+id/buttonLeftC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonSine"
android:text="#string/buttonLeftC"
android:textSize="20sp" />
<Button
android:id="#+id/buttonRightC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonCosine"
android:layout_toRightOf="#id/buttonLeftC"
android:text="#string/buttonRightC"
android:textSize="20sp" />
<Button
android:id="#+id/buttonExp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonTangent"
android:layout_toRightOf="#id/buttonRightC"
android:text="#string/buttonExp"
android:textSize="20sp" />
<Button
android:id="#+id/buttonPie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonLog"
android:layout_toRightOf="#id/buttonExp"
android:text="#string/buttonPie"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSeven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonLeftC"
android:text="#string/buttonSeven"
android:textSize="20sp" />
<Button
android:id="#+id/buttonEight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonRightC"
android:layout_toRightOf="#id/buttonSeven"
android:text="#string/buttonEight"
android:textSize="20sp" />
<Button
android:id="#+id/buttonNine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonExp"
android:layout_toRightOf="#id/buttonEight"
android:text="#string/buttonNine"
android:textSize="20sp" />
<Button
android:id="#+id/buttonFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonSeven"
android:text="#string/buttonFour"
android:textSize="20sp" />
<Button
android:id="#+id/buttonFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonEight"
android:layout_toRightOf="#id/buttonFour"
android:text="#string/buttonFive"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonNine"
android:layout_toRightOf="#id/buttonFive"
android:text="#string/buttonSix"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonFour"
android:text="#string/buttonOne"
android:textSize="20sp" />
<Button
android:id="#+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonFive"
android:layout_toRightOf="#id/buttonOne"
android:text="#string/buttonTwo"
android:textSize="20sp" />
<Button
android:id="#+id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonSix"
android:layout_toRightOf="#id/buttonTwo"
android:text="#string/buttonThree"
android:textSize="20sp" />
<Button
android:id="#+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonOne"
android:text="#string/buttonDot"
android:textSize="20sp" />
<Button
android:id="#+id/buttonZero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/buttonTwo"
android:layout_toRightOf="#id/buttonDot"
android:text="#string/buttonZero"
android:textSize="20sp" />
<Button
android:id="#+id/buttonEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/buttonNine"
android:layout_below="#id/buttonThree"
android:text="#string/buttonEqual"
android:textSize="20sp" />
<Button
android:id="#+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonNine"
android:layout_toRightOf="#id/buttonNine"
android:text="#string/buttonDIvide"
android:textSize="20sp" />
<Button
android:id="#+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonSix"
android:layout_toRightOf="#id/buttonSix"
android:text="#string/buttonMultiply"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonThree"
android:layout_toRightOf="#id/buttonThree"
android:text="#string/buttonSubtract"
android:textSize="20sp" />
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonEqual"
android:layout_toRightOf="#id/buttonEqual"
android:text="#string/buttonAdd"
android:textSize="20sp" />
</RelativeLayout>
You need to uncomment out:
//import com.example.anant.scientificcalculator.databinding.MainActivityBinding;
That is an autogenerated class for your data binding. You need it. Make it look like this:
com.example.anant.scientificcalculator.databinding.MainActivityBinding;
Secondly, your XML file isn't correct, it should have its RelativeLayout wrapped in a layout tag, like so:
<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anant.scientificcalculator.MainActivity">
...
</layout>
After you do that, do a clean and rebuild, and I think you should be good to go.
Uncomment
import com.example.anant.scientificcalculator.databinding.MainActivityBinding;
And wrap your layout under <layout> tag.
I am using Android Studio
I'm wanting to change the color of my button, with each click.
To cycle it through around 10 colors and start over again in a continuous loop.
For example I'm using setBackgroundResource(#drawable/oval)
oval = blue circle button
oval2 = red circle button
oval3 = green circle button and so on.
So far I have it that button1 starts as oval(blue) and onClick turns into oval2(red)
So my question is, how to add another click to change it to oval3(green) and then cycle it back to the start oval(blue)?
MainActivity.java
package com.example.shadowz.buttononclick;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout) findViewById(R.id.backgroundLayout);
basicText = (TextView) findViewById(R.id.button1);
colorChangeButton = (Button) findViewById(R.id.button1);
// Code Break
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
// Code Break
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button1) {
button1.setBackgroundResource(R.drawable.oval2);
}
}
});
// Code Break
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button2) {
button2.setBackgroundResource(R.drawable.oval3);
}
}
});
// Code Break
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button3) {
button3.setBackgroundResource(R.drawable.oval4);
}
}
});
// Code Break
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button4) {
button4.setBackgroundResource(R.drawable.oval5);
}
}
});
// Code Break
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button5) {
button5.setBackgroundResource(R.drawable.oval6);
}
}
});
}
}
activity_main.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"
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="com.example.shadowz.buttononclick.MainActivity"
android:id="#+id/backgroundLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="#+id/button1"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button1"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button2"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button2"
android:background="#drawable/oval"
android:layout_alignTop="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="#+id/button3"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button3"
android:background="#drawable/oval"
android:singleLine="false"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button4"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button4"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
android:id="#+id/button5"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button5"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="78dp" />
</RelativeLayout>
You can do this by following:
Create integer-array in resources:
<integer-array name="backgrounds">
<item>#drawable/oval1</item>
<item>#drawable/oval2</item>
<item>#drawable/oval3</item>
<item>#drawable/oval4</item>
<item>#drawable/oval5</item>
</integer-array>
Then create special OnClickListener class inside your class code
private static class MyClickListener implements View.OnClickListener {
private int mBackgroundIndex = 0;
private final TypedArray mBackgrounds;
public MyClickListener(Context context) {
mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
}
#Override
public void onClick(View v) {
mBackgroundIndex++;
if (mBackgroundIndex >= mBackgrounds.length()) {
mBackgroundIndex = 0;
}
v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
}
#Override
protected void finalize() throws Throwable {
mBackgrounds.recycle();
super.finalize();
}
}
And then set that listener to each button:
button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));
This should result in the following MainActivity code:
package com.example.shadowz.buttononclick;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout) findViewById(R.id.backgroundLayout);
basicText = (TextView) findViewById(R.id.button1);
colorChangeButton = (Button) findViewById(R.id.button1);
// Code Break
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));
}
private static class MyClickListener implements View.OnClickListener {
private int mBackgroundIndex = 0;
private final TypedArray mBackgrounds;
public MyClickListener(Context context) {
mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
}
#Override
public void onClick(View v) {
mBackgroundIndex++;
if (mBackgroundIndex >= mBackgrounds.length()) {
mBackgroundIndex = 0;
}
v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
}
#Override
protected void finalize() throws Throwable {
mBackgrounds.recycle();
super.finalize();
}
}
}
I am trying to programmatically generate two RadioGroups. I have succesfully generated the first one but I need to generate the second one onCheckedChangeListener of the first radio group. Here is my code for the activity.
package com.packr.activities;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.avast.android.dialogs.fragment.SimpleDialogFragment;
import com.avast.android.dialogs.iface.ISimpleDialogListener;
import com.kbeanie.imagechooser.api.ChooserType;
import com.kbeanie.imagechooser.api.ChosenImage;
import com.kbeanie.imagechooser.api.ImageChooserListener;
import com.kbeanie.imagechooser.api.ImageChooserManager;
import com.packr.R;
import com.packr.adapters.ShipmentsAdapter;
import com.packr.classes.Item;
import com.packr.classes.Packr;
import com.packr.classes.Shipment;
import com.packr.database.DBShipments;
import com.packr.logging.L;
import java.util.ArrayList;
public class ItemDetailsActivity extends AppCompatActivity implements ISimpleDialogListener, ImageChooserListener {
private Toolbar toolbar;
private ImageChooserManager imageChooserManager;
private SeekBar seekBar;
private Intent intent;
private RadioGroup itemTypeRadioGroup, deliveryMethod;
private TextView addImage;
private Bitmap myBitmap;
private ShipmentsAdapter mShipmentAdapter;
private static long back_pressed;
private MyShipmentsActivity activity;
private ImageView itemImage;
private LinearLayout itemDetailsLinearLayout, weightTypeLinearLayout;
private TextInputLayout itemDescriptionText, quantityText, valueOfItemText;
private EditText itemDescription, quantity, valueOfItem;
private TextView weightUnit, weightValue, selectWeight;
private ArrayList<Shipment> shipmentArrayList = new ArrayList<>();
private ArrayList<Item> deliveryTypeArrayList = new ArrayList<>();
private ArrayList<Item> itemTypeArrayList = new ArrayList<>();
private ArrayList<Item> shipmentTypeArrayList = new ArrayList<>();
private ArrayList<Item> weightTypeArrayList = new ArrayList<>();
private String city, state, pincode, recipientName, recipientContact, street, itemType = "", deliveryType = "", route;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_details);
initialize();
onClick();
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Item details");
setSupportActionBar(toolbar);
deliveryTypeArrayList = Packr.getWritableDatabase().readDeliveryType(DBShipments.DELIVERY_TYPE);
for (int i = 0; i < deliveryTypeArrayList.size(); i++) {
}
itemTypeArrayList = Packr.getWritableDatabase().readDeliveryType(DBShipments.ITEM_TYPE);
int itemId;
//first radio group
RadioButton rb = null;
float scale = getResources().getDisplayMetrics().density;
final int dpAsPixels = (int) (10 * scale + 0.5f);
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
rg.setPadding(dpAsPixels, dpAsPixels, dpAsPixels, dpAsPixels);
final Drawable drawableTop = getResources().getDrawable(R.drawable.document_icon);
for (int i = 0; i < itemTypeArrayList.size(); i++) {
if (itemTypeArrayList.get(i).getActive() == 1) {
rb = new RadioButton(this);
rg.addView(rb); //the RadioButtons are added to the radioGroup instead of the layout
rb.setText(itemTypeArrayList.get(i).getCode());
rb.setId(itemTypeArrayList.get(i).getId());
L.m(itemTypeArrayList.get(i).getId() + "hello");
}
if (rb != null) {
rb.setAllCaps(true);
}
assert rb != null;
rb.setGravity(Gravity.CENTER_VERTICAL);
rb.setButtonDrawable(null);
rb.setPadding(dpAsPixels, 0, dpAsPixels, 0);
rb.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop, null, null);
}
itemDetailsLinearLayout.addView(rg);
final RadioGroup weightTypeRadioGroup = new RadioGroup(this); //create the RadioGroup
weightTypeRadioGroup.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
weightTypeRadioGroup.setPadding(dpAsPixels, dpAsPixels, dpAsPixels, dpAsPixels);
shipmentTypeArrayList = Packr.getWritableDatabase().readDeliveryType(DBShipments.SHIPMENT_TYPE);
for (int i = 0; i < shipmentTypeArrayList.size(); i++) {
}
final RadioButton finalRb = rb;
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (finalRb != null) {
checkedId = finalRb.getId();
System.out.print(checkedId);
L.m(group.getCheckedRadioButtonId() + "");
weightTypeArrayList = Packr.getWritableDatabase().readWeightType();
for (int i = 0; i < weightTypeArrayList.size(); i++) {
L.m(weightTypeArrayList.get(i).getTitle());
//second radio group
RadioButton weightTypeRadioButton = null;
weightTypeRadioButton = new RadioButton(getApplicationContext());
weightTypeRadioGroup.addView(weightTypeRadioButton);
weightTypeRadioButton.setText(weightTypeArrayList.get(i).getTitle());
weightTypeRadioButton.setId(weightTypeArrayList.get(i).getId());
}
if (weightTypeRadioGroup.getParent() != null){
((ViewGroup)weightTypeRadioGroup.getParent()).removeView(weightTypeRadioGroup);
weightTypeLinearLayout.addView(weightTypeRadioGroup);
}
}
}
});
deliveryMethod.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.normalDeliveryRadioButton) {
deliveryType = "Normal Delivery";
} else {
deliveryType = "Express Delivery";
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_item_details, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_done) {
if (validationCheck()) {
Shipment shipment = new Shipment();
shipment.setRecipientName(recipientName);
shipment.setRecipientContact(recipientContact);
shipment.setCity(city);
shipment.setState(state);
shipment.setStreetNo(street);
shipment.setRoute(route);
shipment.setPostalCode(pincode);
shipment.setItemQuantity(quantity.getText().toString());
shipment.setItemType(itemType);
shipment.setDeliveryType(deliveryType);
shipmentArrayList.add(shipment);
Packr.getWritableDatabase().insertShipment(shipmentArrayList, false);
mShipmentAdapter = new ShipmentsAdapter(getApplicationContext(), activity);
mShipmentAdapter.setShipmentArrayList(shipmentArrayList);
Intent intent = new Intent(ItemDetailsActivity.this, MyShipmentsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
return true;
}
return super.onOptionsItemSelected(item);
}
public void initialize() {
intent = getIntent();
recipientName = intent.getStringExtra("recipientName");
recipientContact = intent.getStringExtra("recipientContact");
city = intent.getStringExtra("city");
state = intent.getStringExtra("state");
pincode = intent.getStringExtra("pincode");
street = intent.getStringExtra("street");
route = intent.getStringExtra("route");
deliveryMethod = (RadioGroup) findViewById(R.id.radioGroupShippingMethod);
selectWeight = (TextView) findViewById(R.id.selectWeight);
addImage = (TextView) findViewById(R.id.addImage);
itemImage = (ImageView) findViewById(R.id.item_image);
itemDescriptionText = (TextInputLayout) findViewById(R.id.item_description_text_input_layout);
quantityText = (TextInputLayout) findViewById(R.id.item_quantity_text_input_layout);
valueOfItemText = (TextInputLayout) findViewById(R.id.item_value_text_input_layout);
itemDescription = (EditText) findViewById(R.id.item_description_edit_text);
quantity = (EditText) findViewById(R.id.item_quantity_edit_text);
valueOfItem = (EditText) findViewById(R.id.item_value_edit_text);
itemDetailsLinearLayout = (LinearLayout) findViewById(R.id.itemDetailsLinearLayout);
weightTypeLinearLayout = (LinearLayout) findViewById(R.id.weightTypeRadioButton);
}
public void onClick() {
addImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SimpleDialogFragment.createBuilder(getApplicationContext(), getSupportFragmentManager()).setTitle("Choose item image").setMessage(R.string.selectImage).setNegativeButtonText("Gallery").setPositiveButtonText("Camera").show();
}
});
}
public Boolean validationCheck() {
if (itemDescription.getText().length() == 0) {
itemDescriptionText.setErrorEnabled(true);
itemDescriptionText.setError("Please provide an item description");
} else if (quantity.getText().length() == 0) {
quantityText.setErrorEnabled(true);
quantityText.setError("Provide item quantity");
} else if (valueOfItem.getText().length() == 0) {
valueOfItemText.setErrorEnabled(true);
valueOfItemText.setError("Please provide value of item");
} else {
return true;
}
return false;
}
public void chooseImage() {
imageChooserManager = new ImageChooserManager(this,
ChooserType.REQUEST_PICK_PICTURE);
imageChooserManager.setImageChooserListener(this);
try {
imageChooserManager.choose();
} catch (Exception e) {
e.printStackTrace();
}
}
public void snapImage() {
imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_CAPTURE_PICTURE);
imageChooserManager.setImageChooserListener(this);
try {
imageChooserManager.choose();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onNegativeButtonClicked(int i) {
chooseImage();
}
#Override
public void onNeutralButtonClicked(int i) {
}
#Override
public void onPositiveButtonClicked(int i) {
snapImage();
}
#Override
public void onImageChosen(ChosenImage chosenImage) {
myBitmap = BitmapFactory.decodeFile(chosenImage.getFileThumbnail());
runOnUiThread(new Runnable() {
public void run() {
itemImage.setImageBitmap(myBitmap);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK &&
(requestCode == ChooserType.REQUEST_PICK_PICTURE ||
requestCode == ChooserType.REQUEST_CAPTURE_PICTURE)) {
imageChooserManager.submit(requestCode, data);
}
}
#Override
public void onError(String s) {
}
#Override
public void onBackPressed() {
AlertDialog.Builder alert = new AlertDialog.Builder(ItemDetailsActivity.this);
alert.setTitle("Cancel shipment");
alert.setMessage("Are you sure?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(ItemDetailsActivity.this, MyShipmentsActivity.class);
startActivity(intent);
finish();
}
});
alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Cancel
}
});
alert.show();
}
}
Here is the code for my layout xml file.
<RelativeLayout
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="com.packr.activities.ItemDetailsActivity"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"/>
<ScrollView
android:layout_below="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="80dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="What are you sending?"
android:fontFamily="sans-serif-light"
android:padding="20dp"
android:textColor="#color/textColorPrimary"/>
<LinearLayout
android:id="#+id/itemDetailsLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
tools:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">
<ImageView
android:id="#+id/item_image"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:src="#drawable/image_background"
android:layout_margin="16dp"
android:contentDescription="#string/product_image" />
<TextView
android:id="#+id/addImage"
android:padding="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add an image"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textColor="#color/textColorSecondary"
/>
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/item_description_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:visibility="gone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<EditText
android:id="#+id/item_description_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/item_description"
android:inputType="textMultiLine"
android:imeOptions="flagNoFullscreen"
android:lines="5"/>
</android.support.design.widget.TextInputLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/selectWeight"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Permitted weight"
android:fontFamily="sans-serif-light"
android:padding="20dp"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/weightTypeRadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/item_quantity_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:visibility="visible"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<EditText
android:id="#+id/item_quantity_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/itemQuantity"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/item_value_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:visibility="visible"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<EditText
android:id="#+id/item_value_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/item_value"
android:inputType="number"
android:imeOptions="flagNoFullscreen"/>
</android.support.design.widget.TextInputLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose shipping method"
android:fontFamily="sans-serif-light"
android:textColor="#color/textColorPrimary"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="25dp"/>
<RadioGroup
android:id="#+id/radioGroupShippingMethod"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<RadioButton
android:id="#+id/normalDeliveryRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal Delivery"
android:checked="true"/>
<RadioButton
android:id="#+id/expressDeliveryRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Express Delivery"/>
</RadioGroup>
</LinearLayout>
</ScrollView>
//This contains the layout for the bottom price bar //
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/textColorPrimary"
android:padding="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:id="#+id/priceText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:textSize="18sp"
android:fontFamily="sans-serif-light"
android:textColor="#color/white"/>
<TextView
android:id="#+id/equals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/equals"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#color/white"/>
<TextView
android:id="#+id/Rs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Rs"
android:textSize="18sp"
android:fontFamily="sans-serif-light"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:textColor="#color/white"/>
<TextView
android:id="#+id/calculatedPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="550"
android:textSize="18sp"
android:fontFamily="sans-serif-light"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:textColor="#color/white"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
do you not just need to repeat the same proccess inside the onCheckChangedListener?
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//Create another radio group here
//then add it to the view
RadioGroup rg2 = new RadioGroup(this); //create the RadioGroup
rg2.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
rg2.setPadding(dpAsPixels, dpAsPixels, dpAsPixels, dpAsPixels);
final Drawable drawableTop = getResources().getDrawable(R.drawable.document_icon);
for (int i = 0; i < itemTypeArrayList.size(); i++) {
if (itemTypeArrayList.get(i).getActive() == 1) {
rb = new RadioButton(this);
rg2.addView(rb); //the RadioButtons are added to the radioGroup instead of the layout
rb.setText(itemTypeArrayList.get(i).getCode());
rb.setId(itemTypeArrayList.get(i).getId());
}
}
itemDetailsLinearLayout.addView(rg);
}
});
You may need to declare rg2 public at the top of the class so it can be accessed by the onCheckChangedListener
I am using Imageview 2 times in my MainActivity.java class by using LayoutInflater.In that activity i am inflate so many layouts by using scroll every thing works fine.But this imageview is not working properly.When user click on imageview it shows popup.It works when i am show first time inflate.It's not working second time inflate.
In my main activity contain edittext.when user enter High in edittext i want to show 2 imageview times.If user enter Low i want to show single imageview.One imageview shows top of the screen and one shows bottom of the screen.
Here my bit of code.
This is my imageview.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/attach_photo_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp" >
<TextView
android:id="#+id/attach_photo_titile_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="#string/attach_photo_to_job"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/attach_photo_camera_ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/attach_photo_titile_textView"
android:contentDescription="#string/app_name"
android:clickable="true"
android:src="#drawable/image_placeholder" />
</RelativeLayout>
I am using this layout in my main.xml by using .
And i am call that imageview.xml layout in My MainActivity.java by using LayoutInflater.
Here is my code:
public void getAudioRecordLayout() {
testView = testInflater.inflate(
getResources().getLayout(
R.layout.imageview), null);
mWorkDetailAttachImageView = (ImageView) findViewById(R.id.attach_photo_camera_ImageView);
mIncludeHighLayout = (LinearLayout) findViewById(R.id.include_High);
mIncludeLowLayout = (LinearLayout) findViewById(R.id.include_Low);
mIncludeRecordAudioInWorkDetailLayout.setVisibility(View.VISIBLE);
mIncludeRecordAudioInRecordOfInspectionLayout
.setVisibility(View.GONE);
mWorkDetailAttachImageView.setOnClickListener(this);
}
Edit # 1 :
When i am showing imageview second time that imageview shows inside another inflate view.
Image view OnclickListiener :
if (v == mWorkDetailAttachImageView) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
TestResultsActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
Edit # 2
MainActivity.java
package rajesh.dropdowndemo;
import java.security.PublicKey;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Layout holding the droddown view */
private LinearLayout mDropdownFoldOutMenu;
/** Textview holding the title of the droddown */
private TextView mDropdownTitle;
ImageView imagView;
LinearLayout commonIncludeLayout, includeHighLayout;
int i = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getImageViewLayout(1);
getImageViewLayout(2);
// getHighLayout();
mDropdownFoldOutMenu = ((LinearLayout) findViewById(R.id.dropdown_foldout_menu));
mDropdownTitle = ((TextView) findViewById(R.id.dropdown_textview));
commonIncludeLayout = (LinearLayout) findViewById(R.id.include_imageview_layout);
includeHighLayout = (LinearLayout) findViewById(R.id.include_high_imageview_layout);
final TextView dropDownTextView = (TextView) findViewById(R.id.dropdown_textview);
final TextView alt0 = (TextView) findViewById(R.id.dropdown_alt0);
final TextView alt1 = (TextView) findViewById(R.id.dropdown_alt1);
final TextView alt2 = (TextView) findViewById(R.id.dropdown_alt2);
dropDownTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mDropdownFoldOutMenu.getVisibility() == View.GONE) {
openDropdown();
} else {
closeDropdown();
}
}
});
alt0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.VISIBLE);
dropDownTextView.setText(R.string.alt0);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt0,
Toast.LENGTH_SHORT).show();
}
});
alt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt1);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt1,
Toast.LENGTH_SHORT).show();
}
});
alt2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt2);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
Toast.makeText(getBaseContext(), R.string.alt2,
Toast.LENGTH_SHORT).show();
}
});
}
/**
* Animates in the dropdown list
*/
private void openDropdown() {
if (mDropdownFoldOutMenu.getVisibility() != View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 0, 1);
anim.setDuration(getResources().getInteger(
R.integer.dropdown_amination_time));
mDropdownFoldOutMenu.startAnimation(anim);
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_close, 0);
mDropdownFoldOutMenu.setVisibility(View.VISIBLE);
}
}
/**
* Animates out the dropdown list
*/
private void closeDropdown() {
if (mDropdownFoldOutMenu.getVisibility() == View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
anim.setDuration(getResources().getInteger(
R.integer.dropdown_amination_time));
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mDropdownFoldOutMenu.setVisibility(View.GONE);
}
});
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_open, 0);
mDropdownFoldOutMenu.startAnimation(anim);
}
}
/**
*
* This represents Inflate the layout into main.xml layout
*/
public void getImageViewLayout(final int aa) {
// LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
// View view = inflater.inflate(R.layout.imageview, null);
RelativeLayout outer;
if (aa == 1)
outer = (RelativeLayout) findViewById(R.id.image_layout);
else
outer = (RelativeLayout) findViewById(R.id.image_layout1);
imagView = (ImageView) outer
.findViewById(R.id.attach_photo_camera_ImageView);
imagView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Imageview Clicked");
if (aa == 1)
imagView.setTag(1);
else
imagView.setTag(2);
LayoutInflater inflater = LayoutInflater
.from(MainActivity.this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 100);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 200);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK && null != data) {
// get Image
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
System.out.println(imagView.getTag().toString());
String i = imagView.getTag().toString();
if (imagView.getTag(1) == i)
((ImageView) imagView.getTag(1)).setImageBitmap(thumbnail);
else if(imagView.getTag(2) != null)
((ImageView) imagView.getTag(2)).setImageBitmap(thumbnail);
} else if (resultCode == RESULT_CANCELED) {
System.out.println(imagView.getTag());
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT)
.show();
}
if (requestCode == 200 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bit = (Bitmap) BitmapFactory.decodeFile(picturePath);
System.out.println(imagView.getTag().toString());
String i = imagView.getTag().toString();
if (imagView.getTag() == i)
((ImageView) imagView.getTag(1)).setImageBitmap(bit);
else if(imagView.getTag(2) != null)
((ImageView) imagView.getTag(2)).setImageBitmap(bit);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT)
.show();
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/dropdown_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_background"
android:drawableRight="#drawable/icn_dropdown_open"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="Dropdown alts:" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="10dip"
android:padding="100dip"
android:text="other content" />
<LinearLayout
android:id="#+id/include_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:orientation="vertical">
<include layout="#layout/imageview"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dropdown_foldout_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="2dip"
android:background="#drawable/dropdown_background"
android:orientation="vertical"
android:padding="1dip"
android:visibility="gone" >
<TextView
android:id="#+id/dropdown_alt0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:drawableRight="#drawable/icn_dropdown_checked"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt0" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt1" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt2" />
</LinearLayout>
<LinearLayout
android:id="#+id/include_high_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/high_layout"/>
</LinearLayout>
</RelativeLayout>
imageview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/background_dark" >
<TextView
android:id="#+id/attach_photo_titile_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="Attach Photo"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/attach_photo_camera_ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/attach_photo_titile_textView"
android:contentDescription="#string/app_name"
android:clickable="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
dailog.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="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/take_new_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text="Take New Photo" />
<Button
android:id="#+id/choose_from_existing_img_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Choose Existing" />
<Button
android:id="#+id/cancel_img_dialog_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Cancel" />
</LinearLayout>
string.xml
<resources>
<string name="app_name">DropDownDemo</string>
<string name="alt0">High</string>
<string name="alt1">Low</string>
<string name="alt2">General</string>
</resources>
high_layout.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:orientation="vertical" >
<include
layout="#layout/imageview"/>
</LinearLayout>
Please any one help me.
Try this..
testView = testInflater.inflate(getResources().getLayout(R.layout.imageview), null);
mWorkDetailAttachImageView = (ImageView) testView.findViewById(R.id.attach_photo_camera_ImageView);
add in your click
mWorkDetailAttachImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
}
});
Or
if (v.getId() == R.id.attach_photo_camera_ImageView) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
TestResultsActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
EDIT
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
testView = inflater.inflate(R.layout.imageview, null);
EDIT 1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/dropdown_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_background"
android:drawableRight="#drawable/icn_dropdown_open"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="Dropdown alts:" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="10dip"
android:padding="100dip"
android:text="other content" />
<LinearLayout
android:id="#+id/include_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:orientation="vertical">
<include
android:id="#+id/image_layout"
layout="#layout/imageview"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dropdown_foldout_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="2dip"
android:background="#drawable/dropdown_background"
android:orientation="vertical"
android:padding="1dip"
android:visibility="gone" >
<TextView
android:id="#+id/dropdown_alt0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:drawableRight="#drawable/icn_dropdown_checked"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt0" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt1" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt2" />
</LinearLayout>
<LinearLayout
android:id="#+id/include_high_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone">
<include
android:id="#+id/image_layout1"
layout="#layout/imageview"/>
</LinearLayout>
</RelativeLayout>
JAVA
package rajesh.dropdowndemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Layout holding the droddown view */
private LinearLayout mDropdownFoldOutMenu;
/** Textview holding the title of the droddown */
private TextView mDropdownTitle;
LinearLayout commonIncludeLayout,includeHighLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getImageViewLayout(1);
getImageViewLayout(2);
mDropdownFoldOutMenu = ((LinearLayout) findViewById(R.id.dropdown_foldout_menu));
mDropdownTitle = ((TextView) findViewById(R.id.dropdown_textview));
commonIncludeLayout = (LinearLayout) findViewById(R.id.include_imageview_layout);
includeHighLayout = (LinearLayout) findViewById(R.id.include_high_imageview_layout);
final TextView dropDownTextView = (TextView) findViewById(R.id.dropdown_textview);
final TextView alt0 = (TextView) findViewById(R.id.dropdown_alt0);
final TextView alt1 = (TextView) findViewById(R.id.dropdown_alt1);
final TextView alt2 = (TextView) findViewById(R.id.dropdown_alt2);
dropDownTextView.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
if (mDropdownFoldOutMenu.getVisibility() == View.GONE) {
openDropdown();
} else {
closeDropdown();
}
}
});
alt0.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.VISIBLE);
dropDownTextView.setText(R.string.alt0);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt0, Toast.LENGTH_SHORT).show();
}
});
alt1.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt1);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt1, Toast.LENGTH_SHORT).show();
}
});
alt2.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt2);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
Toast.makeText(getBaseContext(), R.string.alt2, Toast.LENGTH_SHORT).show();
}
});
}
/**
* Animates in the dropdown list
*/
private void openDropdown() {
if (mDropdownFoldOutMenu.getVisibility() != View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 0, 1);
anim.setDuration(getResources().getInteger(R.integer.dropdown_amination_time));
mDropdownFoldOutMenu.startAnimation(anim);
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_close, 0);
mDropdownFoldOutMenu.setVisibility(View.VISIBLE);
}
}
/**
* Animates out the dropdown list
*/
private void closeDropdown() {
if (mDropdownFoldOutMenu.getVisibility() == View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
anim.setDuration(getResources().getInteger(R.integer.dropdown_amination_time));
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mDropdownFoldOutMenu.setVisibility(View.GONE);
}
});
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_open, 0);
mDropdownFoldOutMenu.startAnimation(anim);
}
}
/**
*
* This represents Inflate the layout into main.xml layout
*/
public void getImageViewLayout(int aa){
LinearLayout outer;
if(aa == 1)
outer = (LinearLayout) findViewById(R.id.image_layout);
else
outer = (LinearLayout) findViewById(R.id.image_layout1);
ImageView imagView = (ImageView) outer.findViewById(R.id.attach_photo_camera_ImageView);
imagView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Imageview Clicked");
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
});
}
}
Did you checked view in your onClick function like below..
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.attach_photo_camera_ImageView:
//your click code
break;
}
or try below code instad of your code..
mWorkDetailAttachImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
\how use sharedpreferences on all field?
Layout XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1." />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:layout_marginRight="10dp"/>
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="www.google.co.in"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2." />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="00:00" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="www.yahoo.co.in"
android:ems="10" />
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_weight="0.11"
android:text="WebView" />
</LinearLayout>
main activicty
package com.example.myproject5;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TextView tv6, tv7, tv11, tv12;
private EditText et1, et2;
private CheckBox cb1, cb2;
private Button submit, webview;
private int hour;
private int minute;
public static final String PREFS_NAME = "MyPrefsFile";
private static final int TIME_DIALOG_ID = 1;
private static final int TIME_DIALOG_ID1 = 2;
private static final int TIME_DIALOG_ID2 = 3;
private static final int TIME_DIALOG_ID3 = 4;
int cur = 0;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private int savelogin;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv6 = (TextView) findViewById(R.id.textView6);
tv7 = (TextView) findViewById(R.id.textView7);
tv11 = (TextView) findViewById(R.id.textView11);
tv12 = (TextView) findViewById(R.id.textView12);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb2 = (CheckBox) findViewById(R.id.checkBox2);
submit = (Button) findViewById(R.id.button1);
webview = (Button) findViewById(R.id.button2);
tv6.setEnabled(false);
tv7.setEnabled(false);
tv11.setEnabled(false);
tv12.setEnabled(false);
et1.setEnabled(false);
et2.setEnabled(false);
loginPreferences = getSharedPreferences(PREFS_NAME, 0);
savelogin = loginPreferences.getInt("abc", 0); // save data
if (savelogin == 1) {
tv6.setText(savelogin);
} else if (savelogin == 2) {
tv7.setText(savelogin);
} else if (savelogin == 3) {
tv11.setText(0);
} else if (savelogin == 4) {
tv12.setText(0);
}
cb1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (cb1.isChecked()) {
tv6.setEnabled(true);
tv11.setEnabled(true);
et1.setEnabled(true);
} else {
tv6.setEnabled(false);
tv11.setEnabled(false);
et1.setEnabled(false);
}
}
});
cb2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (cb2.isChecked()) {
tv7.setEnabled(true);
tv12.setEnabled(true);
et2.setEnabled(true);
} else {
tv7.setEnabled(false);
tv12.setEnabled(false);
et2.setEnabled(false);
}
}
});
tv6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
loadpreferences1("abc", 1);
}
});
tv7.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID1);
loadpreferences1("abc", 2);
}
});
tv11.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID2);
loadpreferences1("abc", 3);
}
});
tv12.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID3);
loadpreferences1("abc", 4);
}
});
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
webview.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
loadpreferences1("abc", 5);
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
cur = TIME_DIALOG_ID;
return new TimePickerDialog(this, timePickerListener, hour, minute, true);
case TIME_DIALOG_ID1:
cur = TIME_DIALOG_ID1;
return new TimePickerDialog(this, timePickerListener, hour, minute, true);
case TIME_DIALOG_ID2:
cur = TIME_DIALOG_ID2;
return new TimePickerDialog(this, timePickerListener, hour, minute, true);
case TIME_DIALOG_ID3:
cur = TIME_DIALOG_ID3;
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
if (cur == TIME_DIALOG_ID) {
tv6.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
} else if (cur == TIME_DIALOG_ID1) {
tv7.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
} else if (cur == TIME_DIALOG_ID2) {
tv11.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
} else if (cur == TIME_DIALOG_ID3) {
tv12.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
}
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
private void loadpreferences1(String str, int in) {
loginPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
loginPrefsEditor.putInt(str, in);
loginPrefsEditor.commit();
}
}
Use these method to save and get values from any where in your app change parameter according to your requirment
this method to save value in shared prefrences
public static void setPref(Context c, String pref, String val) {
Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
e.putString(pref, val);
e.commit();
}
this method to get value from shared prefrences
public static String getPref(Context c, String pref, String val) {
return PreferenceManager.getDefaultSharedPreferences(c).getString(pref,
val);
}