GridView Help - Android Q&A, Help & Troubleshooting

The only thing I can find on this is how to organize apps on the homescreen. I'm setting up a GridView for my primary menu. I'm wanting to know how I can move the individual icons around. I'm going to have a total of at least five activities for this app so I want one icon in each corner and then one in the middle. Also, wouldn't hurt to know how I could have the icons autosize themselves depending on screen size what not. Here's my current code for the gridview.
Code:
package com.ThatOneNoob.smarthaul;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
[user=439709]@override[/user]
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
[user=439709]@override[/user]
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
[user=439709]@override[/user]
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
[user=439709]@override[/user]
public int getItemViewType(int arg0) {
// TODO Auto-generated method stub
return 0;
}
[user=439709]@override[/user]
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.data2, R.drawable.routing2
};
}

Related

[Q] somebody help me im losing steam!!

problem is in CFGameView???
hi, i will make this short but to the point...
making a 2D app with a space theme and involves making a Game View which needs to use two images to make the background which is scrolling in portrait.
im stuck on the CFGameView part at the bottom.
i think i need to link them some how??
its blatantly simple and im just being thick..
cheers
-----------------CFGameRenderer-----------
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class CFGameRenderer implements Renderer {
private CFBackground background = new CFBackground();
private CFBackground background2 = new CFBackground();
private float bgScroll1;
private float bgScroll2;
@Override
public void onDrawFrame(GL10 gl) { try { Thread.sleep(CFEngine.GAME_THREAD_FPS_SLEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
scrollBackground1(gl);
scrollBackground2(gl);
//ALL OTHER GAME DRAWING WILL BE CALLED HERE
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
}
private void scrollBackground1(GL10 gl){
if (bgScroll1 == Float.MAX_VALUE){
bgScroll1 = 0f;
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(1f, 1f, 1f);
gl.glTranslatef(0f, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, bgScroll1, 0.0f);
background.draw(gl);
gl.glPopMatrix();
bgScroll1 += CFEngine.SCROLL_BACKGROUND1;
gl.glLoadIdentity();
}
private void scrollBackground2(GL10 gl){
if (bgScroll2 == Float.MAX_VALUE){
bgScroll2 = 0f;
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(.5f, 1f, 1f);
gl.glTranslatef(1.5f, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, bgScroll2, 0.0f);
background2.draw(gl);
gl.glPopMatrix();
bgScroll2 += CFEngine.SCROLL_BACKGROUND2;
gl.glLoadIdentity();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0f, 1f, 0f, 1f, -1f, 1f);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
background.loadTexture(gl, CFEngine.BACKGROUND_LAYER_ONE, CFEngine.context);
background2.loadTexture(gl, CFEngine.BACKGROUND_LAYER_TWO, CFEngine.context);
}
}
---------------- CFGame-------------
import android.app.Activity;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
public class CFGame extends Activity {
private CFGameView gameView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gameView = new CFGameView(this);
setContentView(gameView);
}
@Override
protected void onResume() {
super.onResume();
gameView.onResume();
}
@Override
protected void onPause() {
super.onPause();
gameView.onPause();
}
}
--------------- CFGamView ---------------
import android.content.Context;
import android.opengl.GLSurfaceView;
public class CFGameView extends GLSurfaceView{
public CFGameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
basically how do i properly create and extend the GLSurfaceView so it starts my game.
i keep getting this logcat error no matter what i try.
03-09 17:42:16.649: E/AndroidRuntime(17570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blah blah/com.blah blah.CFGame}: java.lang.NullPointerException

[HELP] Program a Scrollable tabs

Hello guys,
I need your help to find how to code a Scrollable tabs (ics), I find no help and I'm looking but I can not find!
Had you any idea? Thank you in advance
http://developer.android.com/design/media/tabs_scrolly.mp4
Source: http://developer.android.com/design/patterns/actionbar.html#contextual
youpi01 said:
Hello guys,
I need your help to find how to code a Scrollable tabs (ics), I find no help and I'm looking but I can not find!
Had you any idea? Thank you in advance
http://developer.android.com/design/media/tabs_scrolly.mp4
Source: http://developer.android.com/design/patterns/actionbar.html#contextual
Click to expand...
Click to collapse
Create an actionbar and edit the layout to fit your needs.
Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle startInfo) {
super.onCreate(startInfo);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tabCPU = actionBar.newTab()
.setText(R.string.cpu_tab_title)
.setTabListener(new TabListener<CpuInfoActivity>(
this, "artist", CpuInfoActivity.class));
actionBar.addTab(tabCPU);
Tab tabMEM = actionBar.newTab()
.setText(R.string.mem_tab_title)
.setTabListener(new TabListener<MemInfoActivity>(
this, "artist", MemInfoActivity.class));
actionBar.addTab(tabMEM);
}
thank you very much I will test it now!
Well I just tried but I can not, it shows me many errors!
Is there source code available for me to view?
Hello, I have made ​​some progress ...
I would just like to add tabs and be able to edit in xml!
How do I add Java code to the link to my different layout (tabs)
Attached is the file in question!
Code:
package com.youpi.app;
import com.actionbarsherlock.app.SherlockActivity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class AppActivity extends SherlockActivity implements OnPageChangeListener {
private ViewPager viewPager;
private static int NUM_VIEWS = 2;
private MyPagerAdapter pagerAdapter;
private TextView pageIndicator;
private int currentPage = 0;
private View[] tabViews = new View[NUM_VIEWS];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pagerAdapter = new MyPagerAdapter();
viewPager = (ViewPager) findViewById(R.id.viewpager1);
TextView textView1 = new TextView(this);
textView1.setText("Tunandroid View Pager Example");
textView1.setTextSize(20);
ImageView imageView1 = new ImageView(this);
imageView1.setImageResource(R.drawable.ic_launcher);
[COLOR="Red"]tabViews[0] = textView1;
tabViews[1] = imageView1;
tabViews[2]= here add a new tab but in xml![/COLOR]
viewPager.setAdapter(pagerAdapter);
viewPager.setOnPageChangeListener(this);
viewPager.setCurrentItem(currentPage);
pageIndicator = (TextView)findViewById(R.id.pageIndicator);
pageIndicator.setText(String.valueOf(currentPage+1));
}
private class MyPagerAdapter extends PagerAdapter
{
@Override
public int getCount() {
return NUM_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
((ViewPager) collection).addView(tabViews[position],0);
return tabViews[position];
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View)view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==(object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
currentPage = position;
pageIndicator.setText(String.valueOf(currentPage+1));
}
}

android quiz help

So I've decided to to teach myself how to program for android and have a general understanding for java. Well i decided to borrow someones code from the internet and utilize it to assist in creating my first quiz app. I am getting some errors in this code and was wondering if anyone would be so kind to assist me. Thanks
Code:
package com.danyluk.MedicStudyGuide;
import java.io.IOException;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Quiz extends Activity{
/** Called when the activity is first created. */
private RadioButton radioButton;
private TextView quizQuestion;
private TextView tvScore;
private int rowIndex = 1;
private static int score=0;
private int questNo=0;
private boolean checked=false;
private boolean flag=true;
private RadioGroup radioGroup;
String[] corrAns = new String[5];
final DataBaseHelper db = new DataBaseHelper(this);
Cursor c1;
Cursor c2;
Cursor c3;
int counter=1;
String label;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
String options[] = new String[19];
// get reference to radio group in layout
RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c3 = db.getCorrAns();
tvScore = (TextView) findViewById(R.id.tvScore);
for (int i=0;i<=4;i++)
{
corrAns[i]=c3.getString(0);
c3.moveToNext();
}
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0;;){
RadioButton btn = (RadioButton) radioGroup.getChildAt(i);
String text;
if (btn.isPressed() && btn.isChecked() && questNo < 5)
{
Log.e("corrAns[questNo]",corrAns[questNo]);
if (corrAns[questNo].equals(btn.getText()) && flag==true)
{
score++;
flag=false;
checked = true;
}
else if(checked==true)
{
score--;
flag=true;
checked = false;
}
}
}
tvScore.setText = ("Score: " + Integer.toString(score) + "/5");
Log.e("Score:", Integer.toString(score));
}
});
quizQuestion = (TextView) findViewById(R.id.TextView01);
displayQuestion();
/*Displays the next options and sets listener on next button*/
Button btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(btnNext_Listener);
/*Saves the selected values in the database on the save button*/
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(btnSave_Listener);
}
/*Called when next button is clicked*/
private View.OnClickListener btnNext_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {
flag=true;
checked = false;
questNo++;
if (questNo < 5)
{
c1.moveToNext();
displayQuestion();
}
}
};
/*Called when save button is clicked*/
private View.OnClickListener btnSave_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
private void displayQuestion()
{
//Fetching data quiz data and incrementing on each click
c1=db.getQuiz_Content(rowIndex);
c2 =db.getAns(rowIndex++);
quizQuestion.setText(c1.getString(0));
radioGroup.removeAllViews();
for (int i=0;i<=3;i++)
{
//Generating and adding 4 radio buttons dynamically
radioButton = new RadioButton(this);
radioButton.setText(c2.getString(0));
radioButton.setId(i);
c2.moveToNext();
radioGroup.addView(radioButton);
}
}}}
You forgot give us the error that you get

[Q] Camera stream

Hi,
I have to develop an application for my studies which consists in capture and broadcasting live on Web via an android phone
At present my code allows me to capture but I do not manage to broadcast the video on a server
Can you help me ?
Code:
package com.androidvideocapture;
import java.io.IOException;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class AndroidVideoCapture extends Activity implements SurfaceHolder.Callback{
Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean recording;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
mediaRecorder = new MediaRecorder();
initMediaRecorder();
setContentView(R.layout.activity_android_video_capture);
SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
surfaceHolder = myVideoView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
}
private Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(recording){
mediaRecorder.stop();
mediaRecorder.release();
finish();
}else{
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}};
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
prepareMediaRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
private void initMediaRecorder(){
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(camcorderProfile_HQ);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
}
private void prepareMediaRecorder(){
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

[Q] App crashes after sending a message using sockets

I'm developing a simple chat app and it just stops after transmitting the message. The Server shows the message that means that it is sent successfully but my app crashes after that. This is the Client code :
Code:
package com.example.androidpc;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Chat extends Activity {
Socket socket;
EditText msg_txt;
TextView chatfield_tv;
Button send_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
msg_txt = (EditText) findViewById(R.id.msg_txt);
chatfield_tv = (TextView) findViewById(R.id.chatfield_tv);
send_btn = (Button) findViewById(R.id.send_btn);
socket = SocketWrapper.getSocket();
send_btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new chatIO().execute();
}
});
}
private class chatIO extends AsyncTask<String,String,String>{
PrintStream ps;
Scanner scan;
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
ps = new PrintStream(socket.getOutputStream());
ps.println(msg_txt.getText().toString());
publishProgress("you");
scan = new Scanner(socket.getInputStream());
publishProgress("i");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
if(values[0].equals("you"))
{
chatfield_tv.append("You : "+msg_txt.getText().toString());
msg_txt.setText("");
}
else
{
chatfield_tv.append("Server : "+scan.nextLine());
}
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
ps.flush();
scan.close();
super.onPostExecute(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chat, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I don't have a logcat since i don't test on an emulator or adb

Categories

Resources