show EditText on condition - android

Right now I have an idea for a project and I would like to know if anyone can help me on the same logic.
As such I need to create or generate a number of EditText according to amount you enter, ie, to select or enter a number such as 5, show me 5 EditText layout for type 5 values​​. They know that the form could accomplish this? Any ideas please?
I guess it must be a way to do it with a loop, but not like carrying this calculation Java to XML. Thank you.

Here is an example of generating EditText items based on the number you enter. You can ignore the scrollview in the layout file I just put it in case someone added a lot of EditText items that would go off the screen.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText count = (EditText) findViewById(R.id.count);
final TextView output = (TextView) findViewById(R.id.output);
final Button generate = (Button) findViewById(R.id.generate);
final Button values = (Button) findViewById(R.id.values);
final LinearLayout container = (LinearLayout) findViewById(R.id.container);
generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int numberOfControlsToGenerate = 0;
try {
numberOfControlsToGenerate = Integer.parseInt(count.getText().toString().trim());
} catch (NumberFormatException e) {
Log.e(TAG, e.getMessage(), e);
}
if (numberOfControlsToGenerate > 0) {
if (container.getChildCount() > 0) {
container.removeAllViews();
}
for (int counter = 0; counter < numberOfControlsToGenerate; counter++) {
addEditText(container);
}
}
}
});
values.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] editTextValues = new String[container.getChildCount()];
StringBuilder editTextValuesBuilder = new StringBuilder();
for (int counter = 0; counter < container.getChildCount(); counter++) {
EditText child = (EditText) container.getChildAt(counter);
editTextValues[counter] = child.getText().toString().trim();
editTextValuesBuilder.append(editTextValues[counter]).append("\n");
}
output.setText(editTextValuesBuilder.toString());
}
});
}
private void addEditText(LinearLayout container) {
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
EditText editTextToAdd = new EditText(this);
editTextToAdd.setLayoutParams(params);
container.addView(editTextToAdd);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp"
tools:context="${packageName}.${activityClass}" >
<EditText
android:id="#+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="number"
android:textSize="20sp" />
<Button
android:id="#+id/generate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go" />
<Button
android:id="#+id/values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Values" />
<TextView
android:id="#+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>

Related

How to create a view programmatically at 12 AM every day

How would I go about creating a TextView at 12 AM every day, regardless if the app is open or not (unbound to app lifetime)? I already have the code to create the TextView, I just need to know how I would execute the code at 12 AM every day.
I'm not going to be opening another activity, everything will be done within the same activity.
Here is my code and my xml:
package com.example.app1;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import java.util.ArrayList;
import java.util.Calendar;
public class NotesActivity extends AppCompatActivity implements View.OnClickListener {
LinearLayout linearLayout;
ImageButton addNoteButton, infoButton;
TextView titleTextNotes;
ArrayList<TextView> notesList;
SharedPreferences sharedPreferences;
Calendar calendar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
sharedPreferences = this.getSharedPreferences("Notes Preferences", Context.MODE_PRIVATE);
linearLayout = findViewById(R.id.linearLayout);
addNoteButton = findViewById(R.id.addNoteButton);
infoButton = findViewById(R.id.infoButton);
titleTextNotes = findViewById(R.id.titleTextNotes);
notesList = new ArrayList<>();
Bundle extras = getIntent().getExtras();
String noteTitle = extras.getString("noteTitle");
if(notesList.size() == 0) {
notesList.add(getStyledNote(noteTitle));
linearLayout.addView(notesList.get(notesList.size() - 1));
}
/*addNoteButton.setOnClickListener(view -> {
notesList.add(getStyledNote("My Log"));
notesList.get(notesList.size() - 1).setOnClickListener(this);
linearLayout.addView(notesList.get(notesList.size() - 1));
});*/
for(int i = 0; i < notesList.size(); i++) {
notesList.get(i).setOnClickListener(this);
}
titleTextNotes.setOnClickListener(this);
for(int i = 0; i < sharedPreferences.getInt("Number of notes", 0); i++) {
notesList.add(getStyledNote(sharedPreferences.getString("Note " + i, "My Log")));
linearLayout.addView(notesList.get(notesList.size() - 1));
}
infoButton.setOnClickListener(view -> callInfoActivity());
}
#Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("Number of notes", notesList.size());
for (int i = 0; i < notesList.size(); i++) {
editor.putString("Note " + i, notesList.get(i).getText().toString());
}
editor.apply();
for (int i = 0; i < notesList.size(); i++) {
if (view.getId() == notesList.get(i).getId())
callMainActivity(i);
}
}
public void callMainActivity(int pos) {
Intent sendInfoBack = new Intent();
sendInfoBack.putExtra("noteTitleSentBack", notesList.get(pos).getText());
setResult(RESULT_OK, sendInfoBack);
overridePendingTransition(R.anim.leave, R.anim.enter);
finish();
}
public void callInfoActivity() {
Intent intentToLoad = new Intent(NotesActivity.this, InfoActivity.class);
startActivity(intentToLoad);
overridePendingTransition(R.anim.enter, R.anim.leave);
}
public TextView getStyledNote(String noteTitle) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 175);
params.gravity = Gravity.CENTER;
params.setMargins(0, 10, 0, 10);
TextView note = new TextView(NotesActivity.this);
note.setId(View.generateViewId());
note.setLayoutParams(params);
note.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.note_style, null));
note.setTextSize(20);
note.setTextColor(Color.BLACK);
note.setGravity(Gravity.CENTER);
note.setTypeface(getResources().getFont(R.font.sfprodisplaybold));
if(noteTitle == null || noteTitle.equals(""))
note.setText("My Log");
else
note.setText(noteTitle);
return note;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".NotesActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/infoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight=".2"
android:background="#android:color/transparent"
app:srcCompat="#drawable/info_mark" />
<TextView
android:id="#+id/titleTextNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight=".8"
android:fontFamily="#font/sfprodisplaybold"
android:gravity="center"
android:text="My Logs"
android:textColor="#color/teal_200"
android:textSize="30sp" />
<ImageButton
android:id="#+id/addNoteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight=".2"
android:background="#android:color/transparent"
app:srcCompat="#drawable/bar_chart" />
</LinearLayout>
<View
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="40dp"
android:layout_weight="1"
android:background="#color/teal_200" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
You need to schedule a periodic task using Work Manager.
val myUploadWork = PeriodicWorkRequestBuilder<SaveImageToFileWorker>(
24,TimeUnit.HOURS) // The work is scheduled to repeat after 24 hours.
.build()
Then you can submit the request to system :
WorkManager
.getInstance(myContext)
.enqueue(myUploadWork)
You can get more code and help from here.

How to set a line break after a set of EditText which have been dynamically created?

I am trying to show few EditText field just like n*n Matrix. I am creating these EditText field dynamically.
Problem is all my EditText field prints in the same line horizontally.
I wanted to know is there anything like "System.out.println();" in Android Studio!
Note: I want this line break in between Views, not in between Text, so \n doesn't work here!
This is my activity_main.xml file,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
tools:context=".MainActivity">
<EditText
android:id="#+id/a"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:gravity="center"
android:inputType="number" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/holo_purple"
android:text="Click"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/child_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<!--EditText Fields going to be here!-->
</LinearLayout>
</LinearLayout>
And Here's my MainActivity.java file
package com.ridi.magicfield;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
public class MainActivity extends AppCompatActivity {
private static int a;
private static EditText magicField, ainput;
private static Button btn;
private static LinearLayout child_layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
child_layout = (LinearLayout) findViewById(R.id.child_layout);
ainput = (EditText) findViewById(R.id.a);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = Integer.parseInt(ainput.getText().toString());
child_layout.removeAllViews();
createMatrixInputField();
}
});
}
private void createMatrixInputField() {
int p = 1;
for (int t = 1; t <= a; t++) {
for (int i = 1; i <= a; i++) {
magicField = new EditText(MainActivity.this);
magicField.setId((p));
magicField.setHint("et-" + (Integer.toString(p)));
magicField.setGravity(Gravity.CENTER);
child_layout.addView(magicField);
p++;
}
}
}
}
Here's my output screen. I want et-3 and et-4 to go to next line.
Is there anyone who can help please...
Output Screen Image
Use TableLayout instead of LinearLayout for n*n matrix pattern
for (int i = 0; i < noOfRows; i++) {
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT
,TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
for(int j = 0; j < noOfColumns; j++) {
EditText et = new EditText(this);
//Extra properties goes here...
et.setText("Your text");
row.addView(et,j);
}
tableLayout.addView(row,i);
}

How can store Data text view data?

In this file i have two Edit text and one textview.I want to sum two number.then result are show in text view.Then i want to store Textview data. please help.i am trying but i am fail.
Activity_main.xml//
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Edit"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Edit1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add"
android:id="#+id/add"
android:onClick="add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:id="#+id/sub"
android:onClick="sub"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:paddingBottom="30dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pr"
android:text="Previoues Data"
/>
</LinearLayout>
</LinearLayout>
the java file is
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText Edit,Edit1;
Button add, sub, pr;
TextView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Edit = (EditText) findViewById(R.id.Edit);
Edit1 = (EditText) findViewById(R.id.Edit1);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
pr = (Button) findViewById(R.id.pr);
view = (TextView) findViewById(R.id.view);
Edit.setText("0");
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int value1 = Integer.parseInt(Edit.getText().toString());
int value2 = Integer.parseInt(Edit1.getText().toString());
int result = value1 + value2;
view.setText(Integer.toString(result));
// Edit.setText(Integer.toString(result));
SharedPreferences sharedPreferences=getSharedPreferences("Mydata",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("value",result);
editor.commit();
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int value1 = Integer.parseInt(Edit.getText().toString());
int value2 = Integer.parseInt(Edit1.getText().toString());
int result = value1 - value2;
view.setText(Integer.toString(result));
Edit.setText(Integer.toString(result));
}
});
pr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("Mydata",Context.MODE_PRIVATE);
// int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
String v=sharedPreferences.getInt("value","");
view.setText(v);
}
});
}
}
update your xml file remove onClick from your button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add"
android:id="#+id/add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:id="#+id/sub"
/>
Problem is with your getting value from sharedPreference
String v=sharedPreferences.getInt("value","");
why are you passing string as default value and your
try this String
v = Integer.toString(sharedPreferences.getInt("value",0));
in the layout you´ve got android:onClick="add" there should be a public void add(View v) to handle the event. Do the sum inside that handler.
Please, follow the documentation
http://developer.android.com/reference/android/widget/Button.html and see the :onCLick handling.

How to set a TextView from an ArrayList, character by character, sequentially?

I want to put a text in a TextView sequentially, with a known time between characters, from a written text in an EditText
That is the solution I made:
I wrote two ArrayList charged from an EditText, the first one with Characters from the EditText , the second one with Integers for determine the time between characters.
Then I parse the ArrayLists, times load of the time integers are done sequentially, but not the characters, the TextViews are drawn only when the cycle ends.
My code
The MainActivity:
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView showCharacter;
private TextView showAppendCharacter;
private EditText incomingText;
private Button readTextEdit;
private ArrayList<CharSequence> toText = new ArrayList<CharSequence>();
private ArrayList<Integer> timePlay = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showCharacter = (TextView) findViewById(R.id.showCharacterTextView);
showAppendCharacter = (TextView) findViewById(R.id.showAppendCharacterTextView);
incomingText = (EditText) findViewById(R.id.incomingEditText);
readTextEdit = (Button) findViewById(R.id.readTextButton);
readTextEdit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toText.clear();
timePlay.clear();
showAppendCharacter.setText("");
String text = incomingText.getText().toString();
for (int base = 0; base < text.length(); base++) {
if (String.valueOf(text.charAt(base)).equals("a")) {
toText.add(("a"));
timePlay.add(500);
} else if (String.valueOf(text.charAt(base)).equals("b")) {
toText.add(("b"));
timePlay.add(650);
} else if (String.valueOf(text.charAt(base)).equals("c")) {
toText.add(("c"));
timePlay.add(800);
} else {
toText.add(("_"));
timePlay.add(1000);
}
}
for (int pos = 0; pos < toText.size(); pos++) {
try {
Thread.sleep(timePlay.get(pos));
showCharacter.setText((String) toText.get(pos));
showAppendCharacter.append((String) toText.get(pos));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
The activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/showCharacterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/showCharacterTextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/showAppendCharacterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/showAppendCharactersTextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/incomingEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/incomingTextEditText"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/readTextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/readButton" />
The strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">texto Desde ArrayList</string>
<string name="action_settings">Settings</string>
<string name="showCharacterTextView">Show last Character</string>
<string name="showAppendCharactersTextView">Show append Characters</string>
<string name="incomingTextEditText">Incoming text</string>
<string name="readButton">Read text</string>
Try something like this... You can get rid of the Arraylists. Remember a String is basically an array of chars. create a custom text view that implements runnable so the work that your trying to do with the text is not done on the main thread.
package com.example.stackquestion;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView implements Runnable {
String text = null;
int i = 0;
int length = 0;
String currentText = "";
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
reset();
}
#Override
public void run() {
if (i < length) {
currentText = currentText + text.charAt(i);
setText(currentText);
if (text.charAt(i) == 'a') {
postDelayed(this, 500);
} else if (text.charAt(i) == 'b') {
postDelayed(this, 650);
} else if (text.charAt(i) == 'c') {
postDelayed(this, 800);
} else if (text.charAt(i) == '_') {
postDelayed(this, 1000);
} else
postDelayed(this, 1);
i++;
}
}
public void setString(String text) {
this.text = text;
this.length = text.length();
}
public void reset() {
currentText = "";
text = null;
i = 0;
setText("");
}
}
Here is the main Activity
package com.example.stackquestion;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView showCharacter;
private CustomTextView showAppendCharacter;
private EditText incomingText;
private Button readTextEdit;
private ArrayList<CharSequence> toText = new ArrayList<CharSequence>();
private ArrayList<Integer> timePlay = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showCharacter = (TextView) findViewById(R.id.showCharacterTextView);
showAppendCharacter = (CustomTextView) findViewById(R.id.showAppendCharacterTextView);
incomingText = (EditText) findViewById(R.id.incomingEditText);
readTextEdit = (Button) findViewById(R.id.readTextButton);
readTextEdit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == R.id.readTextButton)
showAppendCharacter.reset();
showAppendCharacter
.setString(incomingText.getText().toString());
showAppendCharacter.run();
}
});
}
}
woops. I forgot to give you the Layout. But looks like you got it. Just change the type of textview to a fully qualified CustomTextView. Here was mine.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/showCharacterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/showCharacterTextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.example.stackquestion.CustomTextView
android:id="#+id/showAppendCharacterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/showAppendCharactersTextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/incomingEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/incomingTextEditText"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/readTextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/readButton" />
</LinearLayout>

How to fit my design to all devices?

Hi I am developing a app in which alphabets are not fitting for every device. For HCL ME tablet my design won't fit. For samsung it is working. MY XML file is:
<?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:layout_weight="2" android:orientation="vertical">
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="match_parent"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_weight="1">
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="match_parent"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_weight="1">
<TextView android:id="#+id/letter1" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
<TextView android:id="#+id/letter2" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip"></TextView>
<TextView android:id="#+id/letter3" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip"></TextView>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_weight="1">
<ImageView android:id="#+id/imag"
android:gravity="center"
android:scaleType = "fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_gravity="bottom"
android:id="#+id/linearLayout2"
android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="match_parent">
<Button android:id="#+id/previous" android:layout_width="wrap_content" android:layout_weight="1" android:text="Previous" android:layout_height="wrap_content" ></Button>
<Button android:id="#+id/practice" android:layout_width="wrap_content" android:layout_weight="1" android:text="Practice" android:layout_height="wrap_content" android:onClick="onClick"></Button>
<Button android:id="#+id/home" android:layout_width="wrap_content" android:layout_weight="1" android:text="Home" android:layout_height="wrap_content"></Button>
<Button android:id="#+id/spell" android:layout_width="wrap_content" android:layout_weight="1" android:text="Spell" android:layout_height="wrap_content" android:onClick="Content"></Button>
<Button android:id="#+id/next" android:layout_width="wrap_content" android:layout_weight="1" android:text="Next" android:layout_height="wrap_content" android:onClick="Content"></Button>
</LinearLayout>
</LinearLayout>
and my java file is:
package com.android;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.SimpleGestureFilter.SimpleGestureListener;
public class MyAcivity extends Activity implements SimpleGestureListener {
private SimpleGestureFilter detector;
private static int counter=-1;
private String[] mBtn1 ={"C","D","E","F","G","H","IÄ","J","K","L","M","N","O","CA","CB"};
private TextView txtLetter;
private ImageView imgLetter;
private int[] imgArr={R.drawable.w1,R.drawable.w2,R.drawable.w3,R.drawable.w4,R.drawable.w5,R.drawable.w6,R.drawable.w7,R.drawable.w8,R.drawable.w9,R.drawable.w10,R.drawable.w11,R.drawable.w12,
R.drawable.w13,R.drawable.w14,R.drawable.w15};
private TextView txtKannada;
private String[] mBtn2 = {"CgÀ¸À","DªÉÄ","E°","F±À","GqÀ","Hl","IĶ","J¯É","Kr","LzÀÄ","M¯É","N¯É","OµÀzsÀ",
"CAUÀr","CB"};
private String[] mBtn3 = {"ARASA","AME","ILI","ISA","UDA","UTA","RUSHI","ELE","EDI","AIDU","oLE","OLE","AUSHADA",
"ANGADI","AHA"};
private TextView txtEnglish;
private int[] mAudio = {R.raw.a,R.raw.b,R.raw.c,R.raw.d,R.raw.e,R.raw.f,R.raw.g,R.raw.h,R.raw.i,R.raw.j,
R.raw.k,R.raw.l,R.raw.m,R.raw.n,R.raw.o};
protected MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
detector = new SimpleGestureFilter(this,this);
if(counter == -1)
counter =getIntent().getExtras().getInt("POSITION");
Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(), "fonts/brhknd.ttf");
txtLetter = (TextView)findViewById(R.id.letter1);
txtKannada = (TextView)findViewById(R.id.letter2);
txtEnglish = (TextView)findViewById(R.id.letter3);
imgLetter = (ImageView)findViewById(R.id.imag);
txtLetter.setTypeface(tf);
txtLetter.setText(mBtn1[counter]);
txtLetter.setTextSize(350);
txtKannada.setTypeface(tf);
txtKannada.setText(mBtn2[counter]);
txtKannada.setTextSize(100);
txtEnglish.setText(mBtn3[counter]);
txtEnglish.setTextSize(50);
Button btnNext = (Button)findViewById(R.id.next);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(counter<imgArr.length-1)
counter++;
changeContent();
}
});
Button mPlay = (Button)findViewById(R.id.spell);
mPlay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(MySwara.this, mAudio[counter]);
mp.start();
}
});
Button btnPrvs = (Button)findViewById(R.id.previous);
btnPrvs.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(counter>0)
counter--;
changeContent();
}
});
Button btnPractice = (Button)findViewById(R.id.practice);
btnPractice.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MySwara.this,DrawingActivity.class);
startActivity(intent);
}
});
Button btnHome = (Button)findViewById(R.id.home);
btnHome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MySwara.this,mainClass.class);
startActivity(intent);
}
});
}
public void changeContent()
{
txtLetter.setText(mBtn1[counter]);
txtKannada.setText(mBtn2[counter]);
txtEnglish.setText(mBtn3[counter]);
//imgLetter.setBackgroundResource(imgArr[counter]);
Bitmap bm = BitmapFactory.decodeResource(getResources(), imgArr[counter]);
imgLetter.setImageBitmap(bm);
}
#Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
#Override
public void onSwipe(int direction) {
String str = "";
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";
if(counter>0)
counter--;
changeContent();
break;
case SimpleGestureFilter.SWIPE_LEFT : str = "Swipe Left";
if(counter<imgArr.length-1)
counter++;
changeContent();
break;
}
}
}
How i can fit to all devices. Can anyone help?? Thanks in advance.
There are couple of possibilities:
use different xml files for different sizes of screen -> check here http://developer.android.com/guide/practices/screens_support.html
you may use scrollview -> http://developer.android.com/reference/android/widget/ScrollView.html
or simply make your layout fit to the smallest screen size you like to support and then just have "a little" unused space on the larger screens.
It really depends on what you need. Probably the most compatible and advanced solution would be 1.
BUT be aware of the fact that EVERY change on the screen layout has to be duplicated for each xml file following this route!
create xml for each device separately. follow this
second way:
create a parent LinearLayout and pass it to the following method
public static boolean isTabletPC(Context context) {
Display display = ((WindowManager) context
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if (display.getWidth() >= 1200) {
isTabletPC = true;
return isTabletPC;
} else {
isTabletPC = false;
return isTabletPC;
}
}
public static void fixUiDeviceDependencies(
Activity act, LinearLayout llParent) {
if (Utilities.isTabletPC(act)) {
Display display = ((WindowManager) act
.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
LayoutParams params = (LayoutParams) llParent
.getLayoutParams();
params.width = display.getWidth() / 2;
llParent.setLayoutParams(params);
}
}

Categories

Resources